lab2 fix
This commit is contained in:
+16
-27
@@ -20,22 +20,13 @@ const (
|
||||
DefaultLockTTL = 10 * time.Second
|
||||
DefaultWaitTimeout = 2 * time.Second
|
||||
minLockTTL = time.Second
|
||||
releaseTimeout = time.Second
|
||||
)
|
||||
|
||||
var ErrLoadInProgress = errors.New("distributed cache: load already in progress")
|
||||
|
||||
type TimeoutPolicy int
|
||||
|
||||
const (
|
||||
TimeoutError TimeoutPolicy = iota
|
||||
TimeoutLoad
|
||||
redisOpTimeout = 200 * time.Millisecond
|
||||
releaseTimeout = 300 * time.Millisecond
|
||||
)
|
||||
|
||||
type DistributedConfig struct {
|
||||
LockTTL time.Duration
|
||||
WaitTimeout time.Duration
|
||||
TimeoutPolicy TimeoutPolicy
|
||||
LockTTL time.Duration
|
||||
WaitTimeout time.Duration
|
||||
}
|
||||
|
||||
type LockStore interface {
|
||||
@@ -49,7 +40,6 @@ type DistributedCache struct {
|
||||
store LockStore
|
||||
lockTTL time.Duration
|
||||
waitTimeout time.Duration
|
||||
policy TimeoutPolicy
|
||||
log *slog.Logger
|
||||
group singleflight.Group
|
||||
}
|
||||
@@ -73,15 +63,10 @@ func NewDistributed(store LockStore, cfg DistributedConfig, log *slog.Logger) (*
|
||||
return nil, fmt.Errorf("cache: wait timeout %s must not exceed lock ttl %s", cfg.WaitTimeout, cfg.LockTTL)
|
||||
}
|
||||
|
||||
if cfg.TimeoutPolicy != TimeoutError && cfg.TimeoutPolicy != TimeoutLoad {
|
||||
return nil, fmt.Errorf("cache: unknown wait-timeout policy %d", cfg.TimeoutPolicy)
|
||||
}
|
||||
|
||||
return &DistributedCache{
|
||||
store: store,
|
||||
lockTTL: cfg.LockTTL,
|
||||
waitTimeout: cfg.WaitTimeout,
|
||||
policy: cfg.TimeoutPolicy,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
@@ -157,11 +142,7 @@ func (d *DistributedCache) getOrLoad(
|
||||
case waitDegraded:
|
||||
return loader(ctx)
|
||||
case waitTimeout:
|
||||
if d.policy == TimeoutLoad {
|
||||
return loader(ctx)
|
||||
}
|
||||
|
||||
return nil, ErrLoadInProgress
|
||||
return loader(ctx)
|
||||
case waitRetry:
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
@@ -193,19 +174,27 @@ func (d *DistributedCache) fill(
|
||||
}
|
||||
}()
|
||||
|
||||
if value, err := d.store.Get(base, key); err == nil {
|
||||
getCtx, cancelGet := context.WithTimeout(base, redisOpTimeout)
|
||||
value, err := d.store.Get(getCtx, key)
|
||||
cancelGet()
|
||||
|
||||
if err == nil {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
loadCtx, cancel := context.WithTimeout(base, d.lockTTL-minLockTTL)
|
||||
defer cancel()
|
||||
|
||||
value, err := loader(loadCtx)
|
||||
value, err = loader(loadCtx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if setErr := d.store.Set(base, key, value, ttl); setErr != nil {
|
||||
setCtx, cancelSet := context.WithTimeout(base, redisOpTimeout)
|
||||
setErr := d.store.Set(setCtx, key, value, ttl)
|
||||
cancelSet()
|
||||
|
||||
if setErr != nil {
|
||||
d.warn(base, "distributed cache write failed", key, setErr)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user