func()

in v2/lockrenewer.go [181:217]


func (plr *peekLockRenewer) renewMessageLock(ctx context.Context, message *azservicebus.ReceivedMessage, options *azservicebus.RenewMessageLockOptions) error {
	span := trace.SpanFromContext(ctx)
	lockLostErr := &azservicebus.Error{Code: azservicebus.CodeLockLost}
	if message.LockedUntil == nil || time.Until(*message.LockedUntil) < 0 {
		// if the lock doesn't exist or is already expired, we should not attempt to renew it.
		return lockLostErr
	}
	renewalTimeout := time.Until(*message.LockedUntil)
	if *plr.renewalTimeout > 0 {
		renewalTimeout = *plr.renewalTimeout
	}
	// we should keep retrying until lock expiry or until message context is done
	for time.Until(*message.LockedUntil) > 0 && ctx.Err() == nil {
		var renewErr error
		func() {
			getLogger(ctx).Info(fmt.Sprintf("renewing lock with timeout: %s", renewalTimeout))
			renewalCtx, cancel := context.WithTimeout(ctx, renewalTimeout)
			defer cancel()
			renewErr = plr.lockRenewer.RenewMessageLock(renewalCtx, message, options)
		}()
		if renewErr != nil {
			getLogger(ctx).Error(fmt.Sprintf("failed to renew lock: %s", renewErr))
			span.AddEvent("failed to renew message lock", trace.WithAttributes(attribute.String("errorDetails", renewErr.Error())))
		}
		// exit the renewal if no error or if we get any error other than context deadline exceeded
		if !errors.Is(renewErr, context.DeadlineExceeded) {
			return renewErr
		}
		// renewErr is context.DeadlineExceeded, increment metric and retry
		plr.metrics.IncMessageLockRenewalTimeoutCount(message)
	}
	// lock is expired or message context is done
	if ctx.Err() != nil {
		return ctx.Err()
	}
	return lockLostErr
}