Skip to content

Commit 2a18092

Browse files
authored
refactor: rename package remote to package etcd (#175)
* refactor: etcd != remote. * test: fix unbounded context * feat: add WithKey helper * fix: eliminate pointers BREAKING CHANGE: package remote no longer exists.
1 parent 7d7f997 commit 2a18092

File tree

4 files changed

+52
-47
lines changed

4 files changed

+52
-47
lines changed

c.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/DoNewsCode/core/codec/yaml"
1717
"github.com/DoNewsCode/core/config"
18-
"github.com/DoNewsCode/core/config/remote"
1918
"github.com/DoNewsCode/core/config/watcher"
2019
"github.com/DoNewsCode/core/container"
2120
"github.com/DoNewsCode/core/contract"
@@ -24,7 +23,6 @@ import (
2423
"github.com/go-kit/kit/log"
2524
"github.com/knadh/koanf/providers/confmap"
2625
"github.com/knadh/koanf/providers/file"
27-
clientv3 "go.etcd.io/etcd/client/v3"
2826
)
2927

3028
// C stands for the core of the application. It contains service definitions and
@@ -93,13 +91,6 @@ func WithYamlFile(path string) (CoreOption, CoreOption) {
9391
WithConfigWatcher(watcher.File{Path: path})
9492
}
9593

96-
// WithRemoteYamlFile is a two-in-one coreOption. It uses the remote key on etcd as the
97-
// source of configuration, and watches the change of that key for hot reloading.
98-
func WithRemoteYamlFile(key string, cfg clientv3.Config) (CoreOption, CoreOption) {
99-
r := remote.Provider(key, &cfg)
100-
return WithConfigStack(r, config.CodecParser{Codec: yaml.Codec{}}), WithConfigWatcher(r)
101-
}
102-
10394
// WithInline is a CoreOption that creates a inline config in the configuration stack.
10495
func WithInline(key string, entry interface{}) CoreOption {
10596
return WithConfigStack(confmap.Provider(map[string]interface{}{

config/remote/remote.go renamed to config/remote/etcd/etcd.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1-
package remote
1+
// Package etcd allows the core package to bootstrap its configuration from an etcd server.
2+
package etcd
23

34
import (
45
"context"
56
"errors"
67
"fmt"
7-
8+
"github.com/DoNewsCode/core"
9+
"github.com/DoNewsCode/core/config"
10+
"github.com/DoNewsCode/core/contract"
811
"go.etcd.io/etcd/client/v3"
912
)
1013

11-
// Remote is a core.ConfProvider and contract.ConfigWatcher implementation to read and watch remote config key.
14+
// ETCD is a core.ConfProvider and contract.ConfigWatcher implementation to read and watch remote config key.
1215
// The remote client uses etcd.
13-
type Remote struct {
14-
key string
15-
clientConfig *clientv3.Config
16+
type ETCD struct {
17+
key string
18+
clientConfig clientv3.Config
1619
}
1720

18-
// Provider create a *Remote
19-
func Provider(key string, clientConfig *clientv3.Config) *Remote {
20-
return &Remote{
21-
key: key,
21+
// Provider create a *ETCD
22+
func Provider(clientConfig clientv3.Config, key string) *ETCD {
23+
return &ETCD{
24+
key: key,
2225
clientConfig: clientConfig,
2326
}
2427
}
2528

29+
// WithKey is a two-in-one coreOption. It uses the remote key on etcd as the
30+
// source of configuration, and watches the change of that key for hot reloading.
31+
func WithKey(cfg clientv3.Config, key string, codec contract.Codec) (core.CoreOption, core.CoreOption) {
32+
r := Provider(cfg, key)
33+
return core.WithConfigStack(r, config.CodecParser{Codec: codec}), core.WithConfigWatcher(r)
34+
}
35+
2636
// ReadBytes reads the contents of a key from etcd and returns the bytes.
27-
func (r *Remote) ReadBytes() ([]byte, error) {
28-
client, err := clientv3.New(*r.clientConfig)
37+
func (r *ETCD) ReadBytes() ([]byte, error) {
38+
client, err := clientv3.New(r.clientConfig)
2939
if err != nil {
3040
return nil, err
3141
}
@@ -43,16 +53,16 @@ func (r *Remote) ReadBytes() ([]byte, error) {
4353
}
4454

4555
// Read is not supported by the remote provider.
46-
func (r *Remote) Read() (map[string]interface{}, error) {
56+
func (r *ETCD) Read() (map[string]interface{}, error) {
4757
return nil, errors.New("remote provider does not support this method")
4858
}
4959

5060
// Watch watches the change to the remote key from etcd. If the key is edited or created, the reload function
5161
// will be called. note the reload function should not just load the changes made within this key, but rather
5262
// it should reload the whole config stack. For example, if the flag or env takes precedence over the config
5363
// key, they should remain to be so after the key changes.
54-
func (r *Remote) Watch(ctx context.Context, reload func() error) error {
55-
client, err := clientv3.New(*r.clientConfig)
64+
func (r *ETCD) Watch(ctx context.Context, reload func() error) error {
65+
client, err := clientv3.New(r.clientConfig)
5666
if err != nil {
5767
return err
5868
}
@@ -74,4 +84,3 @@ func (r *Remote) Watch(ctx context.Context, reload func() error) error {
7484
}
7585
}
7686
}
77-

config/remote/remote_test.go renamed to config/remote/etcd/etcd_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package remote
1+
package etcd
22

33
import (
44
"context"
@@ -19,12 +19,12 @@ func TestRemote(t *testing.T) {
1919
return
2020
}
2121
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
22-
cfg := &clientv3.Config{
22+
cfg := clientv3.Config{
2323
Endpoints: addrs,
2424
DialTimeout: 2 * time.Second,
2525
}
2626

27-
r := Provider("config.yaml", cfg)
27+
r := Provider(cfg, "config.yaml")
2828

2929
var testVal = "name: app"
3030
// PREPARE TEST DATA
@@ -67,16 +67,16 @@ func TestError(t *testing.T) {
6767
}
6868
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
6969
var (
70-
r *Remote
70+
r *ETCD
7171
err error
7272
)
7373

74-
cfg := &clientv3.Config{
74+
cfg := clientv3.Config{
7575
Endpoints: []string{},
7676
DialTimeout: 2 * time.Second,
7777
}
7878

79-
r = Provider("config.yaml", cfg)
79+
r = Provider(cfg, "config.yaml")
8080
err = put(r, "test")
8181
assert.Error(t, err)
8282

@@ -88,15 +88,15 @@ func TestError(t *testing.T) {
8888
})
8989
assert.Error(t, err)
9090

91-
cfg = &clientv3.Config{
91+
cfg = clientv3.Config{
9292
Endpoints: addrs,
9393
DialTimeout: 2 * time.Second,
9494
}
95-
r = Provider("config-test1", cfg)
95+
r = Provider(cfg, "config-test1")
9696
_, err = r.ReadBytes()
9797
assert.Error(t, err)
9898

99-
r = Provider("config-test2", cfg)
99+
r = Provider(cfg, "config-test2")
100100

101101
// Confirm that the two coroutines are finished
102102
g := sync.WaitGroup{}
@@ -126,14 +126,17 @@ func TestError(t *testing.T) {
126126
g.Wait()
127127
}
128128

129-
func put(r *Remote, val string) error {
130-
client, err := clientv3.New(*r.clientConfig)
129+
func put(r *ETCD, val string) error {
130+
client, err := clientv3.New(r.clientConfig)
131131
if err != nil {
132132
return err
133133
}
134134
defer client.Close()
135135

136-
_, err = client.Put(context.Background(), r.key, val)
136+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
137+
defer cancel()
138+
139+
_, err = client.Put(ctx, r.key, val)
137140
if err != nil {
138141
return err
139142
}

config/remote/integration_test.go renamed to config/remote/etcd/example_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
package remote_test
1+
package etcd_test
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/DoNewsCode/core"
6-
"github.com/stretchr/testify/assert"
7+
"github.com/DoNewsCode/core/codec/yaml"
8+
"github.com/DoNewsCode/core/config/remote/etcd"
79
clientv3 "go.etcd.io/etcd/client/v3"
810
"os"
911
"strings"
10-
"testing"
1112
"time"
1213
)
1314

14-
func Test_integration(t *testing.T) {
15+
func Example() {
1516
addr := os.Getenv("ETCD_ADDR")
1617
if addr == "" {
17-
t.Skip("set ETCD_ADDR for run remote test")
18+
fmt.Println("set ETCD_ADDR for run example")
1819
return
1920
}
2021
key := "core.yaml"
@@ -23,13 +24,14 @@ func Test_integration(t *testing.T) {
2324
Endpoints: envEtcdAddrs,
2425
DialTimeout: time.Second,
2526
}
26-
if err := put(cfg, key, "name: remote"); err != nil {
27-
t.Fatal(err)
28-
}
27+
_ = put(cfg, key, "name: etcd")
2928

30-
c := core.New(core.WithRemoteYamlFile(key, cfg))
29+
c := core.New(etcd.WithKey(cfg, key, yaml.Codec{}))
3130
c.ProvideEssentials()
32-
assert.Equal(t, "remote", c.String("name"))
31+
fmt.Println(c.String("name"))
32+
33+
// Output:
34+
// etcd
3335
}
3436

3537
func put(cfg clientv3.Config, key, val string) error {

0 commit comments

Comments
 (0)