Skip to content

Commit 903bb5c

Browse files
Code patterns for chapter 6
1 parent fed2c59 commit 903bb5c

5 files changed

Lines changed: 25 additions & 149 deletions

File tree

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// http://www.golangpatterns.info/concurrency/semaphores
2-
31
// Copyright Information.
42
//
53
// This sample program demonstrations how to implement a semaphore using
@@ -100,7 +98,7 @@ func main() {
10098
// goroutines to process the work, returning the created ReaderWriter value.
10199
func start(name string, maxReads int, maxReaders int) *readerWriter {
102100
// Create a value of readerWriter and initialize.
103-
rw := &readerWriter{
101+
rw := readerWriter{
104102
name: name,
105103
shutdown: make(chan empty),
106104
maxReads: maxReads,
@@ -118,7 +116,7 @@ func start(name string, maxReads int, maxReaders int) *readerWriter {
118116
rw.reportShutdown.Add(1)
119117
go rw.writer()
120118

121-
return rw
119+
return &rw
122120
}
123121

124122
// stop signals to all goroutines to shutdown and reports back
@@ -250,7 +248,7 @@ func (rw *readerWriter) WriteUnlock() {
250248
// Acquire attempts to secure the specified number of buffers from the
251249
// semaphore channel.
252250
func (s semaphore) Acquire(buffers int) {
253-
e := empty{}
251+
var e empty
254252

255253
// Write data to secure each buffer.
256254
for buffer := 0; buffer < buffers; buffer++ {
@@ -264,4 +262,4 @@ func (s semaphore) Release(buffers int) {
264262
for buffer := 0; buffer < buffers; buffer++ {
265263
<-s
266264
}
267-
}
265+
}

‎chapter6/patterns/stream/station/station.go‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ import (
66
"log"
77
"os"
88

9-
"test/book/timezone"
9+
"github.com/goinaction/code/chapter6/patterns/stream/timezone"
1010
)
1111

12-
const (
13-
fixture = "station/stations.json"
14-
)
12+
const fixture = "station/stations.json"
1513

1614
type (
17-
1815
// StationLocation contains the buoys latitude and longitude location.
1916
StationLocation struct {
2017
Type string `bson:"type"`
@@ -32,7 +29,7 @@ type (
3229
)
3330

3431
// LoadStations retrieves and unmarshals the data for the program.
35-
func LoadStations() []Station {
32+
func LoadStations() []*Station {
3633
pwd, err := os.Getwd()
3734
if err != nil {
3835
log.Fatalln(err)
@@ -43,7 +40,7 @@ func LoadStations() []Station {
4340
log.Fatalln(err)
4441
}
4542

46-
var stations []Station
43+
var stations []*Station
4744
err = json.Unmarshal(data, &stations)
4845
if err != nil {
4946
log.Fatalln(err)

‎chapter6/patterns/stream/stream.go‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"log"
99
"sync"
1010

11-
"test/book/station"
12-
"test/book/timezone"
11+
"github.com/goinaction/code/chapter6/patterns/stream/station"
12+
"github.com/goinaction/code/chapter6/patterns/stream/timezone"
1313
)
1414

1515
const (
@@ -25,14 +25,14 @@ func main() {
2525
// FindTimezones use concurrency to retrieve timezones for a set of stations.
2626
func FindTimezones() {
2727
// This channel contains all the stations that need to be processed.
28-
stream := make(chan station.Station, buffer)
28+
stream := make(chan *station.Station, buffer)
2929

3030
// This channel contains the current stations being processed.
31-
work := make(chan station.Station, totalWorkers)
31+
work := make(chan *station.Station, totalWorkers)
3232

3333
// This channel is used to communicate stations that have been processed
3434
// back to the main processing routine.
35-
processed := make(chan station.Station, 1)
35+
processed := make(chan *station.Station, 1)
3636

3737
totalStations := StartStream(stream)
3838
LaunchWorkRoutines(work, processed)
@@ -50,7 +50,7 @@ func FindTimezones() {
5050
}
5151

5252
// StartStream loads the stations into the stream channel so they can be process.
53-
func StartStream(stream chan<- station.Station) int {
53+
func StartStream(stream chan<- *station.Station) int {
5454
// Retrieve the set of stations to process.
5555
stations := station.LoadStations()
5656

@@ -73,7 +73,7 @@ func StartStream(stream chan<- station.Station) int {
7373

7474
// LaunchWorkRoutines launch the goroutines call into the GeoNames api to retrieve
7575
// the timezone information for each station.
76-
func LaunchWorkRoutines(work <-chan station.Station, processed chan<- station.Station) {
76+
func LaunchWorkRoutines(work <-chan *station.Station, processed chan<- *station.Station) {
7777
for worker := 0; worker <= totalWorkers; worker++ {
7878
// Launch a goroutine to process work.
7979
go func() {
@@ -100,13 +100,13 @@ func LaunchWorkRoutines(work <-chan station.Station, processed chan<- station.St
100100

101101
// ProcessWork coordinates the work of retrieving timezone information for all the stations in the stream. It pushes
102102
// stations on the work channel and receives processed stations.
103-
func ProcessWork(stream <-chan station.Station, work chan<- station.Station, processed <-chan station.Station, totalStations int) []station.Station {
103+
func ProcessWork(stream <-chan *station.Station, work chan<- *station.Station, processed <-chan *station.Station, totalStations int) []*station.Station {
104104
// Create a waitgroup to wait for all the stations to be processed.
105105
var waitGroup sync.WaitGroup
106106
waitGroup.Add(totalStations)
107107

108108
// Slice of processes stations to be returned.
109-
stations := make([]station.Station, totalStations)
109+
stations := make([]*station.Station, totalStations)
110110

111111
go func() {
112112
streamBuffer := stream // Using a temp vartiable to control the flow of the stream.

‎chapter6/patterns/stream/timezone/timezone.go‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,15 @@ func RetrieveGoogleTimezone(latitude float64, longitude float64) (*GoogleTimezon
118118
return nil, err
119119
}
120120

121-
defer func() {
122-
resp.Body.Close()
123-
}()
121+
defer resp.Body.Close()
124122

125123
rawDocument, err := ioutil.ReadAll(resp.Body)
126124
if err != nil {
127125
return nil, err
128126
}
129127

130-
var googleTimezone *GoogleTimezone
131-
err = json.Unmarshal(rawDocument, &googleTimezone)
128+
var googleTimezone GoogleTimezone
129+
err = json.Unmarshal(rawDocument, googleTimezone)
132130
if err != nil {
133131
return nil, err
134132
}
@@ -141,7 +139,7 @@ func RetrieveGoogleTimezone(latitude float64, longitude float64) (*GoogleTimezon
141139
return nil, fmt.Errorf("Error : No Timezone Id Provided")
142140
}
143141

144-
return googleTimezone, err
142+
return &googleTimezone, err
145143
}
146144

147145
// RetrieveGeoNamesTimezone calls the GeoNames API to retrieve the timezone for the lat/lng.
@@ -154,26 +152,19 @@ func RetrieveGeoNamesTimezone(latitude float64, longitude float64, userName stri
154152

155153
// Make the web call to get the XML data
156154
resp, err := http.Get(uri)
157-
158-
defer func() {
159-
if resp != nil {
160-
resp.Body.Close()
161-
}
162-
}()
163-
164155
if err != nil {
165156
return nil, err
166157
}
167158

168-
rawDocument, err := ioutil.ReadAll(resp.Body)
159+
defer resp.Body.Close()
169160

161+
rawDocument, err := ioutil.ReadAll(resp.Body)
170162
if err != nil {
171163
return nil, err
172164
}
173165

174-
var geoNamesTimezone *GeoNamesTimezone
166+
var geoNamesTimezone GeoNamesTimezone
175167
err = json.Unmarshal(rawDocument, &geoNamesTimezone)
176-
177168
if err != nil {
178169
return nil, err
179170
}
@@ -182,7 +173,7 @@ func RetrieveGeoNamesTimezone(latitude float64, longitude float64, userName stri
182173
return nil, fmt.Errorf("Error : No Timezone Id Provided")
183174
}
184175

185-
return geoNamesTimezone, err
176+
return &geoNamesTimezone, err
186177
}
187178

188179
// CatchPanic is used to catch any Panic

‎chapter6/patterns/timer.go‎

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)