forked from lesismal/arpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
227 lines (197 loc) · 4.64 KB
/
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
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package arpc
import (
"context"
"net"
"sync"
"sync/atomic"
"time"
"github.com/lesismal/arpc/codec"
"github.com/lesismal/arpc/log"
"github.com/lesismal/arpc/util"
)
// Server represents an arpc Server.
type Server struct {
Accepted int64
CurrLoad int64
MaxLoad int64
// 64-aligned on 32-bit
seq uint64
Codec codec.Codec
Handler Handler
Listener net.Listener
mux sync.Mutex
running bool
chStop chan error
clients map[*Client]util.Empty
}
// Serve starts service with listener.
func (s *Server) Serve(ln net.Listener) error {
s.Listener = ln
s.chStop = make(chan error)
log.Info("%v Running On: \"%v\"", s.Handler.LogTag(), ln.Addr())
defer log.Info("%v Stopped", s.Handler.LogTag())
return s.runLoop()
}
// Run starts tcp service on addr.
func (s *Server) Run(addr string) error {
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Info("%v Running failed: %v", s.Handler.LogTag(), err)
return err
}
s.Listener = ln
s.chStop = make(chan error)
log.Info("%v Running On: \"%v\"", s.Handler.LogTag(), ln.Addr())
// defer log.Info("%v Stopped", s.Handler.LogTag())
return s.runLoop()
}
func (s *Server) Broadcast(method string, v interface{}, args ...interface{}) {
msg := s.NewMessage(CmdNotify, method, v, args...)
s.mux.Lock()
defer func() {
msg.Release()
s.mux.Unlock()
}()
for c := range s.clients {
msg.Retain()
c.PushMsg(msg, TimeZero)
}
}
func (s *Server) BroadcastWithFilter(method string, v interface{}, filter func(*Client) bool, args ...interface{}) {
msg := s.NewMessage(CmdNotify, method, v, args...)
s.mux.Lock()
defer func() {
msg.Release()
s.mux.Unlock()
}()
for c := range s.clients {
if filter == nil || filter(c) {
msg.Retain()
c.PushMsg(msg, TimeZero)
}
}
}
func (s *Server) ForEach(h func(*Client)) {
s.mux.Lock()
defer s.mux.Unlock()
for c := range s.clients {
h(c)
}
}
func (s *Server) ForEachWithFilter(h func(*Client), filter func(*Client) bool) {
s.mux.Lock()
defer s.mux.Unlock()
for c := range s.clients {
if filter == nil || filter(c) {
h(c)
}
}
}
// Stop stops service.
func (s *Server) Stop() error {
defer log.Info("%v %v Stop", s.Handler.LogTag(), s.Listener.Addr())
s.running = false
s.Listener.Close()
select {
case <-s.chStop:
case <-time.After(time.Second):
return ErrTimeout
default:
}
return nil
}
// Shutdown shutdown service.
func (s *Server) Shutdown(ctx context.Context) error {
defer log.Info("%v %v Shutdown", s.Handler.LogTag(), s.Listener.Addr())
s.running = false
s.Listener.Close()
select {
case <-s.chStop:
case <-ctx.Done():
return ErrTimeout
}
return nil
}
// NewMessage creates a Message.
func (s *Server) NewMessage(cmd byte, method string, v interface{}, args ...interface{}) *Message {
if len(args) == 0 {
return newMessage(cmd, method, v, false, false, atomic.AddUint64(&s.seq, 1), s.Handler, s.Codec, nil)
}
return newMessage(cmd, method, v, false, false, atomic.AddUint64(&s.seq, 1), s.Handler, s.Codec, args[0].(map[interface{}]interface{}))
}
func (s *Server) addLoad() int64 {
return atomic.AddInt64(&s.CurrLoad, 1)
}
func (s *Server) subLoad() int64 {
return atomic.AddInt64(&s.CurrLoad, -1)
}
func (s *Server) addClient(c *Client) {
s.mux.Lock()
s.clients[c] = util.Empty{}
s.mux.Unlock()
}
func (s *Server) deleteClient(c *Client) {
s.mux.Lock()
delete(s.clients, c)
s.mux.Unlock()
}
func (s *Server) clearClients() {
s.mux.Lock()
for c := range s.clients {
go c.Stop()
}
s.clients = map[*Client]util.Empty{}
s.mux.Unlock()
}
func (s *Server) runLoop() error {
var (
err error
cli *Client
conn net.Conn
)
s.running = true
defer func() {
s.clearClients()
close(s.chStop)
}()
for s.running {
conn, err = s.Listener.Accept()
if err == nil {
load := s.addLoad()
if s.MaxLoad <= 0 || load <= s.MaxLoad {
s.Accepted++
cli = newClientWithConn(conn, s.Codec, s.Handler, func(c *Client) {
s.deleteClient(c)
s.subLoad()
})
s.addClient(cli)
s.Handler.OnConnected(cli)
} else {
conn.Close()
s.subLoad()
}
} else {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
log.Error("%v Accept error: %v; retrying...", s.Handler.LogTag(), err)
time.Sleep(time.Second / 20)
} else {
log.Error("%v Accept error: %v", s.Handler.LogTag(), err)
break
}
}
}
return err
}
// NewServer creates an arpc Server.
func NewServer() *Server {
h := DefaultHandler.Clone()
h.SetLogTag("[ARPC SVR]")
return &Server{
Codec: codec.DefaultCodec,
Handler: h,
clients: map[*Client]util.Empty{},
}
}