Skip to content

Commit

Permalink
fix: concurrent random access
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Sep 12, 2023
1 parent e6d6e0e commit 741580d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion components/ledger/internal/api/v1/middlewares_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"math/rand"
"net/http"
"sync"
"time"

"github.com/formancehq/ledger/internal/api/backend"
Expand All @@ -11,7 +12,10 @@ import (
"github.com/go-chi/chi/v5"
)

var r *rand.Rand
var (
r *rand.Rand
mu sync.Mutex
)

func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
Expand All @@ -20,6 +24,9 @@ func init() {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

func randomTraceID(n int) string {
mu.Lock()
defer mu.Unlock()

b := make([]rune, n)
for i := range b {
b[i] = letterRunes[r.Intn(len(letterRunes))]
Expand Down
9 changes: 8 additions & 1 deletion components/ledger/internal/api/v2/middlewares_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v2
import (
"math/rand"
"net/http"
"sync"
"time"

"github.com/formancehq/ledger/internal/api/backend"
Expand All @@ -11,7 +12,10 @@ import (
"github.com/go-chi/chi/v5"
)

var r *rand.Rand
var (
r *rand.Rand
mu sync.Mutex
)

func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano()))
Expand All @@ -20,6 +24,9 @@ func init() {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

func randomTraceID(n int) string {
mu.Lock()
defer mu.Unlock()

b := make([]rune, n)
for i := range b {
b[i] = letterRunes[r.Intn(len(letterRunes))]
Expand Down
1 change: 0 additions & 1 deletion components/ledger/internal/storage/ledgerstore/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (store *Store) GetAggregatedBalances(ctx context.Context, q *GetAggregatedB
NewSelect().
Table(MovesTableName).
ColumnExpr("distinct on (moves.account_address, moves.asset) moves.*").
Order("account_address", "asset", "moves.seq desc").
Apply(filterPIT(q.Options.Options.PIT, "insertion_date")) // todo(gfyrag): expose capability to use effective_date

if q.Options.QueryBuilder != nil {
Expand Down

0 comments on commit 741580d

Please sign in to comment.