Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Commercetools Async Pool Laravel Package

bestit/commercetools-async-pool

Laravel-friendly async pool for commercetools PHP SDK requests. Schedule, batch, and execute API calls concurrently with configurable limits, retries, and callbacks, helping speed up imports, sync jobs, and background processing while keeping resource usage under control.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require bestit/commercetools-async-pool
    
  2. Basic Configuration Require the package in your project:

    use Bestit\CommercetoolsAsyncPool\AsyncPool;
    use Bestit\CommercetoolsAsyncPool\Client\CommercetoolsClient;
    
  3. First Use Case: Batch API Requests Initialize the pool with a CommercetoolsClient (assuming you have a ClientBuilder or similar):

    $client = new CommercetoolsClient($clientBuilder);
    $pool = new AsyncPool($client, 5); // 5 concurrent requests
    

    Queue requests (e.g., fetching products in parallel):

    $requests = [
        $client->getRequest('GET', '/products/1'),
        $client->getRequest('GET', '/products/2'),
        // ...
    ];
    
    $results = $pool->execute($requests);
    

Implementation Patterns

Workflow: Parallel Processing

  1. Request Batching Group related API calls (e.g., fetching product variants, inventory updates) into a single batch:

    $requests = collect($productIds)->map(fn($id) =>
        $client->getRequest('GET', "/products/{$id}/variants")
    );
    $pool->execute($requests->toArray());
    
  2. Error Handling Use execute() with a callback for error handling:

    $results = $pool->execute($requests, function($request, $response) {
        if ($response->isError()) {
            logger()->error("Failed: {$request->getUrl()}", ['error' => $response->getBody()]);
        }
    });
    
  3. Integration with Laravel Queues Dispatch long-running batches to a queue job:

    class ProcessProductBatch implements ShouldQueue
    {
        public function handle()
        {
            $pool = new AsyncPool($this->client, 10);
            $results = $pool->execute($this->requests);
            // Process results...
        }
    }
    

Advanced Patterns

  • Dynamic Pool Sizing Adjust concurrency based on system load (e.g., via config):

    $pool = new AsyncPool($client, config('async_pool.max_concurrency'));
    
  • Retry Logic Combine with a retry package (e.g., spatie/laravel-queue-retries) for transient failures.


Gotchas and Tips

Pitfalls

  1. Rate Limiting Commercetools enforces rate limits. Monitor response headers (X-RateLimit-Remaining) and throttle requests if needed.

  2. Memory Leaks Large batches may exhaust memory. Use chunking:

    $chunkedRequests = array_chunk($requests, 100);
    foreach ($chunkedRequests as $chunk) {
        $pool->execute($chunk);
    }
    
  3. Idempotency Ensure requests are idempotent (e.g., PATCH instead of POST for updates) to avoid duplicate side effects.

Debugging Tips

  • Logging Enable debug logging for the CommercetoolsClient to inspect raw requests/responses:

    $client = new CommercetoolsClient($clientBuilder, [
        'debug' => true,
    ]);
    
  • Timeouts Set timeouts on the underlying HTTP client (e.g., Guzzle) to avoid hanging:

    $clientBuilder->setHttpClient(new \GuzzleHttp\Client([
        'timeout' => 30,
    ]));
    

Extension Points

  1. Custom Response Handling Extend AsyncPool to transform responses:

    $pool = new AsyncPool($client, 5, new class {
        public function transform($response) {
            return json_decode($response->getBody(), true);
        }
    });
    
  2. Progress Tracking Use Laravel’s ProgressBar for CLI feedback:

    use Symfony\Component\Console\Helper\ProgressBar;
    
    $progress = new ProgressBar($output, count($requests));
    $results = $pool->execute($requests, function() use ($progress) {
        $progress->advance();
    });
    
  3. Middleware Inject middleware (e.g., auth tokens) via the CommercetoolsClient constructor.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor