This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
mongobetween is a lightweight MongoDB connection pooler written in Go by Coinbase. It handles a large number of incoming connections and multiplexes them across a smaller connection pool to MongoDB clusters. The proxy appears to applications as an always-available MongoDB shard router (mongos).
# Build the binary
make build
# or
go build -o bin/mongobetween .
# Run tests with race detection
make test
# or
go test -count 1 -race ./...
# Run linter
make lint
# or
GOGC=75 golangci-lint run --timeout 10m --concurrency 32 -v -E golint ./...
# Run with Docker
make docker
# or
docker-compose up# Basic TCP socket example
./bin/mongobetween ":27016=mongodb+srv://username:password@cluster.mongodb.net/database?maxpoolsize=10&label=cluster0"
# Unix socket example
./bin/mongobetween -network unix "/tmp/mongo.sock=mongodb+srv://username:password@cluster.mongodb.net/database?maxpoolsize=10&label=cluster0"Main Entry Point (mongobetween.go)
- Parses configuration and creates proxy instances
- Runs each proxy in a separate goroutine
- Handles graceful shutdown via signal handling
Configuration (config/)
config.go: Parses command line flags and creates client configurations- Handles MongoDB connection URI parsing and validation
- Sets up logging (zap) and metrics (statsd) clients
Proxy Layer (proxy/)
proxy.go: Main proxy server that listens on TCP/Unix socketsconnection.go: Handles individual client connectionsdynamic.go: Supports dynamic configuration for write disabling and traffic redirection
MongoDB Integration (mongo/)
mongo.go: Wrapper around the official MongoDB Go driveroperations.go: Core message handling and request/response proxying (largest file ~1000 lines)ismaster.go: Handles ismaster/hello commands by responding as a mongos routercursor_cache.go: Maps cursor IDs to specific MongoDB servers for proper routingtransaction_cache.go: Tracks client sessions for transaction pinning to servers
Utilities
util/: Statsd metrics utilitieslruttl/: LRU cache with TTL functionality
- Client connects to mongobetween proxy socket
- Proxy creates a Connection object to handle the client
- Connection receives MongoDB wire protocol messages
- Messages are parsed and routed through mongo.Mongo
- mongo.Mongo uses the official MongoDB Go driver to forward requests
- Responses are returned back through the same path
- Special handling for ismaster commands (responds directly without proxying)
- Connection Multiplexing: Many client connections share fewer MongoDB driver connections
- Request Proxying: Wire protocol messages are parsed and forwarded with minimal modification
- Server Pinning: Cursors and transactions are pinned to specific MongoDB servers
- Graceful Shutdown: Signal handling allows for clean connection closure
The CI uses Docker containers running MongoDB instances (mongo1, mongo2, mongo3) to test proxy functionality. Tests use gotestsum for JUnit report generation.