46 lines
1.6 KiB
Go
46 lines
1.6 KiB
Go
package redisactivity
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
controllerPool "proxy-pool/internal/controller/pool"
|
|
"proxy-pool/internal/domain/activitypool"
|
|
)
|
|
|
|
var _ controllerPool.InventoryReader = (*Adapter)(nil)
|
|
|
|
func (a *Adapter) ReadInventory(
|
|
ctx context.Context,
|
|
upstreamID string,
|
|
safetyMargin time.Duration,
|
|
) (controllerPool.InventorySnapshot, error) {
|
|
if ctx == nil || a == nil || !runtimeClean(upstreamID) || safetyMargin < 0 {
|
|
return controllerPool.InventorySnapshot{}, activitypool.ErrInvalidInventory
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return controllerPool.InventorySnapshot{}, err
|
|
}
|
|
result, err := runScript(ctx, a.client, capacityScript, []string{
|
|
a.keys.records, a.keys.inventory, a.keys.upstream(upstreamID), a.keys.owned(upstreamID), a.keys.owners,
|
|
a.keys.workerSessions, a.keys.workerSessionExpiry,
|
|
a.keys.workerRuntime, a.keys.workerRuntimeExpiry,
|
|
}, upstreamID, durationMillis(safetyMargin), a.options.MaxInventoryScan, a.options.CleanupLimit)
|
|
if err != nil {
|
|
return controllerPool.InventorySnapshot{}, err
|
|
}
|
|
var reply capacityScriptReply
|
|
if err := decodeScriptResult(result, &reply); err != nil {
|
|
return controllerPool.InventorySnapshot{}, err
|
|
}
|
|
if reply.Status == scriptInvalid {
|
|
return controllerPool.InventorySnapshot{}, activitypool.ErrInvalidInventory
|
|
}
|
|
if reply.Status != scriptOK || reply.Managed < 0 || reply.AvailableSlots < 0 {
|
|
return controllerPool.InventorySnapshot{}, invalidScriptReply("capacity inventory is unavailable")
|
|
}
|
|
return controllerPool.InventorySnapshot{
|
|
Managed: reply.Managed, AvailableSlots: reply.AvailableSlots,
|
|
}, nil
|
|
}
|