safe truncation
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 27s
Build and Publish / BuildAndDeploy (push) Successful in 2m19s

This commit is contained in:
matst80
2024-11-12 20:51:39 +01:00
parent 983d1d52ae
commit 39244350e8
3 changed files with 26 additions and 11 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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) {