chanspy

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 2 Imported by: 0

README

Chanspy

Go Channel Spy

Goreportcard Go Reference

This module contains helpers for Go channel types like:

  • Checking if channel is closed without read.
  • Obtaining channel length and capacity.
  • etc.

Disclaimer

This package (ab)uses unsafe techniques, use with caution.

I'm not responsible for broken deployments, segfaults, thermonuclear war, or you getting fired because undefined behavior in the code led to a company going bankrupt. Please do some research if you have any concerns about features included in this package before using it! YOU are choosing to make these modifications, and if you point the finger at me for messing up your program, I will laugh at you.

Usage

package main

import (
    "fmt"

    "github.com/x1unix/chanspy"
)

func main() {
    // This is a fast way to check if channel is closed without block.
    // Use chanspy.ValueOf(ch, chanspy.WithLock) for thread-safe way.
    ch := make(chan int)
    fmt.Println(chanspy.IsClosed(ch)) // Prints: false

    close(ch)
    fmt.Println(chanspy.IsClosed(ch)) // Prints: true
}

See examples for more details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsClosed

func IsClosed[T any](ch chan T) bool

IsClosed reports whether a channel is closed.

Example
package main

import (
	"fmt"

	"github.com/x1unix/chanspy"
)

func main() {
	ch := make(chan int)
	close(ch)
	fmt.Println(chanspy.IsClosed(ch))
}
Output:

true

Types

type Chan

type Chan interface {
	// Empty reports whether a read from a channel would block (that is, the channel is empty).
	Empty() bool

	// Closed returns true if a channel is closed.
	Closed() bool

	// Locked is true if channel lock is active.
	//
	// Lock is active when any write operation is performed on a channel at the same goroutine.
	//
	// See: https://github.com/golang/go/blob/master/src/runtime/chan.go#L52
	Locked() bool

	// Data returns a pointer to a channel buffer.
	Data() unsafe.Pointer

	// Pointer returns a raw pointer to underlying channel structure.
	Pointer() unsafe.Pointer

	// Len returns a number of elements in a channel buffer.
	Len() int

	// Cap returns a size of a channel buffer.
	Cap() int
}

func ValueOf

func ValueOf[T any](ch chan T, options ...Option) Chan

ValueOf returns a Chan instance for a given channel.

Second argument sync controls whether channel operations should lock a channel. Please note that locking a channel may cause a deadlock if the channel is used.

Example
package main

import (
	"fmt"

	"github.com/x1unix/chanspy"
)

func main() {
	// Simple case with immediate access.
	// May cause race condition if the channel is used for read/write atm.
	ch := make(chan int, 10)
	ch <- 1
	ch <- 2
	ch <- 3
	ch <- 4
	defer close(ch)

	// c.Locked() will be true as channel has active write operation in this goroutine.
	c := chanspy.ValueOf(ch)
	fmt.Printf(
		"Regular: IsClosed=%t Locked=%t Len=%d Cap=%d\n",
		c.Closed(), c.Locked(), c.Len(), c.Cap(),
	)

	// Thread-safe access.
	// Will try to lock a channel before performing any operations.
	// Useful for concurrent access.
	done := make(chan struct{})
	go func() {
		// c.Locked() is false as we're in a different goroutine.
		c := chanspy.ValueOf(ch, chanspy.WithLock)
		_ = <-ch
		_ = <-ch
		fmt.Printf(
			"WithLock: IsClosed=%t Locked=%t Len=%d Cap=%d\n",
			c.Closed(), c.Locked(), c.Len(), c.Cap(),
		)
		done <- struct{}{}
	}()
	<-done
}
Output:

Regular: IsClosed=false Locked=true Len=4 Cap=10
WithLock: IsClosed=false Locked=false Len=2 Cap=10

type Option

type Option func(*opts)

Option is a functional option for ValueOf function.

var WithLock Option = func(o *opts) {
	o.lock = true
}

WithLock locks a channel before performing any operations.

Please note that locking a channel may cause a deadlock if the channel is used.

Directories

Path Synopsis
internal
chantype
Package chantype contains internal implementation of channel type reflection.
Package chantype contains internal implementation of channel type reflection.

Jump to

Keyboard shortcuts

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