Skip to content

Commit d5c29de

Browse files
authored
Merge pull request #4 from xconnio/structure-fix
Rename module, package and APIs
2 parents c306fb2 + bd73d4c commit d5c29de

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: workerpool CI
1+
name: timertask CI
22

33
on:
44
push:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ lint:
22
golangci-lint run
33

44
test:
5-
go test ./...
5+
go test -count=1 ./... -v
66

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
# workerpool
2-
Imagine a websocket server that sends ping message every X seconds of inactivity to each client, to be able to acheive that, the server would need to run on goroutine per client. Those goroutines are mostly idle and result in memory consumption. workpool attempts to solve that problems by running a single loop that runs every second to check which clients should be sent ping message.
1+
# timertask
2+
Imagine a websocket server that sends ping message every X seconds of inactivity to
3+
each client, to be able to achieve that, the server would need to run on goroutine
4+
per client. Those goroutines are mostly idle and result in memory consumption.
5+
timertask attempts to solve that problems by running a single loop that runs every
6+
second to check which clients should be sent ping message.
7+
8+
This library enables that use-case and probably others.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/xconnio/workerpool
1+
module github.com/xconnio/timertask
22

33
go 1.19
44

workerpool.go renamed to timertask.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
package workerpool
1+
package timertask
22

33
import (
44
"log"
55
"sync"
66
"time"
77
)
88

9-
type wpConfig struct {
9+
type taskConfig struct {
1010
interval time.Duration
1111
callback func() error
1212
}
1313

14-
type WorkerPool struct {
15-
workers map[int64]map[int64]wpConfig
14+
type Manager struct {
15+
workers map[int64]map[int64]taskConfig
1616
workersByIntervals map[int64]int64
1717
mutex sync.Mutex
1818
}
1919

20-
// NewWorkerPool creates a new WorkerPool instance.
21-
func NewWorkerPool() *WorkerPool {
22-
return &WorkerPool{
23-
workers: make(map[int64]map[int64]wpConfig),
20+
// NewManager creates a new Manager instance.
21+
func NewManager() *Manager {
22+
return &Manager{
23+
workers: make(map[int64]map[int64]taskConfig),
2424
workersByIntervals: make(map[int64]int64),
2525
}
2626
}
2727

28-
// Add registers a new worker.
29-
func (m *WorkerPool) Add(id int64, interval time.Duration, callback func() error) {
28+
// Schedule registers a new worker.
29+
func (m *Manager) Schedule(id int64, interval time.Duration, callback func() error) {
3030
// Calculate the next worker time in seconds
3131
timeToWork := time.Now().Add(interval).Unix()
3232

3333
m.mutex.Lock()
3434
defer m.mutex.Unlock()
3535
if m.workers[timeToWork] == nil {
36-
m.workers[timeToWork] = make(map[int64]wpConfig)
36+
m.workers[timeToWork] = make(map[int64]taskConfig)
3737
}
3838

39-
m.workers[timeToWork][id] = wpConfig{
39+
m.workers[timeToWork][id] = taskConfig{
4040
interval: interval,
4141
callback: callback,
4242
}
4343
m.workersByIntervals[id] = timeToWork
4444
}
4545

46-
// Remove unregisters a worker.
47-
func (m *WorkerPool) Remove(id int64) {
46+
// Cancel unregisters a worker.
47+
func (m *Manager) Cancel(id int64) {
4848
m.mutex.Lock()
4949
defer m.mutex.Unlock()
5050

@@ -59,7 +59,7 @@ func (m *WorkerPool) Remove(id int64) {
5959
}
6060

6161
// Reset reschedules the worker for a specific id.
62-
func (m *WorkerPool) Reset(id int64) {
62+
func (m *Manager) Reset(id int64) {
6363
m.mutex.Lock()
6464
defer m.mutex.Unlock()
6565

@@ -73,15 +73,15 @@ func (m *WorkerPool) Reset(id int64) {
7373

7474
newTime := time.Now().Add(wpCfg.interval).Unix()
7575
if m.workers[newTime] == nil {
76-
m.workers[newTime] = make(map[int64]wpConfig)
76+
m.workers[newTime] = make(map[int64]taskConfig)
7777
}
7878
m.workers[newTime][id] = wpCfg
7979
m.workersByIntervals[id] = newTime
8080
}
8181
}
8282

8383
// Start begins the worker process.
84-
func (m *WorkerPool) Start() {
84+
func (m *Manager) Start() {
8585
go func() {
8686
for {
8787
nowSeconds := time.Now().Unix()
@@ -93,14 +93,14 @@ func (m *WorkerPool) Start() {
9393
err := wpCfg.callback()
9494
if err != nil {
9595
log.Printf("error pinging client: %v", err)
96-
m.Remove(id)
96+
m.Cancel(id)
9797
continue
9898
}
9999

100100
// Reschedule the next worker
101101
timeToWork := time.Now().Add(wpCfg.interval).Unix()
102102
if m.workers[timeToWork] == nil {
103-
m.workers[timeToWork] = make(map[int64]wpConfig)
103+
m.workers[timeToWork] = make(map[int64]taskConfig)
104104
}
105105
m.workers[timeToWork][id] = wpCfg
106106
m.workersByIntervals[id] = timeToWork

workerpool_test.go renamed to timertask_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package workerpool_test
1+
package timertask_test
22

33
import (
44
"testing"
55
"time"
66

77
"github.com/stretchr/testify/require"
88

9-
"github.com/xconnio/workerpool"
9+
"github.com/xconnio/timertask"
1010
)
1111

12-
func TestManager(t *testing.T) {
13-
manager := workerpool.NewWorkerPool()
12+
func TestManager_Schedule(t *testing.T) {
13+
manager := timertask.NewManager()
1414

1515
messages1 := 0
16-
manager.Add(1, 2*time.Second, func() error {
16+
manager.Schedule(1, 2*time.Second, func() error {
1717
messages1++
1818
return nil
1919
})
2020

2121
messages2 := 0
22-
manager.Add(2, 1*time.Second, func() error {
22+
manager.Schedule(2, 1*time.Second, func() error {
2323
messages2++
2424
return nil
2525
})
@@ -32,25 +32,25 @@ func TestManager(t *testing.T) {
3232
require.NotZero(t, messages1)
3333
require.NotZero(t, messages2)
3434

35-
// Remove worker1 and check
36-
manager.Remove(1)
35+
// Cancel worker1 and check
36+
manager.Cancel(1)
3737
time.Sleep(3 * time.Second)
3838

3939
require.Equal(t, messages1, 1)
4040

4141
require.Greater(t, messages2, 2)
4242
}
4343

44-
func TestManagerReset(t *testing.T) {
45-
manager := workerpool.NewWorkerPool()
44+
func TestManager_Reset(t *testing.T) {
45+
manager := timertask.NewManager()
4646

4747
messages := 0
48-
manager.Add(1, 2*time.Second, func() error {
48+
manager.Schedule(1, 2*time.Second, func() error {
4949
messages++
5050
return nil
5151
})
5252

53-
go manager.Start()
53+
manager.Start()
5454

5555
// Wait for first work
5656
time.Sleep(3 * time.Second)

0 commit comments

Comments
 (0)