Skip to content

Commit

Permalink
feat(signal): Improved invocation of asynchronous features (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Jan 30, 2024
1 parent 3168cbf commit ad0f9e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func (h *exampleHandler) Handle(sig os.Signal) {
println("exampleHandler signal:", sig)
}

type example2Handler struct{}
type example2Handler struct{
signal.AsyncFeature // async feature
}

func (h *example2Handler) Listen() []os.Signal {
return []os.Signal{syscall.SIGUSR1}
Expand Down
8 changes: 7 additions & 1 deletion signal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type Handler interface {
Handle(os.Signal)
}

type AsyncFeature interface {
type asyncFeature interface {
Async() bool
}

type AsyncFeature struct{}

func (*AsyncFeature) Async() bool {
return true
}
4 changes: 2 additions & 2 deletions signal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (s *Server) Start(ctx context.Context) error {
case sig := <-ch:
if hs, ok := handlers[sig]; ok {
for _, h := range hs {
// if Support AsyncFeature
if async, ok := h.(AsyncFeature); ok && async.Async() {
// if Support asyncFeature
if async, ok := h.(asyncFeature); ok && async.Async() {
go s.handle(sig, h)
} else {
s.handle(sig, h)
Expand Down

0 comments on commit ad0f9e0

Please sign in to comment.