sing-box/adapter/lifecycle.go

65 lines
1.1 KiB
Go
Raw Permalink Normal View History

package adapter
2024-11-10 08:46:59 +00:00
import E "github.com/sagernet/sing/common/exceptions"
type StartStage uint8
const (
StartStateInitialize StartStage = iota
StartStateStart
StartStatePostStart
StartStateStarted
)
var ListStartStages = []StartStage{
StartStateInitialize,
StartStateStart,
StartStatePostStart,
StartStateStarted,
}
2024-11-10 08:46:59 +00:00
func (s StartStage) String() string {
switch s {
case StartStateInitialize:
return "initialize"
case StartStateStart:
return "start"
case StartStatePostStart:
return "post-start"
case StartStateStarted:
2024-11-10 08:46:59 +00:00
return "finish-start"
default:
panic("unknown stage")
}
}
2024-11-10 08:46:59 +00:00
type Lifecycle interface {
Start(stage StartStage) error
Close() error
}
2024-11-10 08:46:59 +00:00
type LifecycleService interface {
Name() string
Lifecycle
}
func Start(stage StartStage, services ...Lifecycle) error {
for _, service := range services {
err := service.Start(stage)
if err != nil {
return err
}
}
return nil
}
func StartNamed(stage StartStage, services []LifecycleService) error {
for _, service := range services {
err := service.Start(stage)
if err != nil {
return E.Cause(err, stage.String(), " ", service.Name())
}
}
return nil
}