Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions mailbox/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,15 @@ func (c *Client) AwaitRPC(ctx context.Context, correlationID string,
resp proto.Message) error {

for {
data, ok := c.popPending(correlationID)
if ok {
return (proto.UnmarshalOptions{
data, ch, hasPending := c.popPendingOrAddWaiter(correlationID)
if hasPending {
unmarshal := proto.UnmarshalOptions{
DiscardUnknown: true,
}).Unmarshal(data, resp)
}

return unmarshal.Unmarshal(data, resp)
}

ch := c.addWaiter(correlationID)
select {
case <-ch:
case <-ctx.Done():
Expand Down Expand Up @@ -341,31 +342,25 @@ func (c *Client) handleEnvelope(env *mailboxpb.Envelope) {
delete(c.waiters, correlationID)
}

// popPending returns and removes a cached response for correlationID.
func (c *Client) popPending(correlationID string) ([]byte, bool) {
// popPendingOrAddWaiter atomically checks for a pending response and, if one
// is not present, registers a waiter channel for a future response.
func (c *Client) popPendingOrAddWaiter(correlationID string) (
[]byte, chan struct{}, bool) {

c.mu.Lock()
defer c.mu.Unlock()

data, ok := c.pending[correlationID]
if !ok {
return nil, false
}

delete(c.pending, correlationID)
if ok {
delete(c.pending, correlationID)

return data, true
}
return data, nil, true
}

// addWaiter registers a waiter for correlationID and returns its channel.
func (c *Client) addWaiter(correlationID string) chan struct{} {
ch := make(chan struct{})

c.mu.Lock()
defer c.mu.Unlock()

c.waiters[correlationID] = append(c.waiters[correlationID], ch)

return ch
return nil, ch, false
}

// removeWaiter removes a previously registered waiter channel.
Expand Down
Loading