74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
package pool
|
|
|
|
import (
|
|
"time"
|
|
|
|
ownershipDomain "github.com/proxy-pool/proxy-pool/internal/domain/ownership"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidOwnership = ownershipDomain.ErrInvalidOwnership
|
|
ErrOwnershipUnavailable = ownershipDomain.ErrOwnershipUnavailable
|
|
ErrAlreadyOwned = ownershipDomain.ErrAlreadyOwned
|
|
ErrStaleAssignment = ownershipDomain.ErrStaleAssignment
|
|
ErrNotDraining = ownershipDomain.ErrNotDraining
|
|
ErrDrainNotReady = ownershipDomain.ErrDrainNotReady
|
|
)
|
|
|
|
type Assignment = ownershipDomain.Assignment
|
|
|
|
type OwnershipManager struct {
|
|
repository ownershipDomain.Repository
|
|
}
|
|
|
|
// NewOwnershipManager requires the same authoritative repository used by
|
|
// extraction, so Worker assignment and AVAILABLE -> EXTRACTED cannot race.
|
|
func NewOwnershipManager(repository ownershipDomain.Repository) (*OwnershipManager, error) {
|
|
if repository == nil {
|
|
return nil, ErrInvalidOwnership
|
|
}
|
|
return &OwnershipManager{repository: repository}, nil
|
|
}
|
|
|
|
func (m *OwnershipManager) Assign(now time.Time, proxyID, workerID string, ttl time.Duration) (Assignment, error) {
|
|
if m == nil || m.repository == nil {
|
|
return Assignment{}, ErrInvalidOwnership
|
|
}
|
|
return m.repository.Assign(now, proxyID, workerID, ttl)
|
|
}
|
|
|
|
func (m *OwnershipManager) Renew(now time.Time, proxyID, workerID string, epoch uint64, ttl time.Duration) (Assignment, error) {
|
|
if m == nil || m.repository == nil {
|
|
return Assignment{}, ErrInvalidOwnership
|
|
}
|
|
return m.repository.Renew(now, proxyID, workerID, epoch, ttl)
|
|
}
|
|
|
|
func (m *OwnershipManager) BeginDrain(proxyID, workerID string, epoch uint64) (Assignment, error) {
|
|
if m == nil || m.repository == nil {
|
|
return Assignment{}, ErrInvalidOwnership
|
|
}
|
|
return m.repository.BeginDrain(proxyID, workerID, epoch)
|
|
}
|
|
|
|
func (m *OwnershipManager) AcknowledgeDrain(proxyID, workerID string, epoch uint64, active, reserved int64) error {
|
|
if m == nil || m.repository == nil {
|
|
return ErrInvalidOwnership
|
|
}
|
|
return m.repository.AcknowledgeDrain(proxyID, workerID, epoch, active, reserved)
|
|
}
|
|
|
|
func (m *OwnershipManager) Get(proxyID string) (Assignment, bool) {
|
|
if m == nil || m.repository == nil {
|
|
return Assignment{}, false
|
|
}
|
|
return m.repository.Get(proxyID)
|
|
}
|
|
|
|
func (m *OwnershipManager) Expire(now time.Time) []Assignment {
|
|
if m == nil || m.repository == nil {
|
|
return nil
|
|
}
|
|
return m.repository.Expire(now)
|
|
}
|