Skip to content

Commit

Permalink
Merge pull request #1536 from 0chain/fix/create-worker
Browse files Browse the repository at this point in the history
Remove add workers in get allocation
  • Loading branch information
dabasov authored Jun 18, 2024
2 parents 72bc427 + 6c6dc6d commit a4c103a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions wasmsdk/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func getAllocation(allocationId string) (*sdk.Allocation, error) {
Allocation: a,
Expiration: time.Now().Add(120 * time.Minute),
}
if !ok {
addWebWorkers(a)
}

cachedAllocations.Add(allocationId, it)
return it.Allocation, nil
Expand Down Expand Up @@ -74,7 +71,12 @@ func addWebWorkers(alloc *sdk.Allocation) {
if c == nil || len(c.Keys) == 0 {
return
}
isCreated := false
for _, blober := range alloc.Blobbers {
jsbridge.NewWasmWebWorker(blober.ID, blober.Baseurl, c.ClientID, c.Keys[0].PublicKey, c.Keys[0].PrivateKey, c.Mnemonic) //nolint:errcheck
_, isCreated, _ = jsbridge.NewWasmWebWorker(blober.ID, blober.Baseurl, c.ClientID, c.Keys[0].PublicKey, c.Keys[0].PrivateKey, c.Mnemonic) //nolint:errcheck
}
// wait for worker to be instantiated
if isCreated {
time.Sleep(1 * time.Second)
}
}
11 changes: 6 additions & 5 deletions wasmsdk/jsbridge/webworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ var (
workers = make(map[string]*WasmWebWorker)
)

func NewWasmWebWorker(blobberID, blobberURL, clientID, publicKey, privateKey, mnemonic string) (*WasmWebWorker, error) {
func NewWasmWebWorker(blobberID, blobberURL, clientID, publicKey, privateKey, mnemonic string) (*WasmWebWorker, bool, error) {
created := false
_, ok := workers[blobberID]
if ok {
return workers[blobberID], nil
return workers[blobberID], created, nil
}

w := &WasmWebWorker{
Expand All @@ -52,11 +53,11 @@ func NewWasmWebWorker(blobberID, blobberURL, clientID, publicKey, privateKey, mn
}

if err := w.Start(); err != nil {
return nil, err
return nil, created, err
}
workers[blobberID] = w

return w, nil
created = true
return w, created, nil
}

func GetWorker(blobberID string) *WasmWebWorker {
Expand Down

0 comments on commit a4c103a

Please sign in to comment.