-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
151 lines (131 loc) · 2.81 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
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
package rrpc
import (
"context"
"github.com/jjeffcaii/reactor-go/scheduler"
"github.com/rsocket/rsocket-go"
"github.com/rsocket/rsocket-go/payload"
"github.com/rsocket/rsocket-go/rx/flux"
"github.com/rsocket/rsocket-go/rx/mono"
)
// ClientConn struct
type ClientConn struct {
rSocket rsocket.RSocket
meterRegistry MeterRegistry
tracer Tracer
}
// InvokeRequestResponse invoke request response
func (p *ClientConn) InvokeRequestResponse(
ctx context.Context,
srv string,
method string,
data *[]byte,
opts ...CallOption,
) (<-chan payload.Payload, <-chan error) {
o := &callOption{}
for i := range opts {
opt := opts[i]
if opt != nil {
opt(o)
}
}
sent, e := NewRequestPayload(srv, method, *data, o.tracing, o.metadata)
if e != nil {
err := make(chan error, 1)
err <- e
close(err)
return nil, err
}
m := p.rSocket.RequestResponse(sent)
return mono.ToChannel(m, ctx)
}
// InvokeRequestStream invoke request stream
func (p *ClientConn) InvokeRequestStream(
ctx context.Context,
srv string,
method string,
data *[]byte,
opts ...CallOption,
) (<-chan payload.Payload, <-chan error) {
o := &callOption{}
for i := range opts {
opt := opts[i]
if opt != nil {
opt(o)
}
}
sent, e := NewRequestPayload(srv, method, *data, o.tracing, o.metadata)
if e != nil {
err := make(chan error, 1)
err <- e
close(err)
return nil, err
}
return flux.ToChannel(p.rSocket.RequestStream(sent), ctx)
}
// NewClientConn creates new client
func NewClientConn(c rsocket.RSocket, m MeterRegistry, t Tracer) *ClientConn {
return &ClientConn{
rSocket: c,
meterRegistry: m,
tracer: t,
}
}
type callOption struct {
tracing []byte
metadata []byte
}
type CallOption func(*callOption)
func WithTracing(tracing []byte) CallOption {
return func(o *callOption) {
o.tracing = tracing
}
}
func WithMetadata(metadata []byte) CallOption {
return func(o *callOption) {
o.metadata = metadata
}
}
func (p *ClientConn) InvokeChannel(
ctx context.Context,
srv string,
method string,
datachan chan *[]byte,
err chan error,
opts ...CallOption) (<-chan payload.Payload, <-chan error) {
o := &callOption{}
for i := range opts {
opt := opts[i]
if opt != nil {
opt(o)
}
}
inchan := make(chan payload.Payload)
inerr := make(chan error)
scheduler.Parallel().Worker().Do(func() {
defer close(inchan)
defer close(inerr)
loop:
for {
select {
case data, ok := <-datachan:
if ok {
sent, e := NewRequestPayload(srv, method, *data, o.tracing, o.metadata)
if e != nil {
inerr <- e
} else {
inchan <- sent
}
} else {
break loop
}
case e := <-err:
if e != nil {
inerr <- e
}
}
}
})
influx := flux.CreateFromChannel(inchan, inerr)
outflux := p.rSocket.RequestChannel(influx)
return flux.ToChannel(outflux, ctx)
}