Skip to content

Commit

Permalink
add mutex to map assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Varunram committed Dec 1, 2020
1 parent e020b6f commit 20b009b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 4 additions & 3 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ func NewMultiUtxoTxs() {
return
}

ret := make(format.EIUtxoReturn, len(rf))
var ret format.EIUtxoReturn
ret.Ret = make(format.EIUtxoReturnMap, len(rf))
var wg2 sync.WaitGroup
for key, elem := range rf {
wg2.Add(1)
Expand Down Expand Up @@ -443,11 +444,11 @@ func NewMultiUtxoTxs() {
temp.Utxos = append(temp.Utxos, elem)
}
}
ret[key] = temp
ret.Assign(key, temp)
}(&wg2, key, elem)
}
wg2.Wait()
erpc.MarshalSend(w, ret)
erpc.MarshalSend(w, ret.Ret)
})
}

Expand Down
17 changes: 16 additions & 1 deletion format/return.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package format

import "sync"

// UtxoTxReturn is the return structure used in /utxotxs
type UtxoTxReturn struct {
Utxos [][]Utxo `json:"Utxos"`
Transactions []MultigetAddrReturn `json:"Txs"`
}

// EIUtxoReturnMap is a map helper
type EIUtxoReturnMap map[string]UtxoTxReturn

// EIUtxoReturn is the reutrn structure used in /nutxotxs
type EIUtxoReturn map[string]UtxoTxReturn
type EIUtxoReturn struct {
Ret map[string]UtxoTxReturn
mu sync.Mutex
}

// Assign assigns key:elem in a map and locks mutex
func (a *EIUtxoReturn) Assign(key string, elem UtxoTxReturn) {
a.mu.Lock()
a.Ret[key] = elem
a.mu.Unlock()
}

// BalTxReturn is a struct used for the baltxs endpoint
type BalTxReturn struct {
Expand Down

0 comments on commit 20b009b

Please sign in to comment.