-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
48 lines (41 loc) · 906 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
package test
import (
"context"
"sync"
pb "github.com/philip-bui/grpc-zerolog/protos"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
)
type TestClient struct {
ExampleReq *pb.TestMessage
ExampleInvalidReq *pb.TestMessage
pb.TestServiceClient
}
func (c *TestClient) SendReq() (*pb.TestMessage, error) {
return c.TestUnary(context.Background(), c.ExampleReq)
}
func (c *TestClient) SendErr() (*pb.TestMessage, error) {
return c.TestUnary(context.Background(), c.ExampleInvalidReq)
}
var (
client *TestClient
clientSync sync.Once
)
func GetClient() *TestClient {
clientSync.Do(func() {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatal().Err(err).Msg("start client")
}
client = &TestClient{
&pb.TestMessage{
Test: "Hi",
},
&pb.TestMessage{
Test: "",
},
pb.NewTestServiceClient(conn),
}
})
return client
}