This code is written to understand how we can implement a worker threadpool in Go. It demonstrates the basic concepts of task queues, worker pools, and concurrent task processing.
Note: This implementation is for educational purposes only and should not be used in production directly. It lacks the robustness, error handling, and optimizations required for a production-grade system.
worker_threadpool_demo.mp4
To understand and work with this codebase, you should have a basic understanding of the following concepts:
- Go Programming Language: Familiarity with Go syntax, data structures, and concurrency primitives (goroutines, channels, for loops with select statement).
- HTTP Servers: Basic knowledge of how to set up and handle HTTP requests in Go.
- Concurrency: Understanding of concurrent programming concepts, including worker pools and task queues, wait groups & mutex.
- Prime Number Checking: Basic understanding of prime number algorithms (optional, for understanding the task being processed).
- Task Queue: A channel-based queue to hold tasks.
- Fixed Worker Pool: A pool of worker goroutines that process tasks from the queue.
- HTTP Server: An HTTP server that accepts requests and enqueues tasks.
-
Main Function:
- Initializes the task queue with a capacity of 100,000.
- Creates a worker pool with a concurrency level based on the number of CPU cores.
- Starts the worker pool.
- Sets up the HTTP server to handle requests.
- Waits for all goroutines to complete.
-
HTTP Handler:
- Validates the request method.
- Extracts the "number" parameter from the request.
- Creates a new task and enqueues it.
- Returns a response indicating whether the task was successfully added or if the queue is full.
-
Helper Functions:
getNumberFromRequest
: Extracts and converts the "number" parameter from the request.enqueueTask
: Attempts to enqueue a task and returns a boolean indicating success or failure.StartServer
: Starts the HTTP server and handles server errors.
You can test the implementation by sending HTTP GET requests to the server. The server expects a "number" parameter in the query string, which it uses to create a task.
-
Start the Server:
- Run the Go program to start the server on port 8080.
-
Send Requests:
- Use
curl
or any HTTP client to send requests to the server.
- Use
# Send a request with a number parameter
curl "http://localhost:8080/?number=123456789"
# Send multiple requests in parallel
for i in {1..10}; do curl "http://localhost:8080/?number=$i" & done
-
Successful Task Addition:
Task added: 123456789 & Queue length is 1
-
Queue Full:
503 Service Unavailable: Task queue is full (current length: 100000)
This implementation is for educational purposes only and should not be used in production directly. It lacks the robustness, error handling, and optimizations required for a production-grade system.