-
-
Save xeoncross/6b4195452d852ef92362e0a652f43f68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "math/rand" | |
| "sync" | |
| "time" | |
| ) | |
| type DataEvent struct { | |
| Data interface{} | |
| Topic string | |
| } | |
| // DataChannel is a channel which can accept an DataEvent | |
| type DataChannel chan DataEvent | |
| // DataChannelSlice is a slice of DataChannels | |
| type DataChannelSlice []DataChannel | |
| // EventBus stores the information about subscribers interested for a particular topic | |
| type EventBus struct { | |
| subscribers map[string]DataChannelSlice | |
| rm sync.RWMutex | |
| } | |
| func (eb *EventBus) Publish(topic string, data interface{}) { | |
| eb.rm.RLock() | |
| if chans, found := eb.subscribers[topic]; found { | |
| go func(data DataEvent, dataChannelSlices DataChannelSlice) { | |
| for _, ch := range dataChannelSlices { | |
| ch <- data | |
| } | |
| }(DataEvent{Data: data, Topic: topic}, chans) | |
| } | |
| eb.rm.RUnlock() | |
| } | |
| func (eb *EventBus) Subscribe(topic string, ch DataChannel) { | |
| eb.rm.Lock() | |
| if prev, found := eb.subscribers[topic]; found { | |
| eb.subscribers[topic] = append(prev, ch) | |
| } else { | |
| eb.subscribers[topic] = append([]DataChannel{}, ch) | |
| } | |
| eb.rm.Unlock() | |
| } | |
| var eb = &EventBus{ | |
| subscribers: map[string]DataChannelSlice{}, | |
| } | |
| func printDataEvent(ch string, data DataEvent) { | |
| fmt.Printf("Channel: %s; Topic: %s; DataEvent: %v\n", ch, data.Topic, data.Data) | |
| } | |
| func publisTo(topic string, data string) { | |
| for { | |
| eb.Publish(topic, data) | |
| time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) | |
| } | |
| } | |
| func main() { | |
| ch1 := make(chan DataEvent) | |
| ch2 := make(chan DataEvent) | |
| ch3 := make(chan DataEvent) | |
| eb.Subscribe("topic1", ch1) | |
| eb.Subscribe("topic2", ch2) | |
| eb.Subscribe("topic2", ch3) | |
| go publisTo("topic1", "Hi topic 1") | |
| go publisTo("topic2", "Welcome to topic 2") | |
| for { | |
| select { | |
| case d := <-ch1: | |
| go printDataEvent("ch1", d) | |
| case d := <-ch2: | |
| go printDataEvent("ch2", d) | |
| case d := <-ch3: | |
| go printDataEvent("ch3", d) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment