Skip to content

Commit d6cc880

Browse files
committed
manager: remove stored runCtx
1 parent 9cb5b5f commit d6cc880

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

instantout/manager.go

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ var (
2020
ErrSwapDoesNotExist = errors.New("swap does not exist")
2121
)
2222

23+
// InitInstantOutRequest is a request to initialize an instant out.
24+
type InitInstantOutRequest struct {
25+
// reqCtx is the eventCtx for the OnStart event.
26+
reqCtx *InitInstantOutCtx
27+
// errResChan is a channel that will receive the result of the
28+
// initialization.
29+
errResChan chan error
30+
// fsmResChan is a channel that will receive the FSM.
31+
fsmResChan chan *FSM
32+
}
33+
2334
// Manager manages the instantout state machines.
2435
type Manager struct {
2536
// cfg contains all the services that the reservation manager needs to
@@ -29,23 +40,25 @@ type Manager struct {
2940
// activeInstantOuts contains all the active instantouts.
3041
activeInstantOuts map[lntypes.Hash]*FSM
3142

43+
// instantOutInitRequests contains all the instant out init requests.
44+
instantOutInitRequests chan *InitInstantOutRequest
45+
3246
// currentHeight stores the currently best known block height.
3347
currentHeight int32
3448

3549
// blockEpochChan receives new block heights.
3650
blockEpochChan chan int32
3751

38-
runCtx context.Context
39-
4052
sync.Mutex
4153
}
4254

4355
// NewInstantOutManager creates a new instantout manager.
4456
func NewInstantOutManager(cfg *Config) *Manager {
4557
return &Manager{
46-
cfg: cfg,
47-
activeInstantOuts: make(map[lntypes.Hash]*FSM),
48-
blockEpochChan: make(chan int32),
58+
cfg: cfg,
59+
activeInstantOuts: make(map[lntypes.Hash]*FSM),
60+
blockEpochChan: make(chan int32),
61+
instantOutInitRequests: make(chan *InitInstantOutRequest),
4962
}
5063
}
5164

@@ -61,7 +74,6 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
6174
runCtx, cancel := context.WithCancel(ctx)
6275
defer cancel()
6376

64-
m.runCtx = runCtx
6577
m.currentHeight = height
6678

6779
err := m.recoverInstantOuts(runCtx)
@@ -89,6 +101,27 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{},
89101
m.currentHeight = height
90102
m.Unlock()
91103

104+
case initReq := <-m.instantOutInitRequests:
105+
m.Lock()
106+
instantOut, err := NewFSM(
107+
m.cfg, ProtocolVersionFullReservation,
108+
)
109+
if err != nil {
110+
m.Unlock()
111+
initReq.errResChan <- err
112+
continue
113+
}
114+
m.activeInstantOuts[instantOut.InstantOut.SwapHash] = instantOut
115+
m.Unlock()
116+
117+
// Start the instantout FSM.
118+
go func() {
119+
err := instantOut.SendEvent(runCtx, OnStart, initReq.reqCtx)
120+
if err != nil {
121+
log.Errorf("Error sending event: %v", err)
122+
}
123+
}()
124+
92125
case err := <-newBlockErrChan:
93126
return err
94127
}
@@ -152,33 +185,30 @@ func (m *Manager) NewInstantOut(ctx context.Context,
152185
}
153186
}
154187

155-
m.Lock()
156188
// Create the instantout request.
157-
request := &InitInstantOutCtx{
158-
cltvExpiry: m.currentHeight + int32(defaultCltv),
159-
reservations: reservations,
160-
initationHeight: m.currentHeight,
161-
protocolVersion: CurrentProtocolVersion(),
162-
sweepAddress: sweepAddr,
189+
request := &InitInstantOutRequest{
190+
reqCtx: &InitInstantOutCtx{
191+
cltvExpiry: m.currentHeight + int32(defaultCltv),
192+
reservations: reservations,
193+
initationHeight: m.currentHeight,
194+
protocolVersion: CurrentProtocolVersion(),
195+
sweepAddress: sweepAddr,
196+
},
197+
errResChan: make(chan error),
198+
fsmResChan: make(chan *FSM),
163199
}
164200

165-
instantOut, err := NewFSM(
166-
m.cfg, ProtocolVersionFullReservation,
167-
)
168-
if err != nil {
169-
m.Unlock()
201+
m.instantOutInitRequests <- request
202+
203+
var instantOut *FSM
204+
205+
select {
206+
case err := <-request.errResChan:
170207
return nil, err
171-
}
172-
m.activeInstantOuts[instantOut.InstantOut.SwapHash] = instantOut
173-
m.Unlock()
174208

175-
// Start the instantout FSM.
176-
go func() {
177-
err := instantOut.SendEvent(m.runCtx, OnStart, request)
178-
if err != nil {
179-
log.Errorf("Error sending event: %v", err)
180-
}
181-
}()
209+
case instantOut = <-request.fsmResChan:
210+
211+
}
182212

183213
// If everything went well, we'll wait for the instant out to be
184214
// waiting for sweepless sweep to be confirmed.

0 commit comments

Comments
 (0)