blob: 30587e0ee625cb3fba642e65243bdf01da1282f7 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001/*
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
17// This file implements an exit handler that tries to shut down all the
18// running servers before finally exiting. There are 2 triggers to this
19// clean exit thread: signals and an exit channel.
20
21package afrouter
22
23import (
Scott Bakerf579f132019-10-24 14:31:41 -070024 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakere7144bc2019-10-01 14:16:47 -070025 "os"
26 "os/signal"
27 "syscall"
28)
29
30var errChan = make(chan error)
31var doneChan = make(chan error)
32
33func InitExitHandler() error {
34
35 // Start the signal handler
36 go signalHandler()
37 // Start the error handler
38 go errHandler()
39
40 return nil
41}
42
43func signalHandler() {
44 // Make signal channel and register notifiers for Interupt and Terminate
45 sigchan := make(chan os.Signal, 1)
46 signal.Notify(sigchan, os.Interrupt)
47 signal.Notify(sigchan, syscall.SIGTERM)
Scott Bakere7144bc2019-10-01 14:16:47 -070048
49 // Block until we receive a signal on the channel
50 <-sigchan
51
52 log.Info("shutting down on signal as requested")
53
54 cleanExit(nil)
55}
56
57func errHandler() {
58
59 err := <-errChan
60
61 cleanExit(err)
62}
63
64func cleanExit(err error) {
Divya Desai43b86882019-10-07 09:59:34 +000065
66 //Closing the streaming connections
67 for _, cl := range clusters {
68 for _, bknd := range cl.backends {
69 log.Debugf("Closing backend %s", bknd.name)
70 for streamReq := range bknd.activeRequests {
71 if streamReq.isStreamingResponse || streamReq.isStreamingRequest {
72 connection := streamReq.backend.connections
73 for _, conn := range connection {
74 log.Debugf("Forcefully closing connection %s serving Streaming request/response", conn.name)
75 conn.close()
76 }
77 }
78 }
79 }
80 }
81
Scott Bakere7144bc2019-10-01 14:16:47 -070082 // Log the shutdown
83 if arProxy != nil {
84 for _, srvr := range arProxy.servers {
85 if srvr.running {
86 log.With(log.Fields{"server": srvr.name}).Debug("Closing server")
87 srvr.proxyServer.GracefulStop()
88 srvr.proxyListener.Close()
89 }
90 }
91 }
Divya Desai43b86882019-10-07 09:59:34 +000092
Scott Bakere7144bc2019-10-01 14:16:47 -070093 doneChan <- err
94 //os.Exit(0)
95}