在 golang 后端,我想為多個客戶端提供一個值,我們稱之為分數。分數隨時間變化,計算速度慢。計算不依賴于先前的結果。當沒有客戶時,我根本不想計算它。所以計算應該只在請求時進行。但還有另一個事實 - 分數不能在 5 秒內改變。所以我嘗試了不同的方法,但一切都有其缺點:
- 在沒有客戶的情況下進行昂貴的計算:
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)
}
- 在很長的計算時間內阻止所有客戶端(但實際上可能只是向它們提供舊資料)。并且您不能移出
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. i think this is a generic problem, and a pattern - does it have a special name?
uj5u.com熱心網友回復:
可能有多種解決方案。一個簡單的解決方案是使用指定的 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{}),
}
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)
}
注意: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)
}
uj5u.com熱心網友回復:
這是我已經實作的,與 icza 的答案相關,但有一些更多的功能:
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())
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318752.html
標籤:multithreading go synchronization
