forked from ThreeDotsLabs/watermill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub.go
More file actions
51 lines (47 loc) · 2.09 KB
/
Copy pathpubsub.go
File metadata and controls
51 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package message
import (
"context"
)
// Publisher is the emitting part of a Pub/Sub.
type Publisher interface {
// Publish publishes provided messages to the given topic.
//
// Publish can be synchronous or asynchronous - it depends on the implementation.
//
// Most publisher implementations don't support atomic publishing of messages.
// This means that if publishing one of the messages fails, the next messages will not be published.
//
// Publish does not work with a single Context.
// Use the Context() method of each message instead.
//
// Publish must be thread safe.
Publish(topic string, messages ...*Message) error
// Close should flush unsent messages if publisher is async.
Close() error
}
// Subscriber is the consuming part of the Pub/Sub.
type Subscriber interface {
// Subscribe returns an output channel with messages from the provided topic.
// The channel is closed after Close() is called on the subscriber.
//
// To receive the next message, `Ack()` must be called on the received message.
// If message processing fails and the message should be redelivered `Nack()` should be called instead.
//
// When the provided ctx is canceled, the subscriber closes the subscription and the output channel.
// The provided ctx is passed to all produced messages.
// When Nack or Ack is called on the message, the context of the message is canceled.
Subscribe(ctx context.Context, topic string) (<-chan *Message, error)
// Close closes all subscriptions with their output channels and flushes offsets etc. when needed.
Close() error
}
// SubscribeInitializer is used to initialize subscribers.
type SubscribeInitializer interface {
// SubscribeInitialize can be called to initialize subscribe before consume.
// When calling Subscribe before Publish, SubscribeInitialize should be not required.
//
// Not every Pub/Sub requires this initialization, and it may be optional for performance improvements etc.
// For detailed SubscribeInitialize functionality, please check Pub/Subs godoc.
//
// Implementing SubscribeInitialize is not obligatory.
SubscribeInitialize(topic string) error
}