1 Star 0 Fork 0

yonglinux / rpcx-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
client.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
鸟窝 提交于 2020-05-04 11:03 . move examples to rpcxio/rpcx-examples
//go run -tags kcp client.go
package main
import (
"context"
"crypto/sha1"
"flag"
"fmt"
"log"
"net"
"time"
example "github.com/rpcxio/rpcx-examples"
"github.com/smallnest/rpcx/client"
kcp "github.com/xtaci/kcp-go"
"golang.org/x/crypto/pbkdf2"
)
var (
addr = flag.String("addr", "localhost: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
}
1
https://gitee.com/yonglinux/rpcx-examples.git
git@gitee.com:yonglinux/rpcx-examples.git
yonglinux
rpcx-examples
rpcx-examples
v1.1.6

搜索帮助