cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cmd/cloudflared/tunnel/signal.go
79lines · modecode
| 1 | package tunnel |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/signal" |
| 6 | "syscall" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // waitForSignal notifies all routines to shutdownC immediately by closing the |
| 11 | // shutdownC when one of the routines in main exits, or when this process receives |
| 12 | // SIGTERM/SIGINT |
| 13 | func waitForSignal(errC chan error, shutdownC chan struct{}) error { |
| 14 | signals := make(chan os.Signal, 10) |
| 15 | signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT) |
| 16 | defer signal.Stop(signals) |
| 17 | |
| 18 | select { |
| 19 | case err := <-errC: |
| 20 | close(shutdownC) |
| 21 | return err |
| 22 | case <-signals: |
| 23 | close(shutdownC) |
| 24 | case <-shutdownC: |
| 25 | } |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | // waitForSignalWithGraceShutdown notifies all routines to shutdown immediately |
| 30 | // by closing the shutdownC when one of the routines in main exits. |
| 31 | // When this process recieves SIGTERM/SIGINT, it closes the graceShutdownC to |
| 32 | // notify certain routines to start graceful shutdown. When grace period is over, |
| 33 | // or when some routine exits, it notifies the rest of the routines to shutdown |
| 34 | // immediately by closing shutdownC. |
| 35 | // In the case of handling commands from Windows Service Manager, closing graceShutdownC |
| 36 | // initiate graceful shutdown. |
| 37 | func waitForSignalWithGraceShutdown(errC chan error, |
| 38 | shutdownC, graceShutdownC chan struct{}, |
| 39 | gracePeriod time.Duration, |
| 40 | ) error { |
| 41 | signals := make(chan os.Signal, 10) |
| 42 | signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT) |
| 43 | defer signal.Stop(signals) |
| 44 | |
| 45 | select { |
| 46 | case err := <-errC: |
| 47 | close(graceShutdownC) |
| 48 | close(shutdownC) |
| 49 | return err |
| 50 | case <-signals: |
| 51 | close(graceShutdownC) |
| 52 | waitForGracePeriod(signals, errC, shutdownC, gracePeriod) |
| 53 | case <-graceShutdownC: |
| 54 | waitForGracePeriod(signals, errC, shutdownC, gracePeriod) |
| 55 | case <-shutdownC: |
| 56 | close(graceShutdownC) |
| 57 | } |
| 58 | |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func waitForGracePeriod(signals chan os.Signal, |
| 63 | errC chan error, |
| 64 | shutdownC chan struct{}, |
| 65 | gracePeriod time.Duration, |
| 66 | ) { |
| 67 | logger.Infof("Initiating graceful shutdown...") |
| 68 | // Unregister signal handler early, so the client can send a second SIGTERM/SIGINT |
| 69 | // to force shutdown cloudflared |
| 70 | signal.Stop(signals) |
| 71 | graceTimerTick := time.Tick(gracePeriod) |
| 72 | // send close signal via shutdownC when grace period expires or when an |
| 73 | // error is encountered. |
| 74 | select { |
| 75 | case <-graceTimerTick: |
| 76 | case <-errC: |
| 77 | } |
| 78 | close(shutdownC) |
| 79 | } |
| 80 | |