Skip to content

Feature/graceful shutdown manager - #175

Open
KolaSailaja wants to merge 2 commits into
DevanshuTripathi:mainfrom
KolaSailaja:feature/graceful-shutdown-manager
Open

Feature/graceful shutdown manager#175
KolaSailaja wants to merge 2 commits into
DevanshuTripathi:mainfrom
KolaSailaja:feature/graceful-shutdown-manager

Conversation

@KolaSailaja

Copy link
Copy Markdown

Summary

This PR introduces a Graceful Shutdown Manager with lifecycle hooks to improve application lifecycle management in Vodka.

The implementation allows developers to register startup and shutdown hooks, configure shutdown timeouts, and gracefully handle application termination signals such as SIGINT and SIGTERM.

Changes Made

Lifecycle Hooks

  • Added support for startup hooks using OnStart()
  • Added support for shutdown hooks using OnShutdown()
  • Added priority-based shutdown hooks using OnShutdownWithPriority()

Graceful Shutdown

  • Added graceful handling of SIGINT and SIGTERM
  • Ensures the server stops accepting new requests before shutdown
  • Allows active requests to complete before termination
  • Executes registered shutdown hooks during shutdown

Shutdown Timeout

  • Added configurable shutdown timeout via:

    app.SetShutdownTimeout(30 * time.Second)
  • Uses context-based timeout control for shutdown operations

Hook Execution

  • Startup hooks execute before the server starts
  • Shutdown hooks execute in descending priority order
  • Hooks with equal priority maintain registration order
  • Aggregates shutdown errors while continuing hook execution

Testing

  • Added tests covering:

    • Startup hook execution
    • Shutdown hook execution
    • Hook priority ordering
    • Timeout behavior
    • Error aggregation
    • Lifecycle execution flow

Documentation

  • Updated documentation with examples demonstrating:

    • Startup hooks
    • Shutdown hooks
    • Priority-based hooks
    • Shutdown timeout configuration

Motivation

As applications grow in complexity, developers often need to coordinate cleanup tasks such as:

  • Closing database connections
  • Flushing logs
  • Stopping background workers
  • Closing message queue consumers
  • Releasing external resources

This feature provides a built-in and reusable solution, reducing boilerplate and improving reliability during deployments and application restarts.

Backward Compatibility

  • No breaking API changes
  • Existing applications continue to work without modification
  • Existing routing, middleware, authentication, and server behavior remain unchanged

Example

app := vodka.DefaultRouter()

app.OnStart(func() error {
    return initializeDatabase()
})

app.OnShutdown(func(ctx context.Context) error {
    return db.Close()
})

app.OnShutdownWithPriority(100, func(ctx context.Context) error {
    return worker.Stop(ctx)
})

app.SetShutdownTimeout(30 * time.Second)

app.Run(":8080")

Checklist

  • Startup hooks implemented
  • Shutdown hooks implemented
  • Graceful signal handling added
  • Configurable shutdown timeout added
  • Hook priority execution supported
  • Tests added and passing
  • Documentation updated
  • Backward compatibility maintained

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

@KolaSailaja is attempting to deploy a commit to the Devanshu Tripathi's projects Team on Vercel.

A member of the Team first needs to authorize it.

@KolaSailaja

Copy link
Copy Markdown
Author

Hi @DevanshuTripathi ,

I've completed the implementation of the Graceful Shutdown Manager with lifecycle hooks and submitted this PR for review.

The feature includes:

  • Startup hooks (OnStart)
  • Shutdown hooks (OnShutdown)
  • Priority-based shutdown hooks
  • Graceful handling of SIGINT and SIGTERM
  • Configurable shutdown timeouts
  • Tests and documentation updates

I would greatly appreciate your review and feedback. If there are any improvements, adjustments, or project-specific conventions you'd like me to follow, I'd be happy to make the necessary changes.

Thank you for your time and for maintaining Vodka!

@DevanshuTripathi

Copy link
Copy Markdown
Owner

@KolaSailaja Why do you have 2 PRS for the same issue?

@KolaSailaja

Copy link
Copy Markdown
Author

@DevanshuTripathi I am sorry l just missed a look at my exisitng pr and i submitted another one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants