-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[azservicebus] Create a programmatically useful code for when a sessi…
…on is not available (#19113) Adding a new error Code (CodeTimeout) for when we get back a com.microsoft:timeout error from Service Bus. The primary use is if we are doing an AcceptNextSessionFor<Entity>() and there aren't any available sessions. This should allow customers to figure out if there's a genuine failure or if their session-enabled queue/subscription is "empty". Fixes #19039
- Loading branch information
1 parent
efe7e47
commit 523337c
Showing
12 changed files
with
510 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
sdk/messaging/azservicebus/example_session_roundrobin_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package azservicebus_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus" | ||
) | ||
|
||
func ExampleClient_AcceptNextSessionForQueue_roundrobin() { | ||
var sessionEnabledQueueName = "exampleSessionQueue" | ||
|
||
for { | ||
// You can have multiple active session receivers, provided they're each receiving | ||
// from different sessions. | ||
// | ||
// AcceptNextSessionForQueue (or AcceptNextSessionForSubscription) makes it simple to implement | ||
// this pattern, consuming multiple session receivers in parallel. | ||
sessionReceiver, err := client.AcceptNextSessionForQueue(context.TODO(), sessionEnabledQueueName, nil) | ||
|
||
if err != nil { | ||
var sbErr *azservicebus.Error | ||
|
||
if errors.As(err, &sbErr) && sbErr.Code == azservicebus.CodeTimeout { | ||
fmt.Printf("No sessions available\n") | ||
|
||
// NOTE: you could also continue here, which will block and wait again for a | ||
// session to become available. | ||
break | ||
} | ||
|
||
panic(err) | ||
} | ||
|
||
fmt.Printf("Got receiving for session '%s'\n", sessionReceiver.SessionID()) | ||
|
||
// consume the session | ||
go func() { | ||
defer func() { | ||
fmt.Printf("Closing receiver for session '%s'\n", sessionReceiver.SessionID()) | ||
err := sessionReceiver.Close(context.TODO()) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
// we're only reading a few messages here, but you can also receive in a loop | ||
messages, err := sessionReceiver.ReceiveMessages(context.TODO(), 10, nil) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("Received %d messages from session '%s'\n", len(messages), sessionReceiver.SessionID()) | ||
|
||
for _, m := range messages { | ||
if err := processMessageFromSession(context.TODO(), sessionReceiver, m); err != nil { | ||
panic(err) | ||
} | ||
} | ||
}() | ||
} | ||
} | ||
|
||
func processMessageFromSession(ctx context.Context, receiver *azservicebus.SessionReceiver, receivedMsg *azservicebus.ReceivedMessage) error { | ||
fmt.Printf("Received message for session %s, message ID %s\n", *receivedMsg.SessionID, receivedMsg.MessageID) | ||
return receiver.CompleteMessage(ctx, receivedMsg, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.