blob: c550842f132c2cff0c8cf64793bbb1b25d28d944 [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)
51
52 // Block until we receive a signal on the channel
53 <-sigchan
54
55 log.Info("shutting down on signal as requested")
56
57 cleanExit(nil)
58}
59
60func errHandler() {
61
62 err := <-errChan
63
64 cleanExit(err)
65}
66
67func cleanExit(err error) {
68 // Log the shutdown
69 if arProxy != nil {
70 for _, srvr := range arProxy.servers {
71 if srvr.running {
72 log.With(log.Fields{"server":srvr.name}).Debug("Closing server")
73 srvr.proxyServer.GracefulStop();
74 srvr.proxyListener.Close();
75 }
76 }
77 }
78 for _,cl := range(bClusters) {
79 for _, bknd := range(cl.backends) {
80 log.Debugf("Closing backend %s", bknd.name)
81 for _,conn := range(bknd.connections) {
82 log.Debugf("Closing connection %s", conn.name)
83 conn.close()
84 }
85 }
86 }
87 doneChan <- err
88 //os.Exit(0)
89}
90