-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
170 lines (137 loc) · 3.47 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
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
package avroipc
import (
"fmt"
"time"
"github.com/myzhan/avroipc/layers"
"github.com/myzhan/avroipc/protocols"
"github.com/myzhan/avroipc/transports"
)
// An avro client implementation
type Client interface {
Close() error
SendMessage(method string, datum interface{}) (string, error)
}
type client struct {
sendTimeout time.Duration
transport transports.Transport
framingLayer layers.FramingLayer
callProtocol protocols.CallProtocol
handshakeProtocol protocols.HandshakeProtocol
}
// NewClient creates an avro client with considering values of options from
// the passed configuration object and connects to the specified remote Flume
// endpoint immediately.
//
// This constructor supposed to be used in production environments.
func NewClientWithConfig(addr string, proto protocols.MessageProtocol, config *Config) (Client, error) {
c := &client{}
c.sendTimeout = config.SendTimeout
err := c.initTransports(addr, config)
if err != nil {
return nil, err
}
c.initProtocols(proto)
return c, c.handshake()
}
func (c *client) initProtocols(proto protocols.MessageProtocol) {
// All errors here are only related to compilations of Avro schemas
// and are not possible at runtime because they will be caught by unit tests.
c.framingLayer = layers.NewFraming(c.transport)
c.callProtocol, _ = protocols.NewCall(proto)
c.handshakeProtocol, _ = protocols.NewHandshake(proto)
}
func (c *client) initTransports(addr string, config *Config) (err error) {
c.transport, err = transports.NewSocket(addr, config.Timeout)
if err != nil {
return err
}
if config.CompressionLevel > 0 {
c.transport, err = transports.NewZlib(c.transport, config.CompressionLevel)
if err != nil {
return err
}
}
if config.TLSConfig != nil {
c.transport, err = transports.NewTLS(c.transport, config.TLSConfig)
if err != nil {
return err
}
}
if config.BufferSize > 0 {
c.transport = transports.NewBuffered(c.transport, config.BufferSize)
}
return
}
func (c *client) send(request []byte) ([]byte, error) {
err := c.applyDeadline()
if err != nil {
return nil, err
}
err = c.framingLayer.Write(request)
if err != nil {
return nil, err
}
err = c.transport.Flush()
if err != nil {
return nil, err
}
response, err := c.framingLayer.Read()
if err != nil {
return nil, err
}
return response, nil
}
func (c *client) handshake() error {
request, err := c.handshakeProtocol.PrepareRequest()
if err != nil {
return err
}
responseBytes, err := c.send(request)
if err != nil {
return err
}
needResend, err := c.handshakeProtocol.ProcessResponse(responseBytes)
if err != nil {
return err
}
if needResend {
err = c.handshake()
if err != nil {
return err
}
}
return nil
}
func (c *client) applyDeadline() error {
if c.sendTimeout > 0 {
d := time.Now().Add(c.sendTimeout)
return c.transport.SetDeadline(d)
}
return nil
}
func (c *client) Close() error {
err := c.applyDeadline()
if err != nil {
return err
}
return c.transport.Close()
}
func (c *client) SendMessage(method string, datum interface{}) (string, error) {
request, err := c.callProtocol.PrepareRequest(method, datum)
if err != nil {
return "", err
}
responseBytes, err := c.send(request)
if err != nil {
return "", err
}
response, err := c.callProtocol.ParseResponse(method, responseBytes)
if err != nil {
return "", err
}
status, ok := response.(string)
if !ok {
return "", fmt.Errorf("cannot convert status to string: %v", response)
}
return status, nil
}