mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 15:11:09 +02:00
cluster: configurable session TTL for the leader elector
This commit is contained in:
@@ -35,7 +35,7 @@ type etcdLeaderElector struct {
|
||||
|
||||
// newEtcdLeaderElector constructs a new etcdLeaderElector.
|
||||
func newEtcdLeaderElector(ctx context.Context, id, electionPrefix string,
|
||||
cfg *etcd.Config) (*etcdLeaderElector, error) {
|
||||
leaderSessionTTL int, cfg *etcd.Config) (*etcdLeaderElector, error) {
|
||||
|
||||
clientCfg := clientv3.Config{
|
||||
Context: ctx,
|
||||
@@ -72,7 +72,9 @@ func newEtcdLeaderElector(ctx context.Context, id, electionPrefix string,
|
||||
cli.Lease = namespace.NewLease(cli.Lease, cfg.Namespace)
|
||||
log.Infof("Applied namespace to leader elector: %v", cfg.Namespace)
|
||||
|
||||
session, err := concurrency.NewSession(cli)
|
||||
session, err := concurrency.NewSession(
|
||||
cli, concurrency.WithTTL(leaderSessionTTL),
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("Unable to start new leader election session: %v",
|
||||
err)
|
||||
|
@@ -15,9 +15,9 @@ import (
|
||||
func makeEtcdElector(ctx context.Context, args ...interface{}) (LeaderElector,
|
||||
error) {
|
||||
|
||||
if len(args) != 3 {
|
||||
if len(args) != 4 {
|
||||
return nil, fmt.Errorf("invalid number of arguments to "+
|
||||
"cluster.makeEtcdElector(): expected 3, got %v",
|
||||
"cluster.makeEtcdElector(): expected 4, got %v",
|
||||
len(args))
|
||||
}
|
||||
|
||||
@@ -33,13 +33,21 @@ func makeEtcdElector(ctx context.Context, args ...interface{}) (LeaderElector,
|
||||
"cluster.makeEtcdElector(), expected: string")
|
||||
}
|
||||
|
||||
etcdCfg, ok := args[2].(*etcd.Config)
|
||||
leaderSessionTTL, ok := args[2].(int)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid argument (2) to " +
|
||||
"cluster.makeEtcdElector(), expected: int")
|
||||
}
|
||||
|
||||
etcdCfg, ok := args[3].(*etcd.Config)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid argument (3) to " +
|
||||
"cluster.makeEtcdElector(), expected: *etcd.Config")
|
||||
}
|
||||
|
||||
return newEtcdLeaderElector(ctx, id, electionPrefix, etcdCfg)
|
||||
return newEtcdLeaderElector(
|
||||
ctx, id, electionPrefix, leaderSessionTTL, etcdCfg,
|
||||
)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@@ -57,15 +57,16 @@ func TestEtcdElector(t *testing.T) {
|
||||
election = "/election/"
|
||||
id1 = "e1"
|
||||
id2 = "e2"
|
||||
ttl = 5
|
||||
)
|
||||
|
||||
e1, err := newEtcdLeaderElector(
|
||||
ctx, id1, election, etcdCfg,
|
||||
ctx, id1, election, ttl, etcdCfg,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
e2, err := newEtcdLeaderElector(
|
||||
ctx, id2, election, etcdCfg,
|
||||
ctx, id2, election, ttl, etcdCfg,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
Reference in New Issue
Block a user