golatch

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 6 Imported by: 0

README

Golatch 🔒

lint test report version license godoc

go get -u github.com/1pkg/golatch

blog post article

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
package main

import (
	"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()
}
package main

import (
	"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 to patch all existing channel receive entrypoints:

  • direct chan receive
  • chan select statement
  • reflect chan receive

This makes golatch inherits the same list of restrictions as bou.ke/monkey has. 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 for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cancel

type Cancel func()

Cancel defines Close cancelation function type.

func Close

func Close(ch interface{}, val interface{}) (Cancel, error)

Close 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` to patch all existing channel receive entrypoints, so golatch inherits the same list of restrictions.

type ChannelTypeMismatch

type ChannelTypeMismatch struct {
	ValKind, ChKind reflect.Kind
}

ChannelTypeMismatch defines type mismatch between underlying channel and value kind error type.

func (ChannelTypeMismatch) Error

func (err ChannelTypeMismatch) Error() string

type NotWritableChannel

type NotWritableChannel struct {
	Kind reflect.Kind
	Dir  *reflect.ChanDir
}

NotWritableChannel defines not writable channel error type.

func (NotWritableChannel) Error

func (err NotWritableChannel) Error() string

Jump to

Keyboard shortcuts

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