Skip to content

Commit

Permalink
x - launch resolvers concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Nov 19, 2024
1 parent ed503b5 commit e2f240d
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions contractcourt/channel_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1575,22 +1575,52 @@ func (c *ChannelArbitrator) resolveContracts(resolvers []ContractResolver) {
}
}

// launchResolvers launches all the active resolvers.
// launchResolvers launches all the active resolvers concurrently.
func (c *ChannelArbitrator) launchResolvers() {
for _, contract := range c.resolvers() {
resolvers := c.resolvers()

// errChans is a map of channels that will be used to receive errors
// returned from launching the resolvers.
errChans := make(map[ContractResolver]chan error, len(resolvers))

// Launch each resolver in goroutines.
for _, r := range resolvers {
// If the contract is already resolved, there's no need to
// launch it again.
if contract.IsResolved() {
if r.IsResolved() {
log.Debugf("ChannelArbitrator(%v): skipping resolver "+
"%T as it's already resolved", c.id(), contract)
"%T as it's already resolved", c.id(), r)

continue
}

if err := contract.Launch(); err != nil {
// Create a signal chan.
errChan := make(chan error, 1)
errChans[r] = errChan

go func() {
err := r.Launch()
errChan <- err
}()

}

// Wait for all resolvers to finish launching.
for r, errChan := range errChans {
select {
case err := <-errChan:
if err == nil {
continue
}

log.Errorf("ChannelArbitrator(%v): unable to launch "+
"contract resolver(%T): %v", c.id(),
contract, err)
"contract resolver(%T): %v", c.id(), r, err)

case <-c.quit:
log.Debugf("ChannelArbitrator quit signal received, " +
"exit launchResolvers")

return
}
}
}
Expand Down

0 comments on commit e2f240d

Please sign in to comment.