/
server.go
135 lines (120 loc) · 2.55 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
package sofarpc
import (
"net"
"sync"
"time"
"mosn.io/mosn/pkg/buffer"
)
type MockServer struct {
Addr string
ServeFunc ServeFunc
// runtime
mutex sync.Mutex
listener net.Listener
id uint64
connections map[uint64]net.Conn
delay time.Duration // wait delay time before response
idleTimeout time.Duration // if idle timeout is not zero, set a connection timeout, and will close the connection when timeout
// stats
ServerStats *ServerStats
}
func NewMockServer(addr string, f ServeFunc) *MockServer {
if f == nil {
f = DefaultBoltV1Serve.Serve
}
return &MockServer{
Addr: addr,
ServeFunc: f,
mutex: sync.Mutex{},
connections: make(map[uint64]net.Conn),
ServerStats: NewServerStats(),
}
}
func (srv *MockServer) SetResponseDelay(d time.Duration) {
srv.delay = d
}
func (srv *MockServer) SetConnectionIdleTimeout(d time.Duration) {
srv.idleTimeout = d
}
func (srv *MockServer) Close() {
srv.mutex.Lock()
defer srv.mutex.Unlock()
if srv.listener == nil {
return
}
srv.listener.Close()
srv.listener = nil
for key, conn := range srv.connections {
conn.Close()
delete(srv.connections, key)
srv.ServerStats.CloseConnection()
}
}
func (srv *MockServer) Start() {
srv.mutex.Lock()
if srv.listener != nil {
srv.mutex.Unlock()
return
}
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
panic(err)
}
srv.listener = ln
srv.mutex.Unlock()
for {
conn, err := ln.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
continue
}
return
}
srv.ServerStats.ActiveConnection()
srv.mutex.Lock()
srv.id++
srv.connections[srv.id] = conn
srv.mutex.Unlock()
go srv.Serve(srv.id, conn)
}
}
func (srv *MockServer) Serve(id uint64, conn net.Conn) {
defer func() {
srv.ServerStats.CloseConnection()
srv.mutex.Lock()
delete(srv.connections, id)
srv.mutex.Unlock()
}()
iobuf := buffer.NewIoBuffer(10240)
for {
if srv.idleTimeout == 0 {
conn.SetReadDeadline(time.Time{}) // no timeout
} else {
conn.SetReadDeadline(time.Now().Add(srv.idleTimeout))
}
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
if err, ok := err.(net.Error); ok && err.Temporary() {
continue
}
return
}
if n > 0 {
iobuf.Write(buf[:n])
DECODE:
for iobuf.Len() > 0 {
resp := srv.ServeFunc(iobuf)
if resp == nil {
break DECODE
}
srv.ServerStats.Request()
if srv.delay > 0 {
time.Sleep(srv.delay)
}
conn.Write(resp.DataToWrite)
srv.ServerStats.Response(resp.Status)
}
}
}
}