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

lightningnetwork / lnd / 15736109134

18 Jun 2025 02:46PM UTC coverage: 58.197% (-10.1%) from 68.248%
15736109134

Pull #9752

github

web-flow
Merge d2634a68c into 31c74f20f
Pull Request #9752: routerrpc: reject payment to invoice that don't have payment secret or blinded paths

6 of 13 new or added lines in 2 files covered. (46.15%)

28331 existing lines in 455 files now uncovered.

97860 of 168153 relevant lines covered (58.2%)

1.81 hits per line

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

91.11
/multimutex/multimutex.go
1
package multimutex
2

3
import (
4
        "fmt"
5
        "sync"
6
)
7

8
// cntMutex is a struct that wraps a counter and a mutex, and is used to keep
9
// track of the number of goroutines waiting for access to the
10
// mutex, such that we can forget about it when the counter is zero.
11
type cntMutex struct {
12
        cnt int
13
        sync.Mutex
14
}
15

16
// Mutex is a struct that keeps track of a set of mutexes with a given ID. It
17
// can be used for making sure only one goroutine gets given the mutex per ID.
18
type Mutex[T comparable] struct {
19
        // mutexes is a map of IDs to a cntMutex. The cntMutex for a given ID
20
        // will hold the mutex to be used by all callers requesting access for
21
        // the ID, in addition to the count of callers.
22
        mutexes map[T]*cntMutex
23

24
        // mapMtx is used to give synchronize concurrent access to the mutexes
25
        // map.
26
        mapMtx sync.Mutex
27
}
28

29
// NewMutex creates a new Mutex.
30
func NewMutex[T comparable]() *Mutex[T] {
3✔
31
        return &Mutex[T]{
3✔
32
                mutexes: make(map[T]*cntMutex),
3✔
33
        }
3✔
34
}
3✔
35

36
// Lock locks the mutex by the given ID. If the mutex is already locked by this
37
// ID, Lock blocks until the mutex is available.
38
func (c *Mutex[T]) Lock(id T) {
3✔
39
        c.mapMtx.Lock()
3✔
40
        mtx, ok := c.mutexes[id]
3✔
41
        if ok {
6✔
42
                // If the mutex already existed in the map, we increment its
3✔
43
                // counter, to indicate that there now is one more goroutine
3✔
44
                // waiting for it.
3✔
45
                mtx.cnt++
3✔
46
        } else {
6✔
47
                // If it was not in the map, it means no other goroutine has
3✔
48
                // locked the mutex for this ID, and we can create a new mutex
3✔
49
                // with count 1 and add it to the map.
3✔
50
                mtx = &cntMutex{
3✔
51
                        cnt: 1,
3✔
52
                }
3✔
53
                c.mutexes[id] = mtx
3✔
54
        }
3✔
55
        c.mapMtx.Unlock()
3✔
56

3✔
57
        // Acquire the mutex for this ID.
3✔
58
        mtx.Lock()
3✔
59
}
60

61
// Unlock unlocks the mutex by the given ID. It is a run-time error if the
62
// mutex is not locked by the ID on entry to Unlock.
63
func (c *Mutex[T]) Unlock(id T) {
3✔
64
        // Since we are done with all the work for this update, we update the
3✔
65
        // map to reflect that.
3✔
66
        c.mapMtx.Lock()
3✔
67

3✔
68
        mtx, ok := c.mutexes[id]
3✔
69
        if !ok {
3✔
UNCOV
70
                // The mutex not existing in the map means an unlock for an ID
×
UNCOV
71
                // not currently locked was attempted.
×
UNCOV
72
                panic(fmt.Sprintf("double unlock for id %v",
×
UNCOV
73
                        id))
×
74
        }
75

76
        // Decrement the counter. If the count goes to zero, it means this
77
        // caller was the last one to wait for the mutex, and we can delete it
78
        // from the map. We can do this safely since we are under the mapMtx,
79
        // meaning that all other goroutines waiting for the mutex already have
80
        // incremented it, or will create a new mutex when they get the mapMtx.
81
        mtx.cnt--
3✔
82
        if mtx.cnt == 0 {
6✔
83
                delete(c.mutexes, id)
3✔
84
        }
3✔
85
        c.mapMtx.Unlock()
3✔
86

3✔
87
        // Unlock the mutex for this ID.
3✔
88
        mtx.Unlock()
3✔
89
}
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