-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Reworking the event stream so its also an actor (#79)
* added new event stream that is actually an actor * fixed bug on sending event with remotes * renamed event to event stream * fixed engine address bug when using remote * fixed address error and wrapped up big portion of the PR
- Loading branch information
Showing
15 changed files
with
215 additions
and
178 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
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
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 |
---|---|---|
@@ -1,70 +1,36 @@ | ||
package actor | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"sync" | ||
|
||
"github.com/anthdm/hollywood/log" | ||
) | ||
|
||
// EventSub is the message that will be send to subscribe to the event stream. | ||
type EventSub struct { | ||
id uint32 | ||
} | ||
|
||
type EventStreamFunc func(event any) | ||
|
||
type EventStream struct { | ||
mu sync.RWMutex | ||
subs map[*EventSub]EventStreamFunc | ||
logger log.Logger | ||
pid *PID | ||
} | ||
|
||
func NewEventStream() *EventStream { | ||
return &EventStream{ | ||
subs: make(map[*EventSub]EventStreamFunc), | ||
} | ||
// EventUnSub is the message that will be send to unsubscribe from the event stream. | ||
type EventUnsub struct { | ||
pid *PID | ||
} | ||
|
||
func (e *EventStream) Unsubscribe(sub *EventSub) { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
delete(e.subs, sub) | ||
|
||
e.logger.Debugw("unsubscribe", | ||
"subs", len(e.subs), | ||
"id", sub.id, | ||
) | ||
type EventStream struct { | ||
subs map[*PID]bool | ||
} | ||
|
||
func (e *EventStream) Subscribe(f EventStreamFunc) *EventSub { | ||
e.mu.Lock() | ||
defer e.mu.Unlock() | ||
|
||
sub := &EventSub{ | ||
id: uint32(rand.Intn(math.MaxUint32)), | ||
func NewEventStream() Producer { | ||
return func() Receiver { | ||
return &EventStream{ | ||
subs: make(map[*PID]bool), | ||
} | ||
} | ||
e.subs[sub] = f | ||
|
||
e.logger.Debugw("subscribe", | ||
"subs", len(e.subs), | ||
"id", sub.id, | ||
) | ||
|
||
return sub | ||
} | ||
|
||
func (e *EventStream) Publish(msg any) { | ||
e.mu.RLock() | ||
defer e.mu.RUnlock() | ||
for _, f := range e.subs { | ||
go f(msg) | ||
func (e *EventStream) Receive(c *Context) { | ||
switch msg := c.Message().(type) { | ||
case EventSub: | ||
e.subs[msg.pid] = true | ||
case EventUnsub: | ||
delete(e.subs, msg.pid) | ||
default: | ||
for sub := range e.subs { | ||
c.Forward(sub) | ||
} | ||
} | ||
} | ||
|
||
func (e *EventStream) Len() int { | ||
e.mu.RLock() | ||
defer e.mu.RUnlock() | ||
return len(e.subs) | ||
} |
Oops, something went wrong.