-
Notifications
You must be signed in to change notification settings - Fork 814
/
mux_pool.go
147 lines (130 loc) · 3.25 KB
/
mux_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package netpollmux
import (
"context"
"net"
"sync"
"sync/atomic"
"github.com/cloudwego/netpoll"
"github.com/cloudwego/kitex/pkg/remote"
"golang.org/x/sync/singleflight"
)
var _ remote.LongConnPool = &MuxPool{}
// NewMuxConnPool size must be adjust to 2^N
func NewMuxConnPool(size int) *MuxPool {
lp := &MuxPool{
size: int32(size),
}
return lp
}
// MuxPool manages a pool of long connections.
type MuxPool struct {
// mod int32
size int32
sfg singleflight.Group
connMap sync.Map // key address, value *muxCliConn
}
// Get pick or generate a net.Conn and return
func (mp *MuxPool) Get(ctx context.Context, network, address string, opt remote.ConnOption) (net.Conn, error) {
v, ok := mp.connMap.Load(address)
if ok {
connection := v.(*conns).get()
if connection != nil && connection.IsActive() {
return connection, nil
}
}
connection, err, _ := mp.sfg.Do(address, func() (i interface{}, e error) {
conn, err := opt.Dialer.DialTimeout(network, address, opt.ConnectTimeout)
if err != nil {
return nil, err
}
connection := newMuxCliConn(conn.(netpoll.Connection))
var cs *conns
if ok {
cs = v.(*conns)
} else {
cs = &conns{size: mp.size, conns: make([]*muxCliConn, mp.size)}
}
cs.put(connection)
mp.connMap.Store(address, cs)
return connection, nil
})
if err != nil {
return nil, err
}
return connection.(*muxCliConn), nil
}
// Put implements the ConnPool interface.
func (mp *MuxPool) Put(conn net.Conn) error {
if _, ok := conn.(*muxCliConn); ok {
return nil
}
return conn.Close()
}
// Discard implements the ConnPool interface.
func (mp *MuxPool) Discard(conn net.Conn) error {
if _, ok := conn.(*muxCliConn); ok {
return nil
}
return conn.Close()
}
// Clean implements the LongConnPool interface.
func (mp *MuxPool) Clean(network, address string) {
if v, ok := mp.connMap.Load(address); ok {
mp.connMap.Delete(address)
v.(*conns).close()
}
}
// Close is to release resource of ConnPool, it is executed when client is closed.
func (mp *MuxPool) Close() error {
mp.connMap.Range(func(addr, v interface{}) bool {
mp.connMap.Delete(addr)
v.(*conns).close()
return true
})
return nil
}
type conns struct {
index int32
size int32
conns []*muxCliConn
}
func (c *conns) get() *muxCliConn {
i := atomic.AddInt32(&c.index, 1)
return c.conns[i%c.size]
}
// put and close together
func (c *conns) put(conn *muxCliConn) {
for i := 0; i < int(c.size); i++ {
if c.conns[i] == nil {
c.conns[i] = conn
return
}
if !c.conns[i].IsActive() {
c.conns[i].close()
c.conns[i] = conn
return
}
}
}
func (c *conns) close() {
for i := range c.conns {
if c.conns[i] != nil {
c.conns[i].close()
}
}
}