discovery

package module
Version: v1.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

README

Cloud Application Framework

This library aims to expose a common set of interfaces for handling common cloud platform tasks. Such as queuing messages, publishing events, calling cloud functions etc.

This library is AWS centric, however, can by modified and extended to support others. Using the interfaces and configuration options shown below.

Being AWS centric, the default options are:

  • Locator / Service Discovery: AWS Cloudmap
  • Request: AWS Lambda
  • Pubsub: AWS SNS
  • Queue: AWS SQS
  • Automate: AWS SSM

Registering a service - Cloudformation

For the Cloudmap locator, the easiest way to register a service is through Cloudformation (or Terraform etc):

CloudMapService:
  Type: AWS::ServiceDiscovery::Service
  Properties:
    Description: User service
    Name: users
    NamespaceId: <namespace-id-here>

CreateUserInstance:
  Type: "AWS::ServiceDiscovery::Instance"
  Properties:
    InstanceAttributes:
      arn: create-user
      handler: create-job-run
      type: function
    InstanceId: create-user
    ServiceId:
      Ref: CloudMapService

Use with default settings

func main() {
  d := discover.NewDiscovery()

  token, err := d.Queue("acme-prod.my-service->my-queue", types.Request{
    Body: []byte("{}"),
  })
  ...
}

Use with custom integrations

func main() {
  ...
  d := discover.NewDiscovery(
    discover.SetQueue(NewKafkaAdapter(kafkaClient)),
    discover.SetPubsub(NewNATSAdapter(natsClient)),
    discover.SetLocator(NewConsulLocator(consul)),
  )
}
Request
d := discover.NewDiscovery()
d.Request("my-namespace.users->create-user", types.Request{
  Body: []byte("{ \"hello\": \"world\" }"),
})
Queue
d := discover.NewDiscovery()
d.Queue("my-namespace.my-service->my-queue", types.Request{
  Body: jsonString,
})

go func() {
  messages, err := d.Listen("my-namespace.my-service->my-queue")
  for message := range message {
    log.Println(string(message.Body))
  }
}()
Pubsub
d := discovery.NewDiscovery()
d.Publish("my-namespace.my-service->my-event", types.Request{
  Body: jsonEvent,
})
Automate
d := discovery.NewDiscovery()
d.Automate("my-namespace.my-service->my-script", types.Request{
  Body: jsonEvent,
})

Custom integrations

You can customise the behaviour and create your own integrations by conforming to the following interfaces, and use the SetLocator, SetQueue, SetPubsub, SetAutomate and SetFunction methods when creating an instance of the Discovery library.

Locator interface
Discover(signature *types.Signature) (*types.Service, error)
Queue interface
// QueueAdapter -
type QueueAdapter interface {

	// Queue a message, return a token or message id
	QueueWithOpts(service *types.Service, request types.Request, opts types.Options) (string, error)
	ListenWithOpts(service *types.Service, opts types.Options) (<-chan *types.Response, error)
}
Function interface
// FunctionAdapter -
type FunctionAdapter interface {
	CallWithOpts(service *types.Service, request types.Request, opts types.Options) (*types.Response, error)
}
AutomateAdapter interface
// AutomateAdapter -
type AutomateAdapter interface {
	ExecuteWithOpts(service *types.Service, request types.Request, opts types.Options) (*types.Response, error)
}
PubsubAdapter interface
// PubsubAdapter -
type PubsubAdapter interface {
	PublishWithOpts(service *types.Service, request types.Request, opts types.Options) error
	SubscribeWithOpts(service *types.Service, opts types.Options) (<-chan *types.Response, error)
}

Acknowledgements: inspired by the amazing work at Micro

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AutomateAdapter

type AutomateAdapter interface {
	ExecuteWithOpts(service *types.Service, request types.Request, opts types.Options) (*types.Response, error)
}

AutomateAdapter is an interface defining a System Management service

type Discover

Discover instance

func NewDiscovery

func NewDiscovery(opts ...Option) *Discover

NewDiscovery -

func (*Discover) Automate

func (d *Discover) Automate(signature string, request types.Request) (*types.Response, error)

Automate calls an infrastructure script through the AutomateAdapter

func (*Discover) AutomateWithOpts added in v1.0.0

func (d *Discover) AutomateWithOpts(signature string, request types.Request, opts types.Options) (*types.Response, error)

AutomateWithOpts calls an infrastructure script through the AutomateAdapter, with options

func (*Discover) Call

func (d *Discover) Call(service types.ServiceRequest, opts types.Options) (*types.Response, error)

Call sends a request to the proper adapter depending on the service type. (potentially not needed, as the behavioural methods say)

func (*Discover) CallWithOpts added in v1.0.0

func (d *Discover) CallWithOpts(service types.ServiceRequest, opts types.Options) (*types.Response, error)

CallWithOpts sends a request to the proper adapter depending on the service type, with options. (potentially not needed, as the behavioural methods say)

func (*Discover) Discover added in v1.0.3

func (d *Discover) Discover(addr string) (*types.Service, error)

func (*Discover) Listen

func (d *Discover) Listen(signature string) (<-chan *types.Response, error)

Listen creates a listener channel through the QueueAdapter

func (*Discover) ListenWithOpts added in v1.0.0

func (d *Discover) ListenWithOpts(signature string, opts types.Options) (<-chan *types.Response, error)

ListenWithOpts creates a listener channel through the QueueAdapter, with options

func (*Discover) Publish

func (d *Discover) Publish(signature string, request types.Request) error

Publish publishes an asynchronous event through the PubsubAdapter

func (*Discover) PublishWithOpts added in v1.0.0

func (d *Discover) PublishWithOpts(signature string, request types.Request, opts types.Options) error

PublishWithOpts - publishes an asynchronous event through the PubsubAdapter, with options

func (*Discover) Queue

func (d *Discover) Queue(signature string, request types.Request) (string, error)

Queue queues a request through the QueueAdapter

func (*Discover) QueueWithOpts added in v1.0.0

func (d *Discover) QueueWithOpts(signature string, request types.Request, opts types.Options) (string, error)

QueueWithOpts queues a request through the QueueAdapter, with options

func (*Discover) Request

func (d *Discover) Request(signature string, request types.Request) (*types.Response, error)

Request makes synchronous call through the FunctionAdapter

func (*Discover) RequestWithOpts added in v1.0.0

func (d *Discover) RequestWithOpts(signature string, request types.Request, opts types.Options) (*types.Response, error)

RequestWithOpts makes synchronous call through the FunctionAdapter, with options

func (*Discover) Subscribe

func (d *Discover) Subscribe(signature string) (<-chan *types.Response, error)

Subscribe subscribes to an event through the PubsubAdapter Warning, not possible with SNS

func (*Discover) SubscribeWithOpts added in v1.0.0

func (d *Discover) SubscribeWithOpts(signature string, opts types.Options) (<-chan *types.Response, error)

SubscribeWithOpts subscribes to an event through the PubsubAdapter, with options Warning, not possible with SNS

type FunctionAdapter

type FunctionAdapter interface {
	CallWithOpts(service *types.Service, request types.Request, opts types.Options) (*types.Response, error)
}

FunctionAdapter is an interface defining a Serverless Functions service

type Locator

type Locator interface {
	Discover(signature *types.Signature) (*types.Service, error)
}

Locator is an interface defining a Service Discovery service

type LogAdapter

type LogAdapter interface {
	Log(service *types.Service, message string)
}

LogAdapter is an interface defining a Logging service

type Option

type Option func(*Options)

Option is a function that modifies the Options object

func SetAutomate

func SetAutomate(automateAdapter AutomateAdapter) Option

SetAutomate sets the given automation adapter to be used

func SetFunction

func SetFunction(functionAdapter FunctionAdapter) Option

SetFunction sets the given function adapter to be used

func SetLocator

func SetLocator(locator Locator) Option

SetLocator sets the service discovery adapter to be used

func SetLogger

func SetLogger(logger LogAdapter) Option

SetLogger sets the logger service

func SetPubsub

func SetPubsub(pubsubAdapter PubsubAdapter) Option

SetPubsub sets the given pubsub adapter to be used

func SetQueue

func SetQueue(queueAdapter QueueAdapter) Option

SetQueue sets the given queue adapter to be used

func SetTracer

func SetTracer(tracer TraceAdapter) Option

SetTracer sets the tracer to be used

func WithAWSBackend added in v1.0.0

func WithAWSBackend() Option

WithAWSBackend initializes the Discovery object with default AWS services. Override these services by using the Set methods.

type Options

Options allows the client to configure the integrations.

type PubsubAdapter

type PubsubAdapter interface {
	PublishWithOpts(service *types.Service, request types.Request, opts types.Options) error
	SubscribeWithOpts(service *types.Service, opts types.Options) (<-chan *types.Response, error)
}

PubsubAdapter is an interface defining a PubSub Messaging service

type QueueAdapter

type QueueAdapter interface {

	// Queue a message, return a token or message id
	QueueWithOpts(service *types.Service, request types.Request, opts types.Options) (string, error)
	ListenWithOpts(service *types.Service, opts types.Options) (<-chan *types.Response, error)
}

QueueAdapter is an interface defining a Queue service

type TraceAdapter

type TraceAdapter interface {
	Trace(service *types.Service)
}

TraceAdapter is an interface defining a Tracing service

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL