Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:A616567126 GPT WEB JAVA Task Queueing

From Leeroopedia
Knowledge Sources
Domains Distributed_Systems, Concurrency
Last Updated 2026-10-13 00:00 GMT

Overview

Pattern that decouples task submission from execution by placing tasks into a queue, allowing controlled sequential or parallel processing with backpressure management.

Description

Task queueing separates the act of requesting work from the act of performing it. Producers submit tasks to a queue, and consumers process them at their own pace. This decoupling provides backpressure (rejecting tasks when the queue is full), ordering guarantees, and allows the processing rate to differ from the submission rate.

In this repository, two queueing systems exist:

  • Redis-backed queue for Stable Diffusion: QueueUtil uses a Redis list (user_queue) with FIFO semantics. A scheduled task (MyTask) polls the queue every 5 seconds and processes the head item.
  • Thread pool queue for Midjourney: DiscordInstanceImpl uses ThreadPoolTaskExecutor with a bounded queue. Tasks are submitted to the thread pool and executed with per-account concurrency limits.

Usage

Use this principle when:

  • Processing takes longer than the client can wait (async processing)
  • Needing to limit concurrency per resource (e.g., one Discord account)
  • Requiring backpressure when the system is overloaded

Theoretical Basis

FIFO Queue with Backpressure:

# Abstract algorithm (NOT real implementation)
def add_to_queue(task):
    if queue.size() >= MAX_SIZE:
        raise QueueFullError()
    queue.push_right(task)

def process_queue():
    if queue.size() > 0:
        task = queue.peek(0)
        try:
            result = execute(task)
            queue.remove(task)
        except Exception:
            refund_credits(task.user)
            queue.remove(task)

Key Properties:

  • FIFO ordering: First-in, first-out via redisTemplate.opsForList().rightPush()
  • Bounded capacity: CommonConst.QUEUE_SIZE limits queue length
  • Duplicate detection: getPosition() checks if identical request already queued
  • Atomic processing: synchronized on createImage prevents race conditions

Practical Guide

For Stable Diffusion:

  1. QueueUtil.addUserToQueue(value) pushes serialized request to Redis list
  2. MyTask.run() (every 5s) reads the head item, calls SD WebUI API, and removes it
  3. QueueUtil.getPosition(value) lets clients poll their position in the queue
  4. SdController.getQueue() returns queue state (1=queuing with position, 2=generating with progress)

For Midjourney:

  1. DiscordInstanceImpl.submitTask() saves the task and submits to ThreadPoolTaskExecutor
  2. If the thread pool queue is full, RejectedExecutionException triggers task removal
  3. executeTask() runs the Discord API call and waits for WSS response

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment