support multiple mutations
This commit is contained in:
@@ -29,8 +29,8 @@ func NewPoolServer(pool actor.GrainPool[*CartGrain], pod_name string, klarnaClie
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PoolServer) ApplyLocal(id CartId, mutation proto.Message) (*CartGrain, error) {
|
||||
return s.Apply(uint64(id), mutation)
|
||||
func (s *PoolServer) ApplyLocal(id CartId, mutation ...proto.Message) (*CartGrain, error) {
|
||||
return s.Apply(uint64(id), mutation...)
|
||||
}
|
||||
|
||||
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request, id CartId) error {
|
||||
@@ -178,28 +178,30 @@ func (s *PoolServer) HandleSetCartItems(w http.ResponseWriter, r *http.Request,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reply, err := s.ApplyLocal(id, &messages.ClearCartRequest{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msgs := make([]proto.Message, 0, len(setCartItems.Items)+1)
|
||||
msgs = append(msgs, &messages.ClearCartRequest{})
|
||||
wg := sync.WaitGroup{}
|
||||
for _, item := range setCartItems.Items {
|
||||
wg.Add(1)
|
||||
go func(sku string, quantity int) {
|
||||
defer wg.Done()
|
||||
msg, err := GetItemAddMessage(sku, quantity, setCartItems.Country, nil)
|
||||
if err != nil {
|
||||
log.Printf("error adding item %s: %v", sku, err)
|
||||
return
|
||||
}
|
||||
reply, err = s.ApplyLocal(id, msg)
|
||||
if err != nil {
|
||||
log.Printf("error applying message %v: %v", msg, err)
|
||||
return
|
||||
}
|
||||
wg.Done()
|
||||
msgs = append(msgs, msg)
|
||||
|
||||
}(item.Sku, item.Quantity)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
reply, err := s.ApplyLocal(id, msgs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.WriteResult(w, reply)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user