-
Notifications
You must be signed in to change notification settings - Fork 4
/
pool.go
95 lines (78 loc) · 1.68 KB
/
pool.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
package ws
import (
"reflect"
"sync"
)
var (
messagePool = sync.Pool{
New: func() interface{} {
msg := &Message{}
msg.isPool = true
return msg
},
}
dataMsgPools = make(map[uint32]*sync.Pool)
dataMsgTypes = make(map[uint32]reflect.Type)
srvConnectionPool = sync.Pool{
New: func() interface{} {
connection := &Connection{}
return connection
},
}
)
func getPoolMessage() *Message {
msg := messagePool.Get().(*Message)
msg.isPool = true
return msg
}
func putPoolMessage(msg *Message) {
if !msg.isPool {
return
}
if msg.dataMsg != nil {
putPoolDataMsg(msg.protocolId, msg.dataMsg)
msg.dataMsg = nil
}
msg.protocolId = 0
msg.sn = 0
msg.data = nil
msg.sc = nil
messagePool.Put(msg)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func getPoolDataMsg(protocolId uint32) IDataMessage {
pool, ok := dataMsgPools[protocolId]
if !ok {
return nil
}
return pool.Get().(IDataMessage)
}
func getDataMsg(protocolId uint32) IDataMessage {
typ, ok := dataMsgTypes[protocolId]
if !ok {
return nil
}
return reflect.New(typ).Interface().(IDataMessage)
}
func putPoolDataMsg(protocolId uint32, dataMsg IDataMessage) {
pool, ok := dataMsgPools[protocolId]
if !ok {
return
}
dataMsg.Reset()
pool.Put(dataMsg)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func getPoolConnection() *Connection {
conn := srvConnectionPool.Get().(*Connection)
conn.init()
conn.isPool = true
return conn
}
func putPoolConnection(conn *Connection) {
if !conn.isPool {
return
}
conn.reset()
srvConnectionPool.Put(conn)
}