-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
201 lines (164 loc) · 4.04 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
Append(event *Event) (string, error)
AppendBatch(events []*Event) (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 default option values and
// connects to the specified remote Flume endpoint immediately.
//
// Very useful for the testing purposes and to build simple examples.
func NewClient(addr string) (Client, error) {
return NewClientWithConfig(addr, NewConfig())
}
// 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, config *Config) (Client, error) {
c := &client{}
c.sendTimeout = config.SendTimeout
err := c.initTransports(addr, config)
if err != nil {
return nil, err
}
err = c.initProtocols()
if err != nil {
return nil, err
}
return c, c.handshake()
}
func (c *client) initProtocols() error {
proto, err := protocols.NewAvroSource()
if err != nil {
return err
}
c.framingLayer = layers.NewFraming(c.transport)
c.callProtocol, err = protocols.NewCall(proto)
if err != nil {
return err
}
c.handshakeProtocol, err = protocols.NewHandshake()
if err != nil {
return err
}
return nil
}
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.BufferSize > 0 {
c.transport = transports.NewBuffered(c.transport, config.BufferSize)
}
if config.CompressionLevel > 0 {
c.transport, err = transports.NewZlib(c.transport, config.CompressionLevel)
if err != nil {
return err
}
}
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) 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
}
func (c *client) applyDeadline() error {
if c.sendTimeout > 0 {
d := time.Now().Add(c.sendTimeout)
return c.transport.SetDeadline(d)
}
return nil
}
// Append sends event to flume
func (c *client) Append(event *Event) (string, error) {
datum := event.toMap()
return c.sendMessage("append", datum)
}
// Append sends events to flume
func (c *client) AppendBatch(events []*Event) (string, error) {
datum := make([]map[string]interface{}, 0)
for _, event := range events {
datum = append(datum, event.toMap())
}
return c.sendMessage("appendBatch", datum)
}
func (c *client) Close() error {
err := c.applyDeadline()
if err != nil {
return err
}
return c.transport.Close()
}