Skip to content

Commit

Permalink
making all references to EventBusListeners pointers
Browse files Browse the repository at this point in the history
adding a keepalive event type.
  • Loading branch information
AnalogJ committed Sep 13, 2023
1 parent 7078f78 commit ce8efdb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
24 changes: 17 additions & 7 deletions backend/pkg/event_bus/event_bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ func GetEventBusServer(logger logrus.FieldLogger) *EventBus {
singletonEventBusInstance = &EventBus{
Logger: logger,
Message: make(chan EventBusMessage),
NewListener: make(chan EventBusListener),
ClosedListener: make(chan EventBusListener),
TotalRoomListeners: make(map[string][]EventBusListener),
NewListener: make(chan *EventBusListener),
ClosedListener: make(chan *EventBusListener),
TotalRoomListeners: make(map[string][]*EventBusListener),
}

// Start processing requests
go singletonEventBusInstance.listen()

//background keep-alive for testing
//go func() {
// for {
// time.Sleep(time.Second * 10)
// // Send current time
// singletonEventBusInstance.PublishMessage(models.NewEventKeepAlive("keep-alive"))
// }
//}()

} else {
fmt.Println("Single instance already created.")
}
Expand All @@ -55,13 +65,13 @@ type EventBus struct {
Message chan EventBusMessage

// New client connections
NewListener chan EventBusListener
NewListener chan *EventBusListener

// Closed client connections
ClosedListener chan EventBusListener
ClosedListener chan *EventBusListener

// Total client connections
TotalRoomListeners map[string][]EventBusListener
TotalRoomListeners map[string][]*EventBusListener
}

type EventBusListener struct {
Expand All @@ -84,7 +94,7 @@ func (bus *EventBus) listen() {
case listener := <-bus.NewListener:
//check if this userId room already exists, or create it
if _, exists := bus.TotalRoomListeners[listener.UserID]; !exists {
bus.TotalRoomListeners[listener.UserID] = []EventBusListener{}
bus.TotalRoomListeners[listener.UserID] = []*EventBusListener{}
}
bus.TotalRoomListeners[listener.UserID] = append(bus.TotalRoomListeners[listener.UserID], listener)
log.Printf("Listener added to room: `%s`. %d registered listeners", listener.UserID, len(bus.TotalRoomListeners[listener.UserID]))
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
type EventSourceSyncStatus string

const (
EventTypeKeepAlive EventSourceSyncStatus = "keep_alive"
EventTypeSourceSync EventSourceSyncStatus = "source_sync"
EventTypeSourceComplete EventSourceSyncStatus = "source_complete"
)
Expand Down
18 changes: 18 additions & 0 deletions backend/pkg/models/event_keep_alive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package models

import "time"

type EventKeepAlive struct {
*Event `json:",inline"`
Time string `json:"time"`
}

func NewEventKeepAlive(userID string) *EventKeepAlive {
return &EventKeepAlive{
Event: &Event{
UserID: userID,
EventType: EventTypeKeepAlive,
},
Time: time.Now().Format("2006-01-02 15:04:05"),
}
}
2 changes: 1 addition & 1 deletion backend/pkg/web/handler/server_sent_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func SSEStream(c *gin.Context) {
log.Printf("could not get client channel from context")
return
}
listener, ok := v.(event_bus.EventBusListener)
listener, ok := v.(*event_bus.EventBusListener)
if !ok {
return
}
Expand Down
8 changes: 4 additions & 4 deletions backend/pkg/web/middleware/server_sent_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func SSEEventBusServerMiddleware(logger *logrus.Entry) gin.HandlerFunc {
}

// Send new connection to event server
bus.NewListener <- clientListener
bus.NewListener <- &clientListener

defer func() {
// Send closed connection to event server
bus.ClosedListener <- clientListener
bus.ClosedListener <- &clientListener
}()

c.Set(pkg.ContextKeyTypeSSEEventBusServer, bus)
c.Set(pkg.ContextKeyTypeSSEClientChannel, clientListener)
c.Set(pkg.ContextKeyTypeSSEEventBusServer, &bus)
c.Set(pkg.ContextKeyTypeSSEClientChannel, &clientListener)

c.Next()
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/services/event-bus.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class EventBusService {
this.authService.IsAuthenticatedSubject.subscribe((isAuthenticated) => {
console.log("isAuthenticated changed:", isAuthenticated)
if(isAuthenticated){
console.log("Started listening to event bus")
this.eventBusSubscription = this.listenEventBus().subscribe((event: Event | EventSourceSync | EventSourceComplete)=>{
console.log("eventbus event:", event)
//TODO: start toasts.
Expand All @@ -48,6 +49,7 @@ export class EventBusService {
})
} else {
//no longer authenticated, unsubscribe from eventbus and abort/terminate connection
console.log("Stopped listening to event bus")
this.abortEventBus()
}
});
Expand Down

0 comments on commit ce8efdb

Please # to comment.