Skip to content

Commit def5a49

Browse files
committed
[-] add compacted revision handler, fixes #208
1 parent b5d471f commit def5a49

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

checker/etcd_leader_checker.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"fmt"
89
"log"
910
"os"
1011
"time"
1112

1213
"github.com/cybertec-postgresql/vip-manager/vipconfig"
14+
v3rpc "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
1315
clientv3 "go.etcd.io/etcd/client/v3"
1416
)
1517

@@ -69,8 +71,8 @@ func getTransport(conf *vipconfig.Config) (*tls.Config, error) {
6971
return tlsClientConfig, nil
7072
}
7173

72-
// init gets the current leader from etcd
73-
func (elc *EtcdLeaderChecker) init(ctx context.Context, out chan<- bool) {
74+
// get gets the current leader from etcd
75+
func (elc *EtcdLeaderChecker) get(ctx context.Context, out chan<- bool) {
7476
resp, err := elc.Get(ctx, elc.Key)
7577
if err != nil {
7678
log.Printf("etcd error: %s", err)
@@ -93,8 +95,12 @@ func (elc *EtcdLeaderChecker) watch(ctx context.Context, out chan<- bool) error
9395
return ctx.Err()
9496
case watchResp := <-watchChan:
9597
if err := watchResp.Err(); err != nil {
96-
log.Printf("etcd watcher returned error: %s", err)
97-
out <- false
98+
if errors.Is(err, v3rpc.ErrCompacted) { // revision is compacted, try direct get key
99+
elc.get(ctx, out)
100+
} else {
101+
log.Printf("etcd watcher returned error: %s", err)
102+
out <- false
103+
}
98104
continue
99105
}
100106
for _, event := range watchResp.Events {
@@ -108,7 +114,7 @@ func (elc *EtcdLeaderChecker) watch(ctx context.Context, out chan<- bool) error
108114
// GetChangeNotificationStream monitors the leader in etcd
109115
func (elc *EtcdLeaderChecker) GetChangeNotificationStream(ctx context.Context, out chan<- bool) error {
110116
defer elc.Close()
111-
go elc.init(ctx, out)
117+
go elc.get(ctx, out)
112118
wctx, cancel := context.WithCancel(ctx)
113119
defer cancel()
114120
return elc.watch(wctx, out)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/mdlayher/arp v0.0.0-20220512170110-6706a2966875
88
github.com/spf13/pflag v1.0.5
99
github.com/spf13/viper v1.18.2
10+
go.etcd.io/etcd/api/v3 v3.5.13
1011
go.etcd.io/etcd/client/v3 v3.5.13
1112
golang.org/x/sys v0.19.0
1213
)
@@ -44,7 +45,6 @@ require (
4445
github.com/spf13/afero v1.11.0 // indirect
4546
github.com/spf13/cast v1.6.0 // indirect
4647
github.com/subosito/gotenv v1.6.0 // indirect
47-
go.etcd.io/etcd/api/v3 v3.5.13 // indirect
4848
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
4949
go.uber.org/atomic v1.9.0 // indirect
5050
go.uber.org/multierr v1.9.0 // indirect

0 commit comments

Comments
 (0)