Skip to content

Commit fba1e09

Browse files
Updated code example to be more realistic.
1 parent 6df80fa commit fba1e09

1 file changed

Lines changed: 46 additions & 25 deletions

File tree

‎chapter6/patterns/timer/timer.go‎

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
// Copyright Information.
2-
//
31
// This sample program demonstrations how to use a timer channel and hook
42
// into the OS using a channel to receive OS events.
53
package main
64

75
import (
6+
"errors"
87
"log"
98
"os"
109
"os/signal"
1110
"time"
1211
)
1312

14-
// Give the program 5 seconds to complete the work.
15-
const timeoutSeconds = 5 * time.Second
13+
// Give the program 3 seconds to complete the work.
14+
const timeoutSeconds = 3 * time.Second
1615

1716
var (
1817
// sigChan receives os signals.
@@ -44,7 +43,7 @@ ControlLoop:
4443
select {
4544
case <-sigChan:
4645
// Interrupt event signaled by the operation system.
47-
log.Println("OS INTERRUPT - Shutting Down Early")
46+
log.Println("OS INTERRUPT")
4847

4948
// Close the channel to signal to the processor
5049
// it needs to shutdown.
@@ -60,8 +59,8 @@ ControlLoop:
6059
os.Exit(1)
6160

6261
case err := <-complete:
63-
// Everything complete within the time given.
64-
log.Printf("Task Complete: Error[%s]", err)
62+
// Everything completed within the time given.
63+
log.Printf("Task Completed: Error[%s]", err)
6564
break ControlLoop
6665
}
6766
}
@@ -70,6 +69,23 @@ ControlLoop:
7069
log.Println("Process Ended")
7170
}
7271

72+
// checkShutdown checks the shutdown flag to determine
73+
// if we have been asked to interrupt processing.
74+
func checkShutdown() bool {
75+
select {
76+
case <-shutdown:
77+
// We have been asked to shutdown cleanly.
78+
log.Println("checkShutdown - Shutdown Early")
79+
return true
80+
81+
default:
82+
// If the shutdown channel was not closed,
83+
// presume with normal processing.
84+
}
85+
86+
return false
87+
}
88+
7389
// processor provides the main program logic for the program.
7490
func processor(complete chan<- error) {
7591
log.Println("Processor - Starting")
@@ -81,33 +97,38 @@ func processor(complete chan<- error) {
8197
// Defer the send on the channel so it happens
8298
// regardless of how this function terminates.
8399
defer func() {
84-
log.Println("Processor - Completed")
100+
// Capture any potential panic.
101+
if r := recover(); r != nil {
102+
log.Println("Processor - Panic", r)
103+
}
85104

86105
// Signal the goroutine we have shutdown.
87106
complete <- err
88107
}()
89108

90-
// Simulate some iterative work.
91-
for work := 0; work < 5; work++ {
92-
// Perform some work.
93-
err = doWork()
109+
// Perform the work.
110+
err = doWork()
94111

95-
select {
96-
case <-shutdown:
97-
log.Println("Processor - Shutdown Early")
98-
// We have been asked to shutdown cleanly.
99-
return
100-
101-
default:
102-
// If the shutdown channel was not closed,
103-
// presume with normal processing.
104-
}
105-
}
112+
log.Println("Processor - Completed")
106113
}
107114

108-
// doWork simulates a function we call to get our work done.
115+
// doWork simulates task work.
109116
func doWork() error {
110-
log.Println("Processor - Doing Work")
117+
log.Println("Processor - Task 1")
118+
time.Sleep(2 * time.Second)
119+
120+
if checkShutdown() {
121+
return errors.New("Early Shutdown")
122+
}
123+
124+
log.Println("Processor - Task 2")
125+
time.Sleep(1 * time.Second)
126+
127+
if checkShutdown() {
128+
return errors.New("Early Shutdown")
129+
}
130+
131+
log.Println("Processor - Task 3")
111132
time.Sleep(1 * time.Second)
112133

113134
return nil

0 commit comments

Comments
 (0)