initial commit
This commit is contained in:
74
locker/locker.go
Normal file
74
locker/locker.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package locker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.sour.is/pkg/lg"
|
||||
)
|
||||
|
||||
type Locked[T any] struct {
|
||||
state chan *T
|
||||
}
|
||||
|
||||
// New creates a new locker for the given value.
|
||||
func New[T any](initial *T) *Locked[T] {
|
||||
s := &Locked[T]{}
|
||||
s.state = make(chan *T, 1)
|
||||
s.state <- initial
|
||||
return s
|
||||
}
|
||||
|
||||
type ctxKey struct{ name string }
|
||||
|
||||
// Use will call the function with the locked value
|
||||
func (s *Locked[T]) Use(ctx context.Context, fn func(context.Context, *T) error) error {
|
||||
if s == nil {
|
||||
return fmt.Errorf("locker not initialized")
|
||||
}
|
||||
|
||||
key := ctxKey{fmt.Sprintf("%p", s)}
|
||||
|
||||
if value := ctx.Value(key); value != nil {
|
||||
return fmt.Errorf("%w: %T", ErrNested, s)
|
||||
}
|
||||
ctx = context.WithValue(ctx, key, key)
|
||||
|
||||
ctx, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
var t T
|
||||
span.SetAttributes(
|
||||
attribute.String("typeOf", fmt.Sprintf("%T", t)),
|
||||
)
|
||||
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
select {
|
||||
case state := <-s.state:
|
||||
defer func() { s.state <- state }()
|
||||
return fn(ctx, state)
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// Copy will return a shallow copy of the locked object.
|
||||
func (s *Locked[T]) Copy(ctx context.Context) (T, error) {
|
||||
var t T
|
||||
|
||||
err := s.Use(ctx, func(ctx context.Context, c *T) error {
|
||||
if c != nil {
|
||||
t = *c
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return t, err
|
||||
}
|
||||
|
||||
var ErrNested = errors.New("nested locker call")
|
||||
96
locker/locker_test.go
Normal file
96
locker/locker_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package locker_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
|
||||
"go.sour.is/pkg/locker"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Value string
|
||||
Counter int
|
||||
}
|
||||
|
||||
func TestLocker(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
value := locker.New(&config{})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
c.Value = "one"
|
||||
c.Counter++
|
||||
return nil
|
||||
})
|
||||
is.NoErr(err)
|
||||
|
||||
c, err := value.Copy(context.Background())
|
||||
|
||||
is.NoErr(err)
|
||||
is.Equal(c.Value, "one")
|
||||
is.Equal(c.Counter, 1)
|
||||
|
||||
wait := make(chan struct{})
|
||||
|
||||
go value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
c.Value = "two"
|
||||
c.Counter++
|
||||
close(wait)
|
||||
return nil
|
||||
})
|
||||
|
||||
<-wait
|
||||
cancel()
|
||||
|
||||
err = value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
c.Value = "three"
|
||||
c.Counter++
|
||||
return nil
|
||||
})
|
||||
is.True(err != nil)
|
||||
|
||||
c, err = value.Copy(context.Background())
|
||||
|
||||
is.NoErr(err)
|
||||
is.Equal(c.Value, "two")
|
||||
is.Equal(c.Counter, 2)
|
||||
}
|
||||
|
||||
func TestNestedLocker(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
value := locker.New(&config{})
|
||||
other := locker.New(&config{})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
err := value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
return value.Use(ctx, func(ctx context.Context, t *config) error {
|
||||
return nil
|
||||
})
|
||||
})
|
||||
is.True(errors.Is(err, locker.ErrNested))
|
||||
|
||||
err = value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
return other.Use(ctx, func(ctx context.Context, t *config) error {
|
||||
return nil
|
||||
})
|
||||
})
|
||||
is.NoErr(err)
|
||||
|
||||
err = value.Use(ctx, func(ctx context.Context, c *config) error {
|
||||
return other.Use(ctx, func(ctx context.Context, t *config) error {
|
||||
return value.Use(ctx, func(ctx context.Context, x *config) error {
|
||||
return nil
|
||||
})
|
||||
})
|
||||
})
|
||||
is.True(errors.Is(err, locker.ErrNested))
|
||||
}
|
||||
Reference in New Issue
Block a user