github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/message/pubsub.go (about)

     1  package message
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // Publisher is the emitting part of a Pub/Sub.
     8  type Publisher interface {
     9  	// Publish publishes provided messages to given topic.
    10  	//
    11  	// Publish can be synchronous or asynchronous - it depends on the implementation.
    12  	//
    13  	// Most publishers implementations don't support atomic publishing of messages.
    14  	// This means that if publishing one of the messages fails, the next messages will not be published.
    15  	//
    16  	// Publish must be thread safe.
    17  	Publish(ctx context.Context, topic string, messages ...*Message) error
    18  	// Close should flush unsent messages, if publisher is async.
    19  	Close() error
    20  }
    21  
    22  // Subscriber is the consuming part of the Pub/Sub.
    23  type Subscriber interface {
    24  	// Subscribe returns output channel with messages from provided topic.
    25  	// Channel is closed, when Close() was called on the subscriber.
    26  	//
    27  	// To receive the next message, `Ack()` must be called on the received message.
    28  	// If message processing failed and message should be redelivered `Nack()` should be called.
    29  	//
    30  	// When provided ctx is cancelled, subscriber will close subscribe and close output channel.
    31  	// Provided ctx is set to all produced messages.
    32  	// When Nack or Ack is called on the message, context of the message is canceled.
    33  	Subscribe(ctx context.Context, topic string) (<-chan *Message, error)
    34  	// Close closes all subscriptions with their output channels and flush offsets etc. when needed.
    35  	Close() error
    36  }
    37  
    38  // SubscribeInitializer is used to initialize subscribers.
    39  type SubscribeInitializer interface {
    40  	// SubscribeInitialize can be called to initialize subscribe before consume.
    41  	// When calling Subscribe before Publish, SubscribeInitialize should be not required.
    42  	//
    43  	// Not every Pub/Sub requires this initialization, and it may be optional for performance improvements etc.
    44  	// For detailed SubscribeInitialize functionality, please check Pub/Subs godoc.
    45  	//
    46  	// Implementing SubscribeInitialize is not obligatory.
    47  	SubscribeInitialize(topic string) error
    48  }