forked from klintcheng/kim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_server.go
250 lines (221 loc) · 5.33 KB
/
default_server.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package kim
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/gobwas/pool/pbufio"
"github.com/gobwas/ws"
"github.com/klintcheng/kim/logger"
"github.com/panjf2000/ants/v2"
"github.com/segmentio/ksuid"
)
type Upgrader interface {
Name() string
Upgrade(rawconn net.Conn, rd *bufio.Reader, wr *bufio.Writer) (Conn, error)
}
// ServerOptions ServerOptions
type ServerOptions struct {
Loginwait time.Duration //登录超时
Readwait time.Duration //读超时
Writewait time.Duration //写超时
MessageGPool int
ConnectionGPool int
}
type ServerOption func(*ServerOptions)
func WithMessageGPool(val int) ServerOption {
return func(opts *ServerOptions) {
opts.MessageGPool = val
}
}
func WithConnectionGPool(val int) ServerOption {
return func(opts *ServerOptions) {
opts.ConnectionGPool = val
}
}
// DefaultServer is a websocket implement of the DefaultServer
type DefaultServer struct {
Upgrader
listen string
ServiceRegistration
ChannelMap
Acceptor
MessageListener
StateListener
once sync.Once
options *ServerOptions
quit int32
}
// NewServer NewServer
func NewServer(listen string, service ServiceRegistration, upgrader Upgrader, options ...ServerOption) *DefaultServer {
defaultOpts := &ServerOptions{
Loginwait: DefaultLoginWait,
Readwait: DefaultReadWait,
Writewait: DefaultWriteWait,
MessageGPool: DefaultMessageReadPool,
ConnectionGPool: DefaultConnectionPool,
}
for _, option := range options {
option(defaultOpts)
}
return &DefaultServer{
listen: listen,
ServiceRegistration: service,
options: defaultOpts,
Upgrader: upgrader,
quit: 0,
}
}
// Start server
func (s *DefaultServer) Start() error {
log := logger.WithFields(logger.Fields{
"module": s.Name(),
"listen": s.listen,
"id": s.ServiceID(),
"func": "Start",
})
if s.Acceptor == nil {
s.Acceptor = new(defaultAcceptor)
}
if s.StateListener == nil {
return fmt.Errorf("StateListener is nil")
}
if s.ChannelMap == nil {
s.ChannelMap = NewChannels(100)
}
lst, err := net.Listen("tcp", s.listen)
if err != nil {
return err
}
// 采用协程池来增加复用
mgpool, _ := ants.NewPool(s.options.MessageGPool, ants.WithPreAlloc(true))
defer func() {
mgpool.Release()
}()
log.Info("started")
for {
rawconn, err := lst.Accept()
if err != nil {
if rawconn != nil {
rawconn.Close()
}
log.Warn(err)
continue
}
go s.connHandler(rawconn, mgpool)
if atomic.LoadInt32(&s.quit) == 1 {
break
}
}
log.Info("quit")
return nil
}
func (s *DefaultServer) connHandler(rawconn net.Conn, gpool *ants.Pool) {
rd := pbufio.GetReader(rawconn, ws.DefaultServerReadBufferSize)
wr := pbufio.GetWriter(rawconn, ws.DefaultServerWriteBufferSize)
defer func() {
pbufio.PutReader(rd)
pbufio.PutWriter(wr)
}()
conn, err := s.Upgrade(rawconn, rd, wr)
if err != nil {
logger.Errorf("Upgrade error: %v", err)
rawconn.Close()
return
}
id, meta, err := s.Accept(conn, s.options.Loginwait)
if err != nil {
_ = conn.WriteFrame(OpClose, []byte(err.Error()))
conn.Close()
return
}
if _, ok := s.Get(id); ok {
_ = conn.WriteFrame(OpClose, []byte("channelId is repeated"))
conn.Close()
return
}
if meta == nil {
meta = Meta{}
}
channel := NewChannel(id, meta, conn, gpool)
channel.SetReadWait(s.options.Readwait)
channel.SetWriteWait(s.options.Writewait)
s.Add(channel)
gaugeWithLabel := channelTotalGauge.WithLabelValues(s.ServiceID(), s.ServiceName())
gaugeWithLabel.Inc()
defer gaugeWithLabel.Dec()
logger.Infof("accept channel - ID: %s RemoteAddr: %s", channel.ID(), channel.RemoteAddr())
err = channel.Readloop(s.MessageListener)
if err != nil {
logger.Info(err)
}
s.Remove(channel.ID())
_ = s.Disconnect(channel.ID())
channel.Close()
}
// Shutdown Shutdown
func (s *DefaultServer) Shutdown(ctx context.Context) error {
log := logger.WithFields(logger.Fields{
"module": s.Name(),
"id": s.ServiceID(),
})
s.once.Do(func() {
defer func() {
log.Infoln("shutdown")
}()
if atomic.CompareAndSwapInt32(&s.quit, 0, 1) {
return
}
// close channels
chanels := s.ChannelMap.All()
for _, ch := range chanels {
ch.Close()
select {
case <-ctx.Done():
return
default:
continue
}
}
})
return nil
}
// string channelID
// []byte data
func (s *DefaultServer) Push(id string, data []byte) error {
ch, ok := s.ChannelMap.Get(id)
if !ok {
return errors.New("channel no found")
}
return ch.Push(data)
}
// SetAcceptor SetAcceptor
func (s *DefaultServer) SetAcceptor(acceptor Acceptor) {
s.Acceptor = acceptor
}
// SetMessageListener SetMessageListener
func (s *DefaultServer) SetMessageListener(listener MessageListener) {
s.MessageListener = listener
}
// SetStateListener SetStateListener
func (s *DefaultServer) SetStateListener(listener StateListener) {
s.StateListener = listener
}
// SetChannels SetChannels
func (s *DefaultServer) SetChannelMap(channels ChannelMap) {
s.ChannelMap = channels
}
// SetReadWait set read wait duration
func (s *DefaultServer) SetReadWait(Readwait time.Duration) {
s.options.Readwait = Readwait
}
type defaultAcceptor struct {
}
// Accept defaultAcceptor
func (a *defaultAcceptor) Accept(conn Conn, timeout time.Duration) (string, Meta, error) {
return ksuid.New().String(), Meta{}, nil
}