@@ -18,9 +18,13 @@ import (
1818 "github.com/tarantool/go-storage/watch"
1919)
2020
21- // Client defines the minimal interface needed for etcd operations.
21+ // Client defines the minimal interface needed for etcd operations. It
22+ // is compatible with etcdclientv3.Client.
23+ //
2224// This allows for easier testing and mock implementations.
2325type Client interface {
26+ Watcher
27+
2428 // Txn creates a new transaction.
2529 Txn (ctx context.Context ) etcd.Txn
2630}
@@ -59,62 +63,18 @@ var (
5963 errUnsupportedOperationType = errors .New ("unsupported operation type" )
6064)
6165
62- // etcdClientAdapter wraps etcd.Client to implement our Client interface.
63- type etcdClientAdapter struct {
64- client * etcd.Client
65- }
66-
67- func (a * etcdClientAdapter ) Txn (ctx context.Context ) etcd.Txn {
68- return a .client .Txn (ctx )
69- }
70-
71- // etcdWatcherAdapter wraps etcd.Watcher to implement our Watcher interface.
72- type etcdWatcherAdapter struct {
73- watcher etcd.Watcher
74- }
75-
76- func (a * etcdWatcherAdapter ) Watch (ctx context.Context , key string , opts ... etcd.OpOption ) etcd.WatchChan {
77- return a .watcher .Watch (ctx , key , opts ... )
78- }
79-
80- func (a * etcdWatcherAdapter ) Close () error {
81- return fmt .Errorf ("failed to close: %w" , a .watcher .Close ())
82- }
83-
8466// etcdWatcherFactory implements WatcherFactory for etcd clients.
8567type etcdWatcherFactory struct {}
8668
8769func (f * etcdWatcherFactory ) NewWatcher (client Client ) Watcher {
88- // For etcd clients, we need access to the underlying client.
89- if adapter , ok := client .(* etcdClientAdapter ); ok {
90- return & etcdWatcherAdapter {
91- watcher : etcd .NewWatcher (adapter .client ),
92- }
93- }
94-
95- // For other implementations, return a no-op watcher.
96- return & noopWatcher {}
97- }
98-
99- // noopWatcher is a no-op implementation of Watcher for non-etcd clients.
100- type noopWatcher struct {}
101-
102- func (w * noopWatcher ) Watch (_ context.Context , _ string , _ ... etcd.OpOption ) etcd.WatchChan {
103- ch := make (chan etcd.WatchResponse )
104- close (ch )
105-
106- return ch
107- }
108-
109- func (w * noopWatcher ) Close () error {
110- return nil
70+ return client
11171}
11272
11373// New creates a new etcd driver instance using an existing etcd client.
11474// The client should be properly configured and connected to an etcd cluster.
115- func New (client * etcd. Client ) * Driver {
75+ func New (client Client ) * Driver {
11676 return & Driver {
117- client : & etcdClientAdapter { client : client } ,
77+ client : client ,
11878 watcherFactory : & etcdWatcherFactory {},
11979 }
12080}
0 commit comments