Skip to content

Commit

Permalink
add timeout to all settlement calls in go-shuttle (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
karenychen authored Aug 7, 2024
1 parent ae4c59a commit 9be33d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion v2/managedsettling.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (m *ManagedSettler) Handle(ctx context.Context, settler MessageSettler, mes
m.options.OnError(ctx, m.options, settler, message, err)
return
}
if err := settler.CompleteMessage(ctx, message, nil); err != nil {
settleCtx, cancel := context.WithTimeout(ctx, settlementTimeout)
defer cancel()
if err := settler.CompleteMessage(settleCtx, message, nil); err != nil {
logger.Error(fmt.Sprintf("error completing message: %s", err))
m.options.OnAbandoned(ctx, message, err)
return
Expand Down
8 changes: 7 additions & 1 deletion v2/settlehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package shuttle
import (
"context"
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
"github.com/devigned/tab"
)

const settlementTimeout = 30 * time.Second

// Settlement represents an action to take on a message. Abandon, Complete, DeadLetter, Defer, NoOp
type Settlement interface {
Settle(context.Context, MessageSettler, *azservicebus.ReceivedMessage)
Expand Down Expand Up @@ -107,7 +110,10 @@ type settlement[T any] struct {
func (s settlement[T]) settle(ctx context.Context, settler MessageSettler, message *azservicebus.ReceivedMessage, options T) {
span := tab.FromContext(ctx)
span.Logger().Info(fmt.Sprintf("%s message", s.name))
if err := s.settleFunc(ctx, settler, message, options); err != nil {
settleCtx, cancel := context.WithTimeout(ctx, settlementTimeout)
defer cancel()
getLogger(ctx).Info(fmt.Sprintf("%s message with ID: %s", s.name, message.MessageID))
if err := s.settleFunc(settleCtx, settler, message, options); err != nil {
wrapped := fmt.Errorf("%s settlement failed: %w", s.name, err)
getLogger(ctx).Error(wrapped.Error())
span.Logger().Error(wrapped)
Expand Down

0 comments on commit 9be33d2

Please sign in to comment.