Compare commits
2 Commits
59e01e763f
...
95426acd4a
| Author | SHA1 | Date | |
|---|---|---|---|
| 95426acd4a | |||
| 7e2726244f |
@@ -43,9 +43,10 @@ var (
|
||||
|
||||
type PoolServer struct {
|
||||
actor.GrainPool[*cart.CartGrain]
|
||||
pod_name string
|
||||
klarnaClient *KlarnaClient
|
||||
inventoryService inventory.InventoryService
|
||||
pod_name string
|
||||
klarnaClient *KlarnaClient
|
||||
inventoryService inventory.InventoryService
|
||||
reservationService inventory.CartReservationService
|
||||
}
|
||||
|
||||
func NewPoolServer(pool actor.GrainPool[*cart.CartGrain], pod_name string, klarnaClient *KlarnaClient, inventoryService inventory.InventoryService, inventoryRedisClient *redis.Client) *PoolServer {
|
||||
@@ -266,6 +267,48 @@ type AddRequest struct {
|
||||
StoreId *string `json:"storeId"`
|
||||
}
|
||||
|
||||
func (s *PoolServer) GetReservationTime(item *messages.AddItem) time.Duration {
|
||||
|
||||
return time.Minute * 15
|
||||
//return nil
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleReservations(ctx context.Context, cartId cart.CartId, msgs ...*messages.AddItem) error {
|
||||
if s.reservationService == nil {
|
||||
return nil
|
||||
}
|
||||
for _, item := range msgs {
|
||||
timeout := s.GetReservationTime(item)
|
||||
if timeout == 0 {
|
||||
continue
|
||||
}
|
||||
span := trace.SpanFromContext(ctx)
|
||||
|
||||
locationId := inventory.LocationID("se")
|
||||
if item.StoreId != nil {
|
||||
locationId = inventory.LocationID(*item.StoreId)
|
||||
}
|
||||
span.AddEvent("reserving item", trace.WithAttributes(attribute.String("sku", item.Sku), attribute.String("locationId", string(locationId))))
|
||||
end := time.Now().Add(timeout)
|
||||
err := s.reservationService.ReserveForCart(ctx, inventory.CartReserveRequest{
|
||||
CartID: inventory.CartID(cartId.String()),
|
||||
InventoryReference: &inventory.InventoryReference{
|
||||
LocationID: locationId,
|
||||
SKU: inventory.SKU(item.Sku),
|
||||
},
|
||||
Quantity: uint32(item.Quantity),
|
||||
TTL: 15 * time.Minute,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.InfoContext(ctx, "reserved item", "sku", item.Sku, "location", string(locationId), "expires at", end.String())
|
||||
span.End()
|
||||
item.ReservationEndsTime = end
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PoolServer) AddSkuRequestHandler(w http.ResponseWriter, r *http.Request, id cart.CartId) error {
|
||||
addRequest := AddRequest{Quantity: 1}
|
||||
err := json.NewDecoder(r.Body).Decode(&addRequest)
|
||||
@@ -275,6 +318,9 @@ func (s *PoolServer) AddSkuRequestHandler(w http.ResponseWriter, r *http.Request
|
||||
msg, err := GetItemAddMessage(r.Context(), addRequest.Sku, int(addRequest.Quantity), addRequest.Country, addRequest.StoreId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.reservationService != nil {
|
||||
|
||||
}
|
||||
reply, err := s.ApplyLocal(r.Context(), id, msg)
|
||||
if err != nil {
|
||||
@@ -331,9 +377,11 @@ func getInventoryRequests(items []*cart.CartItem) []inventory.ReserveRequest {
|
||||
continue
|
||||
}
|
||||
requests = append(requests, inventory.ReserveRequest{
|
||||
SKU: inventory.SKU(item.Sku),
|
||||
LocationID: getLocationId(item),
|
||||
Quantity: uint32(item.Quantity),
|
||||
InventoryReference: &inventory.InventoryReference{
|
||||
SKU: inventory.SKU(item.Sku),
|
||||
LocationID: getLocationId(item),
|
||||
},
|
||||
Quantity: uint32(item.Quantity),
|
||||
})
|
||||
}
|
||||
return requests
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/matst80/go-redis-inventory/pkg/inventory"
|
||||
@@ -19,7 +18,8 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
service *inventory.RedisInventoryService
|
||||
inventoryService *inventory.RedisInventoryService
|
||||
reservationService *inventory.RedisCartReservationService
|
||||
}
|
||||
|
||||
func (srv *Server) livezHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -33,17 +33,11 @@ func (srv *Server) readyzHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (srv *Server) getInventoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Parse path: /inventory/{sku}/{location}
|
||||
path := r.URL.Path
|
||||
parts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
if len(parts) != 3 || parts[0] != "inventory" {
|
||||
http.Error(w, "Invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sku := inventory.SKU(parts[1])
|
||||
locationID := inventory.LocationID(parts[2])
|
||||
|
||||
quantity, err := srv.service.GetInventory(r.Context(), sku, locationID)
|
||||
sku := inventory.SKU(r.PathValue("sku"))
|
||||
locationID := inventory.LocationID(r.PathValue("locationId"))
|
||||
|
||||
quantity, err := srv.inventoryService.GetInventory(r.Context(), sku, locationID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -54,6 +48,20 @@ func (srv *Server) getInventoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (srv *Server) getReservationHandler(w http.ResponseWriter, r *http.Request) {
|
||||
sku := inventory.SKU(r.PathValue("sku"))
|
||||
locationID := inventory.LocationID(r.PathValue("locationId"))
|
||||
|
||||
summary, err := srv.reservationService.GetReservationSummary(r.Context(), sku, locationID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(summary)
|
||||
}
|
||||
|
||||
var country = "se"
|
||||
var redisAddress = "10.10.3.18:6379"
|
||||
var redisPassword = "slaskredis"
|
||||
@@ -87,12 +95,19 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
server := &Server{service: s}
|
||||
r, err := inventory.NewRedisCartReservationService(rdb)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to connect to reservation redis: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
server := &Server{inventoryService: s, reservationService: r}
|
||||
|
||||
// Set up HTTP routes
|
||||
http.HandleFunc("/livez", server.livezHandler)
|
||||
http.HandleFunc("/readyz", server.readyzHandler)
|
||||
http.HandleFunc("/inventory/", server.getInventoryHandler)
|
||||
http.HandleFunc("/inventory/{sku}/{locationId}", server.getInventoryHandler)
|
||||
http.HandleFunc("/reservations/{sku}/{locationId}", server.getReservationHandler)
|
||||
|
||||
stockhandler := &StockHandler{
|
||||
MainStockLocationID: inventory.LocationID(country),
|
||||
|
||||
2
go.mod
2
go.mod
@@ -5,7 +5,7 @@ go 1.25.4
|
||||
require (
|
||||
github.com/gogo/protobuf v1.3.2
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e
|
||||
github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/rabbitmq/amqp091-go v1.10.0
|
||||
|
||||
2
go.sum
2
go.sum
@@ -115,6 +115,8 @@ github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8
|
||||
github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841 h1:CCHa+LuxwGOPPbIpuFYNT7VtC5rHjSulB337Y+Yyys8=
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251125181530-7ebbc97e3841/go.mod h1:d42+bHxhmMg73OQM+5i5U7vyEQfuVLJkt7QPsEbr8kI=
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e h1:Z7A73W6jsxFuFKWvB1efQmTjs0s7+x2B7IBM2ukkI6Y=
|
||||
github.com/matst80/go-redis-inventory v0.0.0-20251126173508-51b30de2d86e/go.mod h1:9P52UwIlLWLZvObfO29aKTWUCA9Gm62IuPJ/qv4Xvs0=
|
||||
github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a h1:EfUO5BNDK3a563zQlwJYTNNv46aJFT9gbSItAwZOZ/Y=
|
||||
github.com/matst80/slask-finder v0.0.0-20251125182907-9e57f193127a/go.mod h1:VIPNkIvU0dZKwbSuv75zZcB93MXISm2UyiIPly/ucXQ=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
|
||||
@@ -3,6 +3,7 @@ package messages;
|
||||
option go_package = "git.k6n.net/go-cart-actor/proto;messages";
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
message ClearCartRequest {}
|
||||
|
||||
@@ -32,6 +33,7 @@ message AddItem {
|
||||
optional string storeId = 22;
|
||||
optional uint32 parentId = 23;
|
||||
string cgm = 25;
|
||||
optional google.protobuf.Timestamp reservationEndTime = 26
|
||||
}
|
||||
|
||||
message RemoveItem { uint32 Id = 1; }
|
||||
|
||||
Reference in New Issue
Block a user