-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
182 lines (153 loc) · 3.88 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
package wspubsub
import (
"net/http"
"sync/atomic"
"time"
"github.com/pkg/errors"
)
// Client represents a connection to the WebSocket server.
type Client struct {
options ClientOptions
id UUID
upgrader WebsocketConnectionUpgrader
logger Logger
receiveHandler atomic.Value
errorHandler atomic.Value
connection WebsocketConnection
messages chan Message
isConnected bool
quit chan struct{}
}
// ID returns unique client id.
func (c *Client) ID() UUID {
return c.id
}
// Connect upgrades the HTTP server connection to the WebSocket protocol.
func (c *Client) Connect(response http.ResponseWriter, request *http.Request) error {
if c.options.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > c.options.DebugFuncTimeLimit {
c.logger.Warnf("wspubsub.client.connect: took=%s", end)
}
}()
}
if c.isConnected {
return NewClientRepeatConnectError(c.id)
}
connection, err := c.upgrader.Upgrade(response, request)
if err != nil {
return errors.WithStack(NewClientConnectError(c.id, err))
}
c.connection = connection
c.isConnected = true
go c.runReader()
go c.runWriter()
return nil
}
// OnReceive registers a handler for incoming messages.
func (c *Client) OnReceive(handler ReceiveHandler) {
c.receiveHandler.Store(handler)
}
// OnError registers a handler for errors occurred while reading or writing connection.
func (c *Client) OnError(handler ErrorHandler) {
c.errorHandler.Store(handler)
}
// Send writes a message to client connection asynchronously.
func (c *Client) Send(message Message) error {
if c.options.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > c.options.DebugFuncTimeLimit {
c.logger.Warnf("wspubsub.client.send: took=%s", end)
}
}()
}
select {
case c.messages <- message:
default:
return errors.WithStack(NewClientSendBufferOverflowError(c.id))
}
return nil
}
// Close closes a client connection.
func (c *Client) Close() error {
if c.options.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > c.options.DebugFuncTimeLimit {
c.logger.Warnf("wspubsub.client.close: took=%s", end)
}
}()
}
if !c.isConnected {
return nil
}
defer func() {
c.isConnected = false
}()
c.quit <- struct{}{}
err := c.connection.Close()
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *Client) runReader() {
receiveHandler := c.receiveHandler.Load().(ReceiveHandler)
errorHandler := c.errorHandler.Load().(ErrorHandler)
for {
message, err := c.connection.Read()
if err != nil {
err := errors.WithStack(NewClientReceiveError(c.id, message, err))
errorHandler(c.id, err)
return
}
receiveHandler(c.id, message)
}
}
func (c *Client) runWriter() {
pingMessage := NewPingMessage()
pingTicker := time.NewTicker(c.options.PingInterval)
defer pingTicker.Stop()
pings := pingTicker.C
messages := c.messages
errorHandler := c.errorHandler.Load().(ErrorHandler)
for {
select {
case <-c.quit:
return
case <-pings:
err := c.connection.Write(pingMessage)
if err != nil {
err := errors.WithStack(NewClientPingError(c.id, pingMessage, err))
errorHandler(c.id, err)
pings = nil
}
case message := <-messages:
err := c.connection.Write(message)
if err != nil {
err := errors.WithStack(NewClientSendError(c.id, message, err))
errorHandler(c.id, err)
messages = nil
}
}
}
}
// NewClient initializes a new Client.
func NewClient(options ClientOptions, id UUID, upgrader WebsocketConnectionUpgrader, logger Logger) *Client {
client := &Client{
options: options,
id: id,
upgrader: upgrader,
logger: logger,
messages: make(chan Message, options.SendBufferSize),
quit: make(chan struct{}),
}
client.receiveHandler.Store(defaultReceiveHandler)
client.errorHandler.Store(defaultErrorHandler)
return client
}