sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | // gRPC affinity router with active/active backends |
| 17 | |
| 18 | // This file implements an exit handler that tries to shut down all the |
| 19 | // running servers before finally exiting. There are 2 triggers to this |
| 20 | // clean exit thread: signals and an exit channel. |
| 21 | |
| 22 | package afrouter |
| 23 | |
| 24 | import ( |
| 25 | "os" |
| 26 | "syscall" |
| 27 | "os/signal" |
| 28 | "github.com/opencord/voltha-go/common/log" |
| 29 | ) |
| 30 | |
| 31 | var errChan = make(chan error) |
| 32 | var doneChan = make(chan error) |
| 33 | var holdChan = make(chan int) |
| 34 | |
| 35 | |
| 36 | func InitExitHandler() error { |
| 37 | |
| 38 | // Start the signal handler |
| 39 | go signalHandler() |
| 40 | // Start the error handler |
| 41 | go errHandler() |
| 42 | |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func signalHandler() { |
| 47 | // Make signal channel and register notifiers for Interupt and Terminate |
| 48 | sigchan := make(chan os.Signal, 1) |
| 49 | signal.Notify(sigchan, os.Interrupt) |
| 50 | signal.Notify(sigchan, syscall.SIGTERM) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 51 | signal.Notify(sigchan, syscall.SIGKILL) |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 52 | |
| 53 | // Block until we receive a signal on the channel |
| 54 | <-sigchan |
| 55 | |
| 56 | log.Info("shutting down on signal as requested") |
| 57 | |
| 58 | cleanExit(nil) |
| 59 | } |
| 60 | |
| 61 | func errHandler() { |
| 62 | |
| 63 | err := <-errChan |
| 64 | |
| 65 | cleanExit(err) |
| 66 | } |
| 67 | |
| 68 | func cleanExit(err error) { |
| 69 | // Log the shutdown |
| 70 | if arProxy != nil { |
| 71 | for _, srvr := range arProxy.servers { |
| 72 | if srvr.running { |
| 73 | log.With(log.Fields{"server":srvr.name}).Debug("Closing server") |
| 74 | srvr.proxyServer.GracefulStop(); |
| 75 | srvr.proxyListener.Close(); |
| 76 | } |
| 77 | } |
| 78 | } |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 79 | for _,cl := range bClusters { |
| 80 | for _, bknd := range cl.backends { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 81 | log.Debugf("Closing backend %s", bknd.name) |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 82 | for _,conn := range bknd.connections { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 83 | log.Debugf("Closing connection %s", conn.name) |
| 84 | conn.close() |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | doneChan <- err |
| 89 | //os.Exit(0) |
| 90 | } |
| 91 | |