Debian Codegraph @

disconnect.go

raw
1 package centrifuge
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "sync"
8 )
9
10 // Disconnect allows to configure how client will be disconnected from server.
11 // The important note that Disconnect serialized to JSON must be less than 127 bytes
12 // due to WebSocket protocol limitations (because at moment we send Disconnect inside
13 // reason field of WebSocket close handshake).
14 // Note that due to performance reasons we cache Disconnect text representation
15 // for Close Frame on first send to client so changing field values inside existing
16 // Disconnect instance won't be reflected in WebSocket/Sockjs Close frames.
17 type Disconnect struct {
18 // Code is disconnect code.
19 Code uint32 `json:"code,omitempty"`
20 // Reason is a short description of disconnect.
21 Reason string `json:"reason"`
22 // Reconnect gives client an advice to reconnect after disconnect or not.
23 Reconnect bool `json:"reconnect"`
24
25 closeTextOnce sync.Once
26 cachedCloseText string
27 }
28
29 var _ error = (*Disconnect)(nil)
30
31 // String representation.
32 func (d *Disconnect) String() string {
33 return fmt.Sprintf("code: %d, reason: %s, reconnect: %t", d.Code, d.Reason, d.Reconnect)
34 }
35
36 // Error representation.
37 func (d *Disconnect) Error() string {
38 return fmt.Sprintf("disconnected: code: %d, reason: %s, reconnect: %t", d.Code, d.Reason, d.Reconnect)
39 }
40
41 // CloseText allows to build disconnect advice sent inside Close frame.
42 // At moment we don't encode Code here to not duplicate information
43 // since it is sent separately as Code of WebSocket/SockJS Close Frame.
44 func (d *Disconnect) CloseText() string {
45 d.closeTextOnce.Do(func() {
46 buf := strings.Builder{}
47 buf.WriteString(`{"reason":`)
48 reason, _ := json.Marshal(d.Reason)
49 buf.Write(reason)
50 buf.WriteString(`,"reconnect":`)
51 if d.Reconnect {
52 buf.WriteString("true")
53 } else {
54 buf.WriteString("false")
55 }
56 buf.WriteString(`}`)
57 d.cachedCloseText = buf.String()
58 })
59 return d.cachedCloseText
60 }
61
62 // Some predefined disconnect structures used by library internally. Though
63 // it's always possible to create Disconnect with any field values on the fly.
64 // Library users supposed to use codes in range 4000-4999 for custom disconnects.
65 var (
66 // DisconnectNormal is clean disconnect when client cleanly closed connection.
67 DisconnectNormal = &Disconnect{
68 Code: 3000,
69 Reason: "normal",
70 Reconnect: true,
71 }
72 // DisconnectShutdown sent when node is going to shut down.
73 DisconnectShutdown = &Disconnect{
74 Code: 3001,
75 Reason: "shutdown",
76 Reconnect: true,
77 }
78 // DisconnectInvalidToken sent when client came with invalid token.
79 DisconnectInvalidToken = &Disconnect{
80 Code: 3002,
81 Reason: "invalid token",
82 Reconnect: false,
83 }
84 // DisconnectBadRequest sent when client uses malformed protocol
85 // frames or wrong order of commands.
86 DisconnectBadRequest = &Disconnect{
87 Code: 3003,
88 Reason: "bad request",
89 Reconnect: false,
90 }
91 // DisconnectServerError sent when internal error occurred on server.
92 DisconnectServerError = &Disconnect{
93 Code: 3004,
94 Reason: "internal server error",
95 Reconnect: true,
96 }
97 // DisconnectExpired sent when client connection expired.
98 DisconnectExpired = &Disconnect{
99 Code: 3005,
100 Reason: "expired",
101 Reconnect: true,
102 }
103 // DisconnectSubExpired sent when client subscription expired.
104 DisconnectSubExpired = &Disconnect{
105 Code: 3006,
106 Reason: "subscription expired",
107 Reconnect: true,
108 }
109 // DisconnectStale sent to close connection that did not become
110 // authenticated in configured interval after dialing.
111 DisconnectStale = &Disconnect{
112 Code: 3007,
113 Reason: "stale",
114 Reconnect: false,
115 }
116 // DisconnectSlow sent when client can't read messages fast enough.
117 DisconnectSlow = &Disconnect{
118 Code: 3008,
119 Reason: "slow",
120 Reconnect: true,
121 }
122 // DisconnectWriteError sent when an error occurred while writing to
123 // client connection.
124 DisconnectWriteError = &Disconnect{
125 Code: 3009,
126 Reason: "write error",
127 Reconnect: true,
128 }
129 // DisconnectInsufficientState sent when server detects wrong client
130 // position in channel Publication stream. Disconnect allows client
131 // to restore missed publications on reconnect.
132 DisconnectInsufficientState = &Disconnect{
133 Code: 3010,
134 Reason: "insufficient state",
135 Reconnect: true,
136 }
137 // DisconnectForceReconnect sent when server disconnects connection.
138 DisconnectForceReconnect = &Disconnect{
139 Code: 3011,
140 Reason: "force reconnect",
141 Reconnect: true,
142 }
143 // DisconnectForceNoReconnect sent when server disconnects connection
144 // and asks it to not reconnect again.
145 DisconnectForceNoReconnect = &Disconnect{
146 Code: 3012,
147 Reason: "force disconnect",
148 Reconnect: false,
149 }
150 // DisconnectConnectionLimit can be sent when client connection exceeds a
151 // configured connection limit (per user ID or due to other rule).
152 DisconnectConnectionLimit = &Disconnect{
153 Code: 3013,
154 Reason: "connection limit",
155 Reconnect: false,
156 }
157 )