55package main
66
77import (
8- "fmt"
98 "log"
109 "os"
1110 "os/signal"
12- "sync/atomic"
1311 "time"
1412)
1513
16- // Give the program 10 seconds to complete the work.
17- const timeoutSeconds = 10 * time .Second
14+ // Give the program 5 seconds to complete the work.
15+ const timeoutSeconds = 5 * time .Second
1816
19- // Flag to indicate to running goroutines to shut
20- // down the program immediately .
21- var shutdown int32 = 0
17+ var (
18+ // sigChan receives os signals .
19+ sigChan = make ( chan os. Signal , 1 )
2220
21+ // timeout limits the amount of time the program has.
22+ timeout = time .After (timeoutSeconds )
23+
24+ // complete is used to report processing is done.
25+ complete = make (chan error )
26+
27+ // shutdown provides system wide notification.
28+ shutdown = make (chan struct {})
29+ )
30+
31+ // main is the entry point for all Go programs.
2332func main () {
2433 log .Println ("Starting Process" )
2534
26- // Create a channel to talk with the OS.
27- sigChan := make (chan os.Signal , 1 )
35+ // We want to receive all interrupt based signals.
2836 signal .Notify (sigChan , os .Interrupt )
2937
30- // Set the timeout channel.
31- timeout := time .After (timeoutSeconds )
32-
3338 // Launch the process.
34- log .Println ("Launching Processor" )
35- complete := make (chan error )
39+ log .Println ("Launching Processors" )
3640 go processor (complete )
3741
3842ControlLoop:
@@ -41,11 +45,8 @@ ControlLoop:
4145 case <- sigChan :
4246 // Interrupt event signaled by the operation system.
4347 log .Println ("OS INTERRUPT - Shutting Down Early" )
44-
45- // Set the flag to indicate the program should be shutdown early.
46- // Try to shutdown cleanly.
47- atomic .StoreInt32 (& shutdown , 1 )
48- continue
48+ close (shutdown )
49+ sigChan = nil
4950
5051 case <- timeout :
5152 // We have taken too much time. Kill the app hard.
@@ -64,16 +65,6 @@ ControlLoop:
6465 return
6566}
6667
67- // isShutdown returns the value of the shutdown flag.
68- func isShutdown () bool {
69- value := atomic .LoadInt32 (& shutdown )
70- if value == 1 {
71- return true
72- }
73-
74- return false
75- }
76-
7768// processor provides the main program logic for the program.
7869func processor (complete chan <- error ) {
7970 log .Println ("Processor - Starting" )
@@ -91,15 +82,19 @@ func processor(complete chan<- error) {
9182 }()
9283
9384 // Simulate some iterative work.
94- for {
85+ for work := 0 ; work < 5 ; work ++ {
9586 log .Println ("Processor - Doing Work" )
96- time .Sleep (5 * time .Second )
87+ time .Sleep (1 * time .Second )
9788
98- // Check if we are being asked to shutdown before we
99- // complete our work.
100- if isShutdown () {
101- err = fmt . Errorf ( "Processor - Shutting Down" )
89+ select {
90+ case <- shutdown :
91+ log . Println ( "Processor - Shutdown Early" )
92+ // We have been asked to shutdown cleanly.
10293 return
94+
95+ default :
96+ // If the shutdown channel was not closed,
97+ // presume with normal processing.
10398 }
10499 }
105100}
0 commit comments