You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.5 KiB

4 years ago
package watcher
import (
"context"
10 months ago
"errors"
4 years ago
"fmt"
10 months ago
"log/slog"
4 years ago
"time"
"gitoa.ru/go-4devs/config"
)
var (
_ config.Provider = (*Provider)(nil)
_ config.WatchProvider = (*Provider)(nil)
)
10 months ago
func New(duration time.Duration, provider config.Provider, opts ...Option) *Provider {
prov := &Provider{
10 months ago
Provider: provider,
duration: duration,
logger: slog.ErrorContext,
4 years ago
}
for _, opt := range opts {
opt(prov)
4 years ago
}
return prov
4 years ago
}
10 months ago
func WithLogger(l func(context.Context, string, ...any)) Option {
4 years ago
return func(p *Provider) {
p.logger = l
}
}
type Option func(*Provider)
type Provider struct {
10 months ago
config.Provider
duration time.Duration
logger func(context.Context, string, ...any)
4 years ago
}
func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key ...string) error {
10 months ago
old, err := p.Provider.Value(ctx, key...)
4 years ago
if err != nil {
return fmt.Errorf("failed watch variable: %w", err)
4 years ago
}
10 months ago
go func(oldVar config.Value) {
ticker := time.NewTicker(p.duration)
defer func() {
ticker.Stop()
}()
4 years ago
for {
select {
10 months ago
case <-ticker.C:
newVar, err := p.Provider.Value(ctx, key...)
4 years ago
if err != nil {
10 months ago
p.logger(ctx, "get value%v:%v", key, err.Error())
4 years ago
} else if !newVar.IsEquals(oldVar) {
10 months ago
if err := callback(ctx, oldVar, newVar); err != nil {
if errors.Is(err, config.ErrStopWatch) {
return
}
p.logger(ctx, "callback %v:%v", key, err)
}
4 years ago
oldVar = newVar
}
case <-ctx.Done():
return
}
}
10 months ago
}(old)
4 years ago
return nil
}