Skip to content

Commit

Permalink
separate locker from redigo interface (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd committed Nov 23, 2023
1 parent ac10eb3 commit a5125bd
Show file tree
Hide file tree
Showing 44 changed files with 239 additions and 232 deletions.
2 changes: 1 addition & 1 deletion conformance/client_sentinel_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/xh3b4sd/redigo"
"github.com/xh3b4sd/redigo/pkg/simple"
"github.com/xh3b4sd/redigo/simple"
)

func Test_Client_Sentinel_Connection(t *testing.T) {
Expand Down
95 changes: 63 additions & 32 deletions conformance/client_single_locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (

"github.com/xh3b4sd/breakr"
"github.com/xh3b4sd/redigo"
"github.com/xh3b4sd/redigo/pkg/locker"
"github.com/xh3b4sd/redigo/locker"
"github.com/xh3b4sd/redigo/pool"
"github.com/xh3b4sd/tracer"
)

Expand All @@ -28,13 +29,22 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
}

{
err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

var loc locker.Interface
{
loc = locker.New(locker.Config{
Poo: pool.NewSinglePoolWithAddress(cli.Listen()),
})
}

erc := make(chan error, 1)
don := make(chan struct{}, 1)

Expand All @@ -57,7 +67,7 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
go func() {
defer w.Done()

err = cli.Locker().Acquire()
err = loc.Acquire()
if err != nil {
erc <- tracer.Mask(err)
return
Expand All @@ -81,7 +91,7 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
}
}

err = cli.Locker().Release()
err = loc.Release()
if err != nil {
erc <- tracer.Mask(err)
return
Expand All @@ -94,7 +104,7 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
defer w.Done()

for {
err = cli.Locker().Acquire()
err = loc.Acquire()
if locker.IsAcquire(err) {
time.Sleep(50 * time.Millisecond)
continue
Expand Down Expand Up @@ -122,7 +132,7 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
}
}

err = cli.Locker().Release()
err = loc.Release()
if err != nil {
erc <- tracer.Mask(err)
return
Expand Down Expand Up @@ -173,41 +183,48 @@ func Test_Client_Single_Locker_Lifecycle(t *testing.T) {
func Test_Client_Single_Locker_Acquire_Budget(t *testing.T) {
var err error

var bre breakr.Interface
{
bre = breakr.New(breakr.Config{
Failure: breakr.Failure{
Budget: 3,
Cooler: 1 * time.Second,
},
})
}

var cli redigo.Interface
{
c := redigo.Config{
Kind: redigo.KindSingle,
Locker: redigo.ConfigLocker{
Breakr: bre,
Expiry: 1 * time.Second,
},
}

cli, err = redigo.New(c)
if err != nil {
t.Fatal(err)
}
}

{
err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

var brk breakr.Interface
{
brk = breakr.New(breakr.Config{
Failure: breakr.Failure{
Budget: 3,
Cooler: 1 * time.Second,
},
})
}

var loc locker.Interface
{
loc = locker.New(locker.Config{
Brk: brk,
Exp: 1 * time.Second,
Poo: pool.NewSinglePoolWithAddress(cli.Listen()),
})
}

don := make(chan struct{}, 1)

go func() {
err = cli.Locker().Acquire()
err = loc.Acquire()
if err != nil {
panic(err)
}
Expand All @@ -222,7 +239,7 @@ func Test_Client_Single_Locker_Acquire_Budget(t *testing.T) {
// The first Acquire call should still hold the lock on the first try, but
// 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()
err = loc.Acquire()
if err != nil {
t.Fatal(err)
}
Expand All @@ -235,34 +252,41 @@ func Test_Client_Single_Locker_Acquire_Error(t *testing.T) {
{
c := redigo.Config{
Kind: redigo.KindSingle,
Locker: redigo.ConfigLocker{
Breakr: breakr.Fake(),
Expiry: 1 * time.Second,
},
}

cli, err = redigo.New(c)
if err != nil {
t.Fatal(err)
}
}

{
err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

var loc locker.Interface
{
loc = locker.New(locker.Config{
Brk: breakr.Fake(),
Exp: 1 * time.Second,
Poo: pool.NewSinglePoolWithAddress(cli.Listen()),
})
}

// Aquiring the lock the first time.
{
err = cli.Locker().Acquire()
err = loc.Acquire()
if err != nil {
t.Fatal(err)
}
}

// The first Acquire call should still hold the lock.
{
err = cli.Locker().Acquire()
err = loc.Acquire()
if !locker.IsAcquire(err) {
t.Fatal("expected acquireError")
}
Expand All @@ -276,26 +300,33 @@ func Test_Client_Single_Locker_Acquire_Expiry(t *testing.T) {
{
c := redigo.Config{
Kind: redigo.KindSingle,
Locker: redigo.ConfigLocker{
Expiry: 1 * time.Second,
},
}

cli, err = redigo.New(c)
if err != nil {
t.Fatal(err)
}
}

{
err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

var loc locker.Interface
{
loc = locker.New(locker.Config{
Exp: 1 * time.Second,
Poo: pool.NewSinglePoolWithAddress(cli.Listen()),
})
}

don := make(chan struct{}, 1)

go func() {
err = cli.Locker().Acquire()
err = loc.Acquire()
if err != nil {
panic(err)
}
Expand All @@ -308,7 +339,7 @@ func Test_Client_Single_Locker_Acquire_Expiry(t *testing.T) {
<-don

// The first Acquire call should not hold the lock anymore due to expiry.
err = cli.Locker().Acquire()
err = loc.Acquire()
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion conformance/client_single_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/xh3b4sd/redigo"
"github.com/xh3b4sd/redigo/pkg/simple"
"github.com/xh3b4sd/redigo/simple"
)

func Test_Client_Single_Simple_Lifecycle(t *testing.T) {
Expand Down
16 changes: 0 additions & 16 deletions factory.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
package redigo

import (
"time"

"github.com/xh3b4sd/breakr"
"github.com/xh3b4sd/redigo/pkg/fake"
)

func Default() Interface {
var err error

var bre breakr.Interface
{
bre = breakr.New(breakr.Config{
Failure: breakr.Failure{
Budget: 30,
Cooler: 1 * time.Second,
},
})
}

var red Interface
{
c := Config{
Kind: KindSingle,
Locker: ConfigLocker{
Breakr: bre,
},
}

red, err = New(c)
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package redigo
import (
"github.com/gomodule/redigo/redis"
"github.com/xh3b4sd/redigo/pkg/backup"
"github.com/xh3b4sd/redigo/pkg/locker"
"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/redigo/simple"
)

type Interface interface {
Expand All @@ -17,10 +16,10 @@ type Interface interface {
Purge() error
Redis(fun func(con redis.Conn) error) error

// Listen returns the host:port configuration for this redigo instance.
Listen() string

Backup() backup.Interface
Locker() locker.Interface
PubSub() pubsub.Interface
Sorted() sorted.Interface
Simple() simple.Interface
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions locker/fake_acquire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package locker

func (f *Fake) Acquire() error {
if f.FakeAcquire != nil {
return f.FakeAcquire()
}

return nil
}
7 changes: 7 additions & 0 deletions locker/fake_locker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package locker

type Fake struct {
FakeAcquire func() error
FakeRefresh func() error
FakeRelease func() error
}
9 changes: 9 additions & 0 deletions locker/fake_refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package locker

func (f *Fake) Refresh() error {
if f.FakeRefresh != nil {
return f.FakeRefresh()
}

return nil
}
9 changes: 9 additions & 0 deletions locker/fake_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package locker

func (f *Fake) Release() error {
if f.FakeRelease != nil {
return f.FakeRelease()
}

return nil
}
File renamed without changes.
10 changes: 6 additions & 4 deletions pkg/locker/acquire.go → locker/redis_acquire.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/xh3b4sd/tracer"
)

func (l *Locker) Acquire() error {
func (l *Redis) Acquire() error {
act := func() error {
err := l.mut.Lock()
if err != nil {
Expand All @@ -14,9 +14,11 @@ func (l *Locker) Acquire() error {
return nil
}

err := l.brk.Execute(act)
if err != nil {
return tracer.Mask(err)
{
err := l.brk.Execute(act)
if err != nil {
return tracer.Mask(err)
}
}

return nil
Expand Down
Loading

0 comments on commit a5125bd

Please sign in to comment.