-
Notifications
You must be signed in to change notification settings - Fork 0
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 #7 from ryo-yamaoka/add-max-rps
Add max RPS option
- Loading branch information
Showing
9 changed files
with
159 additions
and
43 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,43 @@ | ||
package sema | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/sync/semaphore" | ||
) | ||
|
||
// Sema implements semaphore with it can be unlimited by specifying 0 | ||
type Sema struct { | ||
sem *semaphore.Weighted | ||
} | ||
|
||
// NewWeighted creates a new weighted semaphore with the given maximum combined weight for concurrent access. | ||
// When you specify 0, it means unlimited concurrent access. | ||
func NewWeighted(n int64) *Sema { | ||
var sem *semaphore.Weighted | ||
if n != 0 { | ||
sem = semaphore.NewWeighted(n) | ||
} | ||
return &Sema{ | ||
sem: sem, | ||
} | ||
} | ||
|
||
// Acquire acquires the semaphore with a weight of n, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged. | ||
// If ctx is already done, Acquire may still succeed without blocking. | ||
// If you created semaphore with 0, this method is non blocking. | ||
func (s *Sema) Acquire(ctx context.Context, n int64) error { | ||
if s.sem == nil { | ||
return ctx.Err() | ||
} | ||
return s.sem.Acquire(ctx, n) | ||
} | ||
|
||
// Release releases the semaphore with a weight of n. | ||
// But if you created semaphore with 0, this method is no effect. | ||
func (s *Sema) Release(n int64) { | ||
if s.sem == nil { | ||
return | ||
} | ||
s.sem.Release(n) | ||
} |
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.