Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
f07782d583
|
1
.gitea/CODEOWNERS
Normal file
1
.gitea/CODEOWNERS
Normal file
@@ -0,0 +1 @@
|
||||
* @go/CoreDevelopers @go/Agents
|
||||
19
CHANGELOG.md
19
CHANGELOG.md
@@ -5,6 +5,25 @@ All notable changes to this module will be documented in this file.
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [1.0.0] - 2026-05-12
|
||||
|
||||
### Added
|
||||
|
||||
- `Len() int` on `Provider` — returns the current number of tasks waiting in the queue.
|
||||
Useful for observability, logging backpressure state, and tests.
|
||||
|
||||
### Changed
|
||||
|
||||
- Go directive bumped from 1.25 to 1.26
|
||||
- `launcher` and `logz` dependencies bumped to v1.0.1
|
||||
|
||||
### Stabilization
|
||||
|
||||
- API committed as stable. `Task`, `Provider`, `Component`, `Config`, and `New` are
|
||||
unchanged from v0.9.0 except for the addition of `Len`.
|
||||
|
||||
[1.0.0]: https://code.nochebuena.dev/go/worker/compare/v0.9.0...v1.0.0
|
||||
|
||||
## [0.9.0] - 2026-03-18
|
||||
|
||||
### Added
|
||||
|
||||
6
go.mod
6
go.mod
@@ -1,8 +1,8 @@
|
||||
module code.nochebuena.dev/go/worker
|
||||
|
||||
go 1.25
|
||||
go 1.26
|
||||
|
||||
require (
|
||||
code.nochebuena.dev/go/launcher v0.9.0
|
||||
code.nochebuena.dev/go/logz v0.9.0
|
||||
code.nochebuena.dev/go/launcher v1.0.1
|
||||
code.nochebuena.dev/go/logz v1.0.1
|
||||
)
|
||||
|
||||
8
go.sum
8
go.sum
@@ -1,4 +1,4 @@
|
||||
code.nochebuena.dev/go/launcher v0.9.0 h1:dJHonA9Xm03AQKK0919FJaQn9ZKHZ+RZfB9yxjnx3TA=
|
||||
code.nochebuena.dev/go/launcher v0.9.0/go.mod h1:IBtntmbnyddukjEhxlc7Ysdzz9nZsnd9+8FzAIHt77g=
|
||||
code.nochebuena.dev/go/logz v0.9.0 h1:wfV7vtI4V/8ED7Hm31Fbql7Y5iOGrlHN4X8Z5ajTZZE=
|
||||
code.nochebuena.dev/go/logz v0.9.0/go.mod h1:qODhSbKb+tWE7rdhHLcKweiP5CgwIaWoZxadCT3bQV8=
|
||||
code.nochebuena.dev/go/launcher v1.0.1 h1:hbPV8jNtyxfchrT7igzz3M2tKGI3bm8uWkHBXRvSPgg=
|
||||
code.nochebuena.dev/go/launcher v1.0.1/go.mod h1:1KwndVuqm31JN9Dpl9YvOmlogPlKKzoDMo9aRFkYwmM=
|
||||
code.nochebuena.dev/go/logz v1.0.1 h1:kK9aZo19L208CwCr2D/dbSOMaOv62cXsigMSsdFu+8Y=
|
||||
code.nochebuena.dev/go/logz v1.0.1/go.mod h1:YNpNm03fURm2v0ySh/477z9AJhtfRcd9rFOW6fFqgNM=
|
||||
|
||||
@@ -16,6 +16,8 @@ type Task func(ctx context.Context) error
|
||||
type Provider interface {
|
||||
// Dispatch queues a task. Returns false if the queue is full (backpressure).
|
||||
Dispatch(task Task) bool
|
||||
// Len returns the current number of tasks waiting in the queue.
|
||||
Len() int
|
||||
}
|
||||
|
||||
// Component adds lifecycle management to Provider.
|
||||
@@ -103,6 +105,8 @@ func (w *workerComponent) OnStop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *workerComponent) Len() int { return len(w.taskQueue) }
|
||||
|
||||
func (w *workerComponent) Dispatch(task Task) bool {
|
||||
select {
|
||||
case w.taskQueue <- task:
|
||||
|
||||
@@ -180,6 +180,22 @@ func TestWorker_TaskError(t *testing.T) {
|
||||
c.OnStop() // should not panic
|
||||
}
|
||||
|
||||
func TestWorker_Len(t *testing.T) {
|
||||
c := New(newLogger(), Config{PoolSize: 1, BufferSize: 10, ShutdownTimeout: time.Second})
|
||||
if err := c.OnInit(); err != nil {
|
||||
t.Fatalf("OnInit: %v", err)
|
||||
}
|
||||
// Before start, workers are not running — queued tasks stay in the channel.
|
||||
blocked := make(chan struct{})
|
||||
c.Dispatch(func(ctx context.Context) error { <-blocked; return nil })
|
||||
c.Dispatch(func(ctx context.Context) error { <-blocked; return nil })
|
||||
if got := c.Len(); got != 2 {
|
||||
t.Errorf("Len() = %d, want 2", got)
|
||||
}
|
||||
close(blocked)
|
||||
_ = c.OnStop()
|
||||
}
|
||||
|
||||
func TestWorker_Lifecycle(t *testing.T) {
|
||||
c := New(newLogger(), Config{PoolSize: 2, BufferSize: 10, ShutdownTimeout: time.Second})
|
||||
if err := c.OnInit(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user