forked from facebookarchive/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (87 loc) · 2.77 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
package zk
import (
"context"
"errors"
"fmt"
"net"
"time"
)
// ErrMaxRetries is used to differentiate retryable from non-retryable errors in the client.
var ErrMaxRetries = errors.New("connection failed after max retries")
// Client represents a Zookeeper client abstraction with additional configuration parameters.
type Client struct {
// Dialer is a function to be used to establish a connection to a single host.
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
SessionTimeout time.Duration
MaxRetries int
Network string
Ensemble string
conn *Conn
}
// GetData uses the retryable client to call Get on a Zookeeper server.
func (client *Client) GetData(ctx context.Context, path string) ([]byte, error) {
var err error
var data []byte
err = client.doRetry(ctx, func() error {
data, err = client.conn.GetData(path)
return err
})
return data, err
}
// GetChildren uses the retryable client to call GetChildren on a Zookeeper server.
func (client *Client) GetChildren(ctx context.Context, path string) ([]string, error) {
var children []string
var err error
err = client.doRetry(ctx, func() error {
children, err = client.conn.GetChildren(path)
return err
})
return children, err
}
// Reset closes the client's underlying connection, cancelling any RPCs currently in-flight.
// Future RPC calls will need to re-initialize the connection.
func (client *Client) Reset() error {
return client.conn.Close()
}
// doRetry makes attempts at connection and RPC execution according to the MaxRetries parameter.
// If the MaxRetries value is not set, the RPC is executed only once.
func (client *Client) doRetry(ctx context.Context, fun func() error) error {
var err error
for i := 0; i <= client.MaxRetries; i++ {
if ctx.Err() != nil {
return ctx.Err() // ctx canceled, don't retry
}
if err = client.getConn(ctx); err != nil {
continue
}
err = fun()
// check if we have encountered a server-side error before retrying
var ioError *Error
if errors.As(err, &ioError) {
return fmt.Errorf("ZK server returned error: %w", err) // server errors are non-retryable
}
if err != nil {
continue // retry
}
return nil
}
return fmt.Errorf("%w (%d): %v", ErrMaxRetries, client.MaxRetries, err)
}
// getConn initializes client connection or reuses it if it has already been established.
func (client *Client) getConn(ctx context.Context) error {
if client.conn == nil || !client.conn.isAlive() {
conn, err := client.DialContext(ctx, client.Network, client.Ensemble)
if err != nil {
return err
}
client.conn = conn
}
return nil
}