blob: 5416fda1dabc08027289fb1694211f719ef5bafc [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 (
sslobodr392ebd52019-01-18 12:41:49 -050025 "github.com/opencord/voltha-go/common/log"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040026 "os"
27 "os/signal"
28 "syscall"
sslobodr392ebd52019-01-18 12:41:49 -050029)
30
31var errChan = make(chan error)
32var doneChan = make(chan error)
33var holdChan = make(chan int)
34
sslobodr392ebd52019-01-18 12:41:49 -050035func InitExitHandler() error {
36
37 // Start the signal handler
38 go signalHandler()
39 // Start the error handler
40 go errHandler()
41
42 return nil
43}
44
45func signalHandler() {
46 // Make signal channel and register notifiers for Interupt and Terminate
47 sigchan := make(chan os.Signal, 1)
48 signal.Notify(sigchan, os.Interrupt)
49 signal.Notify(sigchan, syscall.SIGTERM)
sslobodrd6e07e72019-01-31 16:07:20 -050050 signal.Notify(sigchan, syscall.SIGKILL)
sslobodr392ebd52019-01-18 12:41:49 -050051
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 {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040072 log.With(log.Fields{"server": srvr.name}).Debug("Closing server")
73 srvr.proxyServer.GracefulStop()
74 srvr.proxyListener.Close()
sslobodr392ebd52019-01-18 12:41:49 -050075 }
76 }
77 }
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040078 for _, cl := range bClusters {
sslobodr5f0b5a32019-01-24 07:45:19 -050079 for _, bknd := range cl.backends {
sslobodr392ebd52019-01-18 12:41:49 -050080 log.Debugf("Closing backend %s", bknd.name)
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040081 for _, conn := range bknd.connections {
sslobodr392ebd52019-01-18 12:41:49 -050082 log.Debugf("Closing connection %s", conn.name)
83 conn.close()
84 }
85 }
86 }
87 doneChan <- err
88 //os.Exit(0)
89}