Skip to content

Commit 7695e57

Browse files
committed
docs: clarify and expand documentation across core subsystems
- Improve and clarify documentation for errors, options, logger, metric, ring buffer, and thread management throughout the codebase - Expand comments to explain default behaviors, thread safety, and usage examples for queue configuration and job options - Add detailed descriptions of buffer resizing, shutdown, and task handling logic in the ring buffer implementation - Enhance logger and metric interfaces with usage guidance and method explanations - Document thread management utilities for goroutine lifecycle control Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent cd32c5e commit 7695e57

File tree

8 files changed

+427
-103
lines changed

8 files changed

+427
-103
lines changed

errors.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ package queue
33
import "errors"
44

55
var (
6-
// ErrNoTaskInQueue there is nothing in the queue
6+
// ErrNoTaskInQueue is returned by Worker.Request() when the queue is currently empty.
7+
// This is a temporary condition - new tasks may be added later.
8+
// The queue scheduler uses this error to determine when to retry or wait for notifications.
79
ErrNoTaskInQueue = errors.New("golang-queue: no task in queue")
8-
// ErrQueueHasBeenClosed the current queue is closed
10+
11+
// ErrQueueHasBeenClosed is returned by Worker.Request() during/after shutdown when no tasks remain.
12+
// This is a terminal state indicating the queue has been shut down and drained.
13+
// Once this error appears, no new tasks will be processed.
14+
// Triggered by: calling Shutdown() or Release() on the queue.
915
ErrQueueHasBeenClosed = errors.New("golang-queue: queue has been closed")
10-
// ErrMaxCapacity Maximum size limit reached
16+
17+
// ErrMaxCapacity is returned by Queue() or QueueTask() when the queue is at maximum capacity.
18+
// This only occurs when WithQueueSize() is used to set a capacity limit.
19+
// To handle: retry later, drop the task, or process it synchronously.
20+
// Triggered by: attempting to enqueue when count >= capacity.
1121
ErrMaxCapacity = errors.New("golang-queue: maximum size limit reached")
1222
)

job/option.go

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ package job
22

33
import "time"
44

5-
// Options is a set of options for the queue
5+
// Options configures retry behavior and timeouts for individual jobs.
6+
// These settings control how failed tasks are retried and when they time out.
67
type Options struct {
7-
retryCount int64
8-
retryDelay time.Duration
9-
retryFactor float64
10-
retryMin time.Duration
11-
retryMax time.Duration
12-
jitter bool
13-
14-
timeout time.Duration
8+
retryCount int64 // Maximum number of retry attempts (0 means no retries)
9+
retryDelay time.Duration // Fixed delay between retries (0 uses exponential backoff)
10+
retryFactor float64 // Exponential backoff multiplier (default: 2)
11+
retryMin time.Duration // Minimum backoff delay (default: 100ms)
12+
retryMax time.Duration // Maximum backoff delay (default: 10s)
13+
jitter bool // Add randomization to backoff to prevent thundering herd (default: false)
14+
15+
timeout time.Duration // Maximum time allowed for job execution (default: 60 minutes)
1516
}
1617

17-
// newDefaultOptions create new default options
18+
// newDefaultOptions creates job options with sensible defaults:
19+
// - retryCount: 0 (no automatic retries)
20+
// - retryDelay: 0 (uses exponential backoff when retries are enabled)
21+
// - retryFactor: 2 (doubles the delay between each retry)
22+
// - retryMin: 100ms (minimum backoff delay)
23+
// - retryMax: 10s (maximum backoff delay)
24+
// - timeout: 60 minutes (job execution timeout)
25+
// - jitter: false (no randomization in backoff)
1826
func newDefaultOptions() Options {
1927
return Options{
2028
retryCount: 0,
@@ -27,18 +35,38 @@ func newDefaultOptions() Options {
2735
}
2836
}
2937

30-
// AllowOption is a function that sets some option on the Options
38+
// AllowOption provides optional configuration for individual job execution.
39+
// All fields are pointers to distinguish between "not set" and "set to zero value".
40+
// This allows partial configuration while keeping unspecified fields at their defaults.
41+
//
42+
// Example usage:
43+
//
44+
// opts := AllowOption{
45+
// RetryCount: job.Int64(3), // Retry failed jobs up to 3 times
46+
// Timeout: job.Time(5 * time.Minute), // 5 minute timeout
47+
// }
48+
// q.QueueTask(myTask, opts)
3149
type AllowOption struct {
32-
RetryCount *int64
33-
RetryDelay *time.Duration
34-
RetryFactor *float64
35-
RetryMin *time.Duration
36-
RetryMax *time.Duration
37-
Jitter *bool
38-
Timeout *time.Duration
50+
RetryCount *int64 // Maximum retry attempts (nil uses default: 0)
51+
RetryDelay *time.Duration // Fixed delay between retries (nil uses exponential backoff)
52+
RetryFactor *float64 // Backoff multiplier (nil uses default: 2)
53+
RetryMin *time.Duration // Minimum backoff delay (nil uses default: 100ms)
54+
RetryMax *time.Duration // Maximum backoff delay (nil uses default: 10s)
55+
Jitter *bool // Enable backoff jitter (nil uses default: false)
56+
Timeout *time.Duration // Job execution timeout (nil uses default: 60 minutes)
3957
}
4058

41-
// NewOptions create new options
59+
// NewOptions creates a job Options struct by merging defaults with provided AllowOption values.
60+
// Only non-nil fields in AllowOption will override the defaults.
61+
// This allows partial configuration where unspecified options retain their default values.
62+
//
63+
// Example:
64+
//
65+
// // Only override retry count and timeout, keep other defaults
66+
// opts := job.NewOptions(job.AllowOption{
67+
// RetryCount: job.Int64(5),
68+
// Timeout: job.Time(10 * time.Minute),
69+
// })
4270
func NewOptions(opts ...AllowOption) Options {
4371
o := newDefaultOptions()
4472

@@ -75,22 +103,45 @@ func NewOptions(opts ...AllowOption) Options {
75103
return o
76104
}
77105

78-
// Int64 is a helper routine that allocates a new int64 value
106+
// Int64 is a helper function that creates a pointer to an int64 value.
107+
// Useful for setting AllowOption fields that require *int64.
108+
//
109+
// Example:
110+
//
111+
// opts := AllowOption{RetryCount: job.Int64(3)}
79112
func Int64(val int64) *int64 {
80113
return &val
81114
}
82115

83-
// Float64 is a helper routine that allocates a new float64 value
116+
// Float64 is a helper function that creates a pointer to a float64 value.
117+
// Useful for setting AllowOption fields that require *float64.
118+
//
119+
// Example:
120+
//
121+
// opts := AllowOption{RetryFactor: job.Float64(1.5)}
84122
func Float64(val float64) *float64 {
85123
return &val
86124
}
87125

88-
// Time is a helper routine that allocates a new time value
126+
// Time is a helper function that creates a pointer to a time.Duration value.
127+
// Useful for setting AllowOption fields that require *time.Duration.
128+
//
129+
// Example:
130+
//
131+
// opts := AllowOption{
132+
// Timeout: job.Time(5 * time.Minute),
133+
// RetryDelay: job.Time(2 * time.Second),
134+
// }
89135
func Time(v time.Duration) *time.Duration {
90136
return &v
91137
}
92138

93-
// Bool is a helper routine that allocates a new bool value
139+
// Bool is a helper function that creates a pointer to a bool value.
140+
// Useful for setting AllowOption fields that require *bool.
141+
//
142+
// Example:
143+
//
144+
// opts := AllowOption{Jitter: job.Bool(true)}
94145
func Bool(val bool) *bool {
95146
return &val
96147
}

logger.go

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,47 @@ import (
66
"os"
77
)
88

9-
// Logger interface is used throughout gorush
9+
// Logger defines the interface for logging queue events, errors, and fatal conditions.
10+
// The queue uses this interface to report:
11+
// - Info: Normal operations (shutdown, retry attempts)
12+
// - Error: Recoverable errors (task failures, runtime errors)
13+
// - Fatal: Panics and critical failures (includes stack traces)
14+
//
15+
// Implement this interface to integrate with custom logging systems (logrus, zap, etc.).
1016
type Logger interface {
17+
// Infof logs formatted informational messages.
1118
Infof(format string, args ...any)
19+
20+
// Errorf logs formatted error messages.
1221
Errorf(format string, args ...any)
22+
23+
// Fatalf logs formatted fatal errors with stack trace information.
24+
// Used for panics and critical failures.
1325
Fatalf(format string, args ...any)
26+
27+
// Info logs informational messages.
1428
Info(args ...any)
29+
30+
// Error logs error messages.
1531
Error(args ...any)
32+
33+
// Fatal logs fatal errors with stack trace information.
34+
// Used for panics and critical failures.
1635
Fatal(args ...any)
1736
}
1837

19-
// NewLogger for simple logger.
38+
// NewLogger creates a standard logger that writes to stderr with timestamps.
39+
// This is the default logger used by queues unless overridden with WithLogger.
40+
//
41+
// Log format:
42+
// - INFO messages: Simple timestamped output
43+
// - ERROR messages: Simple timestamped output
44+
// - FATAL messages: Includes stack trace with file:line information
45+
//
46+
// Use cases:
47+
// - Development and debugging
48+
// - Simple production deployments without structured logging
49+
// - When detailed error context is needed
2050
func NewLogger() Logger {
2151
return defaultLogger{
2252
infoLogger: log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime),
@@ -25,20 +55,28 @@ func NewLogger() Logger {
2555
}
2656
}
2757

58+
// defaultLogger is the standard logger implementation using Go's log package.
59+
// It provides timestamped output to stderr and includes stack traces for fatal errors.
2860
type defaultLogger struct {
29-
infoLogger *log.Logger
30-
errorLogger *log.Logger
31-
fatalLogger *log.Logger
61+
infoLogger *log.Logger // Logger for informational messages
62+
errorLogger *log.Logger // Logger for error messages
63+
fatalLogger *log.Logger // Logger for fatal errors with stack traces
3264
}
3365

66+
// logWithCallerf logs a formatted message with stack trace information.
67+
// The stack trace helps identify where the fatal error occurred.
68+
// Skip depth of 3: logWithCallerf -> Fatalf -> user code
3469
func (l defaultLogger) logWithCallerf(logger *log.Logger, format string, args ...any) {
35-
stackInfo := stack(3) // Assuming stack(3) returns caller info string
36-
// Prepend stack info to the arguments and adjust the format string
70+
stackInfo := stack(3) // Get caller info (file:line)
71+
// Prepend stack info to the log message
3772
logger.Printf("%s "+format, append([]any{stackInfo}, args...)...)
3873
}
3974

75+
// logWithCaller logs unformatted arguments with stack trace information.
76+
// The stack trace helps identify where the fatal error occurred.
77+
// Skip depth of 3: logWithCaller -> Fatal -> user code
4078
func (l defaultLogger) logWithCaller(logger *log.Logger, args ...any) {
41-
stack := stack(3)
79+
stack := stack(3) // Get caller info (file:line)
4280
logger.Println(append([]any{stack}, args...)...)
4381
}
4482

@@ -66,12 +104,21 @@ func (l defaultLogger) Fatal(args ...any) {
66104
l.logWithCaller(l.fatalLogger, args...)
67105
}
68106

69-
// NewEmptyLogger for simple logger.
107+
// NewEmptyLogger creates a no-op logger that discards all log messages.
108+
// This is useful for:
109+
// - Performance-sensitive production environments where logging overhead matters
110+
// - Testing scenarios where log output would clutter test results
111+
// - Silent background workers that don't need observability
112+
//
113+
// Example:
114+
//
115+
// q := queue.NewPool(5, queue.WithLogger(queue.NewEmptyLogger()))
70116
func NewEmptyLogger() Logger {
71117
return emptyLogger{}
72118
}
73119

74-
// EmptyLogger no meesgae logger
120+
// emptyLogger is a no-op implementation that discards all log messages.
121+
// All methods are implemented as empty functions with no side effects.
75122
type emptyLogger struct{}
76123

77124
func (l emptyLogger) Infof(format string, args ...any) {}

0 commit comments

Comments
 (0)