You know how hard it can be to keep a server steady when a bunch of clients hit it all at once? This project tackles that by giving you a simple TCP server that gracefully handles many connections concurrently. It also helps prevent overload by applying a rate limit and lets you track its health with built-in metrics, so you're always in the loop.
- Concurrent Connection Handling: Efficiently manages multiple incoming TCP connections using a worker pool.
- Rate Limiting: Implements a token bucket algorithm to limit the number of requests the server accepts per second, preventing overload.
- Prometheus Metrics: Exposes key server metrics (total requests, rate-limited requests, queue size) for monitoring and observability.
- Graceful Overload Handling: Automatically responds with a
429 Too Many Requestsstatus to clients when rate limits are exceeded.
To get this TCP server up and running on your local machine, follow these steps.
- Clone the Repository:
git clone https://github.com/SaranHiruthikM/tcp.git cd tcp - Download Dependencies:
This command will fetch all the necessary Go modules defined in
go mod tidy
go.mod.
This project doesn't require any specific environment variables to run. The server listens on port 8080 for TCP connections and port 9090 for Prometheus metrics by default.
Once you've installed the dependencies, you can run the server and interact with it.
-
Start the Server: From the project root directory, run:
go run main.go metrics.go rate_limiter.go
You'll see a log message indicating the server has started:
2024/07/30 10:00:00 Server started at port 8080The server will now be listening for TCP connections on
localhost:8080and exposing Prometheus metrics onlocalhost:9090/metrics. -
Test the TCP Server: You can send requests to the server using
netcator any TCP client. Open a new terminal and try:echo "Hello, server!" | nc localhost 8080
The server will respond with a
HTTP/1.1 200 OK(even though it's not a full HTTP server, it sends back a minimal response).To see the rate limiting in action, try sending many requests quickly in a loop:
for i in $(seq 1 20); do echo "Request $i" | nc -w 1 localhost 8080 & done
You'll notice some connections get a
HTTP/1.1 429 To much Requestsresponse if you exceed the configured rate limit (10 requests per second with a burst of 5 tokens in this case). -
View Prometheus Metrics: Open your web browser or use
curlto access the metrics endpoint:curl http://localhost:9090/metrics
You'll see output similar to this, showing the total requests, rate-limited requests, and current queue size:
# HELP current_queue_size Currently serving connections queue size # TYPE current_queue_size gauge current_queue_size 0 # HELP tcp_rate_limited_requests Total number of Rate Limited connections # TYPE tcp_rate_limited_requests counter tcp_rate_limited_requests 5 # HELP tcp_total_requests Total number of TCP connections accepted # TYPE tcp_total_requests counter tcp_total_requests 25 ...You can then configure Prometheus to scrape these metrics and visualize them in Grafana for full observability.
| Technology | Description | Link |
|---|---|---|
| Go | The programming language used for the server logic | golang.org |
| Prometheus | Open-source monitoring system and time-series database | prometheus.io |
| Prometheus Go Client | Go client library for Prometheus metrics | github.com/prometheus/client_golang |
Contributions are welcome! If you have suggestions for improvements, bug fixes, or new features, feel free to open an issue or submit a pull request. Please ensure your code adheres to Go best practices and includes appropriate tests if applicable.