Update the broker interface

This commit is contained in:
Asim
2015-12-23 19:07:26 +00:00
parent 6226a80e78
commit 02aca819d7
3 changed files with 102 additions and 18 deletions

View File

@@ -4,28 +4,35 @@ type Broker interface {
Address() string
Connect() error
Disconnect() error
Init() error
Publish(string, *Message) error
Subscribe(string, Handler) (Subscriber, error)
Init(...Option) error
Publish(string, *Message, ...PublishOption) error
Subscribe(string, Handler, ...SubscribeOption) (Subscriber, error)
String() string
}
type Handler func(*Message)
// Handler is used to process messages via a subscription of a topic.
// The handler is passed a publication interface which contains the
// message and optional Ack method to acknowledge receipt of the message.
type Handler func(Publication) error
type Message struct {
Header map[string]string
Body []byte
}
// Publication is given to a subscription handler for processing
type Publication interface {
Topic() string
Message() *Message
Ack() error
}
type Subscriber interface {
Config() SubscribeOptions
Topic() string
Unsubscribe() error
}
type options struct{}
type Option func(*options)
var (
DefaultBroker Broker = newHttpBroker([]string{})
)
@@ -34,8 +41,8 @@ func NewBroker(addrs []string, opt ...Option) Broker {
return newHttpBroker(addrs, opt...)
}
func Init() error {
return DefaultBroker.Init()
func Init(opts ...Option) error {
return DefaultBroker.Init(opts...)
}
func Connect() error {
@@ -46,12 +53,12 @@ func Disconnect() error {
return DefaultBroker.Disconnect()
}
func Publish(topic string, msg *Message) error {
return DefaultBroker.Publish(topic, msg)
func Publish(topic string, msg *Message, opts ...PublishOption) error {
return DefaultBroker.Publish(topic, msg, opts...)
}
func Subscribe(topic string, handler Handler) (Subscriber, error) {
return DefaultBroker.Subscribe(topic, handler)
func Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
return DefaultBroker.Subscribe(topic, handler, opts...)
}
func String() string {