safe truncation
This commit is contained in:
@@ -238,7 +238,6 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*CallResult,
|
|||||||
if item.Id == int(msg.Id) {
|
if item.Id == int(msg.Id) {
|
||||||
if item.Quantity <= int(msg.Quantity) {
|
if item.Quantity <= int(msg.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)
|
item.Quantity -= int(msg.Quantity)
|
||||||
}
|
}
|
||||||
@@ -253,13 +252,15 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*CallResult,
|
|||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("expected RemoveItem")
|
err = fmt.Errorf("expected RemoveItem")
|
||||||
} else {
|
} else {
|
||||||
for i, item := range c.Items {
|
items := make([]*CartItem, 0, len(c.Items))
|
||||||
|
for _, item := range c.Items {
|
||||||
if item.Id == int(msg.Id) {
|
if item.Id == int(msg.Id) {
|
||||||
c.TotalPrice -= item.Price * int64(item.Quantity)
|
c.TotalPrice -= item.Price * int64(item.Quantity)
|
||||||
c.Items = append(c.Items[:i], c.Items[i+1:]...)
|
} else {
|
||||||
break
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.Items = items
|
||||||
}
|
}
|
||||||
case SetDeliveryType:
|
case SetDeliveryType:
|
||||||
msg, ok := message.Content.(*messages.SetDelivery)
|
msg, ok := message.Content.(*messages.SetDelivery)
|
||||||
|
|||||||
@@ -87,12 +87,21 @@ func (p *GrainLocalPool) Purge() {
|
|||||||
if item.Expires.Before(time.Now()) {
|
if item.Expires.Before(time.Now()) {
|
||||||
if item.Grain.GetLastChange() > keepChanged {
|
if item.Grain.GetLastChange() > keepChanged {
|
||||||
log.Printf("Expired item %s changed, keeping", item.Grain.GetId())
|
log.Printf("Expired item %s changed, keeping", item.Grain.GetId())
|
||||||
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
|
if i < len(p.expiry)-1 {
|
||||||
p.expiry = append(p.expiry, item)
|
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
|
||||||
|
p.expiry = append(p.expiry, item)
|
||||||
|
} else {
|
||||||
|
p.expiry = append(p.expiry[:i], item)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Item %s expired", item.Grain.GetId())
|
log.Printf("Item %s expired", item.Grain.GetId())
|
||||||
delete(p.grains, item.Grain.GetId())
|
delete(p.grains, item.Grain.GetId())
|
||||||
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
|
if i < len(p.expiry)-1 {
|
||||||
|
p.expiry = append(p.expiry[:i], p.expiry[i+1:]...)
|
||||||
|
} else {
|
||||||
|
p.expiry = p.expiry[:i]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -255,14 +255,19 @@ func (p *SyncedPool) ExcludeKnown(hosts []string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *SyncedPool) RemoveHost(host *RemoteHost) {
|
func (p *SyncedPool) RemoveHost(host *RemoteHost) {
|
||||||
for i, r := range p.remotes {
|
toKeep := make([]*RemoteHost, 0, len(p.remotes))
|
||||||
|
for _, r := range p.remotes {
|
||||||
if r == host {
|
if r == host {
|
||||||
p.RemoveHostMappedCarts(r)
|
p.RemoveHostMappedCarts(r)
|
||||||
p.remotes = append(p.remotes[:i], p.remotes[i+1:]...)
|
|
||||||
connectedRemotes.Set(float64(len(p.remotes)))
|
} else {
|
||||||
return
|
toKeep = append(toKeep, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.remotes = toKeep
|
||||||
|
|
||||||
|
connectedRemotes.Set(float64(len(p.remotes)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SyncedPool) RemoveHostMappedCarts(host *RemoteHost) {
|
func (p *SyncedPool) RemoveHostMappedCarts(host *RemoteHost) {
|
||||||
|
|||||||
Reference in New Issue
Block a user