/
client.go
74 lines (61 loc) · 1.5 KB
/
client.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
//go run -tags kcp client.go
package main
import (
"context"
"crypto/sha1"
"flag"
"fmt"
"log"
"net"
"time"
example "github.com/bigdot123456/RPCWork"
"github.com/smallnest/rpcx/client"
kcp "github.com/xtaci/kcp-go"
"golang.org/x/crypto/pbkdf2"
)
var (
addr = flag.String("addr", "0.0.0.0:8972", "server address")
)
const cryptKey = "rpcx-key"
const cryptSalt = "rpcx-salt"
func main() {
flag.Parse()
pass := pbkdf2.Key([]byte(cryptKey), []byte(cryptSalt), 4096, 32, sha1.New)
bc, _ := kcp.NewAESBlockCrypt(pass)
option := client.DefaultOption
option.Block = bc
d := client.NewPeer2PeerDiscovery("kcp@"+*addr, "")
xclient := client.NewXClient("Arith", client.Failtry, client.RoundRobin, d, option)
defer xclient.Close()
// plugin
cs := &ConfigUDPSession{}
pc := client.NewPluginContainer()
pc.Add(cs)
xclient.SetPlugins(pc)
args := &example.Args{
A: 10,
B: 20,
}
start := time.Now()
for i := 0; i < 10000; i++ {
reply := &example.Reply{}
err := xclient.Call(context.Background(), "Mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
//log.Printf("%d * %d = %d", args.A, args.B, reply.C)
}
dur := time.Since(start)
qps := 10000 * 1000 / int(dur/time.Millisecond)
fmt.Printf("qps: %d call/s", qps)
}
type ConfigUDPSession struct{}
func (p *ConfigUDPSession) ConnCreated(conn net.Conn) (net.Conn, error) {
session, ok := conn.(*kcp.UDPSession)
if !ok {
return conn, nil
}
session.SetACKNoDelay(true)
session.SetStreamMode(true)
return conn, nil
}