blob: 6cc32add60abf89b8f360fb99a0235a35838ff19 [file] [log] [blame]
sslobodr392ebd52019-01-18 12:41:49 -05001/*
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
22package afrouter
23
24import (
25 "os"
26 "syscall"
27 "os/signal"
28 "github.com/opencord/voltha-go/common/log"
29)
30
31var errChan = make(chan error)
32var doneChan = make(chan error)
33var holdChan = make(chan int)
34
35
36func 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
46func 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)
sslobodrd6e07e72019-01-31 16:07:20 -050051 signal.Notify(sigchan, syscall.SIGKILL)
sslobodr392ebd52019-01-18 12:41:49 -050052
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
61func errHandler() {
62
63 err := <-errChan
64
65 cleanExit(err)
66}
67
68func 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 }
sslobodr5f0b5a32019-01-24 07:45:19 -050079 for _,cl := range bClusters {
80 for _, bknd := range cl.backends {
sslobodr392ebd52019-01-18 12:41:49 -050081 log.Debugf("Closing backend %s", bknd.name)
sslobodr5f0b5a32019-01-24 07:45:19 -050082 for _,conn := range bknd.connections {
sslobodr392ebd52019-01-18 12:41:49 -050083 log.Debugf("Closing connection %s", conn.name)
84 conn.close()
85 }
86 }
87 }
88 doneChan <- err
89 //os.Exit(0)
90}
91