This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
360 lines (300 loc) · 9.23 KB
/
client.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package client
import (
"sync/atomic"
"fmt"
"log"
"sync"
"time"
webwire "github.com/qbeon/webwire-go"
msg "github.com/qbeon/webwire-go/message"
pld "github.com/qbeon/webwire-go/payload"
reqman "github.com/qbeon/webwire-go/requestManager"
)
const supportedProtocolVersion = "1.4"
// Status represents the status of a client instance
type Status = int32
const (
// StatDisconnected represents a temporarily disconnected client instance
StatDisconnected Status = 0
// StatConnected represents a connected client instance
StatConnected Status = 1
)
// autoconnectStatus represents the activation of auto-reconnection
type autoconnectStatus = int32
const (
// autoconnectDisabled represents permanently disabled auto-reconnection
autoconnectDisabled = 0
// autoconnectDeactivated represents deactivated auto-reconnection
autoconnectDeactivated = 1
// autoconnectEnabled represents activated auto-reconnection
autoconnectEnabled = 2
)
// Client represents an instance of one of the servers clients
type Client struct {
serverAddr string
impl Implementation
sessionInfoParser webwire.SessionInfoParser
status Status
defaultReqTimeout time.Duration
reconnInterval time.Duration
autoconnect autoconnectStatus
sessionLock sync.RWMutex
session *webwire.Session
// The API lock synchronizes concurrent access to the public client interface.
// Request, TimedRequest and Signal methods are locked with a shared lock
// because performing multiple requests and/or signals simultaneously is fine.
// The Connect, RestoreSession, CloseSession and Close methods are locked exclusively
// because they should temporarily block any other interaction with this client instance.
apiLock sync.RWMutex
// backReconn is a dam that's flushed when the client establishes a connection.
backReconn *dam
// connecting prevents multiple autoconnection attempts from spawning
// superfluous multiple goroutines each polling the server
connecting bool
// connectingLock protects the connecting flag from concurrent access
connectingLock sync.RWMutex
connectLock sync.Mutex
conn webwire.Socket
readerClosing chan bool
requestManager reqman.RequestManager
// Loggers
warningLog *log.Logger
errorLog *log.Logger
}
// NewClient creates a new client instance.
func NewClient(
serverAddress string,
implementation Implementation,
opts Options,
) *Client {
if implementation == nil {
panic(fmt.Errorf(
"A webwire client requires a client implementation, got nil",
))
}
// Prepare configuration
opts.SetDefaults()
// Enable autoconnect by default
autoconnect := autoconnectStatus(autoconnectEnabled)
if opts.Autoconnect == webwire.Disabled {
autoconnect = autoconnectDisabled
}
// Initialize new client
newClt := &Client{
serverAddress,
implementation,
opts.SessionInfoParser,
StatDisconnected,
opts.DefaultRequestTimeout,
opts.ReconnectionInterval,
autoconnect,
sync.RWMutex{},
nil,
sync.RWMutex{},
newDam(),
false,
sync.RWMutex{},
sync.Mutex{},
webwire.NewSocket(),
make(chan bool, 1),
reqman.NewRequestManager(),
opts.WarnLog,
opts.ErrorLog,
}
if autoconnect == autoconnectEnabled {
// Asynchronously connect to the server immediately after initialization.
// Call in another goroutine to not block the contructor function caller.
// Set timeout to zero, try indefinitely until connected.
go newClt.tryAutoconnect(0)
}
return newClt
}
// Status returns the current client status
// which is either disabled, disconnected or connected.
// The client is considered disabled when it was manually closed through client.Close,
// while disconnected is considered a temporary connection loss.
// A disabled client won't autoconnect until enabled again.
func (clt *Client) Status() Status {
return atomic.LoadInt32(&clt.status)
}
// Connect connects the client to the configured server and
// returns an error in case of a connection failure.
// Automatically tries to restore the previous session.
// Enables autoconnect if it was disabled
func (clt *Client) Connect() error {
if atomic.LoadInt32(&clt.autoconnect) == autoconnectDeactivated {
atomic.StoreInt32(&clt.autoconnect, autoconnectEnabled)
}
return clt.connect()
}
// Request sends a request containing the given payload to the server
// and asynchronously returns the servers response
// blocking the calling goroutine.
// Returns an error if the request failed for some reason
func (clt *Client) Request(
name string,
payload webwire.Payload,
) (webwire.Payload, error) {
clt.apiLock.RLock()
defer clt.apiLock.RUnlock()
if err := clt.tryAutoconnect(clt.defaultReqTimeout); err != nil {
return nil, err
}
return clt.sendRequest(
scanPayloadEncoding(payload),
name,
payload,
clt.defaultReqTimeout,
)
}
// TimedRequest sends a request containing the given payload to the server
// and asynchronously returns the servers reply
// blocking the calling goroutine.
// Returns an error if the given timeout was exceeded awaiting the response
// or another failure occurred
func (clt *Client) TimedRequest(
name string,
payload webwire.Payload,
timeout time.Duration,
) (webwire.Payload, error) {
clt.apiLock.RLock()
defer clt.apiLock.RUnlock()
if err := clt.tryAutoconnect(timeout); err != nil {
return nil, err
}
return clt.sendRequest(
scanPayloadEncoding(payload),
name,
payload,
timeout,
)
}
// Signal sends a signal containing the given payload to the server
func (clt *Client) Signal(name string, payload webwire.Payload) error {
clt.apiLock.RLock()
defer clt.apiLock.RUnlock()
if err := clt.tryAutoconnect(clt.defaultReqTimeout); err != nil {
return err
}
// Require either a name or a payload or both
if len(name) < 1 && (payload == nil || len(payload.Data()) < 1) {
return webwire.NewProtocolErr(
fmt.Errorf("Invalid request, request message requires " +
"either a name, a payload or both but is missing both",
),
)
}
// Initialize payload encoding & data
var encoding webwire.PayloadEncoding
var data []byte
if payload != nil {
encoding = payload.Encoding()
data = payload.Data()
}
return clt.conn.Write(msg.NewSignalMessage(
name,
encoding,
data,
))
}
// Session returns an exact copy of the session object or nil if there's no
// session currently assigned to this client
func (clt *Client) Session() *webwire.Session {
clt.sessionLock.RLock()
defer clt.sessionLock.RUnlock()
if clt.session == nil {
return nil
}
clone := &webwire.Session{
Key: clt.session.Key,
Creation: clt.session.Creation,
}
if clt.session.Info != nil {
clone.Info = clt.session.Info.Copy()
}
return clone
}
// SessionInfo returns a copy of the session info field value
// in the form of an empty interface to be casted to either concrete type
func (clt *Client) SessionInfo(fieldName string) interface{} {
clt.sessionLock.RLock()
defer clt.sessionLock.RUnlock()
if clt.session == nil || clt.session.Info == nil {
return nil
}
return clt.session.Info.Value(fieldName)
}
// PendingRequests returns the number of currently pending requests
func (clt *Client) PendingRequests() int {
return clt.requestManager.PendingRequests()
}
// RestoreSession tries to restore the previously opened session.
// Fails if a session is currently already active
func (clt *Client) RestoreSession(sessionKey []byte) error {
clt.apiLock.Lock()
defer clt.apiLock.Unlock()
clt.sessionLock.RLock()
if clt.session != nil {
clt.sessionLock.RUnlock()
return fmt.Errorf("Can't restore session if another one is already active")
}
clt.sessionLock.RUnlock()
if err := clt.tryAutoconnect(clt.defaultReqTimeout); err != nil {
return err
}
restoredSession, err := clt.requestSessionRestoration(sessionKey)
if err != nil {
return err
}
clt.sessionLock.Lock()
clt.session = restoredSession
clt.sessionLock.Unlock()
return nil
}
// CloseSession disables the currently active session
// and acknowledges the server if connected.
// The session will be destroyed if this is it's last connection remaining.
// If the client is not connected then the synchronization is skipped.
// Does nothing if there's no active session
func (clt *Client) CloseSession() error {
clt.apiLock.Lock()
defer clt.apiLock.Unlock()
clt.sessionLock.RLock()
if clt.session == nil {
clt.sessionLock.RUnlock()
return nil
}
clt.sessionLock.RUnlock()
// Synchronize session closure to the server if connected
if atomic.LoadInt32(&clt.status) == StatConnected {
if _, err := clt.sendNamelessRequest(
msg.MsgCloseSession,
pld.Payload{},
clt.defaultReqTimeout,
); err != nil {
return err
}
}
// Reset session locally after destroying it on the server
clt.sessionLock.Lock()
clt.session = nil
clt.sessionLock.Unlock()
return nil
}
// Close gracefully closes the connection and disables the client.
// A disabled client won't autoconnect until enabled again.
func (clt *Client) Close() {
clt.apiLock.Lock()
defer clt.apiLock.Unlock()
if atomic.LoadInt32(&clt.autoconnect) != autoconnectDisabled {
atomic.StoreInt32(&clt.autoconnect, autoconnectDeactivated)
}
if atomic.LoadInt32(&clt.status) != StatConnected {
return
}
if err := clt.conn.Close(); err != nil {
clt.errorLog.Printf("Failed closing connection: %s", err)
}
// Wait for the reader goroutine to die before returning
<-clt.readerClosing
}