AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
Drain Pipeline Reference
The drain pipeline wraps any adapter to add batching, retry with backoff, and buffer overflow protection. Use it in production to reduce network overhead and handle transient failures.
When to Recommend
- The user has a drain adapter (Axiom, OTLP, custom) and is deploying to production
- High-throughput scenarios where one HTTP request per event is wasteful
- The user needs retry logic for unreliable backends
- The user is implementing batching manually with
setIntervaland arrays
Basic Setup
// server/plugins/evlog-drain.ts
import type { DrainContext } from 'evlog'
import { createDrainPipeline } from 'evlog/pipeline'
import { createAxiomDrain } from 'evlog/axiom'
export default defineNitroPlugin((nitroApp) => {
const pipeline = createDrainPipeline<DrainContext>()
const drain = pipeline(createAxiomDrain())
nitroApp.hooks.hook('evlog:drain', drain)
nitroApp.hooks.hook('close', () => drain.flush())
})
Important: Always call drain.flush() on server close hook. Without it, buffered events are lost when the process exits.
Full Configuration
const pipeline = createDrainPipeline<DrainContext>({
batch: {
size: 50, // Max events per batch (default: 50)
intervalMs: 5000, // Max wait before flushing partial batch (default: 5000)
},
retry: {
maxAttempts: 3, // Total attempts including first (default: 3)
backoff: 'exponential', // 'exponential' | 'linear' | 'fixed' (default: 'exponential')
initialDelayMs: 1000, // Base delay for first retry (default: 1000)
maxDelayMs: 30000, // Upper bound for any retry delay (default: 30000)
},
maxBufferSize: 1000, // Max buffered events; oldest dropped on overflow (default: 1000)
onDropped: (events, error) => {
// Called when events are dropped (overflow or retry exhaustion)
console.error(`[evlog] Dropped ${events.length} events:`, error?.message)
},
})
How It Works
drain(ctx)pushes a single event into the buffer- When
buffer.length >= batch.size, the batch is flushed immediately - If the batch isn't full, a timer starts; after
intervalMs, whatever is buffered gets flushed - On flush, the drain function receives
T[](always an array) - If the drain throws, the batch is retried with the configured backoff
- After
maxAttemptsfailures,onDroppedis called and the batch is discarded - If the buffer exceeds
maxBufferSize, the oldest event is dropped andonDroppedis called
Backoff Strategies
| Strategy | Delay Pattern | Best For |
|---|---|---|
exponential |
1s, 2s, 4s, 8s... | Default. Transient failures needing recovery time |
linear |
1s, 2s, 3s, 4s... | Predictable delay growth |
fixed |
1s, 1s, 1s, 1s... | Rate-limited APIs with known cooldown |
Returned Drain Function API
const drain = pipeline(myDrainFn)
drain(ctx) // Push a single event (synchronous, non-blocking)
await drain.flush() // Force-flush all buffered events
drain.pending // Number of events currently buffered (readonly)
Common Patterns
With multiple adapters
const axiom = createAxiomDrain()
const otlp = createOTLPDrain()
const pipeline = createDrainPipeline<DrainContext>()
const drain = pipeline(async (batch) => {
await Promise.allSettled([axiom(batch), otlp(batch)])
})
Custom drain function
const pipeline = createDrainPipeline<DrainContext>({ batch: { size: 100 } })
const drain = pipeline(async (batch) => {
await fetch('https://your-service.com/logs', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(batch.map(ctx => ctx.event)),
})
})
Low-traffic with longer interval
const pipeline = createDrainPipeline<DrainContext>({
batch: { size: 10, intervalMs: 30000 }, // Flush every 30s or 10 events
})
Anti-Patterns
Manual batching with setInterval
// ❌ No retry, no overflow protection, no flush on shutdown
const batch: WideEvent[] = []
setInterval(() => {
if (batch.length > 0) fetch(...)
}, 5000)
Transform to:
// ✅ Use the pipeline
const pipeline = createDrainPipeline<DrainContext>()
const drain = pipeline(async (batch) => { await fetch(...) })
nitroApp.hooks.hook('close', () => drain.flush())
Missing flush on shutdown
// ❌ Buffered events lost on process exit
nitroApp.hooks.hook('evlog:drain', drain)
Fix:
// ✅ Always flush on close
nitroApp.hooks.hook('evlog:drain', drain)
nitroApp.hooks.hook('close', () => drain.flush())
Review Checklist
- Pipeline wraps the adapter for production use
-
drain.flush()called on serverclosehook -
onDroppedcallback logs or reports dropped events - Batch size and interval are appropriate for the traffic volume
-
maxBufferSizeis set to prevent memory leaks under load