⚡ ZapQ In‑Memory FIFO Queue Server A single‑binary Go microservice that exposes a First‑In / First‑Out message queue completely in RAM. This README explains what every endpoint does, why it works internally, and the key computer‑science principles behind each subsystem. This queue deliberately pairs a plain slice + mutex (for data integrity) with lock-free atomic counters (for hot-path metrics) to balance simplicity and high-throughput telemetry. Wrapping that lean core in HTTP/TLS, structured logging, and health endpoints so it can drop straight into micro-service stacks without extra glue. TL;DR Ultra‑low‑latency queue for transient messages (≤ 128 KB each). Hard caps ( maxBytes , maxMsgs ) guard RAM. , ) guard RAM. Concurrency‑safe via a mutex + atomic counters. JSON metrics, TLS option, graceful shutdown. 1. Architecture Overview ┌────────────┐ HTTP/HTTPS ┌────────────────────────┐ Goroutines ┌─────────────┐ │ Producers │ ───────────────▶│ queue ([]byte slices) │◀──────────────▶│ Consumers │ └────────────┘ └────────────────────────┘ └─────────────┘ queue.data – a slice of byte‑slices ( [][]byte ). Index 0 is always the next item to pop, satisfying FIFO. – a slice of byte‑slices ( ). Index is always the next item to pop, satisfying FIFO. queue.mu – a single sync.Mutex serialises structural modifications while permitting parallel HTTP clients . – a single serialises structural modifications while permitting . queue.bytes – running total for O(1) memory‑cap checks. – running total for O(1) memory‑cap checks. Atomic counters record events without taking the mutex (cache‑friendly, lock‑free). Time & Space Complexity Operation Time Space enqueue O(1) append; amortised as Go grows slice capacity exponentially. dequeue O(1) slice header moves, old entry set to nil for GC. clear O(n) nils everything so GC can reclaim. Mutex ensures linearizability (each op appears instantaneous). Because we hold the lock only long enough to modify the slice, throughput scales linearly u...
First seen: 2025-06-28 18:32
Last seen: 2025-06-28 20:32