Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/1pkg/golatch
Golatch 🔒 seamlessly patches go runtime to provide a way to overwrite empty value returned from closed channel.
https://github.com/1pkg/golatch
chan channel close go golang
Last synced: about 9 hours ago
JSON representation
Golatch 🔒 seamlessly patches go runtime to provide a way to overwrite empty value returned from closed channel.
- Host: GitHub
- URL: https://github.com/1pkg/golatch
- Owner: 1pkg
- License: mit
- Created: 2020-11-19T17:02:46.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-21T21:58:20.000Z (7 months ago)
- Last Synced: 2024-04-22T01:54:44.653Z (7 months ago)
- Topics: chan, channel, close, go, golang
- Language: Go
- Homepage:
- Size: 165 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE
Awesome Lists containing this project
README
# Golatch 🔒
[![lint](https://github.com/1pkg/golatch/workflows/lint/badge.svg)](https://github.com/1pkg/golatch/actions?query=workflow%3Alint+branch%3Amaster+)
[![test](https://github.com/1pkg/golatch/workflows/test/badge.svg)](https://github.com/1pkg/golatch/actions?query=workflow%3Atest+branch%3Amaster+)
[![report](https://goreportcard.com/badge/github.com/1pkg/golatch?cache=false)](https://goreportcard.com/report/github.com/1pkg/golatch)
[![version](https://img.shields.io/github/go-mod/go-version/1pkg/golatch)](https://github.com/1pkg/golatch/blob/master/go.mod)
[![license](https://img.shields.io/github/license/1pkg/golatch?cache=false)](LICENSE)
[![godoc](https://img.shields.io/badge/godoc-godoc-green)](https://pkg.go.dev/github.com/1pkg/golatch?tab=doc)`go get -u github.com/1pkg/golatch`
[blog post article](https://1pkg.github.io/posts/lets_make_closed_channels_more_useful/)
## Introduction
Golatch seamlessly patches go runtime to provide a way to close a chan idempotently + overwrite empty value returned from that closed channel.
With Golatch
Without Golatch```go
package mainimport (
"fmt"
"sync""github.com/1pkg/golatch"
)func main() {
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go worker(i, ch, &wg)
}
golatch.Close(ch, 10)
wg.Wait()
}func worker(i int, ch chan int, wg *sync.WaitGroup) {
v, ok := <- ch // 10, false
if ok || v != 10 {
panic("unreachable") // won't panic
}
fmt.Printf("worker %d chan is closed with value %d\n", i, v)
wg.Done()
}
``````go
package mainimport (
"fmt"
"sync"
)func main() {
ch := make(chan int)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go worker(i, ch, &wg)
}
close(ch)
wg.Wait()
}func worker(i int, ch chan int, wg *sync.WaitGroup) {
v, ok := <- ch // 0, false
if ok || v != 10 {
panic("unreachable") // will panic
}
fmt.Printf("worker %d chan is closed with value %d\n", i, v)
wg.Done()
}
```## Internals
Golatch exposes single function `Close` that idempotently closes provided chan and stores provided value to return as this channel closed value instead of empty value. If provided channel is not a writable channel `NotWritableChannel` error is returned. If provided value doesn't match underlying channel type `ChannelTypeMismatch` error is returned.
To cancel the effect of closed value replace call cancel function. Note that provided value won't be automatically collected by GC together with provided channel to remove provided value from the storage call cancel function. Note that after cancelation is called next call to `Close` will cause a panic `close of closed channel`. Note that unless cancelation is called next call to `Close` is safe and won't cause any panic but just update storage value with new provided value. Note that in order to achieve such effect golatch uses package based on [bou.ke/monkey](https://github.com/bouk/monkey) to patch all existing channel receive entrypoints:- direct chan receive
- chan select statement
- reflect chan receiveThis makes golatch inherits the same list of restrictions as `bou.ke/monkey` [has](https://github.com/bouk/monkey#notes). Note that multiselect statement is patched via patch guard, which makes it not thread safe.
## Licence
Golatch is licensed under the MIT License.
See [LICENSE](LICENSE) for the full license text.