forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
etcd.go
33 lines (26 loc) · 1.05 KB
/
etcd.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
package mainboilerplate
import (
"time"
"github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/clientv3/concurrency"
)
// EtcdConfig configures the application Etcd session.
type EtcdConfig struct {
Address protocol.Endpoint `long:"address" env:"ADDRESS" default:"http://localhost:2379" description:"Etcd service address endpoint"`
LeaseTTL time.Duration `long:"lease" env:"LEASE_TTL" default:"20s" description:"Time-to-live of Etcd lease"`
}
// EtcdContext composes an Etcd client and TTL session lease.
type EtcdContext struct {
Etcd *clientv3.Client
Session *concurrency.Session
}
// MustEtcdContext builds an EtcdContext from an EtcdConfig.
func MustEtcdContext(cfg EtcdConfig) EtcdContext {
var etcd, err = clientv3.NewFromURL(string(cfg.Address))
Must(err, "failed to build Etcd client")
session, err := concurrency.NewSession(etcd,
concurrency.WithTTL(int(cfg.LeaseTTL.Seconds())))
Must(err, "failed to establish Etcd lease session")
return EtcdContext{Etcd: etcd, Session: session}
}