Created
January 27, 2025 02:50
-
-
Save yyforyongyu/7cf8d2e2586b2c38d197e05315b9d55d to your computer and use it in GitHub Desktop.
Diff on event loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go | |
| index 720625f2c..60a150e5d 100644 | |
| --- a/htlcswitch/switch.go | |
| +++ b/htlcswitch/switch.go | |
| @@ -280,6 +280,8 @@ type Switch struct { | |
| // this channel. | |
| linkIndex map[lnwire.ChannelID]ChannelLink | |
| + attemptResultReqChan chan *attemptResultReq | |
| + | |
| // forwardingIndex is an index which is consulted by the switch when it | |
| // needs to locate the next hop to forward an incoming/outgoing HTLC | |
| // update to/from. | |
| @@ -369,20 +371,21 @@ func New(cfg Config, currentHeight uint32) (*Switch, error) { | |
| } | |
| s := &Switch{ | |
| - bestHeight: currentHeight, | |
| - cfg: &cfg, | |
| - circuits: circuitMap, | |
| - linkIndex: make(map[lnwire.ChannelID]ChannelLink), | |
| - forwardingIndex: make(map[lnwire.ShortChannelID]ChannelLink), | |
| - interfaceIndex: make(map[[33]byte]map[lnwire.ChannelID]ChannelLink), | |
| - pendingLinkIndex: make(map[lnwire.ChannelID]ChannelLink), | |
| - linkStopIndex: make(map[lnwire.ChannelID]chan struct{}), | |
| - networkResults: newNetworkResultStore(cfg.DB), | |
| - htlcPlex: make(chan *plexPacket), | |
| - chanCloseRequests: make(chan *ChanClose), | |
| - resolutionMsgs: make(chan *resolutionMsg), | |
| - resMsgStore: resStore, | |
| - quit: make(chan struct{}), | |
| + bestHeight: currentHeight, | |
| + cfg: &cfg, | |
| + circuits: circuitMap, | |
| + attemptResultReqChan: make(chan *attemptResultReq, 100), | |
| + linkIndex: make(map[lnwire.ChannelID]ChannelLink), | |
| + forwardingIndex: make(map[lnwire.ShortChannelID]ChannelLink), | |
| + interfaceIndex: make(map[[33]byte]map[lnwire.ChannelID]ChannelLink), | |
| + pendingLinkIndex: make(map[lnwire.ChannelID]ChannelLink), | |
| + linkStopIndex: make(map[lnwire.ChannelID]chan struct{}), | |
| + networkResults: newNetworkResultStore(cfg.DB), | |
| + htlcPlex: make(chan *plexPacket), | |
| + chanCloseRequests: make(chan *ChanClose), | |
| + resolutionMsgs: make(chan *resolutionMsg), | |
| + resMsgStore: resStore, | |
| + quit: make(chan struct{}), | |
| } | |
| s.aliasToReal = make(map[lnwire.ShortChannelID]lnwire.ShortChannelID) | |
| @@ -447,6 +450,11 @@ func (s *Switch) HasAttemptResult(attemptID uint64) (bool, error) { | |
| return false, nil | |
| } | |
| +type attemptResultReq struct { | |
| + networkResultChan <-chan *networkResult | |
| + notifyChan chan *PaymentResult | |
| +} | |
| + | |
| // GetAttemptResult returns the result of the HTLC attempt with the given | |
| // attemptID. The paymentHash should be set to the payment's overall hash, or | |
| // in case of AMP payments the payment's unique identifier. | |
| @@ -489,42 +497,15 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash, | |
| } | |
| resultChan := make(chan *PaymentResult, 1) | |
| + req := &attemptResultReq{ | |
| + networkResultChan: nChan, | |
| + notifyChan: resultChan, | |
| + } | |
| - // Since the attempt was known, we can start a goroutine that can | |
| - // extract the result when it is available, and pass it on to the | |
| - // caller. | |
| - s.wg.Add(1) | |
| - go func() { | |
| - defer s.wg.Done() | |
| - | |
| - var n *networkResult | |
| - select { | |
| - case n = <-nChan: | |
| - case <-s.quit: | |
| - // We close the result channel to signal a shutdown. We | |
| - // don't send any result in this case since the HTLC is | |
| - // still in flight. | |
| - close(resultChan) | |
| - return | |
| - } | |
| - | |
| - log.Debugf("Received network result %T for attemptID=%v", n.msg, | |
| - attemptID) | |
| - | |
| - // Extract the result and pass it to the result channel. | |
| - result, err := s.extractResult( | |
| - deobfuscator, n, attemptID, paymentHash, | |
| - ) | |
| - if err != nil { | |
| - e := fmt.Errorf("unable to extract result: %w", err) | |
| - log.Error(e) | |
| - resultChan <- &PaymentResult{ | |
| - Error: e, | |
| - } | |
| - return | |
| - } | |
| - resultChan <- result | |
| - }() | |
| + select { | |
| + case <-s.quit: | |
| + case s.attemptResultReqChan <- req: | |
| + } | |
| return resultChan, nil | |
| } | |
| @@ -1739,6 +1720,43 @@ out: | |
| // memory. | |
| s.pendingSettleFails = s.pendingSettleFails[:0] | |
| + case req := <-s.attemptResultReqChan: | |
| + nChan := req.networkResultChan | |
| + resultChan := req.notifyChan | |
| + | |
| + // Since the attempt was known, we can start a goroutine that can | |
| + // extract the result when it is available, and pass it on to the | |
| + // caller. | |
| + go func() { | |
| + var n *networkResult | |
| + select { | |
| + case n = <-nChan: | |
| + case <-s.quit: | |
| + // We close the result channel to signal a shutdown. We | |
| + // don't send any result in this case since the HTLC is | |
| + // still in flight. | |
| + close(resultChan) | |
| + return | |
| + } | |
| + | |
| + log.Debugf("Received network result %T for attemptID=%v", n.msg, | |
| + attemptID) | |
| + | |
| + // Extract the result and pass it to the result channel. | |
| + result, err := s.extractResult( | |
| + deobfuscator, n, attemptID, paymentHash, | |
| + ) | |
| + if err != nil { | |
| + e := fmt.Errorf("unable to extract result: %w", err) | |
| + log.Error(e) | |
| + resultChan <- &PaymentResult{ | |
| + Error: e, | |
| + } | |
| + return | |
| + } | |
| + resultChan <- result | |
| + }() | |
| + | |
| case <-s.quit: | |
| return | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment