Skip to content

Commit 36e4ff3

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 36e4ff3

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

staticaddr/deposit/manager.go

Lines changed: 29 additions & 27 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,17 +123,18 @@ 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)
124-
125126
// Communicate to the caller that the address manager has completed its
126127
// initialization.
127128
close(initChan)
128129

129130
for {
130131
select {
131132
case height := <-newBlockChan:
132-
// Inform all active deposits about a new block arrival.
133+
// Poll for new deposits and spin up fsms for them.
134+
go m.pollDeposits(ctx)
135+
136+
// Now inform all active deposits about a new block
137+
// arrival.
133138
m.mu.Lock()
134139
activeDeposits := make([]*FSM, 0, len(m.activeDeposits))
135140
for _, fsm := range m.activeDeposits {
@@ -213,28 +218,25 @@ func (m *Manager) recoverDeposits(ctx context.Context) error {
213218
return nil
214219
}
215220

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

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-
}
225+
select {
226+
case m.pollingGate <- struct{}{}:
227+
defer func() { <-m.pollingGate }()
228+
if err := m.reconcileDeposits(ctx); err != nil {
229+
log.Errorf("unable to reconcile deposits: %v",
230+
err)
236231
}
237-
}()
232+
233+
case <-ctx.Done():
234+
return
235+
236+
default:
237+
log.Tracef("Polling new static address deposits already in " +
238+
"progress, skipping")
239+
}
238240
}
239241

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

0 commit comments

Comments
 (0)