-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* doc(otetcd): fix comment error * feat: add remote config provider, client use etcd * fix: comment error * test: improve test coverage * test: improve test coverage * test(remote): make sure the coroutine is run * doc(otetcd): fix comment error * feat: add remote config provider, client use etcd * fix: comment error * test: improve test coverage * test: improve test coverage * test(remote): make sure the coroutine is run * docs: rename file to key * fix: remove Put, use put in test file * fix: change etcd config ip Co-authored-by: 谷溪 <guxi99@gmail.com>
- Loading branch information
Showing
6 changed files
with
260 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package remote | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
// Remote is a core.ConfProvider and contract.ConfigWatcher implementation to read and watch remote config key. | ||
// The remote client uses etcd. | ||
type Remote struct { | ||
key string | ||
clientConfig *clientv3.Config | ||
} | ||
|
||
// Provider create a *Remote | ||
func Provider(key string, clientConfig *clientv3.Config) *Remote { | ||
return &Remote{ | ||
key: key, | ||
clientConfig: clientConfig, | ||
} | ||
} | ||
|
||
// ReadBytes reads the contents of a key from etcd and returns the bytes. | ||
func (r *Remote) ReadBytes() ([]byte, error) { | ||
client, err := clientv3.New(*r.clientConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer client.Close() | ||
|
||
resp, err := client.Get(context.Background(), r.key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.Count == 0 { | ||
return nil, fmt.Errorf("no such config key: %s", r.key) | ||
} | ||
|
||
return resp.Kvs[0].Value, nil | ||
} | ||
|
||
// Read is not supported by the remote provider. | ||
func (r *Remote) Read() (map[string]interface{}, error) { | ||
return nil, errors.New("remote provider does not support this method") | ||
} | ||
|
||
// Watch watches the change to the remote key from etcd. If the key is edited or created, the reload function | ||
// will be called. note the reload function should not just load the changes made within this key, but rather | ||
// it should reload the whole config stack. For example, if the flag or env takes precedence over the config | ||
// key, they should remain to be so after the key changes. | ||
func (r *Remote) Watch(ctx context.Context, reload func() error) error { | ||
client, err := clientv3.New(*r.clientConfig) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
rch := client.Watch(ctx, r.key) | ||
for { | ||
select { | ||
case resp := <-rch: | ||
if resp.Err() != nil { | ||
return resp.Err() | ||
} | ||
// Trigger event. | ||
if err := reload(); err != nil { | ||
return err | ||
} | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package remote | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
func TestRemote(t *testing.T) { | ||
cfg := &clientv3.Config{ | ||
Endpoints: []string{"127.0.0.1:2379"}, | ||
DialTimeout: 2 * time.Second, | ||
} | ||
|
||
r := Provider("config.yaml", cfg) | ||
|
||
var testVal = "name: app" | ||
// PREPARE TEST DATA | ||
if err := put(r, testVal); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err := r.Read() | ||
assert.Error(t, err) | ||
|
||
bytes, err := r.ReadBytes() | ||
assert.NoError(t, err) | ||
assert.Equal(t, testVal, string(bytes)) | ||
|
||
var ch = make(chan string) | ||
go r.Watch(context.Background(), func() error { | ||
bytes, err := r.ReadBytes() | ||
if err != nil { | ||
ch <- "" | ||
return err | ||
} | ||
ch <- string(bytes) | ||
return nil | ||
}) | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
if err := put(r, testVal); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
newVal := <-ch | ||
assert.Equal(t, testVal, newVal) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
var ( | ||
r *Remote | ||
err error | ||
) | ||
|
||
cfg := &clientv3.Config{ | ||
Endpoints: []string{}, | ||
} | ||
|
||
r = Provider("config.yaml", cfg) | ||
err = put(r, "test") | ||
assert.Error(t, err) | ||
|
||
_, err = r.ReadBytes() | ||
assert.Error(t, err) | ||
|
||
err = r.Watch(context.Background(), func() error { | ||
return nil | ||
}) | ||
assert.Error(t, err) | ||
|
||
cfg = &clientv3.Config{ | ||
Endpoints: []string{"127.0.0.1:2379"}, | ||
DialTimeout: 2 * time.Second, | ||
} | ||
r = Provider("config-test1", cfg) | ||
_, err = r.ReadBytes() | ||
assert.Error(t, err) | ||
|
||
r = Provider("config-test2", cfg) | ||
|
||
// Confirm that the two coroutines are finished | ||
g := sync.WaitGroup{} | ||
g.Add(2) | ||
go func() { | ||
err := r.Watch(context.Background(), func() error { | ||
return fmt.Errorf("for test") | ||
}) | ||
assert.Error(t, err) | ||
g.Done() | ||
}() | ||
|
||
go func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
err := r.Watch(ctx, func() error { | ||
return fmt.Errorf("for test") | ||
}) | ||
assert.Error(t, err) | ||
g.Done() | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
if err := put(r, "name: test"); err != nil { | ||
t.Fatal(err) | ||
} | ||
g.Wait() | ||
} | ||
|
||
func put(r *Remote, val string) error { | ||
client, err := clientv3.New(*r.clientConfig) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
_, err = client.Put(context.Background(), r.key, val) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters