gosem

package module
v0.0.0-...-92d169e Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2017 License: MIT Imports: 9 Imported by: 0

README

gosem

CircleCI

gosem provides multiple semaphore functions. Currently supports inmemory, redis implementation.

Example

// You can set a number of available resources.
var sem = gosem.NewSemaphore(3)

func main() {
  for _, task := range tasks {
    // Do task if available resources exists.
    sem.Acquire()
    go func(task Task) {
      defer sem.Release()
      do(task)
    }(task)
  }
}

Features

Memory-based semaphore

Simple semaphore

Semaphore is a semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine.

sem := gosem.NewSemaphore(5)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()
Time semaphore

TimeSemaphore is a semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine with specified time.

sem := gosem.NewTimeSemaphore(5, time.Second)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()

Redis-based semaphore

Implements a semaphore using Redis.

It can be used between processes that are on the multiple host.

Counting semaphore
import "gopkg.in/redis.v3"

client := redis.NewClient(
  &redis.Options{Network: "tcp", Addr: "127.0.0.1:6379"},
)
sem := gosem.NewRedisSemaphore(client, 5)
// acquire a new resource
sem.Acquire()
// release the resource
sem.Release()

Contribution

  1. Fork (https://github.com/bluele/gosem/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

Author

Jun Kimura

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ExistsKey      = "EXISTS"
	GrabbedKey     = "GRABBED"
	AvailableKey   = "AVAILABLE"
	ReleaseLockKey = "RELEASE_LOCK"

	ResourceValue    = "1"
	DefaultNameSpace = "GOSEM::"
)
View Source
var (
	TimeoutError = errors.New("Timeout error")
)

Functions

This section is empty.

Types

type RedisSemaphore

type RedisSemaphore struct {
	Client             *redis.Client
	Permits            int
	Namespace          string
	UseLocalTime       bool
	StaleClientTimeout time.Duration
	// contains filtered or unexported fields
}

func NewRedisSemaphore

func NewRedisSemaphore(client *redis.Client, permits int) *RedisSemaphore

func (*RedisSemaphore) Acquire

func (sm *RedisSemaphore) Acquire() error

func (*RedisSemaphore) AcquireWithTimeout

func (sm *RedisSemaphore) AcquireWithTimeout(timeout time.Duration) error

func (*RedisSemaphore) Available

func (sm *RedisSemaphore) Available() (int, error)

func (*RedisSemaphore) AvailableKey

func (sm *RedisSemaphore) AvailableKey() string

func (*RedisSemaphore) CurrentTime

func (sm *RedisSemaphore) CurrentTime() (float64, error)

func (*RedisSemaphore) ExistsKey

func (sm *RedisSemaphore) ExistsKey() string

func (*RedisSemaphore) GrabbedKey

func (sm *RedisSemaphore) GrabbedKey() string

func (*RedisSemaphore) Release

func (sm *RedisSemaphore) Release() error

func (*RedisSemaphore) ReleaseLockKey

func (sm *RedisSemaphore) ReleaseLockKey() string

func (*RedisSemaphore) ReleaseStaleLocks

func (sm *RedisSemaphore) ReleaseStaleLocks(expires time.Duration) error

func (*RedisSemaphore) Reset

func (sm *RedisSemaphore) Reset() error

type Semaphore

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

Semaphore is a simple semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine.

func NewSemaphore

func NewSemaphore(permits int) *Semaphore

NewSemaphore returns a new Semaphore object

func (*Semaphore) Acquire

func (sm *Semaphore) Acquire() error

Acquire gets a new resource

func (*Semaphore) AcquireWithTimeout

func (sm *Semaphore) AcquireWithTimeout(timeout time.Duration) error

AcquireWithTimeout try to get a new resource, but this operation will be timeout after specified time.

func (*Semaphore) Available

func (sm *Semaphore) Available() int

Available gets the number of available resources

func (*Semaphore) Release

func (sm *Semaphore) Release() error

Release gives the resource

type TimeSemaphore

type TimeSemaphore struct {
	Permits int
	Logger  *log.Logger
	// contains filtered or unexported fields
}

TimeSemaphore is a semaphore that can be used to coordinate the number of accessing shared data from multiple goroutine with specified time.

func NewTimeSemaphore

func NewTimeSemaphore(permits int, per time.Duration) *TimeSemaphore

NewTimeSemaphore returns a new TimeSemaphore object

func (*TimeSemaphore) Acquire

func (sm *TimeSemaphore) Acquire() error

Acquire gets a new resource

func (*TimeSemaphore) AcquireWithTimeout

func (sm *TimeSemaphore) AcquireWithTimeout(timeout time.Duration) error

AcquireWithTimeout try to get a new resource, but this operation will be timeout after specified time.

func (*TimeSemaphore) Available

func (sm *TimeSemaphore) Available() int

Available gets the number of available resource

func (*TimeSemaphore) Destroy

func (sm *TimeSemaphore) Destroy() bool

Destroy stops observing resources of channels. After call this method, this semaphore object wouldn't be controllable.

func (*TimeSemaphore) Release

func (sm *TimeSemaphore) Release() error

Release gives the resource

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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