/
client.go
115 lines (97 loc) · 2.89 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
// Package client provides a client to access to token bucket server.
package client
import (
"context"
"errors"
"net"
"strings"
"time"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"github.com/orgplace/trafficquota/proto"
"google.golang.org/grpc"
)
// Client is a client to access to token bucket server.
type Client interface {
// Take takes tokens from server.
Take(chunkKey string, bucketKeys ...string) (bool, error)
// TakeContext takes tokens from server.
TakeContext(ctx context.Context, chunkKey string, bucketKeys ...string) (bool, error)
// Ping verifies a connection to the server is still alive.
Ping() error
// PingContext verifies a connection to the server is still alive.
PingContext(ctx context.Context) error
// Close closes a connection.
Close() error
}
var errNotServing = errors.New("server is not serving")
type client struct {
clientConn *grpc.ClientConn
trafficQuota proto.TrafficQuotaClient
health grpc_health_v1.HealthClient
}
// NewInsecureClient constructs a new client without TLS.
func NewInsecureClient(addr string) (Client, error) {
addr, options := parseAddr(addr)
cc, err := grpc.Dial(addr, append(
options,
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 1 * time.Second,
Timeout: 5 * time.Second,
PermitWithoutStream: true,
}),
grpc.WithInsecure(),
)...)
if err != nil {
return nil, err
}
return newClient(cc), nil
}
func newClient(cc *grpc.ClientConn) *client {
return &client{
clientConn: cc,
trafficQuota: proto.NewTrafficQuotaClient(cc),
health: grpc_health_v1.NewHealthClient(cc),
}
}
var unixDomainSocketDialer = grpc.WithDialer(func(a string, t time.Duration) (net.Conn, error) {
return net.Dial("unix", a)
})
func parseAddr(addr string) (string, []grpc.DialOption) {
options := []grpc.DialOption{}
const unixDomainSocketPrefix = "unix:"
if strings.HasPrefix(addr, unixDomainSocketPrefix) {
addr = addr[len(unixDomainSocketPrefix):]
options = append(options, unixDomainSocketDialer)
}
return addr, options
}
func (c *client) Take(chunkKey string, bucketKeys ...string) (bool, error) {
return c.TakeContext(context.Background(), chunkKey, bucketKeys...)
}
func (c *client) TakeContext(ctx context.Context, chunkKey string, bucketKeys ...string) (bool, error) {
res, err := c.trafficQuota.Take(ctx, &proto.TakeRequest{
ChunkKey: chunkKey,
BucketKeys: bucketKeys,
})
if err != nil {
return false, err
}
return res.Allowed, err
}
func (c *client) Ping() error {
return c.PingContext(context.Background())
}
func (c *client) PingContext(ctx context.Context) error {
res, err := c.health.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if res.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return errNotServing
}
return nil
}
func (c *client) Close() error {
return c.clientConn.Close()
}