github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/broker/broker.go (about)

     1  package broker
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  type (
     9  	// IBroker 提供微服务之前的信息交换总线
    10  	IBroker interface {
    11  		String() string // 返回对象名称
    12  		Init(...Option) error
    13  		Config() *Config
    14  		Address() string
    15  		Start() error // 开始阻塞监听
    16  		Close() error
    17  		Publish(topic string, m *Message, opts ...PublishOption) error
    18  		Subscribe(topic string, h Handler, opts ...SubscribeOption) (ISubscriber, error)
    19  	}
    20  
    21  	// Subscriber is a convenience return type for the Subscribe method
    22  	ISubscriber interface {
    23  		Config() *SubscribeConfig
    24  		Topic() string
    25  		Unsubscribe() error
    26  	}
    27  
    28  	// Event is given to a subscription handler for processing
    29  	IEvent interface {
    30  		Topic() string
    31  		Message() *Message
    32  		Ack() error
    33  		Error() error
    34  	}
    35  
    36  	// Handler is used to process messages via a subscription of a topic.
    37  	// The handler is passed a publication interface which contains the
    38  	// message and optional Ack method to acknowledge receipt of the message.
    39  	Handler func(IEvent) error
    40  )
    41  
    42  var brokerMap = make(map[string]func(opts ...Option) IBroker)
    43  var brokerHost sync.Map
    44  var defaultBroker IBroker
    45  
    46  func Default(opts ...Option) IBroker {
    47  	if defaultBroker == nil {
    48  		defaultBroker = Use("http")
    49  	}
    50  	defaultBroker.Init(opts...)
    51  	return defaultBroker
    52  }
    53  
    54  func Register(name string, creator func(opts ...Option) IBroker) {
    55  	brokerMap[name] = creator
    56  }
    57  
    58  func Use(name string, opts ...Option) IBroker {
    59  	cfg := &Config{Name: name}
    60  	cfg.Init(opts...)
    61  
    62  	// 加载存在的服务
    63  	for _, addr := range cfg.Addrs {
    64  		if addr == "" {
    65  			continue
    66  		}
    67  
    68  		altName := fmt.Sprintf("%s-%s", cfg.Name, addr)
    69  		if reg, has := brokerHost.Load(altName); has {
    70  			b := reg.(IBroker)
    71  			b.Init(opts...)
    72  			return b
    73  		}
    74  	}
    75  
    76  	if fn, has := brokerMap[name]; has {
    77  		reg := fn(opts...)
    78  		for _, addr := range reg.Config().Addrs {
    79  			if addr == "" {
    80  				continue
    81  			}
    82  
    83  			regName := fmt.Sprintf("%s-%s", cfg.Name, addr)
    84  			brokerHost.Store(regName, reg)
    85  		}
    86  		return reg
    87  	}
    88  
    89  	// 默认
    90  	def := brokerMap["http"]
    91  	return def(opts...)
    92  }