-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
disconnect.go
181 lines (174 loc) · 6.29 KB
/
disconnect.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
package centrifuge
import (
"fmt"
)
// Disconnect allows configuring how client will be disconnected from a server.
// A server can provide a Disconnect.Code and Disconnect.Reason to a client. Clients
// can execute some custom logic based on a certain Disconnect.Code. Code is also
// used for metric collection. Disconnect.Reason is optional and exists mostly for
// human-readable description of returned code – i.e. for logging, debugging etc.
//
// The important note is that Disconnect.Reason must be less than 127 bytes
// due to WebSocket protocol limitations.
//
// Codes have some rules which should be followed by a client connector implementation.
// These rules described below.
//
// Codes in range 0-2999 should not be used by a Centrifuge library user. Those are
// reserved for the client-side and transport specific needs. Codes in range >=5000
// should not be used also. Those are reserved by Centrifuge.
//
// Client should reconnect upon receiving code in range 3000-3499, 4000-4499, >=5000.
// For codes <3000 reconnect behavior can be adjusted for specific transport.
//
// Codes in range 3500-3999 and 4500-4999 are application terminal codes, no automatic
// reconnect should be made by a client implementation.
//
// Library users supposed to use codes in range 4000-4999 for creating custom
// disconnects.
type Disconnect struct {
// Code is a disconnect code.
Code uint32 `json:"code,omitempty"`
// Reason is a short description of disconnect code for humans.
Reason string `json:"reason"`
}
// String representation.
func (d Disconnect) String() string {
return fmt.Sprintf("code: %d, reason: %s", d.Code, d.Reason)
}
// Error to use Disconnect as a callback handler error to signal Centrifuge
// that client must be disconnected with corresponding Code and Reason.
func (d Disconnect) Error() string {
return d.String()
}
// DisconnectConnectionClosed is a special Disconnect object used when
// client connection was closed without any advice from a server side.
// This can be a clean disconnect, or temporary disconnect of the client
// due to internet connection loss. Server can not distinguish the actual
// reason of disconnect.
var DisconnectConnectionClosed = Disconnect{
Code: 3000,
Reason: "connection closed",
}
// Some predefined non-terminal disconnect structures used by
// the library internally.
var (
// DisconnectShutdown issued when node is going to shut down.
DisconnectShutdown = Disconnect{
Code: 3001,
Reason: "shutdown",
}
// DisconnectServerError issued when internal error occurred on server.
DisconnectServerError = Disconnect{
Code: 3004,
Reason: "internal server error",
}
// DisconnectExpired issued when client connection expired.
DisconnectExpired = Disconnect{
Code: 3005,
Reason: "connection expired",
}
// DisconnectSubExpired issued when client subscription expired.
DisconnectSubExpired = Disconnect{
Code: 3006,
Reason: "subscription expired",
}
// DisconnectSlow issued when client can't read messages fast enough.
DisconnectSlow = Disconnect{
Code: 3008,
Reason: "slow",
}
// DisconnectWriteError issued when an error occurred while writing to
// client connection.
DisconnectWriteError = Disconnect{
Code: 3009,
Reason: "write error",
}
// DisconnectInsufficientState issued when server detects wrong client
// position in channel Publication stream. Disconnect allows client
// to restore missed publications on reconnect.
DisconnectInsufficientState = Disconnect{
Code: 3010,
Reason: "insufficient state",
}
// DisconnectForceReconnect issued when server disconnects connection.
DisconnectForceReconnect = Disconnect{
Code: 3011,
Reason: "force reconnect",
}
// DisconnectNoPong may be issued when server disconnects bidirectional
// connection due to no pong received to application-level server-to-client
// pings in a configured time.
DisconnectNoPong = Disconnect{
Code: 3012,
Reason: "no pong",
}
// DisconnectTooManyRequests may be issued when client sends too many commands to a server.
DisconnectTooManyRequests = Disconnect{
Code: 3013,
Reason: "too many requests",
}
)
// The codes below are built-in terminal codes.
var (
// DisconnectInvalidToken issued when client came with invalid token.
DisconnectInvalidToken = Disconnect{
Code: 3500,
Reason: "invalid token",
}
// DisconnectBadRequest issued when client uses malformed protocol frames.
DisconnectBadRequest = Disconnect{
Code: 3501,
Reason: "bad request",
}
// DisconnectStale issued to close connection that did not become
// authenticated in configured interval after dialing.
DisconnectStale = Disconnect{
Code: 3502,
Reason: "stale",
}
// DisconnectForceNoReconnect issued when server disconnects connection
// and asks it to not reconnect again.
DisconnectForceNoReconnect = Disconnect{
Code: 3503,
Reason: "force disconnect",
}
// DisconnectConnectionLimit can be issued when client connection exceeds a
// configured connection limit (per user ID or due to other rule).
DisconnectConnectionLimit = Disconnect{
Code: 3504,
Reason: "connection limit",
}
// DisconnectChannelLimit can be issued when client connection exceeds a
// configured channel limit.
DisconnectChannelLimit = Disconnect{
Code: 3505,
Reason: "channel limit",
}
// DisconnectInappropriateProtocol can be issued when client connection format can not
// handle incoming data. For example, this happens when JSON-based clients receive
// binary data in a channel. This is usually an indicator of programmer error, JSON
// clients can not handle binary.
DisconnectInappropriateProtocol = Disconnect{
Code: 3506,
Reason: "inappropriate protocol",
}
// DisconnectPermissionDenied may be issued when client attempts accessing a server without
// enough permissions.
DisconnectPermissionDenied = Disconnect{
Code: 3507,
Reason: "permission denied",
}
// DisconnectNotAvailable may be issued when ErrorNotAvailable does not fit message type, for example
// we issue DisconnectNotAvailable when client sends asynchronous message without MessageHandler set
// on server side.
DisconnectNotAvailable = Disconnect{
Code: 3508,
Reason: "not available",
}
// DisconnectTooManyErrors may be issued when client generates too many errors.
DisconnectTooManyErrors = Disconnect{
Code: 3509,
Reason: "too many errors",
}
)