sem

package
v1.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Semaphore

type Semaphore struct {
	// contains filtered or unexported fields
}

Semaphore supports to change the max sema amount at runtime.

Semaphore employs golang.org/x/sync/semaphore.Weighted with a maxCapacity.
And tuning the realCapacity by an Acquire and Release in the background.
the realCapacity can not exceed the maxCapacity.

func NewSem

func NewSem(n uint32) *Semaphore

NewSem new a Semaphore

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

Acquire acquires the semaphore

func (*Semaphore) AcquireWithContext added in v1.1.0

func (s *Semaphore) AcquireWithContext(ctx context.Context) error

AcquireWithContext acquires the semaphore with context

func (*Semaphore) Release

func (s *Semaphore) Release()

Release releases one semaphore.

func (*Semaphore) SetMaxCount

func (s *Semaphore) SetMaxCount(n int64) (done chan struct{})

SetMaxCount set the size of 's' to 'n', this is an async operation and the caller can watch the returned 'done' channel like below if it wants to be notified at the completion:

done := s.SetMaxCount(100)
<-done

Note after receiving the notification, the caller should NOT assume the size of 's' is 'n' unless it knows there are no concurrent calls to 'SetMaxCount'.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL