/
connection.go
65 lines (58 loc) · 1.47 KB
/
connection.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
package worker
import (
"context"
"net"
"time"
"github.com/didi/sharingan/replayer-agent/common/handlers/tlog"
)
func newConn(ctx context.Context, remoteAddr string) (net.Conn, error) {
conn, err := net.Dial("tcp", remoteAddr)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg=connect to sut failed||err=%s", err)
}
return conn, err
}
func handleConn(ctx context.Context, conn net.Conn, req []byte, closeConn bool, project string) ([]byte, error) {
defer func() {
if closeConn {
conn.Close()
}
}()
_, err := conn.Write(req)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg=write to sut failed||err=%s", err)
return nil, err
}
response, err := readResponse(conn, project)
if err != nil {
tlog.Handler.Errorf(ctx, tlog.DLTagUndefined, "errmsg=read from sut failed||err=%s", err)
}
return response, err
}
func readResponse(conn net.Conn, project string) ([]byte, error) {
buf := make([]byte, 1024)
t1 := time.Second * 60
t2 := time.Second * 80
conn.SetReadDeadline(time.Now().Add(t1))
bytesRead, err := conn.Read(buf)
if err != nil {
return nil, err
}
response := []byte{}
response = append(response, buf[:bytesRead]...)
if bytesRead < len(buf) {
return response, nil
}
for {
conn.SetReadDeadline(time.Now().Add(t2))
bytesRead, err = conn.Read(buf)
if err != nil {
break
}
response = append(response, buf[:bytesRead]...)
if bytesRead < len(buf) {
return response, nil
}
}
return response, nil
}