ArmorIQ LogoArmorIQ SDK

Connection Pooling

Reuse SDK clients with a simple pool.

Connection Pooling

from armoriq_sdk import ArmorIQClient
import threading

class ArmorIQClientPool:
    def __init__(self, api_key, user_id, agent_id, pool_size=5):
        self.pool = [
            ArmorIQClient(api_key=api_key, user_id=user_id, agent_id=agent_id)
            for _ in range(pool_size)
        ]
        self.lock = threading.Lock()
        self.available = list(self.pool)

    def get_client(self):
        with self.lock:
            if self.available:
                return self.available.pop()
            # Pool exhausted, create new client
            return ArmorIQClient(...)

    def return_client(self, client):
        with self.lock:
            self.available.append(client)

# Usage
pool = ArmorIQClientPool(api_key="...", user_id="...", agent_id="...", pool_size=10)

def process_task(task):
    client = pool.get_client()
    try:
        # Use client
        result = client.invoke(...)
        return result
    finally:
        pool.return_client(client)
import { ArmorIQClient } from '@armoriq/sdk';

class ArmorIQClientPool {
  private pool: ArmorIQClient[] = [];
  private available: ArmorIQClient[] = [];
  private apiKey: string;
  private userId: string;
  private agentId: string;

  constructor(apiKey: string, userId: string, agentId: string, poolSize: number = 5) {
    this.apiKey = apiKey;
    this.userId = userId;
    this.agentId = agentId;
    
    for (let i = 0; i < poolSize; i++) {
      const client = new ArmorIQClient({ apiKey, userId, agentId });
      this.pool.push(client);
      this.available.push(client);
    }
  }

  getClient(): ArmorIQClient {
    if (this.available.length > 0) {
      return this.available.pop()!;
    }
    // Pool exhausted, create new client
    return new ArmorIQClient({
      apiKey: this.apiKey,
      userId: this.userId,
      agentId: this.agentId
    });
  }

  returnClient(client: ArmorIQClient): void {
    this.available.push(client);
  }

  closeAll(): void {
    this.pool.forEach(client => client.close());
    this.pool = [];
    this.available = [];
  }
}

// Usage
const pool = new ArmorIQClientPool(
  process.env.ARMORIQ_API_KEY!,
  process.env.USER_ID!,
  process.env.AGENT_ID!,
  10
);

async function processTask(task: any) {
  const client = pool.getClient();
  try {
    // Use client
    const result = await client.invoke('mcp', 'action', token, {});
    return result;
  } finally {
    pool.returnClient(client);
  }
}

On this page