Merge branch 'main' of git-ssh.tornberg.me:mats/go-cart-actor
This commit is contained in:
@@ -289,16 +289,18 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
|
|||||||
case ChangeQuantityType:
|
case ChangeQuantityType:
|
||||||
msg, ok := message.Content.(*messages.ChangeQuantity)
|
msg, ok := message.Content.(*messages.ChangeQuantity)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected RemoveItem")
|
err = fmt.Errorf("expected ChangeQuantity")
|
||||||
} else {
|
} else {
|
||||||
for i, item := range c.Items {
|
for i, item := range c.Items {
|
||||||
if item.Id == int(msg.Id) {
|
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:]...)
|
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
||||||
} else {
|
} 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
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
func (s *PoolServer) WriteResult(w http.ResponseWriter, result *FrameWithPayload) error {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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("X-Pod-Name", s.pod_name)
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "https://tornberg.me")
|
|
||||||
if result.StatusCode != 200 {
|
if result.StatusCode != 200 {
|
||||||
log.Printf("Call error: %d\n", result.StatusCode)
|
log.Printf("Call error: %d\n", result.StatusCode)
|
||||||
if result.StatusCode >= 200 && result.StatusCode < 600 {
|
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)
|
buf.ReadFrom(res.Body)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Header().Set("X-Pod-Name", s.pod_name)
|
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.WriteHeader(res.StatusCode)
|
||||||
|
|
||||||
w.Write(buf.Bytes())
|
w.Write(buf.Bytes())
|
||||||
@@ -286,6 +288,12 @@ func (a *PoolServer) RewritePath(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *PoolServer) Serve() *http.ServeMux {
|
func (s *PoolServer) Serve() *http.ServeMux {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", s.RewritePath)
|
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}", ErrorHandler(s.HandleGet))
|
||||||
mux.HandleFunc("GET /{id}/add/{sku}", ErrorHandler(s.HandleAddSku))
|
mux.HandleFunc("GET /{id}/add/{sku}", ErrorHandler(s.HandleAddSku))
|
||||||
mux.HandleFunc("POST /{id}", ErrorHandler(s.HandleAddRequest))
|
mux.HandleFunc("POST /{id}", ErrorHandler(s.HandleAddRequest))
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (id CartId) String() string {
|
func (id CartId) String() string {
|
||||||
@@ -54,33 +51,6 @@ func NewRemoteGrain(id CartId, host string) (*RemoteGrain, error) {
|
|||||||
}, nil
|
}, 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) {
|
func (g *RemoteGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPayload, error) {
|
||||||
|
|
||||||
data, err := GetData(message.Write)
|
data, err := GetData(message.Write)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func (h *RemoteHost) Initialize(p *SyncedPool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Remote %s has %d grains\n", h.Host, len(ids))
|
log.Printf("Remote %s has %d grains\n", h.Host, len(ids))
|
||||||
p.mu.Lock()
|
|
||||||
local := 0
|
local := 0
|
||||||
remoteNo := 0
|
remoteNo := 0
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
@@ -32,7 +32,7 @@ func (h *RemoteHost) Initialize(p *SyncedPool) {
|
|||||||
remoteNo++
|
remoteNo++
|
||||||
}
|
}
|
||||||
log.Printf("Removed %d local grains, added %d remote grains\n", local, remoteNo)
|
log.Printf("Removed %d local grains, added %d remote grains\n", local, remoteNo)
|
||||||
p.mu.Unlock()
|
|
||||||
go p.Negotiate()
|
go p.Negotiate()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user