Skip to content

service ​

go
import "github.com/tangoui/tango/service"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package service ties long-lived Go values ("services") to the lifecycle of a tango.App: they start in registration order once the app is ready and shut down in reverse order when it terminates.

A service is any value. If it implements Starter, Startup runs on the main thread when the app is ready (or soon after Register, if it already is); if it implements Stopper, Shutdown runs on the main thread when the app terminates. Both block the main thread, so start long work on a goroutine and stop it with the context Startup receives, which is canceled before any Shutdown runs.

If a Startup fails, the services already started are shut down, the remaining ones are not started, and the app quits; Err reports why.

函数 ​

func Bind ​

go
func Bind(app *tango.App, router *ipc.Router, name string, svc Service) error

Bind registers svc's exported methods on router as "name.Method" (see package bind) and adds svc to app as Register does. Startup and Shutdown are lifecycle hooks, not API: calls to them from the page answer not_found.

func Err ​

go
func Err(app *tango.App) error

Err reports the Startup error that made app quit, if any, joined with the errors Shutdown methods returned.

func Register ​

go
func Register(app *tango.App, svcs ...Service)

Register adds svcs to app. They start, in order, once the app is ready; services from earlier Register calls start first. Nil entries are ignored.

func Shutdown ​

go
func Shutdown(app *tango.App) error

Shutdown shuts down app's services now if the app has not already done so, and returns Err. Use it when Run can return without the app emitting Terminating, e.g. defer service.Shutdown(app) after Run.

类型 ​

Service ​

go
type Service interface{}

Service is any value managed by this package.

Starter ​

go
type Starter interface {
	Startup(ctx context.Context) error
}

Starter is implemented by services that need to start with the app.

Stopper ​

go
type Stopper interface {
	Shutdown() error
}

Stopper is implemented by services that need to release resources when the app terminates.

以 MIT 许可发布