2 Commits

Author SHA1 Message Date
matst80
7c0e3e84a2 implement redis inventory with threadsafe reservation
Some checks failed
Build and Publish / Metadata (push) Successful in 11s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled
2025-11-04 18:13:06 +01:00
matst80
c67ebd7a5f initial inventory service 2025-11-04 12:48:41 +01:00
7 changed files with 323 additions and 2 deletions

54
cmd/inventory/main.go Normal file
View File

@@ -0,0 +1,54 @@
package main
import (
"context"
"log"
"git.tornberg.me/go-cart-actor/pkg/inventory"
"github.com/redis/go-redis/v9"
"github.com/redis/go-redis/v9/maintnotifications"
)
func main() {
var ctx = context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "10.10.3.18:6379",
Password: "slaskredis", // no password set
DB: 0, // use default DB
MaintNotificationsConfig: &maintnotifications.Config{
Mode: maintnotifications.ModeDisabled,
},
})
s, err := inventory.NewRedisInventoryService(rdb, ctx)
if err != nil {
log.Fatalf("Unable to connect to inventory redis", err)
return
}
rdb.Pipelined(ctx, func(p redis.Pipeliner) error {
s.UpdateInventory(p, "1", "1", 10)
s.UpdateInventory(p, "2", "2", 20)
s.UpdateInventory(p, "3", "3", 30)
s.UpdateInventory(p, "4", "4", 40)
return nil
})
err = s.ReserveInventory(inventory.ReserveRequest{
SKU: "1",
LocationID: "1",
Quantity: 3,
}, inventory.ReserveRequest{
SKU: "2",
LocationID: "2",
Quantity: 15,
})
if err != nil {
log.Printf("Unable to reserve inventory: %v", err)
return
}
v, err := s.GetInventory("1", "1")
if err != nil {
log.Printf("Unable to get inventory: %v", err)
return
}
log.Printf("Inventory after reservation: %v", v)
}

2
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/matst80/slask-finder v0.0.0-20251023104024-f788e5a51d68
github.com/prometheus/client_golang v1.23.2
github.com/rabbitmq/amqp091-go v1.10.0
github.com/redis/go-redis/v9 v9.16.0
google.golang.org/grpc v1.76.0
google.golang.org/protobuf v1.36.10
k8s.io/api v0.34.1
@@ -21,6 +22,7 @@ require (
github.com/bits-and-blooms/bitset v1.24.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect

8
go.sum
View File

@@ -4,6 +4,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bits-and-blooms/bitset v1.24.1 h1:hqnfFbjjk3pxGa5E9Ho3hjoU7odtUuNmJ9Ao+Bo8s1c=
github.com/bits-and-blooms/bitset v1.24.1/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@@ -13,6 +17,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960/go.mod h1:9HQzr9D/0PGwMEbC3d5AB7oi67+h4TsQqItC1GVYG58=
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 h1:PRxIJD8XjimM5aTknUK9w6DHLDox2r2M3DI4i2pnd3w=
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936/go.mod h1:ttYvX5qlB+mlV1okblJqcSMtR4c52UKxDiX9GRBS8+Q=
@@ -168,6 +174,8 @@ github.com/prometheus/procfs v0.18.0 h1:2QTA9cKdznfYJz7EDaa7IiJobHuV7E1WzeBwcrhk
github.com/prometheus/procfs v0.18.0/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
github.com/redis/go-redis/v9 v9.16.0 h1:OotgqgLSRCmzfqChbQyG1PHC3tLNR89DG4jdOERSEP4=
github.com/redis/go-redis/v9 v9.16.0/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=

View File

@@ -21,8 +21,8 @@ func TestRegisteredMutationBasics(t *testing.T) {
func(state *cartState, msg *messages.AddItem) error {
state.calls++
// copy to avoid external mutation side-effects (not strictly necessary for the test)
cp := *msg
state.lastAdded = &cp
cp := msg
state.lastAdded = cp
return nil
},
func() *messages.AddItem { return &messages.AddItem{} },

View File

@@ -0,0 +1,47 @@
package inventory
import (
"errors"
"sync"
)
type MemoryInventoryService struct {
warehouses map[LocationID]*Warehouse
mu sync.RWMutex
}
func NewMemoryInventoryService() *MemoryInventoryService {
return &MemoryInventoryService{
warehouses: make(map[LocationID]*Warehouse),
}
}
func (s *MemoryInventoryService) AddWarehouse(warehouse *Warehouse) {
s.mu.Lock()
defer s.mu.Unlock()
s.warehouses[warehouse.ID] = warehouse
}
func (s *MemoryInventoryService) GetInventory(sku SKU, locationID LocationID) (*InventoryItem, error) {
s.mu.RLock()
defer s.mu.RUnlock()
warehouse, ok := s.warehouses[locationID]
if !ok {
return nil, errors.New("warehouse not found")
}
for _, item := range warehouse.Inventory {
if item.SKU == sku {
return &item, nil
}
}
return nil, errors.New("sku not found in warehouse")
}
func (s *MemoryInventoryService) ReserveInventory(req ...ReserveRequest) error {
// We'll implement the reservation logic using Lua script execution here
// For now, let's just return an error indicating it's not implemented
return errors.New("reservation not implemented yet")
}

View File

@@ -0,0 +1,180 @@
package inventory
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/redis/go-redis/v9"
)
type RedisInventoryService struct {
client *redis.Client
ctx context.Context
luaScripts map[string]*redis.Script
}
func NewRedisInventoryService(client *redis.Client, ctx context.Context) (*RedisInventoryService, error) {
rdb := client
// Ping Redis to check connection
_, err := rdb.Ping(ctx).Result()
if err != nil {
return nil, err
}
return &RedisInventoryService{
client: rdb,
ctx: ctx,
luaScripts: make(map[string]*redis.Script),
}, nil
}
func (s *RedisInventoryService) LoadLuaScript(key string) error {
// Get the script from Redis
script, err := s.client.Get(s.ctx, key).Result()
if err != nil {
return err
}
// Load the script into the luaScripts cache
s.luaScripts[key] = redis.NewScript(script)
return nil
}
func (s *RedisInventoryService) AddWarehouse(warehouse *Warehouse) error {
// Convert warehouse to Redis-friendly format
data := map[string]interface{}{
"id": string(warehouse.ID),
"name": warehouse.Name,
"inventory": warehouse.Inventory,
}
// Store in Redis with a key pattern like "warehouse:<ID>"
key := "warehouse:" + string(warehouse.ID)
_, err := s.client.HMSet(s.ctx, key, data).Result()
return err
}
func (s *RedisInventoryService) GetInventory(sku SKU, locationID LocationID) (int64, error) {
cmd := s.client.Get(s.ctx, getInventoryKey(sku, locationID))
if err := cmd.Err(); err != nil {
return 0, err
}
i, err := cmd.Int64()
if err != nil {
return 0, err
}
return i, nil
}
func getInventoryKey(sku SKU, locationID LocationID) string {
return fmt.Sprintf("inventory:%s:%s", sku, locationID)
}
func (s *RedisInventoryService) UpdateInventory(rdb redis.Pipeliner, sku SKU, locationID LocationID, quantity int64) error {
key := getInventoryKey(sku, locationID)
cmd := rdb.Set(s.ctx, key, quantity, 0)
return cmd.Err()
}
var (
ErrInsufficientInventory = errors.New("insufficient inventory")
ErrInvalidQuantity = errors.New("invalid quantity")
ErrMissingReservation = errors.New("missing reservation")
)
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))
}
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
}
var reserveScript = 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
-- If the check passes, store the new value
local new_val = current_val - quantity_to_check
table.insert(new_values, new_val)
-- Add this key and its *new* value to our payload map
payload[key] = new_val
end
-- ---
-- 2. UPDATE PHASE
-- ---
-- If the script reaches this point, all checks passed.
-- Now, loop again and apply all the updates.
for i = 1, num_keys do
local key = KEYS[i]
local new_val = new_values[i]
-- Set the key to its new calculated value
redis.call('SET', key, new_val)
end
local message_payload = cjson.encode(payload)
-- Publish the JSON-encoded message to the specified channel
redis.call('PUBLISH', "inventory_changed", message_payload)
-- Return 1 to indicate the operation was successful
return 1
`)

30
pkg/inventory/types.go Normal file
View File

@@ -0,0 +1,30 @@
package inventory
import "time"
type SKU string
type LocationID string
type InventoryItem struct {
SKU SKU
Quantity int
LastUpdate time.Time
}
type Warehouse struct {
ID LocationID
Name string
Inventory []InventoryItem
}
type InventoryService interface {
GetInventory(sku SKU, locationID LocationID) (int64, error)
ReserveInventory(req ...ReserveRequest) error
}
type ReserveRequest struct {
SKU SKU
LocationID LocationID
Quantity uint32
}