Skip to content

Commit 0e7611d

Browse files
committed
clean up comments
1 parent 3d97dc3 commit 0e7611d

File tree

18 files changed

+41
-44
lines changed

18 files changed

+41
-44
lines changed

examples/composite/worker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewWorker(config WorkerConfig, logger *slog.Logger) (*Worker, error) {
8282
}, nil
8383
}
8484

85-
// String returns the worker's name and interval safely.
85+
// String returns the worker's name and interval.
8686
func (w *Worker) String() string {
8787
return fmt.Sprintf("Worker{config: %s}", w.getConfig())
8888
}
@@ -222,14 +222,14 @@ func (w *Worker) ReloadWithConfig(config any) {
222222
}
223223
}
224224

225-
// getConfig returns the worker's current configuration safely.
225+
// getConfig returns the worker's current configuration.
226226
func (w *Worker) getConfig() WorkerConfig {
227227
w.mu.Lock()
228228
defer w.mu.Unlock()
229229
return w.config
230230
}
231231

232-
// setConfig updates the internal configuration state safely.
232+
// setConfig updates the internal configuration state.
233233
// It returns the *previous* config object.
234234
func (w *Worker) setConfig(newConfig WorkerConfig) (oldCfg WorkerConfig) {
235235
w.mu.Lock()

examples/http/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
const (
17-
// Port the http server should bind to
17+
// Port the http server binds to
1818
ListenOn = ":8080"
1919

2020
// How long the supervisor waits for the HTTP server to drain before forcefully shutting down
@@ -71,7 +71,7 @@ func buildRoutes(logHandler slog.Handler) ([]httpserver.Route, error) {
7171
return nil, fmt.Errorf("failed to create status route: %w", err)
7272
}
7373

74-
// API routes need their middlewares passed explicitly with the updated function
74+
// API routes have their middlewares passed to the updated function
7575
apiRoute, err := httpserver.NewWildcardRoute("/api", wildcardHandler, commonMw...)
7676
if err != nil {
7777
return nil, fmt.Errorf("failed to create wildcard route: %w", err)

internal/networking/port.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var (
1515
ErrPortOutOfRange = errors.New("port number must be between 1 and 65535")
1616
)
1717

18-
// ValidatePort checks if the provided port string is valid and returns a clean, normalized version.
19-
// It accepts formats like ":8080", "localhost:8080", "127.0.0.1:8080", or just "8080".
18+
// ValidatePort checks if the provided port string is valid and returns a normalized version.
19+
// It accepts formats like ":8080", "localhost:8080", "127.0.0.1:8080", or "8080".
2020
// Returns the normalized port format (":PORT") and an error if the port is invalid.
2121
func ValidatePort(portStr string) (string, error) {
2222
if portStr == "" {
@@ -29,12 +29,12 @@ func ValidatePort(portStr string) (string, error) {
2929
return "", fmt.Errorf("%w: negative port numbers are not allowed", ErrInvalidFormat)
3030
}
3131

32-
// If it's just a number, add a colon prefix
32+
// If it's a number without a colon, add a colon prefix
3333
if !strings.Contains(portStr, ":") {
3434
portStr = ":" + portStr
3535
}
3636

37-
// Handle host:port format by extracting just the port
37+
// Handle host:port format by extracting the port
3838
host, port, err := net.SplitHostPort(portStr)
3939
if err != nil {
4040
return "", fmt.Errorf("%w: %w", ErrInvalidFormat, err)

runnables/composite/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/robbyt/go-supervisor/supervisor"
99
)
1010

11-
// runnable is a local alias to ensure the sub-runnables implement the
11+
// runnable is a local alias constraining sub-runnables to implement the
1212
// supervisor.Runnable interface.
1313
type runnable interface {
1414
supervisor.Runnable

runnables/composite/reload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ func (r *Runner[T]) reloadMembershipChanged(newConfig *Config[T]) error {
9898
// reloadConfig handles the case where the membership of runnables has not changed.
9999
func (r *Runner[T]) reloadConfig(logger *slog.Logger, newConfig *Config[T]) {
100100
logger = logger.WithGroup("reloadConfig")
101-
// No membership change, just update config and reload existing runnables
101+
// No membership change, update config and reload existing runnables
102102
r.configMu.Lock()
103103
r.setConfig(newConfig)
104104
r.configMu.Unlock()
105105

106106
// Reload configs of existing runnables
107-
// No need to lock the runnables mutex as we're not changing membership
107+
// Runnables mutex not locked as membership is not changing
108108
for _, entry := range newConfig.Entries {
109109
if reloadableWithConfig, ok := any(entry.Runnable).(ReloadableWithConfig); ok {
110110
// If the runnable implements our ReloadableWithConfig interface, use that to pass the new config

runnables/composite/runner.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Runner[T runnable] struct {
3636
// - configCallback: Required. A function that returns the initial configuration and is called during any reload operations.
3737
// - opts: Optional. A variadic list of Option functions to customize the Runner behavior.
3838
//
39-
// The configCallback must not be nil and will be invoked by Run() to load the initial configuration.
39+
// The configCallback cannot be nil and will be invoked by Run() to load the initial configuration.
4040
func NewRunner[T runnable](
4141
configCallback ConfigCallback[T],
4242
opts ...Option[T],
@@ -60,7 +60,7 @@ func NewRunner[T runnable](
6060
opt(r)
6161
}
6262

63-
// Validate requirements
63+
// Validate configuration
6464
if r.configCallback == nil {
6565
return nil, fmt.Errorf(
6666
"%w: config callback is required",
@@ -175,15 +175,15 @@ func (r *Runner[T]) boot(ctx context.Context) error {
175175
return fmt.Errorf("%w: configuration is unavailable", ErrConfigMissing)
176176
}
177177

178-
// If there are no entries, just log and return successfully instead of error
178+
// If there are no entries, log and return without error
179179
if len(cfg.Entries) == 0 {
180180
logger.Debug("No runnables found in configuration")
181181
return nil
182182
}
183183

184184
logger.Debug("Starting child runnables...", "count", len(cfg.Entries))
185185

186-
// Use a temporary WaitGroup to ensure all goroutines are launched.
186+
// Use a temporary WaitGroup to track that all goroutines have started.
187187
var startWg sync.WaitGroup
188188
startWg.Add(len(cfg.Entries))
189189
for i, e := range cfg.Entries {

runnables/composite/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (r *Runner[T]) setStateError() {
3232

3333
// GetChildStates returns a map of child runnable names to their states.
3434
func (r *Runner[T]) GetChildStates() map[string]string {
35-
// No need for runnables lock, just reading config and querying state
36-
// which doesn't modify any internal state
35+
// Runnables lock not required, reading config and querying state
36+
// does not modify any internal state
3737

3838
states := make(map[string]string)
3939
cfg := r.getConfig()

runnables/httpcluster/entries.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type serverEntry struct {
3535
var _ entriesManager = (*entries)(nil)
3636

3737
// entries is an immutable collection of server entries with pending actions.
38-
// Once created, it should not be modified - only replaced with a new instance.
38+
// Once created, it is not modified - only replaced with a new instance.
3939
type entries struct {
4040
servers map[string]*serverEntry
4141
}
@@ -59,7 +59,7 @@ func newEntries(desiredConfigs map[string]*httpserver.Config) *entries {
5959
return &entries{servers: servers}
6060
}
6161

62-
// removeEntry creates a new entries collection with the specified entry completely removed.
62+
// removeEntry creates a new entries collection with the specified entry removed.
6363
func (e *entries) removeEntry(id string) entriesManager {
6464
_, exists := e.servers[id]
6565
if !exists {
@@ -111,7 +111,7 @@ func (e *entries) countByAction(a action) int {
111111
}
112112

113113
// commit creates a new entries collection with all actions marked as complete.
114-
// This should be called after all pending actions have been executed.
114+
// Called after all pending actions have been executed.
115115
// It removes entries marked for stop and clears all action flags.
116116
func (e *entries) commit() entriesManager {
117117
servers := make(map[string]*serverEntry)
@@ -212,7 +212,7 @@ func processExistingServer(
212212
return
213213
}
214214

215-
// Case 1: Server should be removed
215+
// Case 1: Server is removed
216216
if desiredConfig == nil {
217217
if oldEntry.runner != nil {
218218
newEntry := *oldEntry
@@ -233,7 +233,7 @@ func processExistingServer(
233233

234234
// Case 3: Config changed - need to restart
235235
if oldEntry.runner != nil {
236-
// Running server needs restart
236+
// Running server marked for restart (stop then start)
237237
stopEntry := *oldEntry
238238
stopEntry.action = actionStop
239239

runnables/httpcluster/interfaces.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type entriesManager interface {
2121
getPendingActions() (toStart, toStop []string)
2222

2323
// commit creates a new entries collection with all actions marked as complete.
24-
// This should be called after all pending actions have been executed.
24+
// Called after all pending actions have been executed.
2525
// It removes entries marked for stop and clears all action flags.
2626
commit() entriesManager
2727

@@ -40,8 +40,8 @@ type entriesManager interface {
4040
// Returns nil if the server doesn't exist.
4141
clearRuntime(id string) entriesManager
4242

43-
// removeEntry creates a new entries collection with the specified entry completely removed.
44-
// This is used when a server fails to start and should be completely removed.
43+
// removeEntry creates a new entries collection with the specified entry removed.
44+
// This is used when a server fails to start and is removed from the collection.
4545
removeEntry(id string) entriesManager
4646

4747
// buildPendingEntries creates a new entries collection based on the desired state and the previous state.

runnables/httpcluster/runner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func (r *Runner) executeActions(ctx context.Context, pending entriesManager) ent
305305
if len(toStop) > 0 {
306306
current = r.stopServers(ctx, current, toStop)
307307

308-
// Delay after stopping servers to allow OS to fully release sockets
308+
// Delay after stopping servers to allow OS to release sockets
309309
// before binding to the same ports again
310310
if len(toStart) > 0 && r.restartDelay > 0 {
311311
logger.Debug("Waiting for ports to be released", "delay", r.restartDelay)
@@ -397,7 +397,7 @@ func (r *Runner) startServers(
397397
runner, serverCtx, serverCancel, err := r.createAndStartServer(ctx, entry)
398398
if err != nil {
399399
logger.Error("Failed to create server", "error", err)
400-
// Remove entry completely since server failed to create
400+
// Remove entry since server failed to create
401401
current = current.removeEntry(id)
402402
continue
403403
}
@@ -414,9 +414,8 @@ func (r *Runner) startServers(
414414
"state", runner.GetState())
415415
// Cancel the server context to clean up
416416
serverCancel()
417-
// Stop the runner explicitly to ensure cleanup
418417
runner.Stop()
419-
// Remove entry completely since server failed to start
418+
// Remove entry since server failed to start
420419
current = current.removeEntry(id)
421420
continue
422421
}

runnables/httpserver/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func WithRequestContext(ctx context.Context) ConfigOption {
107107
}
108108

109109
// WithConfigCopy creates a ConfigOption that copies most settings from the source config
110-
// except for ListenAddr and Routes which must be provided directly to NewConfig.
110+
// except for ListenAddr and Routes which are provided directly to NewConfig.
111111
func WithConfigCopy(src *Config) ConfigOption {
112112
return func(dst *Config) {
113113
if src == nil {
@@ -126,7 +126,7 @@ func WithConfigCopy(src *Config) ConfigOption {
126126
}
127127
}
128128

129-
// NewConfig creates a new Config with the required address and routes
129+
// NewConfig creates a new Config with the address and routes
130130
// plus any optional configuration via functional options
131131
func NewConfig(addr string, routes Routes, opts ...ConfigOption) (*Config, error) {
132132
if len(routes) == 0 {

runnables/httpserver/middleware/metrics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// MetricCollector creates a middleware that collects metrics about HTTP requests.
10-
// This is a placeholder implementation that should be extended to integrate with
10+
// This is a placeholder implementation that can be extended to integrate with
1111
// your metrics collection system.
1212
func MetricCollector() Middleware {
1313
return func(next http.HandlerFunc) http.HandlerFunc {
@@ -31,7 +31,7 @@ func MetricCollector() Middleware {
3131
// httpRequestsTotal.WithLabelValues(r.Method, r.URL.Path, strconv.Itoa(rw.statusCode)).Inc()
3232
// httpRequestDuration.WithLabelValues(r.Method, r.URL.Path).Observe(duration.Seconds())
3333

34-
// For now, just log the metrics
34+
// Log the metrics
3535
slog.Debug("HTTP request metrics",
3636
"method", r.Method,
3737
"path", r.URL.Path,

runnables/httpserver/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func WithContext(ctx context.Context) Option {
3131
}
3232
}
3333

34-
// WithConfigCallback sets the function that will be called to load or reload configuration. Using
35-
// this option or WithConfig is required to initialize the Runner instance, because it provides the
34+
// WithConfigCallback sets the function that will be called to load or reload configuration.
35+
// Either this option or WithConfig initializes the Runner instance by providing the
3636
// configuration for the HTTP server managed by the Runner.
3737
func WithConfigCallback(callback ConfigCallback) Option {
3838
return func(r *Runner) {

runnables/httpserver/routes.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func applyMiddlewares(
3232
}
3333

3434
// NewRoute creates a new Route with the given name, path, and handler.
35-
// The name must be unique, the path must be non-empty, and the handler must not be nil.
3635
func NewRoute(name string, path string, handler http.HandlerFunc) (*Route, error) {
3736
if name == "" {
3837
return nil, errors.New("name cannot be empty")

runnables/httpserver/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/robbyt/go-supervisor/supervisor"
1717
)
1818

19-
// Interface guards to ensure all of these are implemented
19+
// Interface guards verify implementation at compile time
2020
var (
2121
_ supervisor.Runnable = (*Runner)(nil)
2222
_ supervisor.Reloadable = (*Runner)(nil)

supervisor/interfaces.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323

2424
// Runnable represents a service that can be run and stopped.
2525
type Runnable interface {
26-
fmt.Stringer // Runnables needs a String() method to be identifiable in logs
26+
fmt.Stringer // Runnables implement a String() method to be identifiable in logs
2727

2828
// Run starts the service with the given context and returns an error if it fails.
29-
// Run must be a blocking call that runs the work unit until it is stopped.
29+
// Run is a blocking call that runs the work unit until it is stopped.
3030
Run(ctx context.Context) error
3131
// Stop signals the service to stop.
32-
// Stop must be a blocking call that stops the work unit.
32+
// Stop is a blocking call that stops the work unit.
3333
Stop()
3434
}
3535

supervisor/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (p *PIDZero) startStateMonitor() {
216216

217217
prev, loaded := p.stateMap.Swap(r, state)
218218
if !loaded {
219-
// The state map entry for this runnable should have been created elsewhere
219+
// The state map entry for this runnable was expected to be created in startRunnable
220220
p.logger.Warn(
221221
"Unexpected State map entry created",
222222
"runnable", r,

supervisor/supervisor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ func (p *PIDZero) Run() error {
237237
}
238238

239239
// blockUntilRunnableReady blocks until the runnable is in a running state.
240-
// This requires implementing the Stateable interface so the runnable can be monitored.
241240
func (p *PIDZero) blockUntilRunnableReady(r Stateable) error {
242241
startupCtx, cancel := context.WithTimeout(p.ctx, p.startupTimeout)
243242
defer cancel()
@@ -343,7 +342,7 @@ func (p *PIDZero) Shutdown() {
343342
"elapsed", time.Since(shutdownStart))
344343
}
345344
} else {
346-
// No timeout configured, just wait
345+
// No timeout configured, wait indefinitely
347346
p.wg.Wait()
348347
}
349348

0 commit comments

Comments
 (0)