lifecycle

package module
v0.0.0-...-c877e39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 10 Imported by: 0

README

lifecycle

Go

服务生命周期管理,可用于同步多个服务的启停等~

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/bunnier/lifecycle"
	"google.golang.org/grpc"
)

func main() {
	// 服务1:grpc
	grpcServer := lifecycle.NewGrpcServerInfo(grpc.NewServer(), "127.0.0.1:8081")
	// 服务2:http
	httpServer1 := &http.Server{
		Addr:    "127.0.0.1:8082",
		Handler: nil,
	}
	// 服务3:http
	httpServer2 := &http.Server{
		Addr:    "127.0.0.1:8083",
		Handler: nil,
	}

	// 托管上面3个服务,一起启停。
	app := lifecycle.NewApp(
		lifecycle.WithGrpcServer(grpcServer),
		lifecycle.WithHttpServer(httpServer1),
		lifecycle.WithHttpServer(httpServer2),
		lifecycle.WithLog(log.Default()),
	)

	time.AfterFunc(time.Second*5, func() {
		app.Stop() // 5s后退出所有服务。
	})

	// 开始服务。
	if err := app.Run(); err != nil {
		log.Println(err)
	}

	fmt.Println("App exited.")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App 是一个Server生命周期托管对象。

func NewApp

func NewApp(opts ...AppOption) *App

NewApp 返回一个 App 对象。

func (*App) Run

func (a *App) Run() error

Start 用来开启服务。

func (*App) Stop

func (a *App) Stop() error

Stop 用来关闭服务。

type AppOption

type AppOption func(app *App)

App 的选项函数。

func WithContext

func WithContext(ctx context.Context) AppOption

WithLog 用于构建向App注册Server的选项函数。

func WithGrpcServer

func WithGrpcServer(grpcServerInfo GrpcServerInfo) AppOption

WithGrpcServer 用于构建向App注册Server的选项函数。

func WithHttpServer

func WithHttpServer(server *http.Server) AppOption

WithHttpServer 用于构建向App注册Server的选项函数。

func WithLog

func WithLog(logger *log.Logger) AppOption

WithLog 用于构建向App注册Server的选项函数。

type GrpcServerInfo

type GrpcServerInfo struct {
	GrpcServer *grpc.Server
	EndPoint   string
}

GrpcServerInfo 存放Grpc服务信息。

func NewGrpcServerInfo

func NewGrpcServerInfo(grpcServer *grpc.Server, endPoint string) GrpcServerInfo

NewGrpcServerInfo 用于创建供App托管生命周期的Grpc服务信息。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL