Skip to content

Commit

Permalink
update breaker (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd committed Nov 22, 2023
1 parent f92a44c commit ac10eb3
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 87 deletions.
26 changes: 9 additions & 17 deletions conformance/client_single_locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -176,27 +173,22 @@ 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
{
c := redigo.Config{
Kind: redigo.KindSingle,
Locker: redigo.ConfigLocker{
Budget: bre,
Breakr: bre,
Expiry: 1 * time.Second,
},
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
},
}
Expand Down
19 changes: 6 additions & 13 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,29 @@ 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
{
c := Config{
Kind: KindSingle,
Locker: ConfigLocker{
Budget: bre,
Breakr: bre,
},
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/locker/acquire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

func (l *Locker) Acquire() error {
act := func() error {
err := l.mutex.Lock()
err := l.mut.Lock()
if err != nil {
return tracer.Mask(err)
}

return nil
}

err := l.bud.Execute(act)
err := l.brk.Execute(act)
if err != nil {
return tracer.Mask(err)
}
Expand Down
53 changes: 27 additions & 26 deletions pkg/locker/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,60 @@ 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
Prefix string
}

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
}
10 changes: 5 additions & 5 deletions pkg/locker/refresh.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/locker/release.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 6 additions & 12 deletions redigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ 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"
"github.com/xh3b4sd/redigo/pkg/pubsub"
"github.com/xh3b4sd/redigo/pkg/simple"
"github.com/xh3b4sd/redigo/pkg/sorted"
"github.com/xh3b4sd/redigo/pkg/walker"
"github.com/xh3b4sd/tracer"
)

const (
Expand All @@ -33,7 +32,7 @@ type Config struct {
}

type ConfigLocker struct {
Budget budget.Interface
Breakr breakr.Interface
Expiry time.Duration
Name string
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac10eb3

Please sign in to comment.