-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_server_example_test.go
149 lines (133 loc) · 3.44 KB
/
client_server_example_test.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
// nolint
// Copyright 20xx The Alipay Authors.
//
// @authors[0]: bingwu.ybw(bingwu.ybw@antfin.com|detailyang@gmail.com)
// @authors[1]: robotx(robotx@antfin.com)
//
// *Legal Disclaimer*
// Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.
// *法律免责声明*
// 关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。
//
//
package examples
import (
"fmt"
"log"
"net"
"time"
"github.com/sofastack/sofa-bolt-go/sofabolt"
)
func ExampleClientAndServer() {
srv, err := sofabolt.NewServer(
sofabolt.WithServerTimeout(
5*time.Second,
5*time.Second,
5*time.Second,
0*time.Second,
),
sofabolt.WithServerHandler(sofabolt.HandlerFunc(func(rw sofabolt.ResponseWriter, req *sofabolt.Request) {
fmt.Println(string(req.GetContent()))
rw.GetResponse().SetContent(req.GetContent())
rw.Write()
})),
)
if err != nil {
log.Fatal(err)
}
p0, p1 := net.Pipe()
go func() {
srv.ServeConn(p0)
}()
c, err := sofabolt.NewClient(
sofabolt.WithClientTimeout(
5*time.Second,
5*time.Second,
5*time.Second,
0*time.Second,
),
sofabolt.WithClientConn(p1),
)
if err != nil {
log.Fatal(err)
}
var (
req = sofabolt.AcquireRequest()
res = sofabolt.AcquireResponse()
)
defer func() {
sofabolt.ReleaseRequest(req)
sofabolt.ReleaseResponse(res)
}()
req.SetContentString("hello world")
err = c.DoTimeout(req, res, 1*time.Second)
if err != nil {
log.Fatal(err)
}
// Output: hello world
}
func ExampleClientAndServerDialer() {
ln, err := net.Listen("tcp4", ":0")
if err != nil {
log.Fatal(err)
}
go func() { // start a temporary server
srv, err := sofabolt.NewServer(
sofabolt.WithServerTimeout(
5*time.Second,
5*time.Second,
5*time.Second,
0*time.Second,
),
sofabolt.WithServerHandler(sofabolt.HandlerFunc(func(rw sofabolt.ResponseWriter, req *sofabolt.Request) {
fmt.Println(string(req.GetContent()))
rw.GetResponse().SetContent(req.GetContent())
rw.Write()
})),
)
if err != nil {
log.Fatal(err)
}
srv.Serve(ln)
}()
c, err := sofabolt.NewClient(
sofabolt.WithClientRedial(sofabolt.DialerFunc(func() (net.Conn, error) {
return net.DialTimeout("tcp4", ln.Addr().String(), 5*time.Second)
})), // always redial
sofabolt.WithClientTimeout(
0, // no read timeout
5*time.Second, // write timeout
0, // no idle timeout
0, // no flush timeout
),
sofabolt.WithClientHandler(sofabolt.HandlerFunc(func(rw sofabolt.ResponseWriter, req *sofabolt.Request) {
rw.Write()
})),
sofabolt.WithClientMaxPendingCommands( // set max pending commands
512),
sofabolt.WithClientHeartbeat(
30*time.Second,
10*time.Second,
0,
func(success bool) {
},
),
)
if err != nil {
log.Fatal(err)
}
var (
req = sofabolt.AcquireRequest()
res = sofabolt.AcquireResponse()
)
defer func() {
sofabolt.ReleaseRequest(req)
sofabolt.ReleaseResponse(res)
}()
req.SetContentString("hello world")
err = c.DoTimeout(req, res, 1*time.Second)
if err != nil {
log.Fatal(err)
}
// Output: hello world
}