-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
323 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package event | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
EventQueueSize = 20 | ||
) | ||
|
||
type EventType string | ||
|
||
type EventSubscriberId int | ||
|
||
type EventHandlerFunc func(Event) | ||
|
||
type Event struct { | ||
Type EventType | ||
Timestamp time.Time | ||
Data any | ||
} | ||
|
||
func NewEvent(eventType EventType, eventData any) Event { | ||
return Event{ | ||
Type: eventType, | ||
Timestamp: time.Now(), | ||
Data: eventData, | ||
} | ||
} | ||
|
||
type EventBus struct { | ||
sync.Mutex | ||
subscribers map[EventType]map[EventSubscriberId]chan Event | ||
lastSubId EventSubscriberId | ||
} | ||
|
||
// NewEventBus creates a new EventBus | ||
func NewEventBus() *EventBus { | ||
return &EventBus{ | ||
subscribers: make(map[EventType]map[EventSubscriberId]chan Event), | ||
} | ||
} | ||
|
||
// Subscribe allows a consumer to receive events of a particular type via a channel | ||
func (e *EventBus) Subscribe(eventType EventType) (EventSubscriberId, <-chan Event) { | ||
e.Lock() | ||
defer e.Unlock() | ||
// Create event channel | ||
evtCh := make(chan Event, EventQueueSize) | ||
// Increment subscriber ID | ||
subId := e.lastSubId + 1 | ||
e.lastSubId = subId | ||
// Add new subscriber | ||
if _, ok := e.subscribers[eventType]; !ok { | ||
e.subscribers[eventType] = make(map[EventSubscriberId]chan Event) | ||
} | ||
evtTypeSubs := e.subscribers[eventType] | ||
evtTypeSubs[subId] = evtCh | ||
return subId, evtCh | ||
} | ||
|
||
// SubscribeFunc allows a consumer to receive events of a particular type via a callback function | ||
func (e *EventBus) SubscribeFunc(eventType EventType, handlerFunc EventHandlerFunc) EventSubscriberId { | ||
subId, evtCh := e.Subscribe(eventType) | ||
go func(evtCh <-chan Event, handlerFunc EventHandlerFunc) { | ||
for { | ||
evt, ok := <-evtCh | ||
if !ok { | ||
return | ||
} | ||
handlerFunc(evt) | ||
} | ||
}(evtCh, handlerFunc) | ||
return subId | ||
} | ||
|
||
// Unsubscribe stops delivery of events for a particular type for an existing subscriber | ||
func (e *EventBus) Unsubscribe(eventType EventType, subId EventSubscriberId) { | ||
e.Lock() | ||
defer e.Unlock() | ||
if evtTypeSubs, ok := e.subscribers[eventType]; ok { | ||
delete(evtTypeSubs, subId) | ||
} | ||
} | ||
|
||
// Publish allows a producer to send an event of a particular type to all subscribers | ||
func (e *EventBus) Publish(eventType EventType, evt Event) { | ||
e.Lock() | ||
defer e.Unlock() | ||
if subs, ok := e.subscribers[eventType]; ok { | ||
for _, subCh := range subs { | ||
// NOTE: this is purposely a blocking operation to prevent dropping data | ||
// XXX: do we maybe want to detect a blocked channel and temporarily set it aside | ||
// to get the event sent to the other subscribers? | ||
subCh <- evt | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2024 Blink Labs Software | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package event_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/blinklabs-io/node/event" | ||
) | ||
|
||
func TestEventBusSingleSubscriber(t *testing.T) { | ||
var testEvtData int = 999 | ||
var testEvtType event.EventType = "test.event" | ||
eb := event.NewEventBus() | ||
_, subCh := eb.Subscribe(testEvtType) | ||
eb.Publish(testEvtType, event.NewEvent(testEvtType, testEvtData)) | ||
select { | ||
case evt, ok := <-subCh: | ||
if !ok { | ||
t.Fatalf("event channel closed unexpectedly") | ||
} | ||
switch v := evt.Data.(type) { | ||
case int: | ||
if v != testEvtData { | ||
t.Fatalf("did not get expected event") | ||
} | ||
default: | ||
t.Fatalf("event data was not of expected type, expected int, got %T", evt.Data) | ||
} | ||
case <-time.After(1 * time.Second): | ||
t.Fatalf("timeout waiting for event") | ||
} | ||
} | ||
|
||
func TestEventBusMultipleSubscribers(t *testing.T) { | ||
var testEvtData int = 999 | ||
var testEvtType event.EventType = "test.event" | ||
eb := event.NewEventBus() | ||
_, sub1Ch := eb.Subscribe(testEvtType) | ||
_, sub2Ch := eb.Subscribe(testEvtType) | ||
eb.Publish(testEvtType, event.NewEvent(testEvtType, testEvtData)) | ||
var gotVal1, gotVal2 bool | ||
for { | ||
if gotVal1 && gotVal2 { | ||
break | ||
} | ||
select { | ||
case evt, ok := <-sub1Ch: | ||
if !ok { | ||
t.Fatalf("event channel closed unexpectedly") | ||
} | ||
if gotVal1 { | ||
t.Fatalf("received unexpected event") | ||
} | ||
switch v := evt.Data.(type) { | ||
case int: | ||
if v != testEvtData { | ||
t.Fatalf("did not get expected event") | ||
} | ||
default: | ||
t.Fatalf("event data was not of expected type, expected int, got %T", evt.Data) | ||
} | ||
gotVal1 = true | ||
case evt, ok := <-sub2Ch: | ||
if !ok { | ||
t.Fatalf("event channel closed unexpectedly") | ||
} | ||
if gotVal2 { | ||
t.Fatalf("received unexpected event") | ||
} | ||
switch v := evt.Data.(type) { | ||
case int: | ||
if v != testEvtData { | ||
t.Fatalf("did not get expected event") | ||
} | ||
default: | ||
t.Fatalf("event data was not of expected type, expected int, got %T", evt.Data) | ||
} | ||
gotVal2 = true | ||
case <-time.After(1 * time.Second): | ||
t.Fatalf("timeout waiting for event") | ||
} | ||
} | ||
} | ||
|
||
func TestEventBusUnsubscribe(t *testing.T) { | ||
var testEvtData int = 999 | ||
var testEvtType event.EventType = "test.event" | ||
eb := event.NewEventBus() | ||
subId, subCh := eb.Subscribe(testEvtType) | ||
eb.Unsubscribe(testEvtType, subId) | ||
eb.Publish(testEvtType, event.NewEvent(testEvtType, testEvtData)) | ||
select { | ||
case _, ok := <-subCh: | ||
if !ok { | ||
t.Fatalf("event channel closed unexpectedly") | ||
} | ||
t.Fatalf("received unexpected event") | ||
case <-time.After(1 * time.Second): | ||
// NOTE: this is the expected way for the test to end | ||
} | ||
} |
Oops, something went wrong.