diff --git a/conformance/client_single_locker_test.go b/conformance/client_single_locker_test.go index 0daf013..c822f30 100644 --- a/conformance/client_single_locker_test.go +++ b/conformance/client_single_locker_test.go @@ -9,13 +9,10 @@ import ( "testing" "time" - "github.com/xh3b4sd/budget/v3" - "github.com/xh3b4sd/budget/v3/pkg/breaker" - "github.com/xh3b4sd/budget/v3/pkg/single" - "github.com/xh3b4sd/tracer" - + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/redigo" "github.com/xh3b4sd/redigo/pkg/locker" + "github.com/xh3b4sd/tracer" ) func Test_Client_Single_Locker_Lifecycle(t *testing.T) { @@ -176,19 +173,14 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) { func Test_Client_Single_Locker_Acquire_Budget(t *testing.T) { var err error - var bre budget.Interface + var bre breakr.Interface { - c := breaker.Config{ - Failure: breaker.Failure{ + bre = breakr.New(breakr.Config{ + Failure: breakr.Failure{ Budget: 3, Cooler: 1 * time.Second, }, - } - - bre, err = breaker.New(c) - if err != nil { - panic(err) - } + }) } var cli redigo.Interface @@ -196,7 +188,7 @@ func Test_Client_Single_Locker_Acquire_Budget(t *testing.T) { c := redigo.Config{ Kind: redigo.KindSingle, Locker: redigo.ConfigLocker{ - Budget: bre, + Breakr: bre, Expiry: 1 * time.Second, }, } @@ -228,7 +220,7 @@ func Test_Client_Single_Locker_Acquire_Budget(t *testing.T) { <-don // The first Acquire call should still hold the lock on the first try, but - // the locker is configured with a budget implementation that retries until + // the locker is configured with a breakr implementation that retries until // the lock expires and then can be acquired a second time here. err = cli.Locker().Acquire() if err != nil { @@ -244,7 +236,7 @@ func Test_Client_Single_Locker_Acquire_Error(t *testing.T) { c := redigo.Config{ Kind: redigo.KindSingle, Locker: redigo.ConfigLocker{ - Budget: single.New(), + Breakr: breakr.Fake(), Expiry: 1 * time.Second, }, } diff --git a/factory.go b/factory.go index 6b2f85e..4f4bfce 100644 --- a/factory.go +++ b/factory.go @@ -3,28 +3,21 @@ package redigo import ( "time" - "github.com/xh3b4sd/budget/v3" - "github.com/xh3b4sd/budget/v3/pkg/breaker" - + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/redigo/pkg/fake" ) func Default() Interface { var err error - var bre budget.Interface + var bre breakr.Interface { - c := breaker.Config{ - Failure: breaker.Failure{ + bre = breakr.New(breakr.Config{ + Failure: breakr.Failure{ Budget: 30, Cooler: 1 * time.Second, }, - } - - bre, err = breaker.New(c) - if err != nil { - panic(err) - } + }) } var red Interface @@ -32,7 +25,7 @@ func Default() Interface { c := Config{ Kind: KindSingle, Locker: ConfigLocker{ - Budget: bre, + Breakr: bre, }, } diff --git a/go.mod b/go.mod index 49912bb..290ac8b 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ require ( github.com/go-redsync/redsync/v4 v4.9.0 github.com/gomodule/redigo v1.8.9 github.com/rafaeljusto/redigomock v2.4.0+incompatible - github.com/xh3b4sd/budget/v3 v3.3.0 - github.com/xh3b4sd/tracer v0.10.1 + github.com/xh3b4sd/breakr v0.1.0 + github.com/xh3b4sd/tracer v0.11.1 ) require ( diff --git a/go.sum b/go.sum index a12d4a0..737c053 100644 --- a/go.sum +++ b/go.sum @@ -47,8 +47,9 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= @@ -109,10 +110,10 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203 h1:QVqDTf3h2WHt08YuiTGPZLls0Wq99X9bWd0Q5ZSBesM= github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203/go.mod h1:oqN97ltKNihBbwlX8dLpwxCl3+HnXKV/R0e+sRLd9C8= -github.com/xh3b4sd/budget/v3 v3.3.0 h1:/mg11kyDuYHI4LOT8hctLsVnK6ZeGN+/Rw5wzZ/Zhlk= -github.com/xh3b4sd/budget/v3 v3.3.0/go.mod h1:epHdjvXVpg5PE63RcBirhgY8D/vVa5dbXkDjQ/ycT40= -github.com/xh3b4sd/tracer v0.10.1 h1:wuzVeQtqYsy51Nkb7P/XavZ8rQYptxxmlM1ivFJ8dXE= -github.com/xh3b4sd/tracer v0.10.1/go.mod h1:8nRbNXXhSSDmPfyoRRYgwQt/RpzAUH+zNsagP9InH8M= +github.com/xh3b4sd/breakr v0.1.0 h1:ZdQPaocCc2ac4ipEdc3eaq1M0zeg54s1kC7QZQn3TLI= +github.com/xh3b4sd/breakr v0.1.0/go.mod h1:dhU5gRl07UxonMdO6KzoGKLN545cWXPd3viKzw2bPPY= +github.com/xh3b4sd/tracer v0.11.1 h1:66G8yNkUkyuTRQ586cQMKrBxrD4mQej8mpR9PYoIiGg= +github.com/xh3b4sd/tracer v0.11.1/go.mod h1:vrAkiLN6hl3VdUeLo71mvqCgUw2TE0YyvzrORa/vHXs= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= diff --git a/pkg/locker/acquire.go b/pkg/locker/acquire.go index 43eff9e..2bba93e 100644 --- a/pkg/locker/acquire.go +++ b/pkg/locker/acquire.go @@ -6,7 +6,7 @@ import ( func (l *Locker) Acquire() error { act := func() error { - err := l.mutex.Lock() + err := l.mut.Lock() if err != nil { return tracer.Mask(err) } @@ -14,7 +14,7 @@ func (l *Locker) Acquire() error { return nil } - err := l.bud.Execute(act) + err := l.brk.Execute(act) if err != nil { return tracer.Mask(err) } diff --git a/pkg/locker/locker.go b/pkg/locker/locker.go index d16350f..aa98c45 100644 --- a/pkg/locker/locker.go +++ b/pkg/locker/locker.go @@ -7,14 +7,12 @@ import ( "github.com/go-redsync/redsync/v4" "github.com/go-redsync/redsync/v4/redis/redigo" "github.com/gomodule/redigo/redis" - "github.com/xh3b4sd/budget/v3" - "github.com/xh3b4sd/budget/v3/pkg/breaker" - + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/redigo/pkg/prefix" ) type Config struct { - Budget budget.Interface + Breakr breakr.Interface Expiry time.Duration Name string Pool *redis.Pool @@ -22,44 +20,47 @@ type Config struct { } type Locker struct { - bud budget.Interface - mutex *redsync.Mutex + brk breakr.Interface + mut *redsync.Mutex } -func New(config Config) (*Locker, error) { - if config.Budget == nil { - config.Budget = breaker.Default() +func New(c Config) *Locker { + if c.Breakr == nil { + c.Breakr = breakr.Default() } - if config.Expiry == 0 { - config.Expiry = 30 * time.Second + if c.Expiry == 0 { + c.Expiry = 30 * time.Second } - if config.Name == "" { - config.Name = "def" + if c.Name == "" { + c.Name = "def" } var r *redsync.Redsync { p := redigo.NewPool( &redis.Pool{ - MaxIdle: config.Pool.MaxIdle, - IdleTimeout: config.Pool.IdleTimeout, - Dial: config.Pool.Dial, - TestOnBorrow: config.Pool.TestOnBorrow, + MaxIdle: c.Pool.MaxIdle, + IdleTimeout: c.Pool.IdleTimeout, + Dial: c.Pool.Dial, + TestOnBorrow: c.Pool.TestOnBorrow, }, ) r = redsync.New(p) } - l := &Locker{ - bud: config.Budget, - mutex: r.NewMutex( - prefix.WithKeys(config.Prefix, fmt.Sprintf("red:%s", config.Name)), - redsync.WithExpiry(config.Expiry), - redsync.WithRetryDelayFunc(func(tries int) time.Duration { return 100 * time.Millisecond }), - redsync.WithTries(1), - ), + var l *Locker + { + l = &Locker{ + brk: c.Breakr, + mut: r.NewMutex( + prefix.WithKeys(c.Prefix, fmt.Sprintf("red:%s", c.Name)), + redsync.WithExpiry(c.Expiry), + redsync.WithRetryDelayFunc(func(tries int) time.Duration { return 100 * time.Millisecond }), + redsync.WithTries(1), + ), + } } - return l, nil + return l } diff --git a/pkg/locker/refresh.go b/pkg/locker/refresh.go index a77e9de..68b4a24 100644 --- a/pkg/locker/refresh.go +++ b/pkg/locker/refresh.go @@ -1,26 +1,26 @@ package locker import ( - "github.com/xh3b4sd/budget/v3" + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/tracer" ) func (l *Locker) Refresh() error { act := func() error { - sta, err := l.mutex.Extend() + sta, err := l.mut.Extend() if err != nil { return tracer.Mask(err) } if !sta { - return tracer.Mask(budget.Cancel) + return tracer.Mask(breakr.Cancel) } return nil } - err := l.bud.Execute(act) - if budget.IsCancel(err) { + err := l.brk.Execute(act) + if breakr.IsCancel(err) { return tracer.Mask(statusError) } else if err != nil { return tracer.Mask(err) diff --git a/pkg/locker/release.go b/pkg/locker/release.go index dca793b..7818ca0 100644 --- a/pkg/locker/release.go +++ b/pkg/locker/release.go @@ -1,26 +1,26 @@ package locker import ( - "github.com/xh3b4sd/budget/v3" + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/tracer" ) func (l *Locker) Release() error { act := func() error { - sta, err := l.mutex.Unlock() + sta, err := l.mut.Unlock() if err != nil { return tracer.Mask(err) } if !sta { - return tracer.Mask(budget.Cancel) + return tracer.Mask(breakr.Cancel) } return nil } - err := l.bud.Execute(act) - if budget.IsCancel(err) { + err := l.brk.Execute(act) + if breakr.IsCancel(err) { return tracer.Mask(statusError) } else if err != nil { return tracer.Mask(err) diff --git a/redigo.go b/redigo.go index d6e0717..0a9237c 100644 --- a/redigo.go +++ b/redigo.go @@ -6,9 +6,7 @@ import ( "time" "github.com/gomodule/redigo/redis" - "github.com/xh3b4sd/budget/v3" - "github.com/xh3b4sd/tracer" - + "github.com/xh3b4sd/breakr" "github.com/xh3b4sd/redigo/pkg/backup" "github.com/xh3b4sd/redigo/pkg/locker" "github.com/xh3b4sd/redigo/pkg/pool" @@ -16,6 +14,7 @@ import ( "github.com/xh3b4sd/redigo/pkg/simple" "github.com/xh3b4sd/redigo/pkg/sorted" "github.com/xh3b4sd/redigo/pkg/walker" + "github.com/xh3b4sd/tracer" ) const ( @@ -33,7 +32,7 @@ type Config struct { } type ConfigLocker struct { - Budget budget.Interface + Breakr breakr.Interface Expiry time.Duration Name string } @@ -84,18 +83,13 @@ func New(con Config) (*Redigo, error) { var loc locker.Interface { - c := locker.Config{ - Budget: con.Locker.Budget, + loc = locker.New(locker.Config{ + Breakr: con.Locker.Breakr, Expiry: con.Locker.Expiry, Name: con.Locker.Name, Pool: con.Pool, Prefix: con.Prefix, - } - - loc, err = locker.New(c) - if err != nil { - return nil, tracer.Mask(err) - } + }) } var pub pubsub.Interface