go2close

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 6 Imported by: 0

README

Go2Close 🔒

lint test report version license godoc

GOPROXY=direct go get -u github.com/1pkg/go2close

Introduction

Go2Close patches go runtime to provides a way to close a chan idempotently + overwrite empty value returned from that closed channel.

With Go2Close Without Go2Close
package main

import (
	"fmt"
	"sync"
	"github.com/1pkg/go2close"
)

func main() {
	ch := make(chan int)
	var wg sync.WaitGroup
	wg.Add(5)
	for i := 0; i < 5; i++ {
		go worker(i, ch, &wg)
	}
	go2close.Close2(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

Go2Close exposes single function Close2 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 Close2 will cause a panic close of closed channel. Note that unless cancelation is called next call to Close2 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 go2close uses bou.ke/monkey package to patch all existing channel receive entrypoints:

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

This makes go2close inherits the same list of restrictions as bou.ke/monkey has.

Licence

Go2Close 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 close2 cancelation function type.

func Close2

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

Close2 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 Close2 will cause a panic `close of closed channel`. Note that unless cancelation is called next call to Close2 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 go2close uses `bou.ke/monkey` package to patch all existing channel receive entrypoints, so go2close 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