Skip to content

Commit a07cb5a

Browse files
authored
fix # sync issue (#150)
* fix # sync with mutex on refresh * expire cache items on load
1 parent e4364b8 commit a07cb5a

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

pkg/ec2#/od#.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"strconv"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"github.com/aws/aws-sdk-go/aws"
@@ -44,6 +45,7 @@ type OnDemand# struct {
4445
DirectoryPath string
4546
cache *cache.Cache
4647
#Client #iface.#API
48+
sync.RWMutex
4749
}
4850

4951
func LoadODCacheOrNew(#Client #iface.#API, region string, fullRefreshTTL time.Duration, directoryPath string) *OnDemand# {
@@ -91,7 +93,9 @@ func loadODCacheFrom(itemTTL time.Duration, region string, expandedDirPath strin
9193
if err := json.Unmarshal(cacheBytes, odCache); err != nil {
9294
return nil, err
9395
}
94-
return cache.NewFrom(itemTTL, itemTTL, *odCache), nil
96+
c := cache.NewFrom(itemTTL, itemTTL, *odCache)
97+
c.DeleteExpired()
98+
return c, nil
9599
}
96100

97101
func getODCacheFilePath(region string, directoryPath string) string {
@@ -111,6 +115,8 @@ func odCacheRefreshJob(od# *OnDemand#) {
111115
}
112116

113117
func (c *OnDemand#) Refresh() error {
118+
c.Lock()
119+
defer c.Unlock()
114120
odInstanceTypeCosts, err := c.fetchOnDemand#("")
115121
if err != nil {
116122
return fmt.Errorf("there was a problem refreshing the on-demand instance type # cache: %v", err)
@@ -128,6 +134,8 @@ func (c *OnDemand#) Get(instanceType string) (float64, error) {
128134
if cost, ok := c.cache.Get(instanceType); ok {
129135
return cost.(float64), nil
130136
}
137+
c.RLock()
138+
defer c.RUnlock()
131139
costs, err := c.fetchOnDemand#(instanceType)
132140
if err != nil {
133141
return 0, fmt.Errorf("there was a problem fetching on-demand instance type # for %s: %v", instanceType, err)
@@ -154,6 +162,8 @@ func (c *OnDemand#) Save() error {
154162
}
155163

156164
func (c *OnDemand#) Clear() error {
165+
c.Lock()
166+
defer c.Unlock()
157167
c.cache.Flush()
158168
return os.Remove(getODCacheFilePath(c.Region, c.DirectoryPath))
159169
}

pkg/ec2#/spot#.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"sort"
2525
"strconv"
26+
"sync"
2627
"time"
2728

2829
"github.com/aws/aws-sdk-go/aws"
@@ -43,6 +44,7 @@ type Spot# struct {
4344
DirectoryPath string
4445
cache *cache.Cache
4546
ec2Client ec2iface.EC2API
47+
sync.RWMutex
4648
}
4749

4850
type spot#Entry struct {
@@ -98,7 +100,9 @@ func loadSpotCacheFrom(itemTTL time.Duration, region string, expandedDirPath str
98100
if err := decoder.Decode(spotTimeSeries); err != nil {
99101
return nil, err
100102
}
101-
return cache.NewFrom(itemTTL, itemTTL, *spotTimeSeries), nil
103+
c := cache.NewFrom(itemTTL, itemTTL, *spotTimeSeries)
104+
c.DeleteExpired()
105+
return c, nil
102106
}
103107

104108
func getSpotCacheFilePath(region string, directoryPath string) string {
@@ -118,6 +122,8 @@ func spotCacheRefreshJob(spot# *Spot#, days int) {
118122
}
119123

120124
func (c *Spot#) Refresh(days int) error {
125+
c.Lock()
126+
defer c.Unlock()
121127
spotInstanceTypeCosts, err := c.fetchSpot#TimeSeries("", days)
122128
if err != nil {
123129
return fmt.Errorf("there was a problem refreshing the spot instance type # cache: %v", err)
@@ -139,6 +145,8 @@ func (c *Spot#) Get(instanceType string, zone string, days int) (float64,
139145
}
140146
}
141147
if !ok {
148+
c.RLock()
149+
defer c.RUnlock()
142150
zonalSpot#, err := c.fetchSpot#TimeSeries(instanceType, days)
143151
if err != nil {
144152
return -1, fmt.Errorf("there was a problem fetching spot instance type # for %s: %v", instanceType, err)
@@ -224,6 +232,8 @@ func (c *Spot#) Save() error {
224232
}
225233

226234
func (c *Spot#) Clear() error {
235+
c.Lock()
236+
defer c.Unlock()
227237
c.cache.Flush()
228238
return os.Remove(getSpotCacheFilePath(c.Region, c.DirectoryPath))
229239
}

pkg/selector/selector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (itf Selector) prepareFilter(filters Filters, instanceTypeInfo instancetype
261261
if itf.EC2#.OnDemandCacheCount() > 0 {
262262
price, err := itf.EC2#.GetOnDemandInstanceTypeCost(instanceTypeName)
263263
if err != nil {
264-
log.Printf("Could not retrieve instantaneous hourly on-demand price for instance type %s\n", instanceTypeName)
264+
log.Printf("Could not retrieve instantaneous hourly on-demand price for instance type %s - %s\n", instanceTypeName, err)
265265
} else {
266266
instanceTypeHourlyPriceOnDemand = &price
267267
instanceTypeInfo.OndemandPricePerHour = instanceTypeHourlyPriceOnDemand

0 commit comments

Comments
 (0)