semaphore

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2020 License: MIT Imports: 2 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-semaphore

Open In GitPod GitHub code size in bytes GitHub commit activity GitHub last commit

spinlock based semaphore in go this package is still experimental. use at your own risk.

this package implements a binary semphore, using spinlock and a counting semaphore using go channels. It is based on analyzing various go opensource libraries, listed in doc.go.

benchmarks

  • benchmark results (go version go1.15 linux/amd64)
Running tool: /home/gitpod/go/bin/go test -benchmem -run=^$ github.com/da-moon/go-semaphore -bench . -v
goos: linux
goarch: amd64
pkg: github.com/da-moon/go-semaphore
BenchmarkBinarySemaphore
BenchmarkBinarySemaphore/1
BenchmarkBinarySemaphore/1-16              	32325340	        37.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinarySemaphore/2
BenchmarkBinarySemaphore/2-16              	26361247	        39.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinarySemaphore/4
BenchmarkBinarySemaphore/4-16              	30186558	        37.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinarySemaphore/8
BenchmarkBinarySemaphore/8-16              	33298560	        38.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkBinarySemaphore/16
BenchmarkBinarySemaphore/16-16             	25791638	        41.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkMutex
BenchmarkMutex/1
BenchmarkMutex/1-16                        	15966688	        78.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkMutex/2
BenchmarkMutex/2-16                        	13372855	        83.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkMutex/4
BenchmarkMutex/4-16                        	19589612	        85.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkMutex/8
BenchmarkMutex/8-16                        	12234087	        96.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkMutex/16
BenchmarkMutex/16-16                       	11732821	        89.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkSlowSemaphorePermitpool200
BenchmarkSlowSemaphorePermitpool200-16     	     100	  10524237 ns/op	  886794 B/op	  110634 allocs/op
BenchmarkSemaphore200
BenchmarkSemaphore200-16                   	     272	   4087844 ns/op	    1184 B/op	       3 allocs/op
BenchmarkSlowSemaphorePermitpool500
BenchmarkSlowSemaphorePermitpool500-16     	      16	  75324527 ns/op	 5827348 B/op	  726940 allocs/op
BenchmarkSemaphore500
BenchmarkSemaphore500-16                   	      43	  25890357 ns/op	    1434 B/op	       3 allocs/op
BenchmarkSlowSemaphorePermitpool1000
BenchmarkSlowSemaphorePermitpool1000-16    	       3	 339437950 ns/op	24780800 B/op	 3090996 allocs/op
BenchmarkSemaphore1000
BenchmarkSemaphore1000-16                  	      10	 106566390 ns/op	    2720 B/op	       7 allocs/op
BenchmarkSlowSemaphorePermitpool3000
BenchmarkSlowSemaphorePermitpool3000-16    	       1	3106959920 ns/op	214735584 B/op	26684397 allocs/op
BenchmarkSemaphore3000
BenchmarkSemaphore3000-16                  	       2	 875797314 ns/op	    1184 B/op	       3 allocs/op

Documentation

Overview

Package semaphore implements binary and counting semaphore and can be used to limit maximum number of outstanding operation based on permitpool github.com/hashicorp/vault/sdk/physical and github.com/marusama/semaphore and github.com/hlts2/lock-free there is a `slowSemaphore` struct that I used to learn more about CAS,sync.Cond and compare performances Counting semaphore ( and by extension slowsemaphore ) is broken when changing go maxproc seems like a pretty shit package mutex seems more performant

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*countingSemaphore)

Option - options setter method

func WithPermits

func WithPermits(arg int) Option

WithPermits - sets number of permits in the permit pool

type Semaphore

type Semaphore interface {
	Signal()
	Wait()
}

Semaphore - main semaphore interface

func NewBinarySemaphore

func NewBinarySemaphore() Semaphore

NewBinarySemaphore returns a new binary semaphore

func NewCountingSemaphore

func NewCountingSemaphore(opts ...Option) Semaphore

New -

Jump to

Keyboard shortcuts

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