forked from jina-ai/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
201 lines (181 loc) · 4.24 KB
/
grpc.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package client
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/jina-ai/client-go/jina"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"
)
type GRPCClient struct {
Host string
conn *grpc.ClientConn
rpcClient jina.JinaRPCClient
ctx context.Context
}
func getSecureDialOptions() grpc.DialOption {
return grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{InsecureSkipVerify: false}))
}
func getInsecureDialOptions() grpc.DialOption {
return grpc.WithTransportCredentials(insecure.NewCredentials())
}
func getHostAndDialOptions(host string) (string, grpc.DialOption) {
if strings.HasPrefix(host, "grpcs://") {
host = strings.TrimPrefix(host, "grpcs://")
return fmt.Sprint(host + ":443"), getSecureDialOptions()
}
if strings.HasPrefix(host, "grpc://") {
host = strings.TrimPrefix(host, "grpc://")
return host, getInsecureDialOptions()
}
return host, getInsecureDialOptions()
}
func NewGRPCClient(host string) (*GRPCClient, error) {
host, dialOptions := getHostAndDialOptions(host)
conn, err := grpc.Dial(host, dialOptions)
if err != nil {
fmt.Println("Error in grpc dial", err)
return nil, err
}
return &GRPCClient{
Host: host,
conn: conn,
rpcClient: jina.NewJinaRPCClient(conn),
ctx: context.Background(),
}, nil
}
func (c GRPCClient) POST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error {
var wg sync.WaitGroup
stream, err := c.rpcClient.Call(c.ctx)
if err != nil {
return err
}
go func() {
for {
resp, err := stream.Recv()
if resp == nil {
break
}
if err != nil {
fmt.Println("Error in receiving response", err)
if onError != nil {
onError(resp)
}
} else if onDone != nil {
onDone(resp)
}
if onAlways != nil {
onAlways(resp)
}
wg.Done()
}
}()
for {
req, ok := <-requests
if !ok {
break
}
if err := stream.Send(req); err != nil {
panic(err)
}
wg.Add(1)
}
wg.Wait()
stream.CloseSend()
return nil
}
func (c GRPCClient) SequentialPOST(requests <-chan *jina.DataRequestProto, onDone, onError, onAlways CallbackType) error {
stream, err := c.rpcClient.Call(c.ctx)
if err != nil {
return err
}
for {
req, ok := <-requests
if !ok {
break
}
if err := stream.Send(req); err != nil {
panic(err)
}
resp, err := stream.Recv()
if resp == nil {
break
}
if err != nil {
fmt.Println("Error in receiving response", err)
if onError != nil {
onError(resp)
}
} else if onDone != nil {
onDone(resp)
}
if onAlways != nil {
onAlways(resp)
}
}
stream.CloseSend()
return nil
}
type GRPCHealthCheckClient struct {
Host string
conn *grpc.ClientConn
rpcClient jina.JinaGatewayDryRunRPCClient
ctx context.Context
}
func NewGRPCHealthCheckClient(host string) (*GRPCHealthCheckClient, error) {
host, dialOptions := getHostAndDialOptions(host)
conn, err := grpc.Dial(host, dialOptions)
if err != nil {
return nil, err
}
return &GRPCHealthCheckClient{
Host: host,
conn: conn,
rpcClient: jina.NewJinaGatewayDryRunRPCClient(conn),
ctx: context.Background(),
}, nil
}
func (c *GRPCHealthCheckClient) HealthCheck() (bool, error) {
status, err := c.rpcClient.DryRun(c.ctx, &emptypb.Empty{})
if err != nil {
return false, err
}
if status.Code == jina.StatusProto_SUCCESS {
return true, nil
}
return false, nil
}
type GRPCInfoClient struct {
Host string
conn *grpc.ClientConn
rpcClient jina.JinaInfoRPCClient
ctx context.Context
}
func NewGRPCInfoClient(host string) (*GRPCInfoClient, error) {
host, dialOptions := getHostAndDialOptions(host)
conn, err := grpc.Dial(host, dialOptions)
if err != nil {
return nil, err
}
return &GRPCInfoClient{
Host: host,
conn: conn,
rpcClient: jina.NewJinaInfoRPCClient(conn),
ctx: context.Background(),
}, nil
}
func (c *GRPCInfoClient) Info() (*jina.JinaInfoProto, error) {
return c.rpcClient.XStatus(c.ctx, &emptypb.Empty{})
}
func (c *GRPCInfoClient) InfoJSON() ([]byte, error) {
info, err := c.Info()
if err != nil {
return nil, err
}
return json.MarshalIndent(info, "", " ")
}