github.com/btccom/go-micro/v2@v2.9.3/broker/broker.go (about)

     1  // Package broker is an interface used for asynchronous messaging
     2  package broker
     3  
     4  // Broker is an interface used for asynchronous messaging.
     5  type Broker interface {
     6  	Init(...Option) error
     7  	Options() Options
     8  	Address() string
     9  	Connect() error
    10  	Disconnect() error
    11  	Publish(topic string, m *Message, opts ...PublishOption) error
    12  	Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
    13  	String() string
    14  }
    15  
    16  // Handler is used to process messages via a subscription of a topic.
    17  // The handler is passed a publication interface which contains the
    18  // message and optional Ack method to acknowledge receipt of the message.
    19  type Handler func(Event) error
    20  
    21  type Message struct {
    22  	Header map[string]string
    23  	Body   []byte
    24  }
    25  
    26  // Event is given to a subscription handler for processing
    27  type Event interface {
    28  	Topic() string
    29  	Message() *Message
    30  	Ack() error
    31  	Error() error
    32  }
    33  
    34  // Subscriber is a convenience return type for the Subscribe method
    35  type Subscriber interface {
    36  	Options() SubscribeOptions
    37  	Topic() string
    38  	Unsubscribe() error
    39  }
    40  
    41  var (
    42  	DefaultBroker Broker = NewBroker()
    43  )
    44  
    45  func Init(opts ...Option) error {
    46  	return DefaultBroker.Init(opts...)
    47  }
    48  
    49  func Connect() error {
    50  	return DefaultBroker.Connect()
    51  }
    52  
    53  func Disconnect() error {
    54  	return DefaultBroker.Disconnect()
    55  }
    56  
    57  func Publish(topic string, msg *Message, opts ...PublishOption) error {
    58  	return DefaultBroker.Publish(topic, msg, opts...)
    59  }
    60  
    61  func Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
    62  	return DefaultBroker.Subscribe(topic, handler, opts...)
    63  }
    64  
    65  func String() string {
    66  	return DefaultBroker.String()
    67  }