@@ -21,8 +21,9 @@ import (
2121 "github.com/dnstapir/observation-encoder/internal/nats"
2222)
2323
24- const c_ENVVAR_OVERRIDE_NATS_URL = "DNSTAPIR_NATS_URL"
24+ const env_DNSTAPIR_NATS_URL = "DNSTAPIR_NATS_URL"
2525
26+ /* Rewritten if building with make */
2627var commit = "BAD-BUILD"
2728
2829type conf struct {
@@ -84,13 +85,16 @@ func main() {
8485
8586 confDecoder := toml .NewDecoder (file )
8687 if confDecoder == nil {
87- log .Error ("Problem decoding config file '%s', exiting..." , configFile )
88+ log .Error ("Problem creating decoder for config file '%s', exiting..." , configFile )
8889 os .Exit (- 1 )
8990 }
9091
9192 confDecoder .DisallowUnknownFields ()
92- confDecoder .Decode (& mainConf )
93- file .Close () // TODO okay to close here while also using defer above?
93+ err = confDecoder .Decode (& mainConf )
94+ if err != nil {
95+ log .Error ("Problem decoding config file '%s': %s" , configFile , err )
96+ os .Exit (- 1 )
97+ }
9498
9599 debugFlag = debugFlag || mainConf .Debug
96100
@@ -105,12 +109,13 @@ func main() {
105109 })
106110 if err != nil {
107111 log .Error ("Error creating nats log: %s" , err )
112+ os .Exit (- 1 )
108113 }
109114
110- envNatsUrl , overrideNatsUrl := os .LookupEnv (c_ENVVAR_OVERRIDE_NATS_URL )
115+ envNatsUrl , overrideNatsUrl := os .LookupEnv (env_DNSTAPIR_NATS_URL )
111116 if overrideNatsUrl {
112117 mainConf .Nats .Url = envNatsUrl
113- log .Info ("Overriding NATS url with environment variable '%s'" , c_ENVVAR_OVERRIDE_NATS_URL )
118+ log .Info ("Overriding NATS url with environment variable '%s'" , env_DNSTAPIR_NATS_URL )
114119 }
115120
116121 mainConf .Nats .Log = natslog
@@ -131,6 +136,7 @@ func main() {
131136 })
132137 if err != nil {
133138 log .Error ("Error creating libtapir log: %s" , err )
139+ os .Exit (- 1 )
134140 }
135141
136142 mainConf .Libtapir .Log = libtapirlog
@@ -151,6 +157,7 @@ func main() {
151157 })
152158 if err != nil {
153159 log .Error ("Error creating app log: %s" , err )
160+ os .Exit (- 1 )
154161 }
155162
156163 mainConf .Log = applog
@@ -173,6 +180,7 @@ func main() {
173180 })
174181 if err != nil {
175182 log .Error ("Error creating cert log: %s" , err )
183+ os .Exit (- 1 )
176184 }
177185
178186 mainConf .Cert .Log = certlog
@@ -193,6 +201,7 @@ func main() {
193201 })
194202 if err != nil {
195203 log .Error ("Error creating API log: %s" , err )
204+ os .Exit (- 1 )
196205 }
197206 mainConf .Api .Log = apilog
198207 mainConf .Api .App = appHandle
@@ -214,20 +223,18 @@ func main() {
214223 defer signal .Stop (sigChan )
215224
216225 ctx , cancel := context .WithCancel (context .Background ())
217- exitCh := make (chan common.Exit )
226+ exitCh := make (chan common.Exit , 10 ) // TODO adjustable buffer?
218227
219228 log .Info ("Starting threads..." )
220229
221- go appHandle . Run ( ctx , exitCh )
222-
223- go apiHandle .Run (ctx , exitCh )
224- go certHandle .Run (ctx , exitCh )
230+ var wg sync. WaitGroup
231+ wg . Go ( func () { appHandle . Run ( ctx , exitCh ) })
232+ wg . Go ( func () { apiHandle .Run (ctx , exitCh ) } )
233+ wg . Go ( func () { certHandle .Run (ctx , exitCh ) } )
225234
226235 log .Info ("Threads started!" )
227236
228237 exitLoop := false
229- var wg sync.WaitGroup
230- wg .Add (1 )
231238 for {
232239 select {
233240 case s , ok := <- sigChan :
@@ -255,45 +262,29 @@ func main() {
255262 }
256263 if exitLoop || (sigChan == nil && exitCh == nil ) {
257264 log .Info ("Leaving toplevel loop" )
258- wg .Done ()
259265 break
260266 }
261267 }
262268
263- log .Info ("Cancelling, giving threads some time to finish... " )
269+ log .Info ("Cancelling threads" )
264270 cancel ()
265- timeout := make (chan bool , 1 )
271+
272+ log .Info ("Waiting for threads to finish" )
273+
274+ done := make (chan struct {})
266275 go func () {
267- time . Sleep ( 2 * time . Second )
268- timeout <- true
276+ wg . Wait ( )
277+ close ( done )
269278 }()
270279
271- TIMEOUT_LOOP:
272- for {
273- select {
274- case exit , ok := <- exitCh :
275- if ok {
276- if exit .Err != nil {
277- log .Error ("%s exited with error: '%s'" , exit .ID , exit .Err )
278- } else {
279- log .Info ("%s done!" , exit .ID )
280- }
281- } else {
282- log .Info ("exit channel was closed during shutdown" )
283- exitCh = nil
284- }
285- case <- timeout :
286- log .Debug ("Time's up. Proceeding with shutdown." )
287- break TIMEOUT_LOOP
288- }
289- if exitCh == nil {
290- log .Warning ("exit channel closed unexpectedly" )
291- break TIMEOUT_LOOP
292- }
280+ const shutdownTimeout = 30 * time .Second
281+ select {
282+ case <- done :
283+ log .Info ("Threads finished" )
284+ case <- time .After (shutdownTimeout ):
285+ log .Error ("Timed out waiting for threads to finish after %s" , shutdownTimeout )
293286 }
294287
295- close (exitCh )
296- wg .Wait ()
297288 log .Info ("Exiting..." )
298289 os .Exit (0 )
299290}
0 commit comments