Files
micromdm/platform/pubsub/inmem/consumer.go
Victor Vrantchan 91c236c8c3 organize essential APIs into platform, workflow and pkg folders (#337)
Add more logic to the way code is organized.

	/pkg -- library code not directly connected to micromdm
	/mdm -- packages meant for the services devices interract with. The MDM protocol.
	/dep -- DEP API and related packages.
	/platform -- Core APIs the server provides. Commands API, Devices API, queue, pubsub etc.
	/workflow -- Packages/API that build on top of platform. Today that's the webhook package.
		     Depending on what ends up here, the workflow folder might become its own repository.
2017-11-23 22:07:57 -05:00

35 lines
646 B
Go

package inmem
import (
"context"
"github.com/micromdm/micromdm/platform/pubsub"
)
func (p *Inmem) Subscribe(_ context.Context, name, topic string) (<-chan pubsub.Event, error) {
events := make(chan pubsub.Event)
sub := subscription{
name: name,
topic: topic,
eventChan: events,
}
p.mtx.Lock()
p.subscriptions[topic] = append(p.subscriptions[topic], sub)
p.mtx.Unlock()
return events, nil
}
func (p *Inmem) dispatch() {
for {
select {
case ev := <-p.publish:
p.mtx.Lock()
for _, sub := range p.subscriptions[ev.Topic] {
go func(s subscription) { s.eventChan <- ev }(sub)
}
p.mtx.Unlock()
}
}
}