mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-11 12:15:34 +08:00
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.
This commit is contained in:
34
platform/pubsub/inmem/consumer.go
Normal file
34
platform/pubsub/inmem/consumer.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
49
platform/pubsub/inmem/inmem_test.go
Normal file
49
platform/pubsub/inmem/inmem_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package inmem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPubSub(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
inmem := NewPubSub()
|
||||
tests := []string{"a", "b", "c"}
|
||||
for _, tt := range tests {
|
||||
if err := inmem.Publish(ctx, tt, []byte(tt+tt+tt)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := inmem.Publish(ctx, tt, []byte(tt+tt)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
subA, err := inmem.Subscribe(ctx, "asub", "a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
subA1, err := inmem.Subscribe(ctx, "asub1", "a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
subB, err := inmem.Subscribe(ctx, "bsub", "b")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case s := <-subA:
|
||||
fmt.Println("asub:", s)
|
||||
case s := <-subA1:
|
||||
fmt.Println("asub1:", s)
|
||||
case s := <-subB:
|
||||
fmt.Println("bsub:", s)
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
38
platform/pubsub/inmem/publisher.go
Normal file
38
platform/pubsub/inmem/publisher.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package inmem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/micromdm/micromdm/platform/pubsub"
|
||||
)
|
||||
|
||||
func NewPubSub() *Inmem {
|
||||
publish := make(chan pubsub.Event)
|
||||
subscriptions := make(map[string][]subscription)
|
||||
inmem := &Inmem{
|
||||
publish: publish,
|
||||
subscriptions: subscriptions,
|
||||
}
|
||||
go inmem.dispatch()
|
||||
return inmem
|
||||
}
|
||||
|
||||
type Inmem struct {
|
||||
mtx sync.RWMutex
|
||||
subscriptions map[string][]subscription
|
||||
|
||||
publish chan pubsub.Event
|
||||
}
|
||||
|
||||
type subscription struct {
|
||||
name string
|
||||
topic string
|
||||
eventChan chan<- pubsub.Event
|
||||
}
|
||||
|
||||
func (p *Inmem) Publish(_ context.Context, topic string, msg []byte) error {
|
||||
event := pubsub.Event{Topic: topic, Message: msg}
|
||||
go func() { p.publish <- event }()
|
||||
return nil
|
||||
}
|
||||
21
platform/pubsub/pubsub.go
Normal file
21
platform/pubsub/pubsub.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package pubsub
|
||||
|
||||
import "context"
|
||||
|
||||
type Event struct {
|
||||
Topic string
|
||||
Message []byte
|
||||
}
|
||||
|
||||
type Publisher interface {
|
||||
Publish(ctx context.Context, topic string, msg []byte) error
|
||||
}
|
||||
|
||||
type Subscriber interface {
|
||||
Subscribe(ctx context.Context, name, topic string) (<-chan Event, error)
|
||||
}
|
||||
|
||||
type PublishSubscriber interface {
|
||||
Publisher
|
||||
Subscriber
|
||||
}
|
||||
Reference in New Issue
Block a user