9
9
10
10
"github.com/gorilla/context"
11
11
"github.com/gorilla/websocket"
12
- "github.com/semaphoreui/semaphore/util"
13
12
log "github.com/sirupsen/logrus"
14
13
)
15
14
@@ -33,6 +32,10 @@ const (
33
32
34
33
// Maximum message size allowed from peer.
35
34
maxMessageSize = 512
35
+
36
+ // Maximum size of the connection.send channel.
37
+ // When the channel is full, the hub closes it (see method hub.run).
38
+ connectionChannelSize = 2048
36
39
)
37
40
38
41
type connection struct {
@@ -41,18 +44,23 @@ type connection struct {
41
44
userID int
42
45
}
43
46
44
- func (c * connection ) logError ( err error , msg string ) {
47
+ func (c * connection ) log ( level log. Level , err error , msg string ) {
45
48
log .WithError (err ).WithFields (log.Fields {
46
49
"context" : "websocket" ,
47
50
"user_id" : c .userID ,
48
- }).Error ( msg )
51
+ }).Log ( level , msg )
49
52
}
50
53
51
- func (c * connection ) debugError (err error , msg string ) {
52
- log .WithError (err ).WithFields (log.Fields {
53
- "context" : "websocket" ,
54
- "user_id" : c .userID ,
55
- }).Debug (msg )
54
+ func (c * connection ) logError (err error , msg string ) {
55
+ c .log (log .ErrorLevel , err , msg )
56
+ }
57
+
58
+ func (c * connection ) logWarn (err error , msg string ) {
59
+ c .log (log .DebugLevel , err , msg )
60
+ }
61
+
62
+ func (c * connection ) logDebug (err error , msg string ) {
63
+ c .log (log .DebugLevel , err , msg )
56
64
}
57
65
58
66
// readPump pumps messages from the websocket connection to the hub.
@@ -65,12 +73,16 @@ func (c *connection) readPump() {
65
73
c .ws .SetReadLimit (maxMessageSize )
66
74
67
75
if err := c .ws .SetReadDeadline (tz .Now ().Add (pongWait )); err != nil {
68
- c .logError (err , "Cannot set read deadline" )
76
+ c .logWarn (err , "Cannot set read deadline" )
69
77
}
70
78
71
79
c .ws .SetPongHandler (func (string ) error {
72
- err2 := c .ws .SetReadDeadline (tz .Now ().Add (pongWait ))
73
- util .LogErrorF (err2 , log.Fields {"error" : "Cannot set read deadline" })
80
+ deadline := tz .Now ().Add (pongWait )
81
+
82
+ if err := c .ws .SetReadDeadline (deadline ); err != nil {
83
+ c .logWarn (err , "Cannot set read deadline" )
84
+ }
85
+
74
86
return nil
75
87
})
76
88
@@ -80,7 +92,7 @@ func (c *connection) readPump() {
80
92
81
93
if err != nil {
82
94
if websocket .IsUnexpectedCloseError (err , websocket .CloseGoingAway ) {
83
- c .logError (err , "Cannot read message from websocket" )
95
+ c .logDebug (err , "Cannot read message from websocket" )
84
96
}
85
97
break
86
98
}
@@ -90,8 +102,10 @@ func (c *connection) readPump() {
90
102
// write writes a message with the given message type and payload.
91
103
func (c * connection ) write (mt int , payload []byte ) error {
92
104
93
- if err := c .ws .SetWriteDeadline (tz .Now ().Add (writeWait )); err != nil {
94
- c .logError (err , "Cannot set write deadline" )
105
+ deadline := tz .Now ().Add (writeWait )
106
+
107
+ if err := c .ws .SetWriteDeadline (deadline ); err != nil {
108
+ c .logWarn (err , "Cannot set write deadline" )
95
109
}
96
110
97
111
return c .ws .WriteMessage (mt , payload )
@@ -109,19 +123,23 @@ func (c *connection) writePump() {
109
123
for {
110
124
select {
111
125
case message , ok := <- c .send :
126
+
112
127
if ! ok {
113
128
if err := c .write (websocket .CloseMessage , []byte {}); err != nil {
114
- c .debugError (err , "Cannot send close message" )
129
+ c .logDebug (err , "Cannot send close message" )
115
130
}
116
131
return
117
132
}
118
133
119
134
if err := c .write (websocket .TextMessage , message ); err != nil {
120
- c .debugError (err , "Cannot send message" )
135
+ c .logDebug (err , "Cannot send message" )
136
+ return
121
137
}
138
+
122
139
case <- ticker .C :
140
+
123
141
if err := c .write (websocket .PingMessage , []byte {}); err != nil {
124
- c .debugError (err , "Cannot send ping message" )
142
+ c .logDebug (err , "Cannot send ping message" )
125
143
return
126
144
}
127
145
}
@@ -138,13 +156,16 @@ func Handler(w http.ResponseWriter, r *http.Request) {
138
156
user := usr .(* db.User )
139
157
ws , err := upgrader .Upgrade (w , r , nil )
140
158
if err != nil {
141
- log .Error (err )
159
+ log .WithError (err ).WithFields (log.Fields {
160
+ "context" : "websocket" ,
161
+ "user_id" : user .ID ,
162
+ }).Error ("Cannot upgrade connection" )
142
163
w .WriteHeader (http .StatusInternalServerError )
143
164
return
144
165
}
145
166
146
167
c := & connection {
147
- send : make (chan []byte , 256 ),
168
+ send : make (chan []byte , connectionChannelSize ),
148
169
ws : ws ,
149
170
userID : user .ID ,
150
171
}
0 commit comments