signal

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 4 Imported by: 0

README

Signal Server

Example

package main

import (
	"os"
	"syscall"

	"github.com/go-kratos/kratos/v2"

	"github.com/go-kratos-ecosystem/components/v2/signal"
)

func main() {
	app := kratos.New(
		kratos.Server(newSignalServer()),
	)

	if err := app.Run(); err != nil {
		panic(err)
	}
}

func newSignalServer() *signal.Server {
	srv := signal.NewServer(
		signal.WithRecoveryHandler(signal.DefaultRecoveryHandler),
	)

	srv.Register(&exampleHandler{}, &example2Handler{})

	return srv
}

type exampleHandler struct{}

func (h *exampleHandler) Listen() []os.Signal {
	return []os.Signal{syscall.SIGUSR1, syscall.SIGUSR2}
}

func (h *exampleHandler) Handle(sig os.Signal) {
	println("exampleHandler signal:", sig)
}

type example2Handler struct{}

func (h *example2Handler) Listen() []os.Signal {
	return []os.Signal{syscall.SIGUSR1}
}

func (h *example2Handler) Handle(os.Signal) {
	panic("example2Handler panic")
}

Send signal:

$ kill -SIGUSR2 42750
$ kill -SIGUSR1 42750

Output:

INFO msg=[Signal] server starting
exampleHandler signal: (0x104ff0240,0x1051875b8)
exampleHandler signal: (0x104ff0240,0x1051875b0)
ERROR msg=[Signal] handler panic (user defined signal 1): example2Handler panic

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRecoveryHandler = func(err interface{}, sig os.Signal, handler Handler) {
	log.Errorf("[Signal] handler panic (%s): %v", sig, err)
}

Functions

This section is empty.

Types

type AsyncFeature added in v2.2.1

type AsyncFeature interface {
	Async() bool
}

type Handler

type Handler interface {
	Listen() []os.Signal
	Handle(os.Signal)
}

type Option

type Option func(*Server)

func AddHandler

func AddHandler(handler ...Handler) Option

func WithRecoveryHandler

func WithRecoveryHandler(handler func(interface{}, os.Signal, Handler)) Option

type Server

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

func NewServer

func NewServer(opts ...Option) *Server

func (*Server) Register

func (s *Server) Register(handlers ...Handler)

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop(context.Context) error

Jump to

Keyboard shortcuts

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