andrey
10 months ago
7 changed files with 561 additions and 1 deletions
@ -0,0 +1,48 @@ |
|||
package etcd_test |
|||
|
|||
import ( |
|||
"context" |
|||
"os" |
|||
"time" |
|||
|
|||
client "go.etcd.io/etcd/client/v3" |
|||
) |
|||
|
|||
const ConfigJSON = `{"duration":1260000000000,"enabled":true}` |
|||
|
|||
func NewEtcd(ctx context.Context) (*client.Client, error) { |
|||
dsn, ok := os.LookupEnv("FDEVS_CONFIG_ETCD_HOST") |
|||
if !ok { |
|||
dsn = "127.0.0.1:2379" |
|||
} |
|||
|
|||
et, err := client.New(client.Config{ |
|||
Endpoints: []string{dsn}, |
|||
DialTimeout: time.Second, |
|||
}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
values := map[string]string{ |
|||
"fdevs/config/db_dsn": "pgsql://user@pass:127.0.0.1:5432", |
|||
"fdevs/config/duration": "12m", |
|||
"fdevs/config/port": "8080", |
|||
"fdevs/config/maintain": "true", |
|||
"fdevs/config/start_at": "2020-01-02T15:04:05Z", |
|||
"fdevs/config/percent": "0.064", |
|||
"fdevs/config/count": "2020", |
|||
"fdevs/config/int64": "2021", |
|||
"fdevs/config/uint64": "2022", |
|||
"fdevs/config/config": ConfigJSON, |
|||
} |
|||
|
|||
for name, val := range values { |
|||
_, err = et.KV.Put(ctx, name, val) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
} |
|||
|
|||
return et, nil |
|||
} |
@ -0,0 +1,32 @@ |
|||
module gitoa.ru/go-4devs/config/provider/etcd |
|||
|
|||
go 1.21 |
|||
|
|||
require ( |
|||
github.com/stretchr/testify v1.8.4 |
|||
gitoa.ru/go-4devs/config v0.0.2 |
|||
go.etcd.io/etcd/api/v3 v3.5.11 |
|||
go.etcd.io/etcd/client/v3 v3.5.11 |
|||
) |
|||
|
|||
require ( |
|||
github.com/coreos/go-semver v0.3.0 // indirect |
|||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect |
|||
github.com/davecgh/go-spew v1.1.1 // indirect |
|||
github.com/gogo/protobuf v1.3.2 // indirect |
|||
github.com/golang/protobuf v1.5.3 // indirect |
|||
github.com/pmezard/go-difflib v1.0.0 // indirect |
|||
go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect |
|||
go.uber.org/atomic v1.7.0 // indirect |
|||
go.uber.org/multierr v1.6.0 // indirect |
|||
go.uber.org/zap v1.17.0 // indirect |
|||
golang.org/x/net v0.17.0 // indirect |
|||
golang.org/x/sys v0.13.0 // indirect |
|||
golang.org/x/text v0.13.0 // indirect |
|||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect |
|||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect |
|||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect |
|||
google.golang.org/grpc v1.59.0 // indirect |
|||
google.golang.org/protobuf v1.31.0 // indirect |
|||
gopkg.in/yaml.v3 v3.0.1 // indirect |
|||
) |
@ -0,0 +1,97 @@ |
|||
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= |
|||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= |
|||
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI= |
|||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= |
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= |
|||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
|||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= |
|||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= |
|||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= |
|||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= |
|||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= |
|||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= |
|||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= |
|||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= |
|||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |
|||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= |
|||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= |
|||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= |
|||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
|||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= |
|||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
|||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
|||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
|||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= |
|||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= |
|||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= |
|||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= |
|||
gitoa.ru/go-4devs/config v0.0.1 h1:9KrOO09YbIMO8qL8aVn/G74DurGdOIW5y3O02bays4I= |
|||
gitoa.ru/go-4devs/config v0.0.1/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss= |
|||
gitoa.ru/go-4devs/config v0.0.2 h1:bkTxW57kDDMf4cj/8W7fxPSN7JCPWEqlhCmL6LP3Vzg= |
|||
gitoa.ru/go-4devs/config v0.0.2/go.mod h1:xfEC2Al9xnMLJUuekYs3KhJ5BIzWAseNwkMwbN6/xss= |
|||
go.etcd.io/etcd/api/v3 v3.5.11 h1:B54KwXbWDHyD3XYAwprxNzTe7vlhR69LuBgZnMVvS7E= |
|||
go.etcd.io/etcd/api/v3 v3.5.11/go.mod h1:Ot+o0SWSyT6uHhA56al1oCED0JImsRiU9Dc26+C2a+4= |
|||
go.etcd.io/etcd/client/pkg/v3 v3.5.11 h1:bT2xVspdiCj2910T0V+/KHcVKjkUrCZVtk8J2JF2z1A= |
|||
go.etcd.io/etcd/client/pkg/v3 v3.5.11/go.mod h1:seTzl2d9APP8R5Y2hFL3NVlD6qC/dOT+3kvrqPyTas4= |
|||
go.etcd.io/etcd/client/v3 v3.5.11 h1:ajWtgoNSZJ1gmS8k+icvPtqsqEav+iUorF7b0qozgUU= |
|||
go.etcd.io/etcd/client/v3 v3.5.11/go.mod h1:a6xQUEqFJ8vztO1agJh/KQKOMfFI8og52ZconzcDJwE= |
|||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= |
|||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= |
|||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= |
|||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= |
|||
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U= |
|||
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= |
|||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= |
|||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= |
|||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= |
|||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= |
|||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= |
|||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= |
|||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= |
|||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= |
|||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= |
|||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= |
|||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= |
|||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
|||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
|||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
|||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
|||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
|||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
|||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= |
|||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
|||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
|||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= |
|||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= |
|||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= |
|||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
|||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= |
|||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= |
|||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= |
|||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
|||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d h1:VBu5YqKPv6XiJ199exd8Br+Aetz+o08F+PLMnwJQHAY= |
|||
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4= |
|||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d h1:DoPTO70H+bcDXcd39vOqb2viZxgqeBeSGtZ55yZU4/Q= |
|||
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk= |
|||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= |
|||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= |
|||
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= |
|||
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= |
|||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= |
|||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= |
|||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= |
|||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
|||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
|||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= |
|||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
|||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
|||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
|||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
@ -0,0 +1,116 @@ |
|||
package etcd |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"strings" |
|||
|
|||
"gitoa.ru/go-4devs/config" |
|||
"gitoa.ru/go-4devs/config/value" |
|||
pb "go.etcd.io/etcd/api/v3/mvccpb" |
|||
client "go.etcd.io/etcd/client/v3" |
|||
) |
|||
|
|||
const ( |
|||
Name = "etcd" |
|||
Separator = "/" |
|||
) |
|||
|
|||
var ( |
|||
_ config.Provider = (*Provider)(nil) |
|||
_ config.WatchProvider = (*Provider)(nil) |
|||
) |
|||
|
|||
type Client interface { |
|||
client.KV |
|||
client.Watcher |
|||
} |
|||
|
|||
func New(namespace, appName string, client Client) *Provider { |
|||
prov := Provider{ |
|||
client: client, |
|||
key: func(s ...string) string { |
|||
return strings.Join(s, Separator) |
|||
}, |
|||
name: Name, |
|||
prefix: namespace + Separator + appName, |
|||
} |
|||
|
|||
return &prov |
|||
} |
|||
|
|||
type Provider struct { |
|||
client Client |
|||
key func(...string) string |
|||
name string |
|||
prefix string |
|||
log func(context.Context, string, ...any) |
|||
} |
|||
|
|||
func (p *Provider) Name() string { |
|||
return p.name |
|||
} |
|||
|
|||
func (p *Provider) Key(s []string) string { |
|||
return p.prefix + Separator + p.key(s...) |
|||
} |
|||
|
|||
func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, error) { |
|||
name := p.Key(path) |
|||
|
|||
resp, err := p.client.Get(ctx, name, client.WithPrefix()) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name()) |
|||
} |
|||
|
|||
val, err := p.resolve(name, resp.Kvs) |
|||
if err != nil { |
|||
return nil, fmt.Errorf("%w: key:%s, prov:%s", err, name, p.Name()) |
|||
} |
|||
|
|||
return val, nil |
|||
} |
|||
|
|||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, path ...string) error { |
|||
go func(ctx context.Context, key string, callback config.WatchCallback) { |
|||
watch := p.client.Watch(ctx, key, client.WithPrevKV(), client.WithPrefix()) |
|||
for w := range watch { |
|||
kvs, olds := p.getEventKvs(w.Events) |
|||
if len(kvs) > 0 { |
|||
newVar, _ := p.resolve(key, kvs) |
|||
oldVar, _ := p.resolve(key, olds) |
|||
|
|||
if err := callback(ctx, oldVar, newVar); err != nil { |
|||
p.log(ctx, "watch callback[%v] %v:%v", p.Name(), path, err) |
|||
} |
|||
} |
|||
} |
|||
}(ctx, p.Key(path), callback) |
|||
|
|||
return nil |
|||
} |
|||
|
|||
func (p *Provider) getEventKvs(events []*client.Event) ([]*pb.KeyValue, []*pb.KeyValue) { |
|||
kvs := make([]*pb.KeyValue, 0, len(events)) |
|||
old := make([]*pb.KeyValue, 0, len(events)) |
|||
|
|||
for i := range events { |
|||
kvs = append(kvs, events[i].Kv) |
|||
old = append(old, events[i].PrevKv) |
|||
} |
|||
|
|||
return kvs, old |
|||
} |
|||
|
|||
func (p *Provider) resolve(key string, kvs []*pb.KeyValue) (config.Value, error) { |
|||
for _, kv := range kvs { |
|||
switch { |
|||
case kv == nil: |
|||
return nil, nil |
|||
case string(kv.Key) == key: |
|||
return value.JBytes(kv.Value), nil |
|||
} |
|||
} |
|||
|
|||
return nil, fmt.Errorf("%w: name %s", config.ErrValueNotFound, key) |
|||
} |
@ -0,0 +1,121 @@ |
|||
package etcd_test |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"log" |
|||
"sync" |
|||
"time" |
|||
|
|||
"gitoa.ru/go-4devs/config" |
|||
"gitoa.ru/go-4devs/config/provider/etcd" |
|||
) |
|||
|
|||
func ExampleClient_Value() { |
|||
const ( |
|||
namespace = "fdevs" |
|||
appName = "config" |
|||
) |
|||
|
|||
ctx := context.Background() |
|||
|
|||
// configure etcd client
|
|||
etcdClient, err := NewEtcd(ctx) |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
config, err := config.New( |
|||
etcd.New(namespace, appName, etcdClient), |
|||
) |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
enabled, err := config.Value(ctx, "maintain") |
|||
if err != nil { |
|||
log.Print("maintain ", err) |
|||
|
|||
return |
|||
} |
|||
|
|||
fmt.Printf("maintain from etcd: %v\n", enabled.Bool()) |
|||
// Output:
|
|||
// maintain from etcd: true
|
|||
} |
|||
|
|||
func ExampleClient_Watch() { |
|||
const ( |
|||
namespace = "fdevs" |
|||
appName = "config" |
|||
) |
|||
|
|||
ctx, cancel := context.WithCancel(context.Background()) |
|||
defer cancel() |
|||
|
|||
// configure etcd client
|
|||
etcdClient, err := NewEtcd(ctx) |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
_, err = etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "pgsql://user@pass:127.0.0.1:5432") |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
defer func() { |
|||
cancel() |
|||
|
|||
if _, err = etcdClient.KV.Delete(context.Background(), "fdevs/config/example_db_dsn"); err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
}() |
|||
|
|||
watcher, err := config.New( |
|||
etcd.New(namespace, appName, etcdClient), |
|||
) |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
wg := sync.WaitGroup{} |
|||
wg.Add(1) |
|||
|
|||
err = watcher.Watch(ctx, func(_ context.Context, oldVar, newVar config.Value) error { |
|||
fmt.Println("update example_db_dsn old: ", oldVar.String(), " new:", newVar.String()) |
|||
wg.Done() |
|||
|
|||
return nil |
|||
}, "example_db_dsn") |
|||
if err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
|
|||
time.AfterFunc(time.Second, func() { |
|||
if _, err := etcdClient.KV.Put(ctx, "fdevs/config/example_db_dsn", "mysql://localhost:5432"); err != nil { |
|||
log.Print(err) |
|||
|
|||
return |
|||
} |
|||
}) |
|||
|
|||
wg.Wait() |
|||
|
|||
// Output:
|
|||
// update example_db_dsn old: pgsql://user@pass:127.0.0.1:5432 new: mysql://localhost:5432
|
|||
} |
@ -0,0 +1,118 @@ |
|||
package etcd_test |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"sync" |
|||
"sync/atomic" |
|||
"testing" |
|||
"time" |
|||
|
|||
"github.com/stretchr/testify/assert" |
|||
"gitoa.ru/go-4devs/config" |
|||
"gitoa.ru/go-4devs/config/provider/etcd" |
|||
"gitoa.ru/go-4devs/config/test" |
|||
"gitoa.ru/go-4devs/config/test/require" |
|||
) |
|||
|
|||
func TestProvider(t *testing.T) { |
|||
t.Parallel() |
|||
|
|||
ctx := context.Background() |
|||
|
|||
et, err := NewEtcd(ctx) |
|||
require.NoError(t, err) |
|||
|
|||
provider := etcd.New("fdevs", "config", et) |
|||
read := []test.Read{ |
|||
test.NewRead(test.DSN, "db_dsn"), |
|||
test.NewRead(12*time.Minute, "duration"), |
|||
test.NewRead(8080, "port"), |
|||
test.NewRead(true, "maintain"), |
|||
test.NewRead(test.Time("2020-01-02T15:04:05Z"), "start_at"), |
|||
test.NewRead(.064, "percent"), |
|||
test.NewRead(uint(2020), "count"), |
|||
test.NewRead(int64(2021), "int64"), |
|||
test.NewRead(int64(2022), "uint64"), |
|||
test.NewReadConfig("config"), |
|||
} |
|||
test.Run(t, provider, read) |
|||
} |
|||
|
|||
func value(cnt int32) string { |
|||
return fmt.Sprintf("test data: %d", cnt) |
|||
} |
|||
|
|||
func TestWatcher(t *testing.T) { |
|||
t.Parallel() |
|||
|
|||
ctx, cancel := context.WithCancel(context.Background()) |
|||
defer cancel() |
|||
|
|||
key := "test_watch" |
|||
|
|||
et, err := NewEtcd(ctx) |
|||
require.NoError(t, err) |
|||
|
|||
defer func() { |
|||
_, err = et.KV.Delete(context.Background(), "fdevs/config/test_watch") |
|||
require.NoError(t, err) |
|||
}() |
|||
|
|||
var cnt, cnt2 int32 |
|||
|
|||
prov := etcd.New("fdevs", "config", et) |
|||
wg := sync.WaitGroup{} |
|||
wg.Add(6) |
|||
|
|||
watch := func(cnt *int32) config.WatchCallback { |
|||
return func(ctx context.Context, oldVar, newVar config.Value) error { |
|||
switch *cnt { |
|||
case 0: |
|||
assert.Equal(t, value(*cnt), newVar.String()) |
|||
assert.Nil(t, oldVar) |
|||
case 1: |
|||
assert.Equal(t, value(*cnt), newVar.String()) |
|||
assert.Equal(t, value(*cnt-1), oldVar.String()) |
|||
case 2: |
|||
_, perr := newVar.ParseString() |
|||
require.NoError(t, perr) |
|||
assert.Equal(t, "", newVar.String()) |
|||
assert.Equal(t, value(*cnt-1), oldVar.String()) |
|||
default: |
|||
t.Error("unexpected watch") |
|||
t.Fail() |
|||
} |
|||
|
|||
wg.Done() |
|||
atomic.AddInt32(cnt, 1) |
|||
|
|||
return nil |
|||
} |
|||
} |
|||
|
|||
err = prov.Watch(ctx, watch(&cnt), key) |
|||
err = prov.Watch(ctx, watch(&cnt2), key) |
|||
require.NoError(t, err) |
|||
|
|||
time.AfterFunc(time.Second, func() { |
|||
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(0)) |
|||
require.NoError(t, err) |
|||
_, err = et.KV.Put(ctx, "fdevs/config/test_watch", value(1)) |
|||
require.NoError(t, err) |
|||
_, err = et.KV.Delete(ctx, "fdevs/config/test_watch") |
|||
require.NoError(t, err) |
|||
}) |
|||
|
|||
time.AfterFunc(time.Second*10, func() { |
|||
assert.Fail(t, "failed watch after 5 sec") |
|||
cancel() |
|||
}) |
|||
|
|||
go func() { |
|||
wg.Wait() |
|||
cancel() |
|||
}() |
|||
|
|||
<-ctx.Done() |
|||
} |
Loading…
Reference in new issue