-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocketManager.go
More file actions
214 lines (169 loc) · 5.63 KB
/
Copy pathsocketManager.go
File metadata and controls
214 lines (169 loc) · 5.63 KB
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
package nymsocketmanager
import (
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/rs/zerolog"
"golang.org/x/xerrors"
)
func NewSocketManager(connectionURI string, messageHandler func([]byte, func([]byte) error), parentLogger *zerolog.Logger) (*SocketManager, error) {
if len(connectionURI) == 0 {
err := xerrors.Errorf("connection URI cannot be empty")
return nil, err
}
if nil == parentLogger {
err := xerrors.Errorf("logger needs to be defined")
return nil, err
}
socketLogger := parentLogger.With().Str(ComponentField, "SocketManager").Logger()
return &SocketManager{
connectionURI: connectionURI,
messageHandler: messageHandler,
logger: &socketLogger,
}, nil
}
type SocketManager struct {
sync.Mutex
connectionURI string
connection *websocket.Conn
selfInstanceStoppedChan chan struct{}
// Related to listening
socketListener *SocketListener
messageHandler func([]byte, func([]byte) error)
closedSocketListenerChan chan struct{}
// Related to sending
senderMutex sync.Mutex
logger *zerolog.Logger
}
func (s *SocketManager) IsRunning() bool {
s.Lock()
defer s.Unlock()
return nil != s.connection
}
func (s *SocketManager) Start() (chan struct{}, error) {
s.Lock()
defer s.Unlock()
s.logger.Debug().Msg("starting SocketManager")
// Do not start if already started
if nil != s.connection {
s.logger.Warn().Msgf("connection to websocket %s already established. Resuming...", s.connectionURI)
return nil, nil
}
// Open WS connection
var e error
s.connection, _, e = websocket.DefaultDialer.Dial(s.connectionURI, nil)
if nil != e {
err := xerrors.Errorf("failed to open connection to \"%v\". Is the websocket up and running?", s.connectionURI)
s.logger.Warn().Msg(err.Error())
return nil, err
}
s.logger.Debug().Msgf("successfully opened connection to \"%v\"", s.connectionURI)
// After which we start a listener for the packets
s.socketListener, s.closedSocketListenerChan, e = NewSocketListener(s.connection, func(msg []byte) {
s.messageHandler(msg, s.Send)
}, s.Stop, s.logger)
if nil != e {
err := xerrors.Errorf("failed to initiate the socketListener: %v", e)
s.logger.Warn().Msg(err.Error())
// Cancel progress so far
s.selfDestruct()
return nil, err
}
go s.socketListener.Listen()
s.selfInstanceStoppedChan = make(chan struct{}, 1)
s.logger.Debug().Msg("started SocketManager")
return s.selfInstanceStoppedChan, nil
}
func (s *SocketManager) Stop() {
s.Lock()
defer s.Unlock()
s.logger.Debug().Msg("stopping SocketManager")
// Do not stop if not running
if nil == s.connection {
return
}
s.selfDestruct()
s.logger.Debug().Msg("stopped SocketManager")
}
// selfDestruct will close all channel and free resources when requested
// called from methods that already acquired the lock
func (s *SocketManager) selfDestruct() {
s.logger.Debug().Msg("selfDestructing")
// Ensure we do not close everthing if everything is closed already
if nil == s.selfInstanceStoppedChan {
s.logger.Warn().Msg("already selfDestructed (selfInstanceStoppedChan is nil)")
return
}
// How to properly close the connection (well, almost):
///////////////////////////////////////////////////////
/* This method properly close it from the other end's perspective
* on this side, it results in an abnormal closure, while we send a CloseNormalClosure message
* It seems to be an issue in this lib (ref: https://github.com/gorilla/websocket/pull/487).
*/
// If socketListener is defined, we close it
if nil != s.socketListener {
// This will close the socketListener
s.logger.Trace().Msg("sending close signal on socket and waiting for confirmation")
s.sendCloseSignal()
// Waiting for confirmation (or timeout)
deadline := 5 * time.Second
select {
case <-s.closedSocketListenerChan:
s.logger.Debug().Msg("underlying connection closed")
case <-time.After(deadline):
s.logger.Debug().Msgf("timed-out (%v) on waiting for underlying connection to close", deadline)
}
s.logger.Trace().Msg("removing socketListener")
s.socketListener = nil
}
if nil != s.connection {
s.logger.Trace().Msg("closing local connection")
e := s.connection.Close()
if e != nil {
s.logger.Err(e).Msg("")
}
s.connection = nil
}
// If initialized, we close the selfInstanceStoppedChan
if nil != s.selfInstanceStoppedChan {
s.logger.Trace().Msg("closing channel to indicate upstream that closed")
close(s.selfInstanceStoppedChan)
s.selfInstanceStoppedChan = nil
}
s.logger.Debug().Msg("selfDestructed")
}
func (s *SocketManager) Send(message []byte) error {
s.senderMutex.Lock()
defer s.senderMutex.Unlock()
if nil == s.connection {
err := xerrors.Errorf("connection is closed, cannot send to %v", s.connectionURI)
s.logger.Warn().Msg(err.Error())
return err
}
e := s.connection.WriteMessage(websocket.TextMessage, message)
if nil != e {
err := xerrors.Errorf("failed to send message: %v", e)
s.logger.Warn().Msg(err.Error())
return err
}
return nil
}
// Send message to properly close the socket connection
// This will close any listener connected to this socket
func (s *SocketManager) sendCloseSignal() error {
s.senderMutex.Lock()
defer s.senderMutex.Unlock()
if nil == s.connection {
err := xerrors.Errorf("connection is undefined. Is the SocketManager started?")
s.logger.Warn().Msg(err.Error())
return err
}
e := s.connection.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if nil != e {
err := xerrors.Errorf("failed to write close: %v", e)
s.logger.Warn().Msg(err.Error())
return err
}
s.logger.Debug().Msg("sent websocket close message")
return nil
}