Merge pull request #41 from pterodactyl/fix/race-1
Fix a possible race condition when Unsubscribing from the EventBus
This commit is contained in:
commit
da26b4c5c7
|
@ -26,9 +26,9 @@ func (h *Handler) ListenForExpiration(ctx context.Context) {
|
|||
jwt := h.GetJwt()
|
||||
if jwt != nil {
|
||||
if jwt.ExpirationTime.Unix()-time.Now().Unix() <= 0 {
|
||||
h.SendJson(&Message{Event: TokenExpiredEvent})
|
||||
_ = h.SendJson(&Message{Event: TokenExpiredEvent})
|
||||
} else if jwt.ExpirationTime.Unix()-time.Now().Unix() <= 180 {
|
||||
h.SendJson(&Message{Event: TokenExpiringEvent})
|
||||
_ = h.SendJson(&Message{Event: TokenExpiringEvent})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (h *Handler) ListenForServerEvents(ctx context.Context) {
|
|||
|
||||
close(eventChannel)
|
||||
default:
|
||||
h.SendJson(&Message{
|
||||
_ = h.SendJson(&Message{
|
||||
Event: d.Topic,
|
||||
Args: []string{d.Data},
|
||||
})
|
||||
|
|
|
@ -46,6 +46,7 @@ func (s *Server) Events() *EventBus {
|
|||
|
||||
// Publish data to a given topic.
|
||||
func (e *EventBus) Publish(topic string, data string) {
|
||||
go func() {
|
||||
e.RLock()
|
||||
defer e.RUnlock()
|
||||
|
||||
|
@ -64,12 +65,13 @@ func (e *EventBus) Publish(topic string, data string) {
|
|||
}
|
||||
|
||||
if ch, ok := e.subscribers[t]; ok {
|
||||
go func(data Event, cs []chan Event) {
|
||||
for _, channel := range cs {
|
||||
data := Event{Data: data, Topic: topic}
|
||||
|
||||
for _, channel := range ch {
|
||||
channel <- data
|
||||
}
|
||||
}(Event{Data: data, Topic: topic}, ch)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (e *EventBus) PublishJson(topic string, data interface{}) error {
|
||||
|
|
Loading…
Reference in New Issue
Block a user