Connection Pooling
Reuse SDK clients with a simple pool.
Connection Pooling
The ArmorIQ SDK maintains an internal HTTP connection pool per client. In most cases, a single shared client instance is the right answer — see Client Lifecycle Management.
If you specifically need a pool of client instances (e.g. sharding across API keys or isolating per-tenant token caches), here's a minimal pattern:
from armoriq_sdk import ArmorIQClient
import threading
class ArmorIQClientPool:
def __init__(self, api_key, pool_size=5):
self.api_key = api_key
self.pool = [ArmorIQClient(api_key=api_key) 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(api_key=self.api_key)
def return_client(self, client):
with self.lock:
self.available.append(client)
# Usage
pool = ArmorIQClientPool(api_key="ak_live_...", pool_size=10)
def process_task(user_email, task):
client = pool.get_client()
try:
scope = client.for_user(user_email)
return scope.invoke(...)
finally:
pool.return_client(client)import { ArmorIQClient } from '@armoriq/sdk';
class ArmorIQClientPool {
private pool: ArmorIQClient[] = [];
private available: ArmorIQClient[] = [];
private apiKey: string;
constructor(apiKey: string, poolSize: number = 5) {
this.apiKey = apiKey;
for (let i = 0; i < poolSize; i++) {
const client = new ArmorIQClient({ apiKey });
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 });
}
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!, 10);
async function processTask(userEmail: string, task: any) {
const client = pool.getClient();
try {
const scope = client.forUser(userEmail);
return await scope.invoke('mcp', 'action', token, {});
} finally {
pool.returnClient(client);
}
}Per-user state (policy context, approver chain) lives on the
for_user(email) scope, not on the client, so one pool of clients can
safely serve many users.