-
Notifications
You must be signed in to change notification settings - Fork 12
/
ingressclient.go
84 lines (68 loc) · 1.91 KB
/
ingressclient.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
package ingressclient
import (
"bytes"
"crypto/tls"
"encoding/gob"
"time"
"github.com/cloudfoundry/metric-store-release/src/pkg/leanstreams"
"github.com/cloudfoundry/metric-store-release/src/pkg/logger"
"github.com/cloudfoundry/metric-store-release/src/pkg/rpc"
)
const (
MAX_BATCH_SIZE_IN_BYTES = 32 * 1024
MAX_INGRESS_PAYLOAD_SIZE_IN_BYTES = 2 * MAX_BATCH_SIZE_IN_BYTES
)
type IngressClient struct {
connection *leanstreams.TCPClient
log *logger.Logger
dialTimeout time.Duration
}
func NewIngressClient(ingressAddress string, tlsConfig *tls.Config, opts ...IngressClientOption) (*IngressClient, error) {
client := &IngressClient{
log: logger.NewNop(),
dialTimeout: 10 * time.Second,
}
for _, o := range opts {
o(client)
}
clientConfig := &leanstreams.TCPClientConfig{
MaxMessageSize: MAX_INGRESS_PAYLOAD_SIZE_IN_BYTES,
Address: ingressAddress,
TLSConfig: tlsConfig,
}
connection, err := leanstreams.DialTCPUntilConnected(clientConfig, client.dialTimeout)
if err != nil {
return nil, err
}
client.connection = connection
return client, nil
}
type IngressClientOption func(*IngressClient)
func WithIngressClientLogger(log *logger.Logger) IngressClientOption {
return func(client *IngressClient) {
client.log = log
}
}
func WithDialTimeout(timeout time.Duration) IngressClientOption {
return func(client *IngressClient) {
client.dialTimeout = timeout
}
}
func (c *IngressClient) Write(points []*rpc.Point) error {
var payload bytes.Buffer
enc := gob.NewEncoder(&payload)
err := enc.Encode(rpc.Batch{Points: points})
if err != nil {
c.log.Error("gob encode error", err)
return err
}
// TODO: consider adding back in a timeout (i.e. 3 seconds)
bytesWritten, err := c.connection.Write(payload.Bytes())
if err == nil {
c.log.Info("wrote bytes", logger.Count(bytesWritten))
}
return err
}
func (c *IngressClient) Close() {
c.connection.Close()
}