add verification on client.Sync to ensure the returned endpoints never empty

Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
This commit is contained in:
Benjamin Wang 2024-06-24 10:05:48 +01:00
parent 9314ef760d
commit 281f177345

View File

@ -34,6 +34,7 @@ import (
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/api/v3/version"
"go.etcd.io/etcd/client/pkg/v3/logutil"
"go.etcd.io/etcd/client/pkg/v3/verify"
"go.etcd.io/etcd/client/v3/credentials"
"go.etcd.io/etcd/client/v3/internal/endpoint"
"go.etcd.io/etcd/client/v3/internal/resolver"
@ -194,6 +195,13 @@ func (c *Client) Sync(ctx context.Context) error {
eps = append(eps, m.ClientURLs...)
}
}
// The linearizable `MemberList` returned successfully, so the
// endpoints shouldn't be empty.
verify.Verify(func() {
if len(eps) == 0 {
panic("empty endpoints returned from etcd cluster")
}
})
c.SetEndpoints(eps...)
c.lg.Debug("set etcd endpoints by autoSync", zap.Strings("endpoints", eps))
return nil