Documentation
¶
Overview ¶
Package teacup provides simple tooling for rapidly building microservices that will operate in a standardized environment. That environment currently assumes:
- service discovery using Hashicorp Consul https://hashicorp.com/consul
- secrets using Hashicorp Vault https://hashicorp.com/vault
- message queues using nsq.io https://nsq.io
- data storage using Redis https://redis.io
Index ¶
- type Producer
- type Queue
- type Subscriber
- type SubscriberFunc
- type Teacup
- func (t *Teacup) Context() context.Context
- func (t *Teacup) Option(_ context.Context, key string) (string, error)
- func (t *Teacup) Queue() *Queue
- func (t *Teacup) Redis(ctx context.Context) (*redis.Client, error)
- func (t *Teacup) Register(worker Worker)
- func (t *Teacup) Secret(_ context.Context, key string) (string, error)
- func (t *Teacup) ServiceAddr(ctx context.Context, name string, port int) string
- func (t *Teacup) ServiceAddrs(ctx context.Context, name string, port int) []string
- func (t *Teacup) Start()
- func (t *Teacup) Stop()
- type Worker
- type WorkerFunc
- type WorkerReg
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Producer ¶
type Producer struct {
// contains filtered or unexported fields
}
Producer provides an abstract way to publish messages to queues. It follows the nsq implementation closely and on other platforms, many operations may be NOOPs.
type Queue ¶
type Queue struct { Client *nats.Conn // contains filtered or unexported fields }
Queue abstracts the underlying message queue from microservices. We will use nsq for "native" deployments but want to have the freedom of easily using a platform provided queue service if available.
type Subscriber ¶
type Subscriber interface { // Message is called for each received message from the given topic/channel combination. // The "reply" channel subject is added to the context for message handlers that need to respond to // a message queue request. // // TODO make the following an actual go doc example // // Example: // // Message(ctx context.Context, t *Teacup, topic, channel string, msg []byte) error { // t.Publisher(ctx).Publish(ctx, ctx.Value("reply"), []byte("{\"foo\":\"bar\"}")) // } // // // Errors can be used to force a requeue of the message. // TODO we need better documentation on exactly how errors are used to force requeues (and any other actions) Message(ctx context.Context, t *Teacup, topic, channel string, msg []byte) error }
Subscriber allows microservices to process queue messages on a topic/channel without dealing directly with the underlying message queue (including all it's various settings and configuration options). Instead, microservices implement Subscriber and Sub to a topic/channel and receive messages as they are ready for processing.
type SubscriberFunc ¶
The SubscriberFunc type is an adapter to allow ordinary functions to act as Subscribers. If f is a function with the appropriate signature, SubscriberFunc(f) is a Subscriber that calls f.
type Teacup ¶
type Teacup struct {
// contains filtered or unexported fields
}
Teacup provides essential tools for writing microservices with minimum boilerplate. Create an empty Teacup and then register at least one Worker or Sub then call Teacup.Start() to begin running.
func (*Teacup) Option ¶
Option returns the configuration setting associated with a key name. The function searches the environment first and if not found, tries to obtain the value from Consul.
func (*Teacup) Redis ¶
Redis returns a redis client ready to use. The first call to Redis() will dial the Redis server and the provided context is used to control things like timeouts.
func (*Teacup) Register ¶
Register a worker for running. THe Worker's Start() method will be called some time after the Teacup.Start() method is called and should not return unless the provided context is done or a fatal error occurs.
func (*Teacup) Secret ¶
Secret returns a secret associated with a key name. Secret searches the environment first and if not found, tries to obtain the value from Vault.
func (*Teacup) ServiceAddr ¶
ServiceAddr searches for a service address `name` by checking for:
* `NAME_ADDR` - an environmental variable containing a host:port pair * `name.service.consul` & `name.local` - a SRV record * `localhost:<port>` - a fallback assuming the service is on the default port
func (*Teacup) ServiceAddrs ¶ added in v0.0.2
ServiceAddr searches for a service address `name` by checking for:
* `NAME_ADDR` - an environmental variable containing comma separated host:port pairs. * `name.service.consul` & `name.local` - one or more SRV records * `localhost:<port>` - a fallback assuming the service is on the default port
type Worker ¶
type Worker interface { // Start the Worker and run until the provided context's done channel is closed. The provided Teacup // reference can be used to access other Teacup managed services. Start(ctx context.Context, t *Teacup) }
Worker allows microservices to run "forever" without having to worry about initializing an environment or properly responding to system inputs. The worker should watch the context done channel to know when to terminate.
type WorkerFunc ¶
The WorkerFunc type is an adapter to allow ordinary functions to act as Workers. If f is a function with the appropriate signature, WorkerFunc(f) is a Worker that calls f.