This commit is contained in:
15
key.go
15
key.go
@@ -1,15 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import "context"
|
|
||||||
|
|
||||||
type Key struct {
|
|
||||||
Name string
|
|
||||||
AppName string
|
|
||||||
Namespace string
|
|
||||||
}
|
|
||||||
|
|
||||||
type KeyFactory func(ctx context.Context, key Key) string
|
|
||||||
|
|
||||||
func (k Key) String() string {
|
|
||||||
return k.Namespace + "_" + k.AppName + "_" + k.Name
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -124,7 +123,6 @@ func (p *Provider) Value(ctx context.Context, path ...string) (config.Value, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
return value.Decode(func(v interface{}) error {
|
return value.Decode(func(v interface{}) error {
|
||||||
log.Println(string(data))
|
|
||||||
return json.Unmarshal(data, v)
|
return json.Unmarshal(data, v)
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package watcher
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
@@ -14,13 +14,11 @@ var (
|
|||||||
_ config.WatchProvider = (*Provider)(nil)
|
_ config.WatchProvider = (*Provider)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(duration time.Duration, provider config.NamedProvider, opts ...Option) *Provider {
|
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
|
||||||
prov := &Provider{
|
prov := &Provider{
|
||||||
NamedProvider: provider,
|
Provider: provider,
|
||||||
ticker: time.NewTicker(duration),
|
duration: duration,
|
||||||
logger: func(_ context.Context, msg string) {
|
logger: slog.ErrorContext,
|
||||||
log.Print(msg)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -30,7 +28,7 @@ func New(duration time.Duration, provider config.NamedProvider, opts ...Option)
|
|||||||
return prov
|
return prov
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithLogger(l func(context.Context, string)) Option {
|
func WithLogger(l func(context.Context, string, ...any)) Option {
|
||||||
return func(p *Provider) {
|
return func(p *Provider) {
|
||||||
p.logger = l
|
p.logger = l
|
||||||
}
|
}
|
||||||
@@ -39,24 +37,29 @@ func WithLogger(l func(context.Context, string)) Option {
|
|||||||
type Option func(*Provider)
|
type Option func(*Provider)
|
||||||
|
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
config.NamedProvider
|
config.Provider
|
||||||
ticker *time.Ticker
|
duration time.Duration
|
||||||
logger func(context.Context, string)
|
logger func(context.Context, string, ...any)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
|
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
|
||||||
oldVar, err := p.NamedProvider.Value(ctx, key...)
|
old, err := p.Provider.Value(ctx, key...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed watch variable: %w", err)
|
return fmt.Errorf("failed watch variable: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func(oldVar config.Value) {
|
||||||
|
ticker := time.NewTicker(p.duration)
|
||||||
|
defer func() {
|
||||||
|
ticker.Stop()
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-p.ticker.C:
|
case <-ticker.C:
|
||||||
newVar, err := p.NamedProvider.Value(ctx, key...)
|
newVar, err := p.Provider.Value(ctx, key...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger(ctx, err.Error())
|
p.logger(ctx, "get value%v:%v", key, err.Error())
|
||||||
} else if !newVar.IsEquals(oldVar) {
|
} else if !newVar.IsEquals(oldVar) {
|
||||||
callback(ctx, oldVar, newVar)
|
callback(ctx, oldVar, newVar)
|
||||||
oldVar = newVar
|
oldVar = newVar
|
||||||
@@ -65,7 +68,7 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}(old)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
11
variable.go
11
variable.go
@@ -1,11 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
type Variable struct {
|
|
||||||
Name string
|
|
||||||
Provider string
|
|
||||||
Value Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v Variable) IsEquals(n Variable) bool {
|
|
||||||
return n.Name == v.Name && n.Provider == v.Provider && n.Value.String() == v.Value.String()
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user