-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
pipe.go
232 lines (210 loc) · 5.59 KB
/
pipe.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package gocent
import (
"encoding/json"
"sync"
)
// Pipe allows to send several commands in one HTTP request.
type Pipe struct {
mu sync.RWMutex
commands []Command
}
// Reset allows to clear client command buffer.
func (p *Pipe) Reset() {
p.mu.Lock()
defer p.mu.Unlock()
p.commands = nil
}
func (p *Pipe) add(cmd Command) error {
p.mu.Lock()
defer p.mu.Unlock()
p.commands = append(p.commands, cmd)
return nil
}
type publishRequest struct {
Channel string `json:"channel"`
Data json.RawMessage `json:"data"`
PublishOptions
}
// AddPublish adds publish command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddPublish(channel string, data []byte, opts ...PublishOption) error {
options := &PublishOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "publish",
Params: publishRequest{
Channel: channel,
Data: data,
PublishOptions: *options,
},
}
return p.add(cmd)
}
type broadcastRequest struct {
Channels []string `json:"channels"`
Data json.RawMessage `json:"data"`
PublishOptions
}
// AddBroadcast adds broadcast command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddBroadcast(channels []string, data []byte, opts ...PublishOption) error {
options := &PublishOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "broadcast",
Params: broadcastRequest{
Channels: channels,
Data: data,
PublishOptions: *options,
},
}
return p.add(cmd)
}
type subscribeRequest struct {
Channel string `json:"channel"`
User string `json:"user"`
SubscribeOptions
}
// AddSubscribe adds subscribe command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddSubscribe(channel string, user string, opts ...SubscribeOption) error {
options := &SubscribeOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "subscribe",
Params: subscribeRequest{
Channel: channel,
User: user,
SubscribeOptions: *options,
},
}
return p.add(cmd)
}
type unsubscribeRequest struct {
Channel string `json:"channel"`
User string `json:"user"`
UnsubscribeOptions
}
// AddUnsubscribe adds unsubscribe command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddUnsubscribe(channel string, user string, opts ...UnsubscribeOption) error {
options := &UnsubscribeOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "unsubscribe",
Params: unsubscribeRequest{
Channel: channel,
User: user,
UnsubscribeOptions: *options,
},
}
return p.add(cmd)
}
type disconnectRequest struct {
User string `json:"user"`
DisconnectOptions
}
// AddDisconnect adds disconnect command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddDisconnect(user string, opts ...DisconnectOption) error {
options := &DisconnectOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "disconnect",
Params: disconnectRequest{
User: user,
DisconnectOptions: *options,
},
}
return p.add(cmd)
}
// AddPresence adds presence command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddPresence(channel string) error {
cmd := Command{
Method: "presence",
Params: map[string]interface{}{
"channel": channel,
},
}
return p.add(cmd)
}
// AddPresenceStats adds presence stats command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddPresenceStats(channel string) error {
cmd := Command{
Method: "presence_stats",
Params: map[string]interface{}{
"channel": channel,
},
}
return p.add(cmd)
}
type historyRequest struct {
Channel string `json:"channel"`
HistoryOptions
}
// AddHistory adds history command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddHistory(channel string, opts ...HistoryOption) error {
options := &HistoryOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "history",
Params: historyRequest{
Channel: channel,
HistoryOptions: *options,
},
}
return p.add(cmd)
}
// AddHistoryRemove adds history remove command to client command buffer but not
// actually sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddHistoryRemove(channel string) error {
cmd := Command{
Method: "history_remove",
Params: map[string]interface{}{
"channel": channel,
},
}
return p.add(cmd)
}
type channelsRequest struct {
Pattern string `json:"pattern,omitempty"`
}
// AddChannels adds channels command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddChannels(opts ...ChannelsOption) error {
options := &ChannelsOptions{}
for _, opt := range opts {
opt(options)
}
cmd := Command{
Method: "channels",
Params: channelsRequest{
Pattern: options.Pattern,
},
}
return p.add(cmd)
}
// AddInfo adds info command to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddInfo() error {
cmd := Command{
Method: "info",
Params: map[string]interface{}{},
}
return p.add(cmd)
}