Fix unblock mutex

- if `receivedEvent.ID` not match `event.ID`, may trigger an error `fatal error: sync: unlock of unlocked mutex`.
- if context cancled, it does not needs mutex.
This commit is contained in:
Wayback Archiver 2023-02-05 16:00:48 +00:00 committed by fiatjaf
parent 4aee139f6c
commit ab2db2dfc5

View File

@ -234,7 +234,6 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
}
sub := r.Subscribe(ctx, Filters{Filter{IDs: []string{event.ID}}})
defer mu.Unlock()
for {
select {
case receivedEvent := <-sub.Events:
@ -242,6 +241,7 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
// we got a success, so update our status and proceed to return
mu.Lock()
status = PublishStatusSucceeded
mu.Unlock()
return status
}
case <-ctx.Done():
@ -250,7 +250,6 @@ func (r *Relay) Publish(ctx context.Context, event Event) Status {
// e.g. if this happens because of the timeout then status will probably be "failed"
// but if it happens because okCallback was called then it might be "succeeded"
// do not return if okCallback is in process
mu.Lock()
return status
}
}