Skip to content

Commit 4dcb882

Browse files
committed
staticaddr: poll deposits on block arrival
In this commit we remove the ticker-based polling of new deposits as this is wasteful, and we replace it by polling on the arrival of new blocks.
1 parent cef0fae commit 4dcb882

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

staticaddr/deposit/manager.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,25 @@ type ManagerConfig struct {
7474
type Manager struct {
7575
cfg *ManagerConfig
7676

77-
// mu guards access to activeDeposits map.
77+
// mu guards access to the activeDeposits map.
7878
mu sync.Mutex
7979

8080
// activeDeposits contains all the active static address outputs.
8181
activeDeposits map[wire.OutPoint]*FSM
8282

83-
// deposits contains all the deposits that have ever been made to the
83+
// deposits contain all the deposits that have ever been made to the
8484
// static address. This field is used to store and recover deposits. It
85-
// also serves as basis for reconciliation of newly detected deposits by
86-
// matching them against deposits in this map that were already seen.
85+
// also serves as a basis for reconciliation of newly detected deposits
86+
// by matching them against deposits in this map that were already seen.
8787
deposits map[wire.OutPoint]*Deposit
8888

8989
// finalizedDepositChan is a channel that receives deposits that have
9090
// been finalized. The manager will adjust its internal state and flush
9191
// finalized deposits from its memory.
9292
finalizedDepositChan chan wire.OutPoint
93+
94+
// pollingGate ensures only one reconcileDeposits runs at a time.
95+
pollingGate chan struct{}
9396
}
9497

9598
// NewManager creates a new deposit manager.
@@ -99,6 +102,7 @@ func NewManager(cfg *ManagerConfig) *Manager {
99102
activeDeposits: make(map[wire.OutPoint]*FSM),
100103
deposits: make(map[wire.OutPoint]*Deposit),
101104
finalizedDepositChan: make(chan wire.OutPoint),
105+
pollingGate: make(chan struct{}, 1),
102106
}
103107
}
104108

@@ -119,8 +123,9 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
119123
return err
120124
}
121125

122-
// Start the deposit notifier.
123-
m.pollDeposits(ctx)
126+
// Poll for deposits once at startup to detect any that occurred while
127+
// offline.
128+
go m.pollDeposits(ctx)
124129

125130
// Communicate to the caller that the address manager has completed its
126131
// initialization.
@@ -129,7 +134,11 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
129134
for {
130135
select {
131136
case height := <-newBlockChan:
132-
// Inform all active deposits about a new block arrival.
137+
// Poll for new deposits and spin up fsms for them.
138+
go m.pollDeposits(ctx)
139+
140+
// Now inform all active deposits about a new block
141+
// arrival.
133142
m.mu.Lock()
134143
activeDeposits := make([]*FSM, 0, len(m.activeDeposits))
135144
for _, fsm := range m.activeDeposits {
@@ -213,28 +222,25 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
213222
return nil
214223
}
215224

216-
// pollDeposits polls new deposits to our static address and notifies the
217-
// manager's event loop about them.
225+
// pollDeposits polls new deposits to our static addresses.
218226
func (m *Manager) pollDeposits(ctx context.Context) {
219-
log.Debugf("Waiting for new static address deposits...")
227+
log.Debugf("Polling new static address deposits...")
220228

221-
go func() {
222-
ticker := time.NewTicker(PollInterval)
223-
defer ticker.Stop()
224-
for {
225-
select {
226-
case <-ticker.C:
227-
err := m.reconcileDeposits(ctx)
228-
if err != nil {
229-
log.Errorf("unable to reconcile "+
230-
"deposits: %v", err)
231-
}
232-
233-
case <-ctx.Done():
234-
return
235-
}
229+
select {
230+
case m.pollingGate <- struct{}{}:
231+
defer func() { <-m.pollingGate }()
232+
if err := m.reconcileDeposits(ctx); err != nil {
233+
log.Errorf("unable to reconcile deposits: %v",
234+
err)
236235
}
237-
}()
236+
237+
case <-ctx.Done():
238+
return
239+
240+
default:
241+
log.Tracef("Polling new static address deposits already in " +
242+
"progress, skipping")
243+
}
238244
}
239245

240246
// reconcileDeposits fetches all spends to our static address from our lnd

0 commit comments

Comments
 (0)