Skip to content
This repository was archived by the owner on Sep 29, 2024. It is now read-only.

Commit 0e602dc

Browse files
authored
add errors and cast check (#468)
* refactor conn.go method(serveRead) * fixed issue#444 golangci-lint error * try my best to avoid ignoring error(exclude test file) * update README.md,change to check error * update README.md,change to check error * revert golangci-lint * revert golangci-lint code * revert golangci-lint code * add error msg * fixed by comments * fixed by comments Co-authored-by: ralphchen <Wwkkvikthh123>
1 parent 0738184 commit 0e602dc

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

engineio/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ func main() {
7272

7373
## License
7474

75-
The 3-clause BSD License - see LICENSE for more details
75+
The 3-clause BSD License - see [LICENSE](https://opensource.org/licenses/BSD-3-Clause) for more details

engineio/client.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package engineio
22

33
import (
4+
"fmt"
45
"io"
56
"net"
67
"net/http"
@@ -66,10 +67,14 @@ func (c *client) NextReader() (session.FrameType, io.ReadCloser, error) {
6667

6768
switch pt {
6869
case packet.PONG:
69-
c.conn.SetReadDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout))
70+
if err = c.conn.SetReadDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)); err != nil {
71+
return 0, nil, err
72+
}
73+
7074
case packet.CLOSE:
7175
c.Close()
7276
return 0, nil, io.EOF
77+
7378
case packet.MESSAGE:
7479
return session.FrameType(ft), r, nil
7580
}
@@ -116,6 +121,9 @@ func (c *client) serve() {
116121
if err := w.Close(); err != nil {
117122
return
118123
}
119-
c.conn.SetWriteDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout))
124+
125+
if err = c.conn.SetWriteDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)); err != nil {
126+
fmt.Printf("set writer's deadline error,msg:%s\n", err.Error())
127+
}
120128
}
121129
}

engineio/transport/polling/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package polling
22

33
import (
44
"bytes"
5+
"fmt"
56
"html/template"
67
"net"
78
"net/http"
@@ -64,7 +65,7 @@ func (c *serverConn) SetHeaders(w http.ResponseWriter, r *http.Request) {
6465
w.Header().Set("X-XSS-Protection", "0")
6566
}
6667

67-
//just in case the default behaviour gets changed and it has to handle an origin check
68+
// just in case the default behaviour gets changed and it has to handle an origin check
6869
checkOrigin := Default.CheckOrigin
6970
if c.transport.CheckOrigin != nil {
7071
checkOrigin = c.transport.CheckOrigin
@@ -136,6 +137,9 @@ func (c *serverConn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
136137
}
137138

138139
_, err = w.Write([]byte("ok"))
140+
if err != nil {
141+
fmt.Printf("ack post err=%s\n", err.Error())
142+
}
139143

140144
default:
141145
http.Error(w, "invalid method", http.StatusBadRequest)

namespace_conn.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,4 @@ func (nc *namespaceConn) dispatch(header parser.Header) {
132132
return
133133
}
134134
}
135-
return
136135
}

redis_broadcast.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,13 @@ func newRedisBroadcast(nsp string, adapter *RedisAdapterOptions) (*redisBroadcas
129129
bc.resChannel = bc.prefix + "-response#" + bc.nsp
130130
bc.requests = make(map[string]interface{})
131131

132-
bc.sub.PSubscribe(bc.prefix + "#" + bc.nsp + "#*")
133-
bc.sub.Subscribe(bc.reqChannel, bc.resChannel)
132+
if err = bc.sub.PSubscribe(bc.prefix + "#" + bc.nsp + "#*"); err != nil {
133+
return nil, err
134+
}
135+
136+
if err = bc.sub.Subscribe(bc.reqChannel, bc.resChannel); err != nil {
137+
return nil, err
138+
}
134139

135140
go func() {
136141
for {
@@ -144,7 +149,10 @@ func newRedisBroadcast(nsp string, adapter *RedisAdapterOptions) (*redisBroadcas
144149
break
145150
}
146151

147-
bc.onMessage(m.Channel, m.Data)
152+
err = bc.onMessage(m.Channel, m.Data)
153+
if err != nil {
154+
return
155+
}
148156
case redis.Subscription:
149157
if m.Count == 0 {
150158
return
@@ -172,9 +180,13 @@ func (bc *redisBroadcast) AllRooms() []string {
172180
req.done = make(chan bool, 1)
173181

174182
bc.requests[req.RequestID] = &req
175-
bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON)
183+
_, err := bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON)
184+
if err != nil {
185+
return []string{} // if error occurred,return empty
186+
}
176187

177188
<-req.done
189+
178190
rooms := make([]string, 0, len(req.rooms))
179191
for room := range req.rooms {
180192
rooms = append(rooms, room)
@@ -338,6 +350,9 @@ func (bc *redisBroadcast) onMessage(channel string, msg []byte) error {
338350
}
339351

340352
event, ok := opts[1].(string)
353+
if !ok {
354+
return errors.New("invalid event")
355+
}
341356

342357
if room != "" {
343358
bc.send(room, event, args...)
@@ -355,19 +370,20 @@ func (bc *redisBroadcast) getNumSub(channel string) (int, error) {
355370
return 0, err
356371
}
357372

358-
var numSub64 int64
359-
numSub64 = rs.([]interface{})[1].(int64)
360-
return int(numSub64), nil
373+
numSub64, ok := rs.([]interface{})[1].(int)
374+
if !ok {
375+
return 0, errors.New("redis reply cast to int error")
376+
}
377+
return numSub64, nil
361378
}
362379

363380
// Handle request from redis channel.
364381
func (bc *redisBroadcast) onRequest(msg []byte) {
365382
var req map[string]string
366-
err := json.Unmarshal(msg, &req)
367-
if err != nil {
383+
384+
if err := json.Unmarshal(msg, &req); err != nil {
368385
return
369386
}
370-
// log.Println("on request:", req)
371387

372388
var res interface{}
373389
switch req["RequestType"] {
@@ -417,7 +433,6 @@ func (bc *redisBroadcast) onResponse(msg []byte) {
417433
return
418434
}
419435

420-
// log.Println("on resp:", res)
421436
switch res["RequestType"] {
422437
case roomLenReqType:
423438
roomLenReq := req.(*roomLenRequest)

0 commit comments

Comments
 (0)