105 lines
2.0 KiB
Go
105 lines
2.0 KiB
Go
package broker
|
|
|
|
import (
|
|
"crypto/tls"
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
type Options struct {
|
|
Addrs []string
|
|
Secure bool
|
|
TLSConfig *tls.Config
|
|
|
|
// Other options for implementations of the interface
|
|
// can be stored in a context
|
|
Context context.Context
|
|
}
|
|
|
|
type PublishOptions struct {
|
|
// Other options for implementations of the interface
|
|
// can be stored in a context
|
|
Context context.Context
|
|
}
|
|
|
|
type SubscribeOptions struct {
|
|
// AutoAck defaults to true. When a handler returns
|
|
// with a nil error the message is acked.
|
|
AutoAck bool
|
|
// Subscribers with the same queue name
|
|
// will create a shared subscription where each
|
|
// receives a subset of messages.
|
|
Queue string
|
|
|
|
// Other options for implementations of the interface
|
|
// can be stored in a context
|
|
Context context.Context
|
|
}
|
|
|
|
type Option func(*Options)
|
|
|
|
type PublishOption func(*PublishOptions)
|
|
|
|
type SubscribeOption func(*SubscribeOptions)
|
|
|
|
type contextKeyT string
|
|
|
|
var (
|
|
registryKey = contextKeyT("github.com/micro/go-micro/registry")
|
|
)
|
|
|
|
func newSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
|
opt := SubscribeOptions{
|
|
AutoAck: true,
|
|
}
|
|
|
|
for _, o := range opts {
|
|
o(&opt)
|
|
}
|
|
|
|
return opt
|
|
}
|
|
|
|
// Addrs sets the host addresses to be used by the broker
|
|
func Addrs(addrs ...string) Option {
|
|
return func(o *Options) {
|
|
o.Addrs = addrs
|
|
}
|
|
}
|
|
|
|
// DisableAutoAck will disable auto acking of messages
|
|
// after they have been handled.
|
|
func DisableAutoAck() SubscribeOption {
|
|
return func(o *SubscribeOptions) {
|
|
o.AutoAck = false
|
|
}
|
|
}
|
|
|
|
// QueueName sets the name of the queue to share messages on
|
|
func QueueName(name string) SubscribeOption {
|
|
return func(o *SubscribeOptions) {
|
|
o.Queue = name
|
|
}
|
|
}
|
|
|
|
func Registry(r registry.Registry) Option {
|
|
return func(o *Options) {
|
|
o.Context = context.WithValue(o.Context, registryKey, r)
|
|
}
|
|
}
|
|
|
|
// Secure communication with the broker
|
|
func Secure(b bool) Option {
|
|
return func(o *Options) {
|
|
o.Secure = b
|
|
}
|
|
}
|
|
|
|
// Specify TLS Config
|
|
func TLSConfig(t *tls.Config) Option {
|
|
return func(o *Options) {
|
|
o.TLSConfig = t
|
|
}
|
|
}
|