-
Notifications
You must be signed in to change notification settings - Fork 789
/
trans_server.go
146 lines (128 loc) · 4.1 KB
/
trans_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
/*
* Copyright 2022 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 gonet contains server and client implementation for go net.
package gonet
import (
"context"
"errors"
"net"
"os"
"runtime/debug"
"sync"
"time"
"github.com/cloudwego/kitex/pkg/remote/trans"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/utils"
)
// NewTransServerFactory creates a default go net transport server factory.
func NewTransServerFactory() remote.TransServerFactory {
return &gonetTransServerFactory{}
}
type gonetTransServerFactory struct{}
// NewTransServer implements the remote.TransServerFactory interface.
func (f *gonetTransServerFactory) NewTransServer(opt *remote.ServerOption, transHdlr remote.ServerTransHandler) remote.TransServer {
return &transServer{
opt: opt,
transHdlr: transHdlr,
lncfg: trans.NewListenConfig(opt),
}
}
type transServer struct {
opt *remote.ServerOption
transHdlr remote.ServerTransHandler
ln net.Listener
lncfg net.ListenConfig
connCount utils.AtomicInt
sync.Mutex
}
var _ remote.TransServer = &transServer{}
// CreateListener implements the remote.TransServer interface.
// The network must be "tcp", "tcp4", "tcp6" or "unix".
func (ts *transServer) CreateListener(addr net.Addr) (ln net.Listener, err error) {
ln, err = ts.lncfg.Listen(context.Background(), addr.Network(), addr.String())
return ln, err
}
// BootstrapServer implements the remote.TransServer interface.
func (ts *transServer) BootstrapServer(ln net.Listener) (err error) {
if ln == nil {
return errors.New("listener is nil in gonet transport server")
}
ts.ln = ln
for {
conn, err := ts.ln.Accept()
if err != nil {
klog.Errorf("bootstrap server accept failed, err=%s", err.Error())
os.Exit(1)
}
go func() {
ri := ts.opt.InitOrResetRPCInfoFunc(nil, conn.RemoteAddr())
ctx := rpcinfo.NewCtxWithRPCInfo(context.Background(), ri)
defer func() {
transRecover(ctx, conn, "OnRead")
// recycle rpcinfo
rpcinfo.PutRPCInfo(ri)
}()
for {
ts.refreshDeadline(rpcinfo.GetRPCInfo(ctx), conn)
err := ts.transHdlr.OnRead(ctx, conn)
if err != nil {
ts.onError(ctx, err, conn)
_ = conn.Close()
return
}
}
}()
}
}
// Shutdown implements the remote.TransServer interface.
func (ts *transServer) Shutdown() (err error) {
ts.Lock()
defer ts.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), ts.opt.ExitWaitTime)
defer cancel()
if g, ok := ts.transHdlr.(remote.GracefulShutdown); ok {
if ts.ln != nil {
// 1. stop listener
_ = ts.ln.Close()
// 2. signal all active connections to close gracefully
_ = g.GracefulShutdown(ctx)
}
}
return
}
// ConnCount implements the remote.TransServer interface.
func (ts *transServer) ConnCount() utils.AtomicInt {
return ts.connCount
}
func (ts *transServer) onError(ctx context.Context, err error, conn net.Conn) {
ts.transHdlr.OnError(ctx, err, conn)
}
func transRecover(ctx context.Context, conn net.Conn, funcName string) {
panicErr := recover()
if panicErr != nil {
if conn != nil {
klog.CtxErrorf(ctx, "KITEX: panic happened in %s, remoteAddress=%s, error=%v\nstack=%s", funcName, conn.RemoteAddr(), panicErr, string(debug.Stack()))
} else {
klog.CtxErrorf(ctx, "KITEX: panic happened in %s, error=%v\nstack=%s", funcName, panicErr, string(debug.Stack()))
}
}
}
func (ts *transServer) refreshDeadline(ri rpcinfo.RPCInfo, conn net.Conn) {
readTimeout := ri.Config().ReadWriteTimeout()
_ = conn.SetReadDeadline(time.Now().Add(readTimeout))
}