Merge branch 'main' of git-ssh.tornberg.me:mats/go-cart-actor
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 33s
Build and Publish / BuildAndDeploy (push) Successful in 3m2s

This commit is contained in:
matst80
2024-11-16 10:15:31 +01:00
4 changed files with 18 additions and 38 deletions

View File

@@ -289,16 +289,18 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
case ChangeQuantityType:
msg, ok := message.Content.(*messages.ChangeQuantity)
if !ok {
err = fmt.Errorf("expected RemoveItem")
err = fmt.Errorf("expected ChangeQuantity")
} else {
for i, item := range c.Items {
if item.Id == int(msg.Id) {
if item.Quantity <= int(msg.Quantity) {
if msg.Quantity <= 0 {
c.TotalPrice -= item.Price * int64(item.Quantity)
c.Items = append(c.Items[:i], c.Items[i+1:]...)
} else {
item.Quantity -= int(msg.Quantity)
diff := int(msg.Quantity) - item.Quantity
item.Quantity = int(msg.Quantity)
c.TotalPrice += item.Price * int64(diff)
}
c.TotalPrice -= item.Price * int64(msg.Quantity)
break
}

View File

@@ -63,8 +63,9 @@ func ErrorHandler(fn func(w http.ResponseWriter, r *http.Request) error) func(w
func (s *PoolServer) WriteResult(w http.ResponseWriter, result *FrameWithPayload) error {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("X-Pod-Name", s.pod_name)
w.Header().Set("Access-Control-Allow-Origin", "https://tornberg.me")
if result.StatusCode != 200 {
log.Printf("Call error: %d\n", result.StatusCode)
if result.StatusCode >= 200 && result.StatusCode < 600 {
@@ -246,7 +247,8 @@ func (s *PoolServer) HandleCheckout(w http.ResponseWriter, r *http.Request) erro
buf.ReadFrom(res.Body)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Pod-Name", s.pod_name)
w.Header().Set("Access-Control-Allow-Origin", "https://tornberg.me")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(res.StatusCode)
w.Write(buf.Bytes())
@@ -286,6 +288,12 @@ func (a *PoolServer) RewritePath(w http.ResponseWriter, r *http.Request) {
func (s *PoolServer) Serve() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", s.RewritePath)
mux.HandleFunc("OPTIONS /", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("GET /{id}", ErrorHandler(s.HandleGet))
mux.HandleFunc("GET /{id}/add/{sku}", ErrorHandler(s.HandleAddSku))
mux.HandleFunc("POST /{id}", ErrorHandler(s.HandleAddRequest))

View File

@@ -3,9 +3,6 @@ package main
import (
"fmt"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
func (id CartId) String() string {
@@ -54,33 +51,6 @@ func NewRemoteGrain(id CartId, host string) (*RemoteGrain, error) {
}, nil
}
var (
remoteCartLatency = promauto.NewCounter(prometheus.CounterOpts{
Name: "cart_remote_grain_calls_total_latency",
Help: "The total latency of remote grains",
})
remoteCartCallsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "cart_remote_grain_calls_total",
Help: "The total number of calls to remote grains",
})
)
// var start time.Time
// func MeasureLatency(fn func() (*CallResult, error)) (*CallResult, error) {
// start = time.Now()
// data, err := fn()
// if err != nil {
// return data, err
// }
// elapsed := time.Since(start).Milliseconds()
// go func() {
// remoteCartLatency.Add(float64(elapsed))
// remoteCartCallsTotal.Inc()
// }()
// return data, nil
// }
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPayload, error) {
data, err := GetData(message.Write)

View File

@@ -24,7 +24,7 @@ func (h *RemoteHost) Initialize(p *SyncedPool) {
return
}
log.Printf("Remote %s has %d grains\n", h.Host, len(ids))
p.mu.Lock()
local := 0
remoteNo := 0
for _, id := range ids {
@@ -32,7 +32,7 @@ func (h *RemoteHost) Initialize(p *SyncedPool) {
remoteNo++
}
log.Printf("Removed %d local grains, added %d remote grains\n", local, remoteNo)
p.mu.Unlock()
go p.Negotiate()
}