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 ¶ added in v1.5.0
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.
Click to show internal directories.
Click to hide internal directories.