36 lines
685 B
Go
36 lines
685 B
Go
package queue
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var ErrClaimsStopped = errors.New("queue claims are stopped")
|
|
|
|
type Controller struct {
|
|
repository Repository
|
|
mutex sync.RWMutex
|
|
stopped bool
|
|
}
|
|
|
|
func NewController(repository Repository) *Controller {
|
|
return &Controller{repository: repository}
|
|
}
|
|
|
|
func (c *Controller) StopClaims() {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
c.stopped = true
|
|
}
|
|
|
|
func (c *Controller) ClaimNext(ctx context.Context, owner string, leaseDuration time.Duration) (*Claim, error) {
|
|
c.mutex.RLock()
|
|
defer c.mutex.RUnlock()
|
|
if c.stopped {
|
|
return nil, ErrClaimsStopped
|
|
}
|
|
return c.repository.ClaimNext(ctx, owner, leaseDuration)
|
|
}
|