-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
59 lines (47 loc) · 1.24 KB
/
conn.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
package websocket
import (
"sync"
"github.com/savsgio/gotils/strconv"
)
var connPool = sync.Pool{
New: func() interface{} {
ws := new(Conn)
ws.values = make(map[string]interface{})
return ws
},
}
func acquireConn() *Conn {
return connPool.Get().(*Conn) // nolint:forcetypeassert
}
func releaseConn(ws *Conn) {
ws.reset()
connPool.Put(ws)
}
func (ws *Conn) reset() {
for k := range ws.values {
delete(ws.values, k)
}
ws.Conn = nil
}
// UserValue returns the value stored via SetUserValue* under the given key.
func (ws *Conn) UserValue(key string) interface{} {
return ws.values[key]
}
// UserValueBytes returns the value stored via SetUserValue* under the given key.
func (ws *Conn) UserValueBytes(key []byte) interface{} {
return ws.UserValue(strconv.B2S(key))
}
// SetUserValue stores the given value (arbitrary object)
// under the given key.
//
// The value stored may be obtained by UserValue*.
func (ws *Conn) SetUserValue(key string, value interface{}) {
ws.values[key] = value
}
// SetUserValueBytes stores the given value (arbitrary object)
// under the given key.
//
// The value stored may be obtained by UserValue*.
func (ws *Conn) SetUserValueBytes(key []byte, value interface{}) {
ws.SetUserValue(string(key), value)
}