This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ping.go
114 lines (100 loc) · 2.75 KB
/
ping.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
package websocket
import (
"io"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer. Needs to be less
// than the idle timeout on whatever frontend server is proxying the
// websocket connections (e.g. nginx).
pongWait = 30 * time.Second
// Send pings to peer with this period. Must be less than pongWait. The peer
// must respond with a pong in < pongWait. But it may take writeWait for the
// pong to be sent. Therefore we want to allow time for that, and a bit of
// delay/round-trip in case the peer is busy. 1/3 of pongWait seems like a
// reasonable amount of time to respond to a ping.
pingPeriod = ((pongWait - writeWait) * 2 / 3)
)
type pingingWebsocket struct {
pinger *time.Timer
readLock sync.Mutex
writeLock sync.Mutex
reader io.Reader
conn *websocket.Conn
}
// Ping adds a periodic ping to a websocket connection.
func Ping(c *websocket.Conn) Websocket {
p := &pingingWebsocket{conn: c}
p.conn.SetPongHandler(p.pong)
p.conn.SetReadDeadline(time.Now().Add(pongWait))
p.pinger = time.AfterFunc(pingPeriod, p.ping)
return p
}
func (p *pingingWebsocket) ping() {
p.writeLock.Lock()
defer p.writeLock.Unlock()
if err := p.conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(writeWait)); err != nil {
p.conn.Close()
return
}
p.pinger.Reset(pingPeriod)
}
func (p *pingingWebsocket) pong(string) error {
p.conn.SetReadDeadline(time.Now().Add(pongWait))
return nil
}
func (p *pingingWebsocket) Read(b []byte) (int, error) {
p.readLock.Lock()
defer p.readLock.Unlock()
for p.reader == nil {
msgType, r, err := p.conn.NextReader()
if err != nil {
if IsExpectedWSCloseError(err) {
return 0, io.EOF
}
return 0, err
}
if msgType != websocket.BinaryMessage {
// Ignore non-binary messages for now.
continue
}
p.reader = r
}
n, err := p.reader.Read(b)
if err == io.EOF {
p.reader = nil
err = nil
}
p.conn.SetReadDeadline(time.Now().Add(pongWait))
return n, err
}
func (p *pingingWebsocket) Write(b []byte) (int, error) {
p.writeLock.Lock()
defer p.writeLock.Unlock()
if err := p.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil {
return 0, err
}
w, err := p.conn.NextWriter(websocket.BinaryMessage)
if err != nil {
return 0, err
}
n, err := w.Write(b)
if err != nil {
return n, err
}
return n, w.Close()
}
func (p *pingingWebsocket) Close() error {
p.writeLock.Lock()
defer p.writeLock.Unlock()
p.pinger.Stop()
if err := p.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "ok"), time.Now().Add(writeWait)); err != nil {
p.conn.Close()
return err
}
return p.conn.Close()
}