---
node_type: semaphore_acquire
category: control
---

# Semaphore Acquire

Blocks workflow execution until a semaphore permit is available, then passes through input data unchanged. Used as a workflow gate to control concurrency.

## Overview

The Semaphore Acquire node creates or uses an existing named semaphore and acquires a permit for the current job. If no permits are available, the node blocks (waits) until one becomes available or a configurable timeout occurs.

When the timeout occurs before acquiring a permit, the node executes the optional `waiting` workflow. This allows you to notify users or perform fallback actions when resources are busy. If no `waiting` workflow is connected, the node returns empty outputs.

**Key Features:**
- Blocks until permit is available or timeout occurs
- Configurable timeout with waiting workflow support
- Associates permits with job_id for automatic cleanup
- Supports key template interpolation for dynamic semaphore naming
- Passes through input data unchanged

## When to Use

Use Semaphore Acquire when you need to:
- Prevent race conditions (e.g., processing messages from the same user sequentially)
- Limit concurrent access to external APIs (rate limiting)
- Coordinate access to shared resources across workflows
- Implement mutual exclusion for critical sections

## Configuration

### Fields

| Field | Type | Required | Default | Description |
|-------|------|----------|---------|-------------|
| key_template | String | Yes | - | Template for semaphore key. Use {{ field }} syntax for interpolation from input. |
| permits | Integer | No | 1 | Maximum number of permits for this semaphore (1-100). First acquire sets this permanently. |
| timeout_seconds | Integer | No | 30 | Timeout in seconds before executing waiting workflow (1-3600). Set to high value for near-indefinite wait. |

### Key Template Interpolation

The key_template supports dot-notation interpolation from the input object:

- `"telegram/{{ chat.id }}"` with input `{ "chat": { "id": "123" } }` → `"telegram/123"`
- `"user/{{ user_id }}"` with input `{ "user_id": "456" }` → `"user/456"`
- `"api-rate-limit"` (no interpolation) → `"api-rate-limit"`

## Inputs

| Socket | Type | Required | Description |
|--------|------|----------|-------------|
| input | Any | Yes | Input data to pass through. Also used as context for key template interpolation. |

## Outputs

| Socket | Type | Description |
|--------|------|-------------|
| output | Any | Same value as input (passthrough) |
| waiting | Any | Executes connected workflow when timeout occurs before permit acquisition |

## Example: Telegram Message Handler

Prevent processing multiple messages from the same chat concurrently:

```json
{
  "nodes": [
    {
      "id": "gate",
      "type": "semaphore_acquire",
      "config": {
        "key_template": "telegram/{{ chat.id }}",
        "permits": 1
      },
      "inputs": {
        "input": "{{ trigger }}"
      }
    },
    {
      "id": "process",
      "type": "ai_agent",
      "inputs": {
        "input": "{{ gate.output }}"
      }
    }
  ]
}
```

**Execution Flow:**
1. Message arrives for chat.id = 123
2. SemaphoreAcquire creates/acquires permit for key "telegram/123"
3. If another job holds the permit, this node blocks until released or timeout occurs
4. Once acquired, passes trigger data to process node
5. When job completes, permit is automatically released

## Example: API Rate Limiting

Limit concurrent calls to an external API:

```json
{
  "nodes": [
    {
      "id": "rate_limit",
      "type": "semaphore_acquire",
      "config": {
        "key_template": "external-api",
        "permits": 5
      },
      "inputs": {
        "input": "{{ trigger }}"
      }
    },
    {
      "id": "call_api",
      "type": "http_get",
      "inputs": {
        "url": "https://api.example.com/data"
      }
    }
  ]
}
```

**Note:** Maximum 5 concurrent API calls across all jobs.

## Example: Message Queueing with Timeout

Show how to notify users when their message is queued:

```json
{
  "nodes": [
    {
      "id": "gate",
      "type": "semaphore_acquire",
      "config": {
        "key_template": "telegram/{{ chat.id }}",
        "permits": 1,
        "timeout_seconds": 10
      },
      "inputs": {
        "input": "{{ trigger }}"
      }
    },
    {
      "id": "notify_queued",
      "type": "telegram_send",
      "inputs": {
        "chat_id": "{{ gate.waiting.chat.id }}",
        "text": "Your message is queued and will be processed soon..."
      }
    },
    {
      "id": "process",
      "type": "ai_agent",
      "inputs": {
        "input": "{{ gate.output }}"
      }
    }
  ]
}
```

**Execution Flow:**
1. Message arrives while another message is being processed
2. After 10 seconds without acquiring a permit, timeout occurs
3. The `waiting` output triggers `notify_queued` to inform the user
4. When the permit eventually becomes available, `output` triggers processing

## Automatic Cleanup

Permits are automatically released when:
- The job completes successfully
- The job fails
- The job is cancelled

Explicit release via SemaphoreRelease node is optional.

## Related Nodes

- [semaphore_release](./semaphore_release.md) - Explicitly release a permit before job completion
