Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go TCP Server with Rate Limiting and Prometheus Metrics

Overview

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.

Features

  • 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 Requests status to clients when rate limits are exceeded.

Getting Started

To get this TCP server up and running on your local machine, follow these steps.

Installation

  1. Clone the Repository:
    git clone https://github.com/SaranHiruthikM/tcp.git
    cd tcp
  2. Download Dependencies:
    go mod tidy
    This command will fetch all the necessary Go modules defined in go.mod.

Environment Variables

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.

Usage

Once you've installed the dependencies, you can run the server and interact with it.

  1. 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 8080
    

    The server will now be listening for TCP connections on localhost:8080 and exposing Prometheus metrics on localhost:9090/metrics.

  2. Test the TCP Server: You can send requests to the server using netcat or 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 Requests response if you exceed the configured rate limit (10 requests per second with a burst of 5 tokens in this case).

  3. View Prometheus Metrics: Open your web browser or use curl to 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.

Technologies Used

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

Contributing

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.

About

Simple Go TCP Server with Rate Limiting and Prometheus Metrics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages