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

lightningnetwork / lnd / 12425886916

20 Dec 2024 05:06AM UTC coverage: 49.675% (-7.9%) from 57.578%
12425886916

Pull #9227

github

yyforyongyu
lntest+itest: export `DeriveFundingShim`
Pull Request #9227: Beat [5/4]: fix itests for `blockbeat`

45 of 49 new or added lines in 8 files covered. (91.84%)

26491 existing lines in 430 files now uncovered.

101120 of 203562 relevant lines covered (49.68%)

2.06 hits per line

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

78.95
/batch/scheduler.go
1
package batch
2

3
import (
4
        "sync"
5
        "time"
6

7
        "github.com/lightningnetwork/lnd/kvdb"
8
)
9

10
// TimeScheduler is a batching engine that executes requests within a fixed
11
// horizon. When the first request is received, a TimeScheduler waits a
12
// configurable duration for other concurrent requests to join the batch. Once
13
// this time has elapsed, the batch is closed and executed. Subsequent requests
14
// are then added to a new batch which undergoes the same process.
15
type TimeScheduler struct {
16
        db       kvdb.Backend
17
        locker   sync.Locker
18
        duration time.Duration
19

20
        mu sync.Mutex
21
        b  *batch
22
}
23

24
// NewTimeScheduler initializes a new TimeScheduler with a fixed duration at
25
// which to schedule batches. If the operation needs to modify a higher-level
26
// cache, the cache's lock should be provided to so that external consistency
27
// can be maintained, as successful db operations will cause a request's
28
// OnCommit method to be executed while holding this lock.
29
func NewTimeScheduler(db kvdb.Backend, locker sync.Locker,
30
        duration time.Duration) *TimeScheduler {
4✔
31

4✔
32
        return &TimeScheduler{
4✔
33
                db:       db,
4✔
34
                locker:   locker,
4✔
35
                duration: duration,
4✔
36
        }
4✔
37
}
4✔
38

39
// Execute schedules the provided request for batch execution along with other
40
// concurrent requests. The request will be executed within a fixed horizon,
41
// parameterizeed by the duration of the scheduler. The error from the
42
// underlying operation is returned to the caller.
43
//
44
// NOTE: Part of the Scheduler interface.
45
func (s *TimeScheduler) Execute(r *Request) error {
4✔
46
        req := request{
4✔
47
                Request: r,
4✔
48
                errChan: make(chan error, 1),
4✔
49
        }
4✔
50

4✔
51
        // Add the request to the current batch. If the batch has been cleared
4✔
52
        // or no batch exists, create a new one.
4✔
53
        s.mu.Lock()
4✔
54
        if s.b == nil {
8✔
55
                s.b = &batch{
4✔
56
                        db:     s.db,
4✔
57
                        clear:  s.clear,
4✔
58
                        locker: s.locker,
4✔
59
                }
4✔
60
                time.AfterFunc(s.duration, s.b.trigger)
4✔
61
        }
4✔
62
        s.b.reqs = append(s.b.reqs, &req)
4✔
63

4✔
64
        // If this is a non-lazy request, we'll execute the batch immediately.
4✔
65
        if !r.lazy {
8✔
66
                go s.b.trigger()
4✔
67
        }
4✔
68

69
        s.mu.Unlock()
4✔
70

4✔
71
        // Wait for the batch to process the request. If the batch didn't
4✔
72
        // ask us to execute the request individually, simply return the error.
4✔
73
        err := <-req.errChan
4✔
74
        if err != errSolo {
8✔
75
                return err
4✔
76
        }
4✔
77

78
        // Obtain exclusive access to the cache if this scheduler needs to
79
        // modify the cache in OnCommit.
UNCOV
80
        if s.locker != nil {
×
UNCOV
81
                s.locker.Lock()
×
UNCOV
82
                defer s.locker.Unlock()
×
UNCOV
83
        }
×
84

85
        // Otherwise, run the request on its own.
UNCOV
86
        commitErr := kvdb.Update(s.db, req.Update, func() {
×
UNCOV
87
                if req.Reset != nil {
×
88
                        req.Reset()
×
89
                }
×
90
        })
91

92
        // Finally, return the commit error directly or execute the OnCommit
93
        // closure with the commit error if present.
UNCOV
94
        if req.OnCommit != nil {
×
95
                return req.OnCommit(commitErr)
×
96
        }
×
97

UNCOV
98
        return commitErr
×
99
}
100

101
// clear resets the scheduler's batch to nil so that no more requests can be
102
// added.
103
func (s *TimeScheduler) clear(b *batch) {
4✔
104
        s.mu.Lock()
4✔
105
        if s.b == b {
8✔
106
                s.b = nil
4✔
107
        }
4✔
108
        s.mu.Unlock()
4✔
109
}
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