Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove single-threadedness coming from rotation lock #6

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions pkg/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ type FairnessTracker struct {

tikr utils.ITicker

rotationLock *sync.Mutex
// Rotation lock to ensure that we don't rotate while updating the structures
// The act of updating is a "read" in this case since multiple updates can happen
// concurrently but none can happen while we are rotating so that's a write.
rotationLock *sync.RWMutex
stopRotation chan bool
}

Expand All @@ -50,7 +53,7 @@ func NewFairnessTrackerWithClockAndTicker(trackerConfig *config.FairnessTrackerC

tikr: tikr,

rotationLock: &sync.Mutex{},
rotationLock: &sync.RWMutex{},
stopRotation: stopRotation,
}

Expand Down Expand Up @@ -89,8 +92,8 @@ func NewFairnessTracker(trackerConfig *config.FairnessTrackerConfig) (*FairnessT

func (ft *FairnessTracker) RegisterRequest(ctx context.Context, clientIdentifier []byte) (*request.RegisterRequestResult, error) {
// We must take the rotation lock to avoid rotation while updating the structures
ft.rotationLock.Lock()
defer ft.rotationLock.Unlock()
ft.rotationLock.RLock()
defer ft.rotationLock.RUnlock()

resp, err := ft.mainStructure.RegisterRequest(ctx, clientIdentifier)
if err != nil {
Expand All @@ -108,8 +111,8 @@ func (ft *FairnessTracker) RegisterRequest(ctx context.Context, clientIdentifier

func (ft *FairnessTracker) ReportOutcome(ctx context.Context, clientIdentifier []byte, outcome request.Outcome) (*request.ReportOutcomeResult, error) {
// We must take the rotation lock to avoid rotation while updating the structures
ft.rotationLock.Lock()
defer ft.rotationLock.Unlock()
ft.rotationLock.RLock()
defer ft.rotationLock.RUnlock()

resp, err := ft.mainStructure.ReportOutcome(ctx, clientIdentifier, outcome)
if err != nil {
Expand Down
Loading