Debian Codegraph @

broker.go

raw
1 package centrifuge
2
3 import (
4 "context"
5 "time"
6 )
7
8 // Publication is a data sent to a channel.
9 type Publication struct {
10 // Offset is an incremental position number inside a history stream.
11 // Zero value means that channel does not maintain Publication stream.
12 Offset uint64
13 // Data published to a channel.
14 Data []byte
15 // Info is an optional information about client connection published this data.
16 Info *ClientInfo
17 }
18
19 // ClientInfo contains information about client connection.
20 type ClientInfo struct {
21 // ClientID is a client unique id.
22 ClientID string
23 // UserID is an ID of authenticated user. Zero value means anonymous user.
24 UserID string
25 // ConnInfo is an additional information about connection.
26 ConnInfo []byte
27 // ChanInfo is an additional information about connection in context of
28 // channel subscription.
29 ChanInfo []byte
30 }
31
32 // BrokerEventHandler can handle messages received from PUB/SUB system.
33 type BrokerEventHandler interface {
34 // HandlePublication to handle received Publications.
35 HandlePublication(ch string, pub *Publication, sp StreamPosition) error
36 // HandleJoin to handle received Join messages.
37 HandleJoin(ch string, info *ClientInfo) error
38 // HandleLeave to handle received Leave messages.
39 HandleLeave(ch string, info *ClientInfo) error
40 // HandleControl to handle received control data.
41 HandleControl(data []byte) error
42 }
43
44 // HistoryFilter allows to filter history according to fields set.
45 type HistoryFilter struct {
46 // Since used to extract publications from stream since provided StreamPosition.
47 Since *StreamPosition
48 // Limit number of publications to return.
49 // -1 means no limit - i.e. return all publications currently in stream.
50 // 0 means that caller only interested in current stream top position so
51 // Broker should not return any publications.
52 Limit int
53 }
54
55 // StreamPosition contains fields to describe position in stream.
56 // At moment this is used for automatic recovery mechanics. More info about stream
57 // recovery in docs: https://centrifugal.github.io/centrifugo/server/recover/.
58 type StreamPosition struct {
59 // Offset defines publication incremental offset inside a stream.
60 Offset uint64
61 // Epoch allows handling situations when storage
62 // lost stream entirely for some reason (expired or lost after restart) and we
63 // want to track this fact to prevent successful recovery from another stream.
64 // I.e. for example we have stream [1, 2, 3], then it's lost and new stream
65 // contains [1, 2, 3, 4], client that recovers from position 3 will only receive
66 // publication 4 missing 1, 2, 3 from new stream. With epoch we can tell client
67 // that correct recovery is not possible.
68 Epoch string
69 }
70
71 // Closer is an interface that Broker and PresenceManager can optionally implement
72 // if they need to close any resources on Centrifuge Node graceful shutdown.
73 type Closer interface {
74 // Close when called should clean up used resources.
75 Close(ctx context.Context) error
76 }
77
78 // PublishOptions define some fields to alter behaviour of Publish operation.
79 type PublishOptions struct {
80 // HistoryTTL sets history ttl to expire inactive history streams.
81 // Current Broker implementations only work with seconds resolution for TTL.
82 HistoryTTL time.Duration
83 // HistorySize sets history size limit to prevent infinite stream growth.
84 HistorySize int
85 // ClientInfo to include into Publication. By default no ClientInfo will be appended.
86 ClientInfo *ClientInfo
87 }
88
89 // Broker is responsible for PUB/SUB mechanics.
90 type Broker interface {
91 // Run called once on start when broker already set to node. At
92 // this moment node is ready to process broker events.
93 Run(BrokerEventHandler) error
94
95 // Subscribe node on channel to listen all messages coming from channel.
96 Subscribe(ch string) error
97 // Unsubscribe node from channel to stop listening messages from it.
98 Unsubscribe(ch string) error
99
100 // Publish allows to send data into channel. Data should be
101 // delivered to all clients subscribed to this channel at moment on any
102 // Centrifuge node (with at most once delivery guarantee).
103 //
104 // Broker can optionally maintain publication history inside channel according
105 // to PublishOptions provided. See History method for rules that should be implemented
106 // for accessing Publications from history stream.
107 //
108 // Saving message to a history stream and publish to PUB/SUB should be an atomic
109 // operation per channel.
110 //
111 // StreamPosition returned here describes current stream top offset and epoch.
112 // For channels without history this StreamPosition should be empty.
113 Publish(ch string, data []byte, opts PublishOptions) (StreamPosition, error)
114 // PublishJoin publishes Join Push message into channel.
115 PublishJoin(ch string, info *ClientInfo) error
116 // PublishLeave publishes Leave Push message into channel.
117 PublishLeave(ch string, info *ClientInfo) error
118 // PublishControl allows to send control command data. If nodeID is empty string
119 // then message should be delivered to all running nodes, if nodeID is set then
120 // message should be delivered only to node with specified ID.
121 PublishControl(data []byte, nodeID string) error
122
123 // History used to extract Publications from history stream.
124 // Publications returned according to HistoryFilter which allows to set several
125 // filtering options. StreamPosition returned describes current history stream
126 // top offset and epoch.
127 History(ch string, filter HistoryFilter) ([]*Publication, StreamPosition, error)
128 // RemoveHistory removes history from channel. This is in general not
129 // needed as history expires automatically (based on history_lifetime)
130 // but sometimes can be useful for application logic.
131 RemoveHistory(ch string) error
132 }