test otel
All checks were successful
Build and Publish / Metadata (push) Successful in 13s
Build and Publish / BuildAndDeployAmd64 (push) Successful in 54s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m19s

This commit is contained in:
matst80
2025-11-05 10:23:00 +01:00
parent eb1f7750df
commit 82d564b136
6 changed files with 275 additions and 22 deletions

View File

@@ -88,19 +88,49 @@ var (
ErrMissingReservation = errors.New("missing reservation")
)
func makeKeysAndArgs(req ...ReserveRequest) ([]string, []string) {
keys := make([]string, len(req))
args := make([]string, len(req))
for i, r := range req {
if r.Quantity <= 0 {
return nil, nil
}
keys[i] = getInventoryKey(r.SKU, r.LocationID)
args[i] = strconv.Itoa(int(r.Quantity))
}
return keys, args
}
func (s *RedisInventoryService) ReservationCheck(req ...ReserveRequest) error {
if len(req) == 0 {
return ErrMissingReservation
}
keys, args := makeKeysAndArgs(req...)
if keys == nil || args == nil {
return ErrInvalidQuantity
}
cmd := reserveScript.Run(s.ctx, s.client, keys, args)
if err := cmd.Err(); err != nil {
return err
}
if val, err := cmd.Int(); err != nil {
return err
} else if val != 1 {
return ErrInsufficientInventory
}
return nil
}
func (s *RedisInventoryService) ReserveInventory(req ...ReserveRequest) error {
if len(req) == 0 {
return ErrMissingReservation
}
keys := make([]string, len(req))
args := make([]string, len(req))
for i, r := range req {
if r.Quantity <= 0 {
return ErrInvalidQuantity
}
keys[i] = getInventoryKey(r.SKU, r.LocationID)
args[i] = strconv.Itoa(int(r.Quantity))
keys, args := makeKeysAndArgs(req...)
if keys == nil || args == nil {
return ErrInvalidQuantity
}
cmd := reserveScript.Run(s.ctx, s.client, keys, args)
if err := cmd.Err(); err != nil {
@@ -114,6 +144,47 @@ func (s *RedisInventoryService) ReserveInventory(req ...ReserveRequest) error {
return nil
}
var reservationCheck = redis.NewScript(`
-- Get the number of keys passed
local num_keys = #KEYS
-- Ensure the number of keys matches the number of quantities
if num_keys ~= #ARGV then
return {err = "Script requires the same number of keys and quantities."}
end
local new_values = {}
local payload = {}
-- ---
-- 1. CHECK PHASE
-- ---
-- Loop through all keys to check their values first
for i = 1, num_keys do
local key = KEYS[i]
local quantity_to_check = tonumber(ARGV[i])
-- Fail if the quantity is not a valid number
if not quantity_to_check then
return {err = "Invalid quantity provided for key: " .. key}
end
-- Get the current value stored at the key
local current_val = tonumber(redis.call('GET', key))
-- Check the condition
-- Fail if:
-- 1. The key doesn't exist (current_val is nil)
-- 2. The value is not > the required quantity
if not current_val or current_val <= quantity_to_check then
-- Return 0 to indicate the operation failed and no changes were made
return 0
end
end
return 1
`)
var reserveScript = redis.NewScript(`
-- Get the number of keys passed
local num_keys = #KEYS