test otel
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user