Skip to content

Commit

Permalink
Initial implementation of GoPool
Browse files Browse the repository at this point in the history
- 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 <tao.hu@merico.dev>
  • Loading branch information
daniel-hutao committed Jul 14, 2023
1 parent 8db7a84 commit b30bc38
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
@@ -1 +1,3 @@
# gopool
# GoPool

GoPool is a high-performance, feature-rich, and easy-to-use Golang thread pool.
3 changes: 3 additions & 0 deletions go.mod
@@ -0,0 +1,3 @@
module github.com/devchat-ai/gopool

go 1.20
32 changes: 32 additions & 0 deletions 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
}
26 changes: 26 additions & 0 deletions 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
}

0 comments on commit b30bc38

Please sign in to comment.