proxy-pool/internal/adapters/redisactivity/scripts.go

65 lines
1.6 KiB
Go

package redisactivity
import (
"context"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
extractionDomain "proxy-pool/internal/domain/extraction"
)
type scriptStatus string
const (
scriptOK scriptStatus = "ok"
scriptInvalid scriptStatus = "invalid"
scriptNotFound scriptStatus = "not_found"
scriptConflict scriptStatus = "conflict"
scriptUnavailable scriptStatus = "unavailable"
scriptInsufficient scriptStatus = "insufficient"
)
type upsertScriptReply struct {
Status scriptStatus `json:"status"`
Accepted int `json:"accepted"`
Inserted int `json:"inserted"`
Refreshed int `json:"refreshed"`
Dropped int `json:"dropped"`
}
type healthScriptReply struct {
Status scriptStatus `json:"status"`
Record string `json:"record,omitempty"`
}
type extractScriptReply struct {
Status scriptStatus `json:"status"`
Record string `json:"record,omitempty"`
}
type ownershipScriptReply struct {
Status scriptStatus `json:"status"`
Record string `json:"record,omitempty"`
}
type maintenanceScriptReply struct {
Status scriptStatus `json:"status"`
Count int `json:"count"`
}
func runScript(ctx context.Context, client redis.Scripter, script *redis.Script, keys []string, args ...any) (any, error) {
result, err := script.Run(ctx, client, keys, args...).Result()
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil, err
}
return nil, errors.Join(
extractionDomain.ErrStoreUnavailable,
fmt.Errorf("run redis activity script: %w", err),
)
}
return result, nil
}