英文:
How to syncronize slow calculation and cache it?
问题
在golang后端中,我想要为多个客户端提供一个值,我们称之为score。score会随时间变化,它的计算速度很慢。计算不依赖于先前的结果。当没有客户端时,我不想计算它。因此,计算应该只在请求时进行。但还有另一个事实-在5秒的时间段内,score不能改变。所以我尝试了不同的方法,每种方法都有其缺点:
1)在没有客户端的情况下进行昂贵的计算:
var score interface{}
// 在单独的goroutine中运行
func calculateScorePeriodically() {
for{
select{
case <-time.After(5*time.Second):
score = calculateScoreExpensiveAndSlow()
}
}
}
func serveScore(w http.ResponseWriter, r* http.Request) {
b, _ := json.Marshal(score)
w.Write(b)
}
2)阻塞所有客户端进行长时间的计算(实际上可能只向它们提供旧数据)。而且你不能将if
移到互斥锁之外,因为这样多个客户端可能同时进入计算块,并且会按顺序而不是在5秒间隔内进行计算:
var (
score interface{}
mutex sync.Mutex
updatedAt time.Time
)
func getCachedScore() float64 {
mutex.Lock()
defer mutex.Unlock()
currentTime := time.Now()
if currentTime.Sub(updatedAt) < 5*time.Second {
return score
}
updatedAt = currentTime
score = calculateScoreExpensiveAndSlow()
return score
}
func serveScore(w http.ResponseWriter, r* http.Request) {
b, _ := json.Marshal(getCachedScore())
w.Write(b)
}
如何解决上述两个缺点?
PS. 我认为这是一个通用的问题,并且有一个模式-它有一个特殊的名称吗?
英文:
In a golang backend I want to serve a value to a multiple clients, lets call it score. Score is changing with time, and its calculation is slow. Calculation does not depend on previous results. When there are no clients I dont want to calculate it at all. So calculation should happen only on request. But also there is another fact - score cannot change within 5 seconds period. So i tried different aproaches and everything has its drawbacks:
- Does expensive calculation in absense of clients:
var score interface{}
// run in a separate goroutine
func calculateScorePeriodically() {
for{
select{
case <-time.After(5*time.Second):
score = calculateScoreExpensiveAndSlow()
}
}
}
func serveScore(w http.ResponseWriter, r* http.Request) {
b, _ := json.Marshal(score)
w.Write(b)
}
- Blocks all clients for a long calculation period (but actually may just serve old data to them). And you cannot move
if
outside a mutex, because then multiple client may enter calculation block simultaneously and would do calculation not within 5 seconds interval but sequentially:
var (
score interface{}
mutex sync.Mutex
updatedAt time.Time
)
func getCachedScore() float64 {
mutex.Lock()
defer mutex.Unlock()
currentTime := time.Now()
if currentTime.Sub(updatedAt) < 5*time.Second {
return score
}
updatedAt = currentTime
score = calculateScoreExpensiveAndSlow()
return score
}
func serveScore(w http.ResponseWriter, r* http.Request) {
b, _ := json.Marshal(getCachedScore())
w.Write(b)
}
How to solve both of above drawbacks?
PS. i think this is a generic problem, and a pattern - does it have a special name?
答案1
得分: 3
可能有多个解决方案。一个简单的解决方案是为计算指定一个专用的goroutine,你可以通过在通道上发送一个值来发出重新计算的信号。发送可以是非阻塞的,所以如果计算正在进行中,什么都不会发生。
这是一个可重用的缓存实现:
type cache struct {
mu sync.RWMutex
value interface{}
updated time.Time
calcCh chan struct{}
expiration time.Duration
}
func NewCache(calc func() interface{}, expiration time.Duration) *cache {
c := &cache{
value: calc(),
updated: time.Now(),
calcCh: make(chan struct{}),
expiration: expiration,
}
go func() {
for range c.calcCh {
v := calc()
c.mu.Lock()
c.value, c.updated = v, time.Now()
c.mu.Unlock()
}
}()
return c
}
func (c *cache) Get() (value interface{}, updated time.Time) {
c.mu.RLock()
value, updated = c.value, c.updated
c.mu.RUnlock()
if time.Since(updated) > c.expiration {
// 触发新的计算(将在另一个goroutine中进行)。
// 如果计算正在进行中,进行非阻塞发送将不会产生任何效果
select {
case c.calcCh <- struct{}{}:
default:
}
}
return
}
func (c *cache) Stop() {
close(c.calcCh)
}
注意:Cache.Stop()
用于停止后台goroutine。在调用Cache.Stop()
之后,不得调用Cache.Get()
。
在你的情况下使用它:
func getCachedScore() interface{} {
// ...
}
var scoreCache = NewCache(getCachedScore, 5*time.Second)
func serveScore(w http.ResponseWriter, r *http.Request) {
score, _ := scoreCache.Get()
b, _ := json.Marshal(score)
w.Write(b)
}
英文:
There may be multiple solutions. A simple solution is to have a designated goroutine for calculation, to which you can signal a need for recalculation by sending a value on a channel. The send may be non-blocking, so if a calculation is in progress, nothing will happen.
Here's a reusable cache implementation:
type cache struct {
mu sync.RWMutex
value interface{}
updated time.Time
calcCh chan struct{}
expiration time.Duration
}
func NewCache(calc func() interface{}, expiration time.Duration) *cache {
c := &cache{
value: calc(),
updated: time.Now(),
calcCh: make(chan struct{}),
}
go func() {
for range c.calcCh {
v := calc()
c.mu.Lock()
c.value, c.updated = v, time.Now()
c.mu.Unlock()
}
}()
return c
}
func (c *cache) Get() (value interface{}, updated time.Time) {
c.mu.RLock()
value, updated = c.value, c.updated
c.mu.RUnlock()
if time.Since(updated) > c.expiration {
// Trigger a new calculation (will happen in another goroutine).
// Do non-blocking send, if a calculation is in progress,
// this will have no effect
select {
case c.calcCh <- struct{}{}:
default:
}
}
return
}
func (c *cache) Stop() {
close(c.calcCh)
}
Note: Cache.Stop()
is to stop the background goroutine. After calling Cache.Stop()
, Cache.Get()
must not be called.
Using it for your case:
func getCachedScore() interface{} {
// ...
}
var scoreCache = NewCache(getCachedScore, 5*time.Second)
func serveScore(w http.ResponseWriter, r* http.Request) {
score, _ := scoreCache.Get()
b, _ := json.Marshal(score)
w.Write(b)
}
答案2
得分: 0
这是我实现的代码,与icza的答案相关,但有一些更多的功能:
package common
import (
"context"
"sync/atomic"
"time"
)
type (
CachedUpdater func() interface{}
ChanStruct chan struct{}
)
type Cached struct {
value atomic.Value // 保存缓存值的interface{}
updatedAt atomic.Value // 保存最后一次更新开始的时间time.Time
updatePeriod time.Duration // 控制更新之间的最小时间间隔
needUpdate ChanStruct
}
//cachedUpdater是一个用户提供的长时间昂贵计算的函数,用于获取当前状态
func MakeCached(ctx context.Context, updatePeriod time.Duration, cachedUpdater CachedUpdater) *Cached {
v := &Cached{
updatePeriod: updatePeriod,
needUpdate: make(ChanStruct),
}
//v.updatedAt.Store(time.Time{}) // "从未更新过",但时间不应该是nil接口
v.doUpdate(time.Now(), cachedUpdater)
go v.updaterController(ctx, cachedUpdater)
return v
}
//客户端将立即获取缓存值,并在需要时触发更新,如果值已过期
func (v *Cached) Get() interface{} {
if v.IsExpired(time.Now()) {
v.RequestUpdate()
}
return v.value.Load()
}
//updateController goroutine可以通过取消提供给maker的上下文或关闭chan来终止
func (v *Cached) Stop() {
close(v.needUpdate)
}
//如果值已过期且更新函数可能尚未被调用,则返回true
func (v *Cached) IsExpired(currentTime time.Time) bool {
updatedAt := v.updatedAt.Load().(time.Time)
return currentTime.Sub(updatedAt) > v.updatePeriod
}
//请求updaterController执行更新,使用非阻塞发送到无缓冲chan。在最近更新了值的情况下,控制器可以决定不进行更新
func (v *Cached) RequestUpdate() bool {
select {
case v.needUpdate <- struct{}{}:
return true
default:
return false
}
}
func (v *Cached) updaterController(ctx context.Context, cachedUpdater CachedUpdater) {
for {
select {
case <-ctx.Done():
return
case _, ok := <-v.needUpdate:
if !ok {
return
}
currentTime := time.Now()
if !v.IsExpired(currentTime) {
continue
}
v.doUpdate(currentTime, cachedUpdater)
}
}
}
func (v *Cached) doUpdate(currentTime time.Time, cachedUpdater CachedUpdater) {
v.updatedAt.Store(currentTime)
v.value.Store(cachedUpdater())
}
英文:
This is what i've implemented, correlates with icza's answer, but has some more features:
package common
import (
"context"
"sync/atomic"
"time"
)
type (
CachedUpdater func() interface{}
ChanStruct chan struct{}
)
type Cached struct {
value atomic.Value // holds the cached value's interface{}
updatedAt atomic.Value // holds time.Time, time when last update sequence was started at
updatePeriod time.Duration // controls minimal anount of time between updates
needUpdate ChanStruct
}
//cachedUpdater is a user-provided function with long expensive calculation, that gets current state
func MakeCached(ctx context.Context, updatePeriod time.Duration, cachedUpdater CachedUpdater) *Cached {
v := &Cached{
updatePeriod: updatePeriod,
needUpdate: make(ChanStruct),
}
//v.updatedAt.Store(time.Time{}) // "was never updated", but time should never be nil interface
v.doUpdate(time.Now(), cachedUpdater)
go v.updaterController(ctx, cachedUpdater)
return v
}
//client will get cached value immediately, and optionally may trigger an update, if value is outdated
func (v *Cached) Get() interface{} {
if v.IsExpired(time.Now()) {
v.RequestUpdate()
}
return v.value.Load()
}
//updateController goroutine can be terminated both by cancelling context, provided to maker, or by closing chan
func (v *Cached) Stop() {
close(v.needUpdate)
}
//returns true if value is outdated and updater function was likely not called yet
func (v *Cached) IsExpired(currentTime time.Time) bool {
updatedAt := v.updatedAt.Load().(time.Time)
return currentTime.Sub(updatedAt) > v.updatePeriod
}
//requests updaterController to perform update, using non-blocking send to unbuffered chan. controller can decide not to update in case if it has recently updated value
func (v *Cached) RequestUpdate() bool {
select {
case v.needUpdate <- struct{}{}:
return true
default:
return false
}
}
func (v *Cached) updaterController(ctx context.Context, cachedUpdater CachedUpdater) {
for {
select {
case <-ctx.Done():
return
case _, ok := <-v.needUpdate:
if !ok {
return
}
currentTime := time.Now()
if !v.IsExpired(currentTime) {
continue
}
v.doUpdate(currentTime, cachedUpdater)
}
}
}
func (v *Cached) doUpdate(currentTime time.Time, cachedUpdater CachedUpdater) {
v.updatedAt.Store(currentTime)
v.value.Store(cachedUpdater())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论