• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12583319996

02 Jan 2025 01:38PM UTC coverage: 57.522% (-1.1%) from 58.598%
12583319996

Pull #9361

github

starius
fn/ContextGuard: use context.AfterFunc to wait

Simplifies context cancellation handling by using context.AfterFunc instead of a
goroutine to wait for context cancellation. This approach avoids the overhead of
a goroutine during the waiting period.

For ctxQuitUnsafe, since g.quit is closed only in the Quit method (which also
cancels all associated contexts), waiting on context cancellation ensures the
same behavior without unnecessary dependency on g.quit.

Added a test to ensure that the Create method does not launch any goroutines.
Pull Request #9361: fn: optimize context guard

102587 of 178344 relevant lines covered (57.52%)

24734.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.48
/chanacceptor/chainedacceptor.go
1
package chanacceptor
2

3
import (
4
        "sync"
5
        "sync/atomic"
6
)
7

8
// ChainedAcceptor represents a conjunction of ChannelAcceptor results.
9
type ChainedAcceptor struct {
10
        acceptorID uint64 // To be used atomically.
11

12
        // acceptors is a map of ChannelAcceptors that will be evaluated when
13
        // the ChainedAcceptor's Accept method is called.
14
        acceptors    map[uint64]ChannelAcceptor
15
        acceptorsMtx sync.RWMutex
16
}
17

18
// NewChainedAcceptor initializes a ChainedAcceptor.
19
func NewChainedAcceptor() *ChainedAcceptor {
109✔
20
        return &ChainedAcceptor{
109✔
21
                acceptors: make(map[uint64]ChannelAcceptor),
109✔
22
        }
109✔
23
}
109✔
24

25
// AddAcceptor adds a ChannelAcceptor to this ChainedAcceptor.
26
//
27
// NOTE: Part of the MultiplexAcceptor interface.
28
func (c *ChainedAcceptor) AddAcceptor(acceptor ChannelAcceptor) uint64 {
2✔
29
        id := atomic.AddUint64(&c.acceptorID, 1)
2✔
30

2✔
31
        c.acceptorsMtx.Lock()
2✔
32
        c.acceptors[id] = acceptor
2✔
33
        c.acceptorsMtx.Unlock()
2✔
34

2✔
35
        // Return the id so that a caller can call RemoveAcceptor.
2✔
36
        return id
2✔
37
}
2✔
38

39
// RemoveAcceptor removes a ChannelAcceptor from this ChainedAcceptor given
40
// an ID.
41
//
42
// NOTE: Part of the MultiplexAcceptor interface.
43
func (c *ChainedAcceptor) RemoveAcceptor(id uint64) {
2✔
44
        c.acceptorsMtx.Lock()
2✔
45
        delete(c.acceptors, id)
2✔
46
        c.acceptorsMtx.Unlock()
2✔
47
}
2✔
48

49
// numAcceptors returns the number of acceptors contained in the
50
// ChainedAcceptor.
51
func (c *ChainedAcceptor) numAcceptors() int {
6✔
52
        c.acceptorsMtx.RLock()
6✔
53
        defer c.acceptorsMtx.RUnlock()
6✔
54
        return len(c.acceptors)
6✔
55
}
6✔
56

57
// Accept evaluates the results of all ChannelAcceptors in the acceptors map
58
// and returns the conjunction of all these predicates.
59
//
60
// NOTE: Part of the ChannelAcceptor interface.
61
func (c *ChainedAcceptor) Accept(req *ChannelAcceptRequest) *ChannelAcceptResponse {
49✔
62
        c.acceptorsMtx.RLock()
49✔
63
        defer c.acceptorsMtx.RUnlock()
49✔
64

49✔
65
        var finalResp ChannelAcceptResponse
49✔
66

49✔
67
        for _, acceptor := range c.acceptors {
51✔
68
                // Call our acceptor to determine whether we want to accept this
2✔
69
                // channel.
2✔
70
                acceptorResponse := acceptor.Accept(req)
2✔
71

2✔
72
                // If we should reject the channel, we can just exit early. This
2✔
73
                // has the effect of returning the error belonging to our first
2✔
74
                // failed acceptor.
2✔
75
                if acceptorResponse.RejectChannel() {
2✔
76
                        return acceptorResponse
×
77
                }
×
78

79
                // If we have accepted the channel, we need to set the other
80
                // fields that were set in the response. However, since we are
81
                // dealing with multiple responses, we need to make sure that we
82
                // have not received inconsistent values (eg a csv delay of 1
83
                // from one acceptor, and a delay of 120 from another). We
84
                // set each value on our final response if it has not been set
85
                // yet, and allow duplicate sets if the value is the same. If
86
                // we cannot set a field, we return an error response.
87
                var err error
2✔
88
                finalResp, err = mergeResponse(finalResp, *acceptorResponse)
2✔
89
                if err != nil {
2✔
90
                        log.Errorf("response for: %x has inconsistent values: %v",
×
91
                                req.OpenChanMsg.PendingChannelID, err)
×
92

×
93
                        return NewChannelAcceptResponse(
×
94
                                false, errChannelRejected, nil, 0, 0,
×
95
                                0, 0, 0, 0, false,
×
96
                        )
×
97
                }
×
98
        }
99

100
        // If we have gone through all of our acceptors with no objections, we
101
        // can return an acceptor with a nil error.
102
        return &finalResp
49✔
103
}
104

105
// A compile-time constraint to ensure ChainedAcceptor implements the
106
// MultiplexAcceptor interface.
107
var _ MultiplexAcceptor = (*ChainedAcceptor)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc