Skip to content

Commit

Permalink
Fix data race in downloads gathering (#217)
Browse files Browse the repository at this point in the history
* Fix data race in downloads gathering

* Run tests with race flag
  • Loading branch information
mraerino committed Oct 21, 2021
1 parent 7e033ed commit 24ac5e8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ lint: ## Lint the code
golint `go list ./... | grep -v /vendor/`

test: ## Run tests.
go test -v `go list ./... | grep -v /vendor/`
go test -v -race ./...
2 changes: 2 additions & 0 deletions models/line_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ func (i *LineItem) Process(config *conf.Configuration, userClaims map[string]int
i.AddonItems[index].Price = lowestPrice.cents
}

order.ModificationLock.Lock()
order.Downloads = append(order.Downloads, i.MissingDownloads(order, meta)...)
order.ModificationLock.Unlock()

return i.calculatePrice(userClaims, meta.Prices, order.Currency)
}
Expand Down
3 changes: 3 additions & 0 deletions models/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"encoding/json"
"fmt"
"sync"
"time"

"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -104,6 +105,8 @@ type Order struct {
CreatedAt time.Time `json:"created_at" sql:"index"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"-" sql:"index"`

ModificationLock sync.Mutex `json:"-" sql:"-"`
}

// TableName returns the database table name for the Order model.
Expand Down

0 comments on commit 24ac5e8

Please sign in to comment.