-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathxc.go
160 lines (127 loc) · 3.45 KB
/
xc.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
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"fmt"
//"os"
"runtime"
"strings"
"sync"
"time"
"github.com/glycerine/idem"
rpc "github.com/glycerine/rpc25519"
)
var bkgCtx = context.Background()
// setup client via rpc
type ClientRpc struct {
name string
Cfg *Config
Halt *idem.Halter
Cli *rpc.Client
ReadIncomingCh chan *rpc.Message
options rpc.Config
prefix []byte
mut sync.Mutex
}
var clicount int
func NewClientRpc(name string, cfg *Config, infWait bool) (r *ClientRpc, err error) {
remoteAddr := cfg.JservAddrNoProto()
//vv("NewClientRpc called with remoteAddr '%v'", remoteAddr)
tcp := insecure
if cfg.UseQUIC {
tcp = false
}
options := rpc.NewConfig()
options.ClientDialToHostPort = remoteAddr
options.TCPonly_no_TLS = tcp
options.UseQUIC = cfg.UseQUIC
options.CertPath = fixSlash(cfg.Home + "/.goq/certs")
options.PreSharedKeyPath = fixSlash(cfg.Home + "/.goq/goqclusterid")
options.ConnectTimeout = 10 * time.Second
options.ClientSendKeepAlive = 10 * time.Second
if infWait {
options.ConnectTimeout = 0
}
cli, err := rpc.NewClient(name, options)
if err != nil {
return nil, err
}
err = cli.Start()
if err != nil {
return nil, err
}
if cli == nil {
return nil, fmt.Errorf("got nil rpc.Client back from rpc.NewClient(name='%v', options='%#v')", name, options)
}
// server push mechanism: how we receive them.
readCh := cli.GetReadIncomingCh()
r = &ClientRpc{
name: name,
Cfg: cfg,
ReadIncomingCh: readCh,
//Discov: d,
options: *options,
Halt: idem.NewHalter(),
}
r.Cli = cli
return r, nil
}
func (c *ClientRpc) LocalAddr() string {
return c.Cli.LocalAddr()
}
func (c *ClientRpc) DoSyncCallWithTimeout(to time.Duration, j *Job) (back *Job, sentSerz []byte, err error) {
//vv("begin DoSynCallWithTimeout")
//defer vv("finishing DoSynCallWithTimeout")
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
time.Sleep(to)
cancelFunc()
}()
back, serz, err := c.DoSyncCallWithContext(ctx, j)
cancelFunc()
return back, serz, err
}
func (c *ClientRpc) DoSyncCall(j *Job) (back *Job, sentSerz []byte, err error) {
return c.DoSyncCallWithContext(context.Background(), j)
}
func (c *ClientRpc) DoSyncCallWithContext(ctx context.Context, j *Job) (back *Job, sentSerz []byte, err error) {
c.mut.Lock()
defer c.mut.Unlock()
sentSerz, err = c.Cfg.jobToBytesWithStamp(j)
if err != nil {
return nil, nil, err
}
args := rpc.NewMessage()
args.JobSerz = sentSerz
args.HDR.Subject = j.Msg.String()
args.HDR.ServiceName = "Ready2"
reply, err := c.Cli.SendAndGetReply(args, ctx.Done(), 0)
if err != nil {
return nil, nil, err
}
back, err = c.Cfg.bytesToJob(reply.JobSerz)
//vv("c.Cli.SendAndGetReply(); sent args.MID='%v' and bytesToJob got back err = '%v'; from reply.MID='%v'; reply.JobSerz='%v'", args.MID.String(), err, reply.MID.String(), string(reply.JobSerz)) // EOF here
return back, sentSerz, err
}
func (c *ClientRpc) AsyncSend(j *Job) error {
c.mut.Lock()
defer c.mut.Unlock()
jobSerz, err := c.Cfg.jobToBytesWithStamp(j)
if err != nil {
return err
}
args := rpc.NewMessage()
args.HDR.Subject = fmt.Sprintf("client.AsyncSend('%v')", j.Msg.String())
args.HDR.ServiceName = "Ready1"
args.JobSerz = jobSerz
return c.Cli.OneWaySend(args, nil, -1)
}
func (c *ClientRpc) Close() error {
c.Halt.ReqStop.Close()
return c.Cli.Close()
}
func fixSlash(s string) string {
if runtime.GOOS != "windows" {
return s
}
return strings.Replace(s, "/", "\\", -1)
}