Skip to content

Commit

Permalink
wrap pool logic with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed Sep 1, 2021
1 parent 5d2a553 commit e99bed3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c
google.golang.org/grpc v1.38.0
google.golang.org/grpc v1.40.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
3 changes: 3 additions & 0 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"path/filepath"
"sync"

"github.com/tendermint/tendermint/libs/log"
"golang.org/x/sync/semaphore"
Expand Down Expand Up @@ -36,6 +37,7 @@ type Keeper struct {
wasmVM types.WasmerEngine
wasmReadVMPool []types.WasmerEngine
wasmReadVMSemaphore *semaphore.Weighted
wasmReadVMMutex *sync.Mutex

querier types.Querier
msgParser types.MsgParser
Expand Down Expand Up @@ -98,6 +100,7 @@ func NewKeeper(
wasmVM: writeWasmVM,
wasmReadVMPool: wasmReadVMPool,
wasmReadVMSemaphore: semaphore.NewWeighted(int64(numReadVms)),
wasmReadVMMutex: &sync.Mutex{},
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
treasuryKeeper: treasuryKeeper,
Expand Down
7 changes: 7 additions & 0 deletions x/wasm/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import (
"github.com/terra-money/core/x/wasm/types"
)

var n = 0

func (k Keeper) getWasmVM(ctx context.Context) (types.WasmerEngine, error) {
err := k.wasmReadVMSemaphore.Acquire(ctx, 1)
if err != nil {
return nil, err
}

k.wasmReadVMMutex.Lock()
wasmVM := k.wasmReadVMPool[0]
k.wasmReadVMPool = k.wasmReadVMPool[1:]
k.wasmReadVMMutex.Unlock()

return wasmVM, nil
}

func (k Keeper) putWasmVM(wasmVM types.WasmerEngine) {
k.wasmReadVMMutex.Lock()
k.wasmReadVMPool = append(k.wasmReadVMPool, wasmVM)
k.wasmReadVMMutex.Unlock()

k.wasmReadVMSemaphore.Release(1)
}

0 comments on commit e99bed3

Please sign in to comment.