github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/broker/broker.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/broker/broker.go
    16  
    17  // Package broker is the micro broker
    18  package broker
    19  
    20  var (
    21  	// DefaultBroker implementation
    22  	DefaultBroker Broker
    23  )
    24  
    25  // Broker is an interface used for asynchronous messaging.
    26  type Broker interface {
    27  	Init(...Option) error
    28  	Options() Options
    29  	Address() string
    30  	Connect() error
    31  	Disconnect() error
    32  	Publish(topic string, m *Message, opts ...PublishOption) error
    33  	Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
    34  	String() string
    35  }
    36  
    37  // Handler is used to process messages via a subscription of a topic.
    38  type Handler func(*Message) error
    39  
    40  type ErrorHandler func(*Message, error)
    41  
    42  type Message struct {
    43  	Header map[string]string
    44  	Body   []byte
    45  }
    46  
    47  // Subscriber is a convenience return type for the Subscribe method
    48  type Subscriber interface {
    49  	Options() SubscribeOptions
    50  	Topic() string
    51  	Unsubscribe() error
    52  }
    53  
    54  // Publish a message to a topic
    55  func Publish(topic string, m *Message) error {
    56  	return DefaultBroker.Publish(topic, m)
    57  }
    58  
    59  // Subscribe to a topic
    60  func Subscribe(topic string, h Handler) (Subscriber, error) {
    61  	return DefaultBroker.Subscribe(topic, h)
    62  }