github.com/Myriad-Dreamin/tarus@v0.0.0-20220422082640-5379b6998284/cmd/tarus/main_linux.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"golang.org/x/sys/unix"
     7  	"os"
     8  )
     9  
    10  var handledSignals = []os.Signal{
    11  	unix.SIGTERM,
    12  	unix.SIGINT,
    13  	unix.SIGUSR1,
    14  	unix.SIGPIPE,
    15  }
    16  
    17  func handleSignals(ctx context.Context, signals chan os.Signal, stop func(), cancel func()) chan struct{} {
    18  	done := make(chan struct{}, 1)
    19  	go func() {
    20  		for {
    21  			select {
    22  			case s := <-signals:
    23  
    24  				// Do not print message when dealing with SIGPIPE, which may cause
    25  				// nested signals and consume lots of cpu bandwidth.
    26  				if s == unix.SIGPIPE {
    27  					continue
    28  				}
    29  
    30  				fmt.Println("received signal")
    31  				switch s {
    32  				case unix.SIGUSR1:
    33  					break
    34  				default:
    35  					cancel()
    36  					stop()
    37  					close(done)
    38  					return
    39  				}
    40  			}
    41  		}
    42  	}()
    43  	return done
    44  }