Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d83bd5d
feat: add pool configuration constants
Mosas2000 May 12, 2026
751e3c9
feat: add pool configuration parser
Mosas2000 May 12, 2026
b7554d2
feat: apply pool configuration to Pool instance
Mosas2000 May 12, 2026
f52288c
feat: integrate pool config into event store factory
Mosas2000 May 12, 2026
109b720
docs: add pool configuration environment variables
Mosas2000 May 12, 2026
ed7cd78
test: add parsePoolConfig unit tests
Mosas2000 May 12, 2026
a9943cb
test: add pool config integration test
Mosas2000 May 12, 2026
3a96a1e
docs: create deployment guide for pool configuration
Mosas2000 May 12, 2026
8b6b6e8
feat: add validation warning for excessive pool size
Mosas2000 May 12, 2026
3c5983b
test: add validation warning test case
Mosas2000 May 12, 2026
fbe617e
refactor: export pool configuration constants
Mosas2000 May 12, 2026
371bfe5
test: verify pool configuration default constants
Mosas2000 May 12, 2026
bbef92f
docs: add inline documentation for parsePoolConfig
Mosas2000 May 12, 2026
37b5149
docs: create README with pool configuration section
Mosas2000 May 12, 2026
e3552c2
docs: add environment configuration examples
Mosas2000 May 12, 2026
52ff350
feat: include pool config in health check response
Mosas2000 May 12, 2026
3f2fc5d
docs: add inline comments for pool parameters
Mosas2000 May 12, 2026
69a591c
docs: update package description
Mosas2000 May 12, 2026
738b4ee
docs: add best practices to deployment guide
Mosas2000 May 12, 2026
e4daaa3
docs: add comprehensive change summary
Mosas2000 May 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions chainhook/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ CHAINHOOK_STORAGE=postgres
DATABASE_URL=
CHAINHOOK_RETENTION_DAYS=30

# PostgreSQL Pool Configuration
DB_POOL_MAX=20
DB_POOL_IDLE_TIMEOUT_MS=30000
DB_POOL_CONNECTION_TIMEOUT_MS=5000
DB_STATEMENT_TIMEOUT_MS=30000

# CORS Security - Comma-separated list of allowed origins
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001

Expand Down
101 changes: 101 additions & 0 deletions chainhook/DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Chainhook Service Deployment Guide

## PostgreSQL Pool Configuration

The chainhook service uses connection pooling to manage database connections efficiently. Proper pool configuration is essential for production deployments to prevent connection exhaustion and ensure optimal performance.

### Configuration Options

The following environment variables control PostgreSQL pool behavior:

#### DB_POOL_MAX
Maximum number of connections in the pool.

- **Default**: 20
- **Recommended**: 10-50 depending on workload
- **Considerations**:
- Higher values allow more concurrent requests but consume more database resources
- Should not exceed your PostgreSQL max_connections setting
- Consider your application's concurrency requirements

#### DB_POOL_IDLE_TIMEOUT_MS
Time in milliseconds before an idle connection is closed.

- **Default**: 30000 (30 seconds)
- **Recommended**: 30000-60000
- **Considerations**:
- Shorter timeouts free up connections faster but may cause reconnection overhead
- Longer timeouts reduce reconnection overhead but may hold connections unnecessarily

#### DB_POOL_CONNECTION_TIMEOUT_MS
Maximum time in milliseconds to wait for a connection from the pool.

- **Default**: 5000 (5 seconds)
- **Recommended**: 3000-10000
- **Considerations**:
- Shorter timeouts fail fast under load
- Longer timeouts may cause request queuing during high traffic

#### DB_STATEMENT_TIMEOUT_MS
Maximum time in milliseconds for a query to execute.

- **Default**: 30000 (30 seconds)
- **Recommended**: 10000-60000 depending on query complexity
- **Considerations**:
- Prevents long-running queries from blocking connections
- Should be tuned based on your slowest expected query
- Too short may cause legitimate queries to fail

### Example Configuration

```bash
# Production settings for moderate load
DB_POOL_MAX=25
DB_POOL_IDLE_TIMEOUT_MS=45000
DB_POOL_CONNECTION_TIMEOUT_MS=7000
DB_STATEMENT_TIMEOUT_MS=30000
```

```bash
# High-traffic production settings
DB_POOL_MAX=50
DB_POOL_IDLE_TIMEOUT_MS=60000
DB_POOL_CONNECTION_TIMEOUT_MS=10000
DB_STATEMENT_TIMEOUT_MS=45000
```

```bash
# Development settings
DB_POOL_MAX=10
DB_POOL_IDLE_TIMEOUT_MS=30000
DB_POOL_CONNECTION_TIMEOUT_MS=5000
DB_STATEMENT_TIMEOUT_MS=30000
```

### Monitoring

Monitor these metrics to tune your pool configuration:

- Connection pool utilization
- Connection wait times
- Query execution times
- Connection errors and timeouts

### Troubleshooting

**Connection pool exhausted**: Increase `DB_POOL_MAX` or reduce `DB_POOL_IDLE_TIMEOUT_MS`

**Slow response times**: Check if `DB_POOL_CONNECTION_TIMEOUT_MS` is being exceeded

**Query timeouts**: Increase `DB_STATEMENT_TIMEOUT_MS` or optimize slow queries

**Database connection limit reached**: Reduce `DB_POOL_MAX` across all service instances

### Best Practices

1. Start with default values and adjust based on monitoring data
2. Set `DB_POOL_MAX` lower than your database's max_connections limit
3. Monitor connection pool metrics in production
4. Use longer timeouts for batch operations
5. Test pool configuration under expected load before deploying
6. Document any custom pool settings in your deployment notes
85 changes: 85 additions & 0 deletions chainhook/POOL_CONFIG_CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# PostgreSQL Pool Configuration Changes

## Summary

Added explicit PostgreSQL connection pool sizing and timeout settings to address issue #347.

## Changes Made

### Code Changes

1. **storage.js**
- Added pool configuration constants (max, idle timeout, connection timeout, statement timeout)
- Created `parsePoolConfig()` function to parse environment variables
- Updated `PostgresEventStore` constructor to accept and apply pool configuration
- Updated `createEventStore()` factory to pass pool configuration
- Added pool configuration to health check response
- Added validation warning for excessive pool sizes

2. **storage.test.js**
- Added comprehensive tests for `parsePoolConfig()` function
- Added tests for pool configuration integration
- Added tests for default constants
- Added test for validation warning
- All 101 tests passing

### Configuration

3. **.env.example**
- Added `DB_POOL_MAX` (default: 20)
- Added `DB_POOL_IDLE_TIMEOUT_MS` (default: 30000)
- Added `DB_POOL_CONNECTION_TIMEOUT_MS` (default: 5000)
- Added `DB_STATEMENT_TIMEOUT_MS` (default: 30000)

### Documentation

4. **DEPLOYMENT.md** (new)
- Comprehensive deployment guide for pool configuration
- Detailed explanation of each configuration option
- Example configurations for different environments
- Monitoring recommendations
- Troubleshooting guide
- Best practices

5. **README.md** (new)
- Service overview
- Configuration guide with pool settings
- API endpoints documentation
- Quick start instructions

6. **examples/.env.production** (new)
- Production environment configuration example
- Optimized pool settings for high-traffic scenarios

7. **examples/.env.development** (new)
- Development environment configuration example
- Relaxed settings for local development

## Acceptance Criteria

- [x] Add explicit pool sizing and timeout configuration
- [x] Document the defaults in the deployment guide
- [x] Add tests for the configured pool options

## Default Values

- `DB_POOL_MAX`: 20 connections
- `DB_POOL_IDLE_TIMEOUT_MS`: 30000ms (30 seconds)
- `DB_POOL_CONNECTION_TIMEOUT_MS`: 5000ms (5 seconds)
- `DB_STATEMENT_TIMEOUT_MS`: 30000ms (30 seconds)

## Benefits

1. Prevents connection exhaustion under load
2. Protects against slow or hanging queries
3. Provides predictable connection behavior
4. Enables production tuning based on workload
5. Improves observability through health endpoint

## Testing

All tests pass (101/101):
- Unit tests for configuration parsing
- Integration tests for pool configuration
- Validation tests for edge cases
- Full test suite verification
80 changes: 80 additions & 0 deletions chainhook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# TipStream Chainhook Service

Webhook listener for TipStream on-chain events from the Stacks blockchain.

## Features

- Event ingestion from chainhook webhooks
- PostgreSQL and in-memory storage backends
- Event deduplication
- Rate limiting and authentication
- Metrics and health endpoints
- Configurable connection pooling

## Configuration

### Storage

Set `CHAINHOOK_STORAGE` to either `postgres` or `memory`.

For PostgreSQL, provide `DATABASE_URL`:

```bash
DATABASE_URL=postgresql://user:password@localhost:5432/tipstream
```

### Connection Pool

Configure PostgreSQL connection pooling for production deployments:

```bash
DB_POOL_MAX=20 # Maximum connections
DB_POOL_IDLE_TIMEOUT_MS=30000 # Idle connection timeout
DB_POOL_CONNECTION_TIMEOUT_MS=5000 # Connection acquisition timeout
DB_STATEMENT_TIMEOUT_MS=30000 # Query execution timeout
```

See [DEPLOYMENT.md](./DEPLOYMENT.md) for detailed pool configuration guidance.

### Authentication

Set `CHAINHOOK_AUTH_TOKEN` to secure the webhook endpoint:

```bash
CHAINHOOK_AUTH_TOKEN=your-secret-token
```

### Rate Limiting

Configure request rate limits:

```bash
RATE_LIMIT_MAX_REQUESTS=100
RATE_LIMIT_WINDOW_MS=60000
```

## Running

```bash
npm start
```

## Testing

```bash
npm test
```

## API Endpoints

- `POST /api/chainhook/events` - Ingest events from chainhook
- `GET /api/tips` - List recent tips
- `GET /api/tips/:id` - Get tip by ID
- `GET /api/tips/user/:address` - Get tips for user
- `GET /api/stats` - Platform statistics
- `GET /health` - Health check
- `GET /metrics` - Prometheus metrics

## Environment Variables

See [.env.example](./.env.example) for all available configuration options.
31 changes: 31 additions & 0 deletions chainhook/examples/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Development Environment Configuration

PORT=3100

# Authentication - Optional for local development
CHAINHOOK_AUTH_TOKEN=

# Storage
CHAINHOOK_STORAGE=postgres
DATABASE_URL=postgresql://localhost:5432/tipstream_dev
CHAINHOOK_RETENTION_DAYS=7

# PostgreSQL Pool - Development Settings
DB_POOL_MAX=10
DB_POOL_IDLE_TIMEOUT_MS=30000
DB_POOL_CONNECTION_TIMEOUT_MS=5000
DB_STATEMENT_TIMEOUT_MS=30000

# CORS
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001

# Rate Limiting - Relaxed for development
RATE_LIMIT_MAX_REQUESTS=1000
RATE_LIMIT_WINDOW_MS=60000

# Logging
LOG_LEVEL=DEBUG

# Metrics
METRICS_AUTH_TOKEN=
HEALTH_CHECK_ALWAYS_ENABLED=true
32 changes: 32 additions & 0 deletions chainhook/examples/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Production Environment Configuration

PORT=3100

# Authentication
CHAINHOOK_AUTH_TOKEN=your-production-token-here

# Storage
CHAINHOOK_STORAGE=postgres
DATABASE_URL=postgresql://user:password@db.example.com:5432/tipstream
DATABASE_SSL=true
CHAINHOOK_RETENTION_DAYS=90

# PostgreSQL Pool - Production Settings
DB_POOL_MAX=50
DB_POOL_IDLE_TIMEOUT_MS=60000
DB_POOL_CONNECTION_TIMEOUT_MS=10000
DB_STATEMENT_TIMEOUT_MS=45000

# CORS
CORS_ALLOWED_ORIGINS=https://tipstream.example.com

# Rate Limiting
RATE_LIMIT_MAX_REQUESTS=200
RATE_LIMIT_WINDOW_MS=60000

# Logging
LOG_LEVEL=INFO

# Metrics
METRICS_AUTH_TOKEN=your-metrics-token-here
HEALTH_CHECK_ALWAYS_ENABLED=true
2 changes: 1 addition & 1 deletion chainhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"engines": {
"node": ">=18"
},
"description": "Chainhook webhook listener for TipStream on-chain events",
"description": "Chainhook webhook listener for TipStream on-chain events with configurable PostgreSQL connection pooling",
"dependencies": {
"pg": "^8.20.0"
}
Expand Down
Loading
Loading