diff --git a/cart-grain.go b/cart-grain.go index 27b2390..b6c512c 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -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 } diff --git a/pool-server.go b/pool-server.go index 8ebfbbf..f8aa648 100644 --- a/pool-server.go +++ b/pool-server.go @@ -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)) diff --git a/remote-grain.go b/remote-grain.go index 533c7eb..5dad844 100644 --- a/remote-grain.go +++ b/remote-grain.go @@ -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) diff --git a/remote-host.go b/remote-host.go index 2dff28c..385dfba 100644 --- a/remote-host.go +++ b/remote-host.go @@ -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() }