Skip to content

Go (Golang) library provides map of semaphores to limit concurrency against some string keys.

License

Notifications You must be signed in to change notification settings

temoto/limitmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What

Limitmap is Go library providing map of semaphores to limit concurrency against some string keys.

  • Blocking Acquire/Release interface.
  • Separate limit value for each key.
  • Code errors such as Release before Acquire result in panic().
  • Semaphore is implemented using sync.Cond.
  • Map access is guarded by shared sync.Mutex.

Example

Example use cases:

  • Limit simultaneous connections to services.
  • Limit IO per storage device.
limits := limitmap.NewLimitMap()
process := func(url *url.URL) {
	// At most 2 concurrent requests to each host.
	limits.Acquire(url.Host, 2)
	defer limits.Release(url.Host)
	defer limits.Release("all")
	r, err := http.Get(url.String())
	// do something with response/error
}
for url := range urlChan {
	// Limit max goroutines only as safety net againist exhausting computing resources, this number may seem big.
	limits.Acquire("all", 200000)
	go process(url)
}
limits.Wait() // until all tasks finished

Install

go get -u github.com/temoto/limitmap

Flair

Build status Coverage Go Report Card License: CC0

About

Go (Golang) library provides map of semaphores to limit concurrency against some string keys.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages