add some locks
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m52s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m52s
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
@@ -36,6 +37,7 @@ type Ttl struct {
|
||||
}
|
||||
|
||||
type GrainLocalPool struct {
|
||||
mu sync.RWMutex
|
||||
grains map[CartId]*CartGrain
|
||||
expiry []Ttl
|
||||
spawn func(id CartId) (*CartGrain, error)
|
||||
@@ -62,6 +64,8 @@ func NewGrainLocalPool(size int, ttl time.Duration, spawn func(id CartId) (*Cart
|
||||
}
|
||||
|
||||
func (p *GrainLocalPool) SetAvailable(availableWithLastChangeUnix map[CartId]int64) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for id := range availableWithLastChangeUnix {
|
||||
if _, ok := p.grains[id]; !ok {
|
||||
p.grains[id] = nil
|
||||
@@ -76,6 +80,8 @@ func (p *GrainLocalPool) SetAvailable(availableWithLastChangeUnix map[CartId]int
|
||||
func (p *GrainLocalPool) Purge() {
|
||||
lastChangeTime := time.Now().Add(-p.Ttl)
|
||||
keepChanged := lastChangeTime.Unix()
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for i := 0; i < len(p.expiry); i++ {
|
||||
item := p.expiry[i]
|
||||
if item.Expires.Before(time.Now()) {
|
||||
@@ -100,6 +106,8 @@ func (p *GrainLocalPool) GetGrains() map[CartId]*CartGrain {
|
||||
|
||||
func (p *GrainLocalPool) GetGrain(id CartId) (*CartGrain, error) {
|
||||
var err error
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
grain, ok := p.grains[id]
|
||||
grainLookups.Inc()
|
||||
if grain == nil || !ok {
|
||||
|
||||
Reference in New Issue
Block a user