-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Snawoot/stale_modes
Stale modes
- Loading branch information
Showing
8 changed files
with
130 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package util | ||
|
||
import ( | ||
"errors" | ||
"sync/atomic" | ||
) | ||
|
||
type StaleMode int | ||
|
||
const ( | ||
BothStale StaleMode = iota | ||
EitherStale | ||
LeftStale | ||
RightStale | ||
) | ||
|
||
func (m *StaleMode) String() string { | ||
if m == nil { | ||
return "<nil>" | ||
} | ||
switch *m { | ||
case BothStale: | ||
return "both" | ||
case EitherStale: | ||
return "either" | ||
case LeftStale: | ||
return "left" | ||
case RightStale: | ||
return "right" | ||
} | ||
return "<unknown>" | ||
} | ||
|
||
func (m *StaleMode) Set(val string) error { | ||
switch val { | ||
case "both": | ||
*m = BothStale | ||
case "either": | ||
*m = EitherStale | ||
case "left": | ||
*m = LeftStale | ||
case "right": | ||
*m = RightStale | ||
default: | ||
return errors.New("unknown stale mode") | ||
} | ||
return nil | ||
} | ||
|
||
type tracker struct { | ||
leftCounter atomic.Int32 | ||
rightCounter atomic.Int32 | ||
leftTimedOutAt atomic.Int32 | ||
rightTimedOutAt atomic.Int32 | ||
staleFunc func() bool | ||
} | ||
|
||
func newTracker(staleMode StaleMode) *tracker { | ||
t := &tracker{} | ||
switch staleMode { | ||
case BothStale: | ||
t.staleFunc = t.bothStale | ||
case EitherStale: | ||
t.staleFunc = t.eitherStale | ||
case LeftStale: | ||
t.staleFunc = t.leftStale | ||
case RightStale: | ||
t.staleFunc = t.rightStale | ||
default: | ||
panic("unsupported stale mode") | ||
} | ||
return t | ||
} | ||
|
||
func (t *tracker) notify(isLeft bool) { | ||
if isLeft { | ||
t.leftCounter.Add(1) | ||
} else { | ||
t.rightCounter.Add(1) | ||
} | ||
} | ||
|
||
func (t *tracker) handleTimeout(isLeft bool) bool { | ||
if isLeft { | ||
t.leftTimedOutAt.Store(t.leftCounter.Load()) | ||
} else { | ||
t.rightTimedOutAt.Store(t.rightCounter.Load()) | ||
} | ||
return !t.staleFunc() | ||
} | ||
|
||
func (t *tracker) leftStale() bool { | ||
return t.leftCounter.Load() == t.leftTimedOutAt.Load() | ||
} | ||
|
||
func (t *tracker) rightStale() bool { | ||
return t.rightCounter.Load() == t.rightTimedOutAt.Load() | ||
} | ||
|
||
func (t *tracker) bothStale() bool { | ||
return t.leftStale() && t.rightStale() | ||
} | ||
|
||
func (t *tracker) eitherStale() bool { | ||
return t.leftStale() || t.rightStale() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters