Skip to content

Commit

Permalink
possible fix for a race condition in topic subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 25, 2023
1 parent 2680ea2 commit dd1af03
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions server/datamodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,8 +1673,8 @@ func ErrLocked(id, topic string, ts time.Time) *ServerComMessage {
return ErrLockedExplicitTs(id, topic, ts, ts)
}

// ErrLockedReply operation rejected because the topic is being deleted with explicit server and
// incoming request timestamps in response to a client request (503).
// ErrLockedReply operation rejected because the topic is being deleted in response
// to a client request (503).
func ErrLockedReply(msg *ClientComMessage, ts time.Time) *ServerComMessage {
return ErrLockedExplicitTs(msg.Id, msg.Original, ts, msg.Timestamp)
}
Expand Down
2 changes: 2 additions & 0 deletions server/store/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
ErrInvalidResponse = StoreError("invalid response")
// ErrRedirected means the subscription request was redirected to another topic.
ErrRedirected = StoreError("redirected")
// ErrAlreadySatisfied means the desired result, such as subscription, is already satisfied.
ErrAlreadySatisfied = StoreError("already satisfied")
)

// Uid is a database-specific record id, suitable to be used as a primary key.
Expand Down
6 changes: 6 additions & 0 deletions server/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,12 @@ func (t *Topic) broadcastToSessions(msg *ServerComMessage) {
func (t *Topic) subscriptionReply(asChan bool, msg *ClientComMessage) error {
// The topic is already initialized by the Hub

// Check if the session is already subscribed to topic.
if msg.sess.getSub(t.name) != nil {
msg.sess.queueOut(InfoAlreadySubscribed(msg.Id, msg.Original, msg.Timestamp))
return types.ErrAlreadySatisfied
}

msgsub := msg.Sub

// For newly created topics report topic creation time.
Expand Down
2 changes: 2 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func decodeStoreErrorExplicitTs(err error, id, topic string, serverTs, incomingR
errmsg = ErrInvalidResponse(id, topic, serverTs, incomingReqTs)
case types.ErrRedirected:
errmsg = InfoUseOther(id, topic, params["topic"].(string), serverTs, incomingReqTs)
case types.ErrAlreadySatisfied:
errmsg = InfoNoAction(id, topic, serverTs, incomingReqTs)
default:
errmsg = ErrUnknownExplicitTs(id, topic, serverTs, incomingReqTs)
}
Expand Down

0 comments on commit dd1af03

Please sign in to comment.