Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/poy/lightsoff
The last one to leave turns the lights off.
https://github.com/poy/lightsoff
Last synced: 26 days ago
JSON representation
The last one to leave turns the lights off.
- Host: GitHub
- URL: https://github.com/poy/lightsoff
- Owner: poy
- License: mit
- Created: 2015-10-20T12:00:17.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-20T21:14:50.000Z (about 6 years ago)
- Last Synced: 2024-06-21T15:39:31.932Z (5 months ago)
- Language: Go
- Size: 2.93 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lights Off
The last one to leave turns the lights off.## Why?
`LightsOff` is built for closing channels when you have a "fan in" architecture. If you have several go routines writing to a channel, then when do you close that channel? Ideally, you don't close it on a go routine that is reading because the writing go routines will panic.
The built-in methods `recover()` and `sync.WaitGroup` both have issues.
`recover()` implies that you close early and the writing go routines get ended early. You don't let the panic end your process because your `recover` it, but you also don't let them finish. This tends to be a hack and doesn't let the data complete its flow.
Using `sync.WaitGroup` means you have to burn a go routine. The idea is that you would do the following:```
var wg sync.WaitGroup
go func(){
wg.Wait()
close(someChannel)
}{}
```While this works fine, it does allocate a go routine. If you use the pattern quite a bit, you end up leaking go routines. This adds up in the go runtime.
Using `LightsOff` costs quite a bit less.