2015-12-23 19:07:26 +00:00
|
|
|
package broker
|
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
import (
|
2016-01-16 23:39:47 +00:00
|
|
|
"crypto/tls"
|
|
|
|
|
2016-01-20 15:22:44 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
2016-01-06 16:25:12 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
type Options struct {
|
2016-03-15 22:12:28 +00:00
|
|
|
Addrs []string
|
2016-01-16 23:39:47 +00:00
|
|
|
Secure bool
|
|
|
|
TLSConfig *tls.Config
|
2015-12-31 18:11:46 +00:00
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 18:11:46 +00:00
|
|
|
}
|
2015-12-23 19:07:26 +00:00
|
|
|
|
2015-12-31 18:14:40 +00:00
|
|
|
type PublishOptions struct {
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 18:14:40 +00:00
|
|
|
}
|
2015-12-23 19:07:26 +00:00
|
|
|
|
|
|
|
type SubscribeOptions struct {
|
2015-12-23 20:26:13 +00:00
|
|
|
// AutoAck defaults to true. When a handler returns
|
|
|
|
// with a nil error the message is acked.
|
2015-12-23 19:07:26 +00:00
|
|
|
AutoAck bool
|
2015-12-23 20:05:47 +00:00
|
|
|
// Subscribers with the same queue name
|
|
|
|
// will create a shared subscription where each
|
|
|
|
// receives a subset of messages.
|
|
|
|
Queue string
|
2015-12-31 18:14:40 +00:00
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-23 19:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
type PublishOption func(*PublishOptions)
|
|
|
|
|
|
|
|
type SubscribeOption func(*SubscribeOptions)
|
|
|
|
|
2016-01-20 15:22:44 +00:00
|
|
|
type contextKeyT string
|
|
|
|
|
|
|
|
var (
|
|
|
|
registryKey = contextKeyT("github.com/micro/go-micro/registry")
|
|
|
|
)
|
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
func newSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
|
|
|
opt := SubscribeOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2016-03-15 22:12:28 +00:00
|
|
|
// Addrs sets the host addresses to be used by the broker
|
|
|
|
func Addrs(addrs ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Addrs = addrs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:07:26 +00:00
|
|
|
// DisableAutoAck will disable auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func DisableAutoAck() SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 20:26:13 +00:00
|
|
|
// QueueName sets the name of the queue to share messages on
|
2015-12-23 20:05:47 +00:00
|
|
|
func QueueName(name string) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Queue = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:22:44 +00:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = context.WithValue(o.Context, registryKey, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 22:13:02 +00:00
|
|
|
// Secure communication with the broker
|
|
|
|
func Secure(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Secure = b
|
2015-12-23 19:07:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 23:39:47 +00:00
|
|
|
|
|
|
|
// Specify TLS Config
|
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|