Merge pull request #41 from pterodactyl/fix/race-1

Fix a possible race condition when Unsubscribing from the EventBus
This commit is contained in:
Dane Everitt
2020-07-31 20:11:21 -07:00
committed by GitHub
2 changed files with 23 additions and 21 deletions

View File

@@ -46,30 +46,32 @@ func (s *Server) Events() *EventBus {
// Publish data to a given topic.
func (e *EventBus) Publish(topic string, data string) {
e.RLock()
defer e.RUnlock()
go func() {
e.RLock()
defer e.RUnlock()
t := topic
// Some of our topics for the socket support passing a more specific namespace,
// such as "backup completed:1234" to indicate which specific backup was completed.
//
// In these cases, we still need to the send the event using the standard listener
// name of "backup completed".
if strings.Contains(topic, ":") {
parts := strings.SplitN(topic, ":", 2)
t := topic
// Some of our topics for the socket support passing a more specific namespace,
// such as "backup completed:1234" to indicate which specific backup was completed.
//
// In these cases, we still need to the send the event using the standard listener
// name of "backup completed".
if strings.Contains(topic, ":") {
parts := strings.SplitN(topic, ":", 2)
if len(parts) == 2 {
t = parts[0]
if len(parts) == 2 {
t = parts[0]
}
}
}
if ch, ok := e.subscribers[t]; ok {
go func(data Event, cs []chan Event) {
for _, channel := range cs {
if ch, ok := e.subscribers[t]; ok {
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 {