Skip to content

Remove provider from queue when node is turned off #435

Open
@chibie

Description

@chibie

User Story
As a developer, I want providers to be immediately removed from the queue when their node is turned off, so that the system does not route orders to unavailable providers.

Acceptance Criteria

  1. GIVEN a provider's IsAvailable status is set to false
    WHEN the provider profile is updated
    THEN the provider should be removed from all relevant Redis queues.

  2. GIVEN a provider is removed from the queue
    WHEN the Redis operation fails
    THEN an error should be logged, and the provider profile update should still proceed.

Tech Details

  1. Modify the UpdateProviderProfile function to:

    • Check if IsAvailable is being set to false.
    • If IsAvailable is set to false, search for and remove the provider from all relevant Redis queues.
  2. Use the provided Redis logic to:

    • Search for the provider in the queue using LIndex.
    • Replace the provider's entry with a placeholder (DELETED_PROVIDER).
    • Remove all occurrences of the placeholder using LRem.
  3. Ensure the Redis operations are atomic and do not block the provider profile update.

Example removal from redis:

// Remove provider from Redis queues
redisKey := fmt.Sprintf("bucket_%s_%s_%s", provider.Edges.Currency.Code, provider.Edges.ProvisionBucket.MinAmount, provider.Edges.ProvisionBucket.MaxAmount)

for index := -1; ; index-- {
	providerData, err := storage.RedisClient.LIndex(ctx, redisKey, int64(index)).Result()
	if err != nil {
		break
	}

	parts := strings.Split(providerData, ":")
	if len(parts) != 5 {
		logger.Errorf("invalid provider data format: %s", providerData)
		continue
	}

	if parts[0] == provider.ID {
		placeholder := "DELETED_PROVIDER"
		_, err := storage.RedisClient.LSet(ctx, redisKey, int64(index), placeholder).Result()
		if err != nil {
			logger.Errorf("failed to set placeholder at index %d: %v", index, err)
		}

		_, err = storage.RedisClient.LRem(ctx, redisKey, 0, placeholder).Result()
		if err != nil {
			logger.Errorf("failed to remove placeholder from circular queue: %v", err)
		}

		break
	}
}

Notes/Assumptions

  1. The Redis queue key format is bucket_<currency>_<minAmount>_<maxAmount>.
  2. The provider data in Redis is stored in the format providerID:token:rate:minAmount:maxAmount.
  3. The feature should handle cases where the provider is not found in the queue.

Open Questions

  1. Should we notify the provider when they are removed from the queue due to unavailability?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions