Sitelet https://github.com/samber/do/issues/120
Skip to content

v2 proposal: a Startupable interface as the counterpart to Shutdownable #120

Description

@changrui0608

The point

samber/do provides Shutdownable interface so we can drop connections, stop background tasks... to achieve a graceful shutdown.

I think it would be good if samber/do also supports Startupable interface like:

type Startupable interface {
    Startup(context.Context) error
}

The mechanism of Startupable could be similar to the OnStart hook in uber/fx.
This would provide a clear place to build connections, start background tasks, and fetch initial data.

Startupable vs. do startup in constructor

#92 suggests doing startup work inside constructors by passing a context.Context.

Having a separate Startup function could be preferable in some cases:

  • Some projects already use a NewXxxStartupShutdown template (which in our view makes the code cleaner, as the constructor only handles injections while Startup handles initializations).
  • Migration from uber/fx to samber/do could be easier.

That said, #92 (the InvokeContext idea) is not in conflict with Startupable. In some cases, we might still want to perform I/O operations during construction, depending on how we understand the business logic and layer the code.

It could be beneficial if samber/do supported both approaches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions