broker

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: MIT Imports: 2 Imported by: 1

README

tests Go version GoDoc reference

Channel Broker

A helper for golang that provides a broker based on channels. So you send to one channel receive by multiple channels. Receving channels can be created and removed at runtime.

Usage Example

package main

import (
    "fmt"
    "github.com/JulianJacobi/ChannelBroker"
)

func main() {
    b := broker.New[string]()

    c1 := b.NewChannel()
    c2 := b.NewChannel()

    b.Chan <- "Test"

    r1 := <-c1.Chan
    fmt.Println(r1)

    r2 := <-c2.Chan
    fmt.Println(r2)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel[T any] struct {
	Chan <-chan T
	// contains filtered or unexported fields
}

Channel represents a receiving channel.

type ChannelBroker

type ChannelBroker[T any] struct {
	Chan chan<- T
	// contains filtered or unexported fields
}

ChannelBroker represents the broker.

func New

func New[T any]() *ChannelBroker[T]

New channel broker instantiates a new ChannelBroker.

func (*ChannelBroker[T]) Clear added in v0.2.0

func (cb *ChannelBroker[T]) Clear()

Clear closes and removes all channels from broker

func (*ChannelBroker[T]) NewChannel

func (cb *ChannelBroker[T]) NewChannel() *Channel[T]

NewChannel creates new receiving Channel and adds it to the broker

func (*ChannelBroker[T]) RemoveChannel

func (cb *ChannelBroker[T]) RemoveChannel(c *Channel[T])

RemoveChannel from broker and closes the containing channel.

Channel object should not be further used after removing it from broker, because the unlaying channel is closed after this call.

Jump to

Keyboard shortcuts

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