@@ -13,7 +13,7 @@ import (
1313)
1414
1515const (
16- buffer = 10000
16+ buffer = 30
1717 totalWorkers = 10
1818 geoNamesUserName = "ardanstudios"
1919)
@@ -24,32 +24,35 @@ func main() {
2424
2525// FindTimezones use concurrency to retrieve timezones for a set of stations.
2626func FindTimezones () {
27- // This channel buffers all the stations that need to be processed .
27+ // This channel buffers all the work coming in off the stream .
2828 stream := make (chan * station.Station , buffer )
2929
30- // This channel buffers the current stations being processed.
30+ // This channel contains the current work being processed.
3131 work := make (chan * station.Station , totalWorkers )
3232
33- // This channel is used to communicate stations that have been processed
33+ // This channel is used to communicate work that has been processed
3434 // back to the main processing routine.
3535 processed := make (chan * station.Station , 1 )
3636
3737 totalStations := StartStream (stream )
3838 LaunchWorkRoutines (work , processed )
3939 stations := ProcessWork (stream , work , processed , totalStations )
4040
41- // Display the results for all the processed station.
41+ // Shutdown the goroutines that perform the work
42+ close (work )
43+
44+ // Display the results for all the processed work.
4245 for _ , station := range stations {
4346 if station .Err != nil {
4447 log .Printf ("Station[%s]\t ERROR[%s]\n " , station .Name , station .Err )
4548 } else {
4649 log .Printf ("Station[%s]\t TZ[%s]\n " , station .Name , station .Timezone .TimezoneId )
4750 }
48-
4951 }
5052}
5153
52- // StartStream loads the stations into the stream channel so they can be process.
54+ // StartStream simulates the loading of work from a stream into the stream channel so
55+ // it can be processed.
5356func StartStream (stream chan <- * station.Station ) int {
5457 // Retrieve the set of stations to process.
5558 stations := station .LoadStations ()
@@ -71,35 +74,34 @@ func StartStream(stream chan<- *station.Station) int {
7174 return len (stations )
7275}
7376
74- // LaunchWorkRoutines launch the goroutines call into the GeoNames api to retrieve
75- // the timezone information for each station.
77+ // LaunchWorkRoutines launch the goroutines that perform the actual work. These goroutines
78+ // call into the GeoNames api to retrieve the timezone information for each station.
7679func LaunchWorkRoutines (work <- chan * station.Station , processed chan <- * station.Station ) {
7780 for worker := 0 ; worker <= totalWorkers ; worker ++ {
7881 // Launch a goroutine to process work.
7982 go func () {
80- for {
81- select {
82- // Pull a station off the work channel.
83- case station := <- work :
84- log .Printf ("Work\t : Processing\t : %s\n " , station .Name )
85-
86- // Call into the geonames api.
87- station .Timezone , station .Err = timezone .RetrieveGeoNamesTimezone (
88- station .Location .Coordinates [1 ],
89- station .Location .Coordinates [0 ],
90- geoNamesUserName )
91-
92- // Push the station on the processed channel
93- // so it can be saved and returned.
94- processed <- station
95- }
83+ // Pull a station off the work channel.
84+ for station := range work {
85+ log .Printf ("Work\t : Processing\t : %s\n " , station .Name )
86+
87+ // Call into the geonames api.
88+ station .Timezone , station .Err = timezone .RetrieveGeoNamesTimezone (
89+ station .Location .Coordinates [1 ],
90+ station .Location .Coordinates [0 ],
91+ geoNamesUserName )
92+
93+ // Push the station on the processed channel
94+ // so it can be saved and returned.
95+ processed <- station
9696 }
97+
98+ log .Println ("Work\t : Shutdown" )
9799 }()
98100 }
99101}
100102
101- // ProcessWork coordinates the work of retrieving timezone information for all the stations in the stream. It pushes
102- // stations on the work channel and receives processed stations .
103+ // ProcessWork coordinates the work of retrieving timezone information for all the work in the stream. It move
104+ // work from the stream into the work channel and receives processed worked .
103105func ProcessWork (stream <- chan * station.Station , work chan <- * station.Station , processed <- chan * station.Station , totalStations int ) []* station.Station {
104106 // Create a waitgroup to wait for all the stations to be processed.
105107 var waitGroup sync.WaitGroup
@@ -116,7 +118,7 @@ func ProcessWork(stream <-chan *station.Station, work chan<- *station.Station, p
116118
117119 for {
118120 select {
119- // Pull stations off the stream.
121+ // Pull work off the stream.
120122 case station , ok := <- streamBuffer :
121123 if ! ok {
122124 log .Println ("Stream\t : Closed\t :" )
@@ -138,7 +140,7 @@ func ProcessWork(stream <-chan *station.Station, work chan<- *station.Station, p
138140 streamBuffer = nil
139141 }
140142
141- // Stations that have been processed.
143+ // Work that has been processed.
142144 case station := <- processed :
143145 busyWorkers --
144146 if station .Err != nil {
0 commit comments