forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.go
88 lines (78 loc) · 1.76 KB
/
echo.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
package tcp
/**
* A echo server to test whether the server is functioning normally
*/
import (
"bufio"
"context"
"github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/lib/sync/atomic"
"github.com/hdt3213/godis/lib/sync/wait"
"io"
"net"
"sync"
"time"
)
// EchoHandler echos received line to client, using for test
type EchoHandler struct {
activeConn sync.Map
closing atomic.Boolean
}
// MakeEchoHandler creates EchoHandler
func MakeEchoHandler() *EchoHandler {
return &EchoHandler{}
}
// EchoClient is client for EchoHandler, using for test
type EchoClient struct {
Conn net.Conn
Waiting wait.Wait
}
// Close close connection
func (c *EchoClient) Close() error {
c.Waiting.WaitWithTimeout(10 * time.Second)
c.Conn.Close()
return nil
}
// Handle echos received line to client
func (h *EchoHandler) Handle(ctx context.Context, conn net.Conn) {
if h.closing.Get() {
// closing handler refuse new connection
_ = conn.Close()
return
}
client := &EchoClient{
Conn: conn,
}
h.activeConn.Store(client, struct{}{})
reader := bufio.NewReader(conn)
for {
// may occurs: client EOF, client timeout, server early close
msg, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
logger.Info("connection close")
h.activeConn.Delete(client)
} else {
logger.Warn(err)
}
return
}
client.Waiting.Add(1)
//logger.Info("sleeping")
//time.Sleep(10 * time.Second)
b := []byte(msg)
_, _ = conn.Write(b)
client.Waiting.Done()
}
}
// Close stops echo handler
func (h *EchoHandler) Close() error {
logger.Info("handler shutting down...")
h.closing.Set(true)
h.activeConn.Range(func(key interface{}, val interface{}) bool {
client := key.(*EchoClient)
_ = client.Close()
return true
})
return nil
}