Skip to content

remove parent context from the httpcluster Runnable #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/httpcluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ func createHTTPCluster(
// Create the httpcluster
cluster, err := httpcluster.NewRunner(
httpcluster.WithLogger(logger.WithGroup("httpcluster")),
httpcluster.WithContext(ctx),
)
if err != nil {
return nil, nil, fmt.Errorf("failed to create httpcluster: %w", err)
Expand Down
9 changes: 0 additions & 9 deletions runnables/httpcluster/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package httpcluster

import (
"context"
"fmt"
"log/slog"
"time"
Expand All @@ -12,14 +11,6 @@ import (
// Option is a function that configures a Runner.
type Option func(*Runner) error

// WithContext sets the parent context for the cluster.
func WithContext(ctx context.Context) Option {
return func(r *Runner) error {
r.parentCtx = ctx
return nil
}
}

// WithLogger sets the logger for the cluster.
func WithLogger(logger *slog.Logger) Option {
return func(r *Runner) error {
Expand Down
4 changes: 0 additions & 4 deletions runnables/httpcluster/options_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package httpcluster

import (
"context"
"log/slog"
"testing"

Expand Down Expand Up @@ -74,18 +73,15 @@ func TestOptionApplicationOrder(t *testing.T) {

// Test that multiple options are applied correctly
logger := slog.Default().WithGroup("test")
ctx := context.Background()

runner, err := NewRunner(
WithLogger(logger),
WithContext(ctx),
WithStateChanBufferSize(15),
WithSiphonBuffer(3),
)
require.NoError(t, err)

assert.Equal(t, logger, runner.logger)
assert.Equal(t, ctx, runner.parentCtx)
assert.Equal(t, 15, runner.stateChanBufferSize)
assert.Equal(t, 3, cap(runner.configSiphon))
}
Expand Down
18 changes: 6 additions & 12 deletions runnables/httpcluster/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ type Runner struct {
restartDelay time.Duration
deadlineServerStart time.Duration

// Context management - similar to composite pattern
parentCtx context.Context // Set during construction
runCtx context.Context // Set during Run()
runCancel context.CancelFunc
// Set by Run()
ctx context.Context
cancel context.CancelFunc

// Configuration siphon channel
configSiphon chan map[string]*httpserver.Config
Expand Down Expand Up @@ -76,7 +75,6 @@ func NewRunner(opts ...Option) (*Runner, error) {
logger: slog.Default().WithGroup("httpcluster.Runner"),
restartDelay: defaultRestartDelay,
deadlineServerStart: defaultDeadlineServerStart,
parentCtx: context.Background(),
configSiphon: make(
chan map[string]*httpserver.Config,
), // unbuffered by default
Expand Down Expand Up @@ -172,8 +170,8 @@ func (r *Runner) Run(ctx context.Context) error {
r.mu.Lock()
runCtx, runCancel := context.WithCancel(ctx)
defer runCancel()
r.runCtx = runCtx
r.runCancel = runCancel
r.ctx = runCtx
r.cancel = runCancel
r.mu.Unlock()

// Transition to running (no servers initially)
Expand All @@ -189,10 +187,6 @@ func (r *Runner) Run(ctx context.Context) error {
logger.Debug("Run context cancelled, initiating shutdown")
return r.shutdown(runCtx)

case <-r.parentCtx.Done():
logger.Debug("Parent context cancelled, initiating shutdown")
return r.shutdown(runCtx)

case newConfigs, ok := <-r.configSiphon:
if !ok {
logger.Debug("Config siphon closed, initiating shutdown")
Expand All @@ -214,7 +208,7 @@ func (r *Runner) Stop() {
logger.Debug("Stopping")

r.mu.Lock()
r.runCancel()
r.cancel()
r.mu.Unlock()
}

Expand Down
39 changes: 3 additions & 36 deletions runnables/httpcluster/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,17 @@ func TestNewRunner(t *testing.T) {
require.NotNil(t, runner)

assert.NotNil(t, runner.logger)
assert.NotNil(t, runner.parentCtx)
assert.NotNil(t, runner.configSiphon)
assert.NotNil(t, runner.currentEntries)
assert.NotNil(t, runner.fsm)
assert.Equal(t, finitestate.StatusNew, runner.GetState())
})

t.Run("with options", func(t *testing.T) {
ctx := t.Context()
runner, err := NewRunner(
WithContext(ctx),
WithSiphonBuffer(1),
)
require.NoError(t, err)
assert.Equal(t, ctx, runner.parentCtx)

cfg := make(map[string]*httpserver.Config)
select {
Expand Down Expand Up @@ -558,7 +554,7 @@ func TestRunnerExecuteActions(t *testing.T) {
ctx := t.Context()

runner.mu.Lock()
runner.runCtx = ctx
runner.ctx = ctx
runner.mu.Unlock()

mockEntries := &MockEntriesManager{}
Expand Down Expand Up @@ -603,35 +599,6 @@ func TestRunnerExecuteActions(t *testing.T) {

func TestRunnerContextManagement(t *testing.T) {
t.Parallel()
t.Run("parent context cancellation", func(t *testing.T) {
parentCtx, parentCancel := context.WithCancel(t.Context())

runner, err := NewRunner(WithContext(parentCtx))
require.NoError(t, err)

runCtx := context.Background()

runErr := make(chan error, 1)
go func() {
runErr <- runner.Run(runCtx)
}()

// Wait for running
require.Eventually(t, func() bool {
return runner.IsRunning()
}, time.Second, 10*time.Millisecond)

// Cancel parent context
parentCancel()

// Should stop gracefully
select {
case err := <-runErr:
assert.NoError(t, err)
case <-time.After(time.Second):
t.Fatal("Runner should stop when parent context cancelled")
}
})

t.Run("run context setup", func(t *testing.T) {
runner, err := NewRunner()
Expand All @@ -652,8 +619,8 @@ func TestRunnerContextManagement(t *testing.T) {

// Check run context is set
runner.mu.RLock()
runCtx := runner.runCtx
runCancel := runner.runCancel
runCtx := runner.ctx
runCancel := runner.cancel
runner.mu.RUnlock()

assert.NotNil(t, runCtx)
Expand Down