add weighted semaphore to golang.org/x/sync?

399 views
Skip to first unread message

Jonathan Amsterdam

unread,
Mar 16, 2017, 4:38:44 PM3/16/17
to golan...@googlegroups.com
Google has a nice implementation of a weighted semaphore that I'd like to put in golang.org/x/sync. Godoc is below.

Are folks OK with this? Does anyone want to review the CL?
---------------------------------------------
package semaphore

    Package semaphore provides semaphore implementations.

TYPES

type Weighted struct {
    // contains filtered or unexported fields
}
    Weighted provides a way to bound concurrent access to a resource. The
    callers can request access with a given weight.

func NewWeighted(n int64) *Weighted
    NewWeighted creates a new weighted semaphore with the given maximum
    combined weight for concurrent access.

func (s *Weighted) Acquire(ctx context.Context, n int64) error
    Acquire acquires the semaphore with a weight of n, blocking only until
    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.

func (s *Weighted) Release(n int64)
    Release releases the semaphore with a weight of n.

func (s *Weighted) TryAcquire(n int64) bool
    TryAcquire acquires the semaphore with a weight of n without blocking.
    On success, returns true. On failure, returns false and leaves the
    semaphore unchanged.



Tylor Arndt

unread,
Mar 24, 2017, 11:47:56 AM3/24/17
to golang-dev
Are weighted semaphores == counting semaphores?

It would be really nice to have a AccquireTimeout like this package has: https://github.com/tarndt/sema
Reply all
Reply to author
Forward
0 new messages