channels

package module
v0.0.0-...-a499989 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package channels provides a simple way to wait for a signal in a thread-safe manner

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Signal

func Signal(x interface{})

Signal stub function. Use Queue.Signal() instead

Example

ExampleSignal: Two examples of signal and wait. One timesout and the other does not.

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core/atomicTypes"
	"github.com/DanielRenne/GoCore/core/channels"
	"github.com/DanielRenne/GoCore/core/extensions"
)

func main() {
	timeout := atomicTypes.AtomicInt{}
	timeout.Set(1000)
	channelQueue := channels.Queue{
		TimeoutMilliseconds: &timeout,
	}

	type myCoolStruct struct {
		Hello       string
		WasReturned bool
	}
	c, any := channelQueue.Wait(myCoolStruct{})

	if any == false {
		go func() {
			log.Println("Sleep 500 ms")
			time.Sleep(500 * time.Millisecond)
			// from some other package or function signal the channel
			channelQueue.Signal(myCoolStruct{
				Hello:       "World",
				WasReturned: true,
			})
		}()
	}

	data := <-c
	log.Println("done, cast your interface to the proper type back to the caller.  Response data: " + data.(myCoolStruct).Hello + "  WasReturned: " + extensions.BoolToString(data.(myCoolStruct).WasReturned))

	timeout2 := atomicTypes.AtomicInt{}
	timeout2.Set(100)
	channelQueue2 := channels.Queue{
		TimeoutMilliseconds: &timeout2,
	}

	c, any = channelQueue2.Wait("initial data")

	if any == false {
		go func() {
			log.Println("Sleep 5 seconds for timeout")
			time.Sleep(5000 * time.Millisecond)
			// from some other package or function signal the channel
			channelQueue.Signal("return will never make it")
		}()
	}

	data2 := <-c
	log.Println("done, cast your interface to the proper type back to the caller.  Response data: " + data2.(string))
}
Output:

			2022/10/04 16:29:57 Sleep 500 ms
			2022/10/04 16:29:57 done, cast your interface to the proper type back to the caller.  Response data: World  WasReturned: true
			2022/10/04 16:29:57 Sleep 5 seconds for timeout
			2022/10/04 16:29:57 done, cast your interface to the proper type back to the caller.  Response data: initial data

Types

type Queue

type Queue struct {
	// Defaults to 10000 ms if none passed
	TimeoutMilliseconds *atomicTypes.AtomicInt
	// contains filtered or unexported fields
}

Queue provides a factory to queue channels sequentially and pop / signal them one at a time in a daisy chain.

func (*Queue) Any

func (q *Queue) Any() (any bool)

Any will return true if there are any current channels waiting.

func (*Queue) Signal

func (q *Queue) Signal(x interface{})

Signal will only signal the first item in the queue.

func (*Queue) Wait

func (q *Queue) Wait(x interface{}) (c chan interface{}, any bool)

Wait will return a channel for your function to wait on.

Jump to

Keyboard shortcuts

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