def (#12)
All checks were successful
Go Action / goaction (push) Successful in 29s

Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
2025-12-26 14:55:42 +03:00
parent 22dacb741f
commit f9a0411192
129 changed files with 4660 additions and 1456 deletions

View File

@@ -39,18 +39,20 @@ type Option func(*Provider)
type Provider struct {
config.Provider
duration time.Duration
logger func(context.Context, string, ...any)
}
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
old, err := p.Provider.Value(ctx, key...)
old, err := p.Value(ctx, key...)
if err != nil {
return fmt.Errorf("failed watch variable: %w", err)
}
go func(oldVar config.Value) {
ticker := time.NewTicker(p.duration)
defer func() {
ticker.Stop()
}()
@@ -58,7 +60,7 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key
for {
select {
case <-ticker.C:
newVar, err := p.Provider.Value(ctx, key...)
newVar, err := p.Value(ctx, key...)
if err != nil {
p.logger(ctx, "get value%v:%v", key, err.Error())
} else if !newVar.IsEquals(oldVar) {
@@ -66,8 +68,10 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key
if errors.Is(err, config.ErrStopWatch) {
return
}
p.logger(ctx, "callback %v:%v", key, err)
}
oldVar = newVar
}
case <-ctx.Done():

View File

@@ -34,6 +34,7 @@ func TestWatcher(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer func() {
cancel()
}()
@@ -48,9 +49,10 @@ func TestWatcher(t *testing.T) {
err := w.Watch(
ctx,
func(ctx context.Context, oldVar, newVar config.Value) error {
func(_ context.Context, _, _ config.Value) error {
atomic.AddInt32(&cnt, 1)
wg.Done()
if atomic.LoadInt32(&cnt) == 2 {
return config.ErrStopWatch
}