/
grpc.go
63 lines (54 loc) · 2.23 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
package net
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"log"
"net"
)
// ListenAndServeGRPC receives a configured gRPC server and starts listening. This function does not block.
// You should have called the appropriate gRPC server configuration function before calling this.
// After calling this function, you should call `server.Stop()` when done with the server.
func ListenAndServeGRPC(server *grpc.Server) *bufconn.Listener {
listener := bufconn.Listen(1024 * 1024)
go func() {
if err := server.Serve(listener); err != nil {
log.Fatalln("Failed while serving gRPC server:", err)
}
}()
return listener
}
// ConnectToGRPCServer connects to a gRPC server and returns the established connection.
// The returned connection should be used to instance a service client.
func ConnectToGRPCServer(listener *bufconn.Listener) (*grpc.ClientConn, error) {
dialer := func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
return grpc.DialContext(
context.Background(),
"",
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialer),
)
}
// GRPCCallOK processes a gRPC response error to verify that a gRPC call returned an OK status.
// The actual contents of the error are ignored. Only the call status error is checked.
func GRPCCallOK(err error) bool {
return status.Code(err) == codes.OK
}
// GRPCCallSuccessful processes a gRPC response error to verify that a gRPC call returned an OK or Unknown status.
// The actual contents of the error are ignored. Only the call status error is checked.
//
// gRPC method implementations that return anything other than a *Status value as their error value (e.g. an error
// generated with errors.New()) will have the value wrapped inside a *Status value and its error code set to Unknown.
//
// This function considers calls with statuses with unknown errors successful. In addition, if the error value is
// not of type *Status, then the call is considered successful.
func GRPCCallSuccessful(err error) bool {
code := status.Code(err)
return code == codes.OK || code == codes.Unknown
}