Skip to content

An easy way of creating a single source of information and having multiple goroutines subscribe to it.

Notifications You must be signed in to change notification settings

sneakykiwi/go-broadcast

Repository files navigation

Broadcaster

This module gives you the ability to publish messages in a shared and thread-safe manner, it is designed to work similarly to how Redis Pub/Sub works.

The module has 100% versatility as it is able to accept all types of callbacks, you could use this for publishing any sorts of events to all subscribed goroutines.

No channels used

It should not be used by an excessive amount of goroutines

Examples

func main(){
    messageToaster := func(message interface{}) {
        fmt.Printf("[New Message]: %v\n", message)
    }
    unwillingReceiver := func(message interface{}) {
        fmt.Println("Do not disturb!")
    }
    broadcaster := SetupBroadcaster()
    broadcaster.Subscribe(1, messageToaster)
    broadcaster.Subscribe(2, messageToaster)
    broadcaster.Subscribe(3, unwillingReceiver)

    go broadcaster.Start()

    broadcaster.Publish("Hello!")

    time.Sleep(time.Second)
    broadcaster.Unsubscribe(3)
    broadcaster.Publish("Goodbye!")
    // [New Message]: Hello!
    // Do not disturb!
    // [New Message]: Hello!
    // [New Message]: Goodbye!
    // [New Message]: Goodbye!
}

Benchmarks

pkg: github.com/sneakykiwi/go-broadcast
cpu: Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
BenchmarkBroadcaster100-2    	1000000000	         0.0000240 ns/op
BenchmarkBroadcaster500-2    	1000000000	         0.0001136 ns/op
BenchmarkBroadcaster1000-2   	1000000000	         0.0002386 ns/op
PASS
ok  	github.com/sneakykiwi/go-broadcast	1.026s

About

An easy way of creating a single source of information and having multiple goroutines subscribe to it.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published