-
Notifications
You must be signed in to change notification settings - Fork 54
/
server.go
226 lines (198 loc) · 7.79 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
// Copyright 2018 HenryLee. All Rights Reserved.
//
// 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 micro
import (
"fmt"
"net"
"strconv"
"time"
"github.com/henrylee2cn/cfgo"
"github.com/henrylee2cn/erpc/v6"
"github.com/henrylee2cn/erpc/v6/plugin/binder"
"github.com/henrylee2cn/erpc/v6/plugin/heartbeat"
)
// SrvConfig server config
// Note:
// yaml tag is used for github.com/henrylee2cn/cfgo
// ini tag is used for github.com/henrylee2cn/ini
type SrvConfig struct {
Network string `yaml:"network" ini:"network" comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
ListenAddress string `yaml:"listen_address" ini:"listen_address" comment:"Listen address; for server role"`
TlsCertFile string `yaml:"tls_cert_file" ini:"tls_cert_file" comment:"TLS certificate file path"`
TlsKeyFile string `yaml:"tls_key_file" ini:"tls_key_file" comment:"TLS key file path"`
DefaultSessionAge time.Duration `yaml:"default_session_age" ini:"default_session_age" comment:"Default session max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
DefaultContextAge time.Duration `yaml:"default_context_age" ini:"default_context_age" comment:"Default CALL or PUSH context max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
SlowCometDuration time.Duration `yaml:"slow_comet_duration" ini:"slow_comet_duration" comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
DefaultBodyCodec string `yaml:"default_body_codec" ini:"default_body_codec" comment:"Default body codec type id"`
PrintDetail bool `yaml:"print_detail" ini:"print_detail" comment:"Is print body and metadata or not"`
CountTime bool `yaml:"count_time" ini:"count_time" comment:"Is count cost time or not"`
EnableHeartbeat bool `yaml:"enable_heartbeat" ini:"enable_heartbeat" comment:"enable heartbeat"`
}
// Reload Bi-directionally synchronizes config between YAML file and memory.
func (s *SrvConfig) Reload(bind cfgo.BindFunc) error {
err := bind()
if len(s.Network) == 0 {
s.Network = "tcp"
}
if len(s.ListenAddress) == 0 {
s.ListenAddress = "0.0.0.0:9090"
}
return err
}
// ListenPort returns the listened port, such as '9090'.
func (s *SrvConfig) ListenPort() string {
_, port, err := net.SplitHostPort(s.ListenAddress)
if err != nil {
erpc.Fatalf("%v", err)
}
return port
}
// InnerIpPort returns the service's intranet address, such as '192.168.1.120:8080'.
func (s *SrvConfig) InnerIpPort() string {
hostPort, err := InnerIpPort(s.ListenPort())
if err != nil {
erpc.Fatalf("%v", err)
}
return hostPort
}
// OuterIpPort returns the service's extranet address, such as '113.116.141.121:8080'.
func (s *SrvConfig) OuterIpPort() string {
hostPort, err := OuterIpPort(s.ListenPort())
if err != nil {
erpc.Fatalf("%v", err)
}
return hostPort
}
func (s *SrvConfig) PeerConfig() erpc.PeerConfig {
host, port, err := net.SplitHostPort(s.ListenAddress)
if err != nil {
erpc.Fatalf("%v", err)
}
listenPort, _ := strconv.Atoi(port)
return erpc.PeerConfig{
DefaultSessionAge: s.DefaultSessionAge,
DefaultContextAge: s.DefaultContextAge,
SlowCometDuration: s.SlowCometDuration,
DefaultBodyCodec: s.DefaultBodyCodec,
PrintDetail: s.PrintDetail,
CountTime: s.CountTime,
Network: s.Network,
LocalIP: host,
ListenPort: uint16(listenPort),
}
}
// Server server peer
type Server struct {
peer erpc.Peer
binder *binder.StructArgsBinder
}
// NewServer creates a server peer.
func NewServer(cfg SrvConfig, globalLeftPlugin ...erpc.Plugin) *Server {
doInit()
if cfg.EnableHeartbeat {
globalLeftPlugin = append(globalLeftPlugin, heartbeat.NewPong())
}
peer := erpc.NewPeer(cfg.PeerConfig(), globalLeftPlugin...)
binder := binder.NewStructArgsBinder(nil)
peer.PluginContainer().AppendRight(binder)
if len(cfg.TlsCertFile) > 0 && len(cfg.TlsKeyFile) > 0 {
err := peer.SetTLSConfigFromFile(cfg.TlsCertFile, cfg.TlsKeyFile)
if err != nil {
erpc.Fatalf("%v", err)
}
}
s := &Server{
peer: peer,
binder: binder,
}
s.SetBindErrorFunc(nil)
return s
}
// Peer returns the peer
func (s *Server) Peer() erpc.Peer {
return s.peer
}
// PluginContainer returns the global plugin container.
func (s *Server) PluginContainer() *erpc.PluginContainer {
return s.peer.PluginContainer()
}
// SetBindErrorFunc sets the binding or balidating error function.
// Note: If fn=nil, set as default.
func (s *Server) SetBindErrorFunc(fn binder.ErrorFunc) {
if fn != nil {
s.binder.SetErrorFunc(fn)
return
}
s.binder.SetErrorFunc(func(handlerName, paramName, reason string) *erpc.Status {
return RerrInvalidParameter.SetCause(fmt.Sprintf(`{"handler": %q, "param": %q, "reason": %q}`, handlerName, paramName, reason))
})
}
// Router returns the root router of call or push handlers.
func (s *Server) Router() *erpc.Router {
return s.peer.Router()
}
// SubRoute adds handler group.
func (s *Server) SubRoute(pathPrefix string, plugin ...erpc.Plugin) *erpc.SubRouter {
return s.peer.SubRoute(pathPrefix, plugin...)
}
// RouteCall registers CALL handlers, and returns the paths.
func (s *Server) RouteCall(ctrlStruct interface{}, plugin ...erpc.Plugin) []string {
return s.peer.RouteCall(ctrlStruct, plugin...)
}
// RouteCallFunc registers CALL handler, and returns the path.
func (s *Server) RouteCallFunc(callHandleFunc interface{}, plugin ...erpc.Plugin) string {
return s.peer.RouteCallFunc(callHandleFunc, plugin...)
}
// RoutePush registers PUSH handlers, and returns the paths.
func (s *Server) RoutePush(ctrlStruct interface{}, plugin ...erpc.Plugin) []string {
return s.peer.RoutePush(ctrlStruct, plugin...)
}
// RoutePushFunc registers PUSH handler, and returns the path.
func (s *Server) RoutePushFunc(pushHandleFunc interface{}, plugin ...erpc.Plugin) string {
return s.peer.RoutePushFunc(pushHandleFunc, plugin...)
}
// SetUnknownCall sets the default handler,
// which is called when no handler for CALL is found.
func (s *Server) SetUnknownCall(fn func(erpc.UnknownCallCtx) (interface{}, *erpc.Status), plugin ...erpc.Plugin) {
s.peer.SetUnknownCall(fn, plugin...)
}
// SetUnknownPush sets the default handler,
// which is called when no handler for PUSH is found.
func (s *Server) SetUnknownPush(fn func(erpc.UnknownPushCtx) *erpc.Status, plugin ...erpc.Plugin) {
s.peer.SetUnknownPush(fn, plugin...)
}
// Close closes server.
func (s *Server) Close() error {
return s.peer.Close()
}
// CountSession returns the number of sessions.
func (s *Server) CountSession() int {
return s.peer.CountSession()
}
// GetSession gets the session by id.
func (s *Server) GetSession(sessionId string) (erpc.Session, bool) {
return s.peer.GetSession(sessionId)
}
// ListenAndServe turns on the listening service.
func (s *Server) ListenAndServe(protoFunc ...erpc.ProtoFunc) error {
return s.peer.ListenAndServe(protoFunc...)
}
// RangeSession ranges all sessions. If fn returns false, stop traversing.
func (s *Server) RangeSession(fn func(sess erpc.Session) bool) {
s.peer.RangeSession(fn)
}
// ServeConn serves the connection and returns a session.
func (s *Server) ServeConn(conn net.Conn, protoFunc ...erpc.ProtoFunc) (erpc.Session, *erpc.Status) {
return s.peer.ServeConn(conn, protoFunc...)
}