-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
harmony: Put max counter Limiter behind an interface
- Loading branch information
Showing
33 changed files
with
128 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package taskhelp | ||
|
||
import ( | ||
"sync/atomic" | ||
) | ||
|
||
type MaxCounter struct { | ||
// maximum number of tasks of this type that can be run | ||
N int | ||
|
||
// current number of tasks of this type that are running (shared) | ||
current *atomic.Int32 | ||
|
||
// current number of tasks of this type that are running (per task) | ||
currentThis *atomic.Int32 | ||
} | ||
|
||
func (m *MaxCounter) AtMax() bool { | ||
return m.Max() > 0 && m.Active() >= m.Max() | ||
} | ||
|
||
func (m *MaxCounter) Max() int { | ||
return m.N | ||
} | ||
|
||
// note: cur can't be called on counters for which max is 0 | ||
func (m *MaxCounter) Active() int { | ||
return int(m.current.Load()) | ||
} | ||
|
||
func (m *MaxCounter) ActiveThis() int { | ||
return int(m.currentThis.Load()) | ||
} | ||
|
||
func (m *MaxCounter) Add(n int) { | ||
m.current.Add(int32(n)) | ||
m.currentThis.Add(int32(n)) | ||
} | ||
|
||
func Max(n int) *MaxCounter { | ||
return &MaxCounter{N: n, current: new(atomic.Int32), currentThis: new(atomic.Int32)} | ||
} |
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
Oops, something went wrong.