-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move sequence incrementation logic into a common struct
- Loading branch information
1 parent
c499b4f
commit 1e541e8
Showing
13 changed files
with
148 additions
and
208 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
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,42 @@ | ||
package sequencer | ||
|
||
import "sync" | ||
|
||
// Sequence is an implementation of sequencer | ||
type Sequence struct { | ||
sync.Mutex | ||
|
||
current uint | ||
time uint | ||
max uint | ||
maxTime uint | ||
now func() uint | ||
} | ||
|
||
// Max returns the maximum possible sequence value for a given time | ||
func (s *Sequence) Max() uint { | ||
return s.max | ||
} | ||
|
||
// MaxTime returns the maximum possible time sequence value | ||
func (s *Sequence) MaxTime() uint { | ||
return s.maxTime | ||
} | ||
|
||
// Next returns the next sequence | ||
func (s *Sequence) Next() (uint, uint) { | ||
s.Lock() | ||
defer s.Unlock() | ||
|
||
s.increment() | ||
return s.time, s.current | ||
} | ||
|
||
func (s *Sequence) increment() { | ||
if s.time < s.now() { | ||
s.time = s.now() | ||
s.current = 0 | ||
} else { | ||
s.current++ | ||
} | ||
} |
Oops, something went wrong.