-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
52 lines (45 loc) · 984 Bytes
/
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
package remote
import (
"context"
"github.com/licaonfee/selina"
"google.golang.org/grpc"
)
var _ selina.Worker = (*Client)(nil)
//ClientOptions customize client
type ClientOptions struct {
Address string
}
//Client connect to a remote grpc endpoint
type Client struct {
opts ClientOptions
}
//Process implements selina.Worker interface
func (c *Client) Process(ctx context.Context, args selina.ProcessArgs) error {
defer close(args.Output)
conn, err := grpc.DialContext(ctx, c.opts.Address, grpc.WithInsecure())
if err != nil {
return err
}
//TODO: handle error
defer conn.Close()
wc := NewWorkerClient(conn)
for {
select {
case msg, ok := <-args.Input:
if !ok {
return nil
}
m := Message{Data: msg}
_, err := wc.Send(ctx, &m)
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
//NewClient create a new Client with given options
func NewClient(opts ClientOptions) *Client {
return &Client{opts: opts}
}