Skip to content

Commit

Permalink
Merge pull request #223 from ipfs-force-community/feat/control-concur…
Browse files Browse the repository at this point in the history
…rency

feat: control concurrency when call CountWinner
  • Loading branch information
LinZexiao authored Aug 30, 2023
2 parents b9e9167 + c4668d0 commit 4e6a912
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 9 additions & 2 deletions miner/miningmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (m *Miner) winCountInRound(ctx context.Context, account string, mAddr addre

var nullRounds abi.ChainEpoch
if epoch > ts.Height() {
log.Infof("winCountInRound had null round, expect epoch %d, actual epoch %d", epoch, ts.Height())
nullRounds = epoch - ts.Height()
}
round := ts.Height() + nullRounds + 1
Expand Down Expand Up @@ -194,8 +193,10 @@ func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start
}
m.lkWPP.Unlock()

controlChan := make(chan struct{}, 100)
if len(minerWpps) > 0 {
sign := m.signerFunc(ctx, m.gatewayNode)

wg.Add(len(minerWpps))
for addr, wpp := range minerWpps {
tAddr := addr
Expand All @@ -209,7 +210,12 @@ func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start
for epoch := start; epoch <= end; epoch++ {
wgWin.Add(1)
go func(epoch abi.ChainEpoch) {
defer wgWin.Done()
defer func() {
wgWin.Done()
<-controlChan
}()

controlChan <- struct{}{}

winner, err := m.winCountInRound(ctx, tWpp.account, tAddr, sign, epoch)
if err != nil {
Expand All @@ -233,6 +239,7 @@ func (m *Miner) CountWinners(ctx context.Context, addrs []address.Address, start
}
wg.Wait()
}
close(controlChan)

return res, nil
}
Expand Down
3 changes: 2 additions & 1 deletion miner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func IsRoundWinner(
miner address.Address,
brand types.BeaconEntry,
mbi *types.MiningBaseInfo,
sign SignFunc) (*types.ElectionProof, error) {
sign SignFunc,
) (*types.ElectionProof, error) {

buf := new(bytes.Buffer)
if err := miner.MarshalCBOR(buf); err != nil {
Expand Down

0 comments on commit 4e6a912

Please sign in to comment.