From b30bc386ebba85c45b69c9bce5bea518d875a677 Mon Sep 17 00:00:00 2001 From: Daniel Hu Date: Fri, 14 Jul 2023 14:47:29 +0800 Subject: [PATCH] Initial implementation of GoPool - Added a basic description to the README.md file. - Created a new go.mod file with the module name and Go version. - Implemented the basic structure of the Pool in pool.go. - Implemented the basic structure of the Worker in worker.go. Signed-off-by: Daniel Hu --- README.md | 4 +++- go.mod | 3 +++ pool.go | 32 ++++++++++++++++++++++++++++++++ worker.go | 26 ++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 pool.go create mode 100644 worker.go diff --git a/README.md b/README.md index 4484634..b1b411b 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# gopool \ No newline at end of file +# GoPool + +GoPool is a high-performance, feature-rich, and easy-to-use Golang thread pool. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2419980 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/devchat-ai/gopool + +go 1.20 diff --git a/pool.go b/pool.go new file mode 100644 index 0000000..bd511c8 --- /dev/null +++ b/pool.go @@ -0,0 +1,32 @@ +package gopool + +import ( + "sync" +) + +type Pool struct { + // capacity represents the maximum number of workers the pool can have + capacity int + // running represents the number of workers that are currently running + running int + // workers represents a pool of workers that are executing tasks + workers WorkerStack + // lock represents a mutex to protect the workers slice + lock sync.Mutex +} + +func (p *Pool) Submit(task Task) { + // TODO +} + +func (p *Pool) Running() int { + return p.running +} + +func (p *Pool) Cap() int { + return p.capacity +} + +func (p *Pool) Release() { + // TODO +} diff --git a/worker.go b/worker.go new file mode 100644 index 0000000..44d24af --- /dev/null +++ b/worker.go @@ -0,0 +1,26 @@ +package gopool + +import ( + "time" +) + +type Task func() + +type Worker struct { + // pool represents a reference to the pool that owns this worker + pool *Pool + // task represents the task that the worker will execute + task chan Task + // lastUsedTime represents the last time the worker was used + lastUsedTime time.Time +} + +type WorkerStack interface { + Push(w *Worker) + Pop() *Worker + Len() int +} + +type workerStack struct { + workers []*Worker +}