Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make isHolCache access thread-safe #127

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions v2/cal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package cal

import "time"
import (
"sync"
"time"
)

// DefaultLoc is the default time.Location to use in functions that do not
// require a full time.Time value.
Expand All @@ -23,6 +26,9 @@ type Calendar struct {
Cacheable bool // indicates that holiday calcs can be cached (don't change holiday defs while enabled)

isHolCache map[holCacheKey]*holCacheEntry // cached results for IsHoliday

isHolCacheInitOnce sync.Once
isHolCacheMutex sync.RWMutex
}

type holCacheKey struct {
Expand Down Expand Up @@ -76,12 +82,16 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
year, month, day := date.Date()

if c.Cacheable {
if c.isHolCache == nil {
c.isHolCacheInitOnce.Do(func() {
c.isHolCache = make(map[holCacheKey]*holCacheEntry)
}
})

c.isHolCacheMutex.RLock()
if v, ok := c.isHolCache[holCacheKey{year: year, month: month, day: day}]; ok {
c.isHolCacheMutex.RUnlock()
return v.act, v.obs, v.hol
}
c.isHolCacheMutex.RUnlock()
}

for _, hol := range c.Holidays {
Expand All @@ -99,9 +109,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if actMatch || obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: actMatch, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return actMatch, obsMatch, hol
}
Expand All @@ -122,9 +134,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: false, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return false, obsMatch, hol
}
Expand All @@ -137,9 +151,11 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}
if obsMatch {
if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] =
&holCacheEntry{act: false, obs: obsMatch, hol: hol}
c.isHolCacheMutex.Unlock()
}
return false, obsMatch, hol
}
Expand All @@ -148,8 +164,10 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
}

if c.Cacheable {
c.isHolCacheMutex.Lock()
c.evict()
c.isHolCache[holCacheKey{year: year, month: month, day: day}] = holFalseEntry
c.isHolCacheMutex.Unlock()
}
return false, false, nil
}
Expand Down
Loading