2022-08-04 14:37:51 -06:00
|
|
|
package locker
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
import (
|
|
|
|
"context"
|
2022-11-20 10:21:06 -07:00
|
|
|
"fmt"
|
2022-09-06 10:35:14 -06:00
|
|
|
|
2022-11-20 10:21:06 -07:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev/internal/lg"
|
2022-08-09 16:23:33 -06:00
|
|
|
)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
|
|
|
type Locked[T any] struct {
|
|
|
|
state chan *T
|
|
|
|
}
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
// New creates a new locker for the given value.
|
2022-08-04 14:37:51 -06:00
|
|
|
func New[T any](initial *T) *Locked[T] {
|
|
|
|
s := &Locked[T]{}
|
|
|
|
s.state = make(chan *T, 1)
|
|
|
|
s.state <- initial
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
// Modify will call the function with the locked value
|
2022-10-30 09:18:08 -06:00
|
|
|
func (s *Locked[T]) Modify(ctx context.Context, fn func(context.Context, *T) error) error {
|
2022-11-20 10:21:06 -07:00
|
|
|
if s == nil {
|
|
|
|
return fmt.Errorf("locker not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, span := lg.Span(ctx)
|
2022-09-06 10:35:14 -06:00
|
|
|
defer span.End()
|
|
|
|
|
2022-11-20 10:21:06 -07:00
|
|
|
var t T
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("typeOf", fmt.Sprintf("%T", t)),
|
|
|
|
)
|
|
|
|
|
2022-08-04 14:37:51 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case state := <-s.state:
|
|
|
|
defer func() { s.state <- state }()
|
2022-10-30 09:18:08 -06:00
|
|
|
return fn(ctx, state)
|
2022-08-04 14:37:51 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2022-08-06 09:52:36 -06:00
|
|
|
}
|
|
|
|
|
2022-08-07 11:55:49 -06:00
|
|
|
// Copy will return a shallow copy of the locked object.
|
2022-08-06 09:52:36 -06:00
|
|
|
func (s *Locked[T]) Copy(ctx context.Context) (T, error) {
|
|
|
|
var t T
|
|
|
|
|
2022-10-30 09:18:08 -06:00
|
|
|
err := s.Modify(ctx, func(ctx context.Context, c *T) error {
|
2022-08-06 09:52:36 -06:00
|
|
|
if c != nil {
|
|
|
|
t = *c
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return t, err
|
|
|
|
}
|