Skip to content

Commit 403b409

Browse files
committed
loopd: move global state into server state
1 parent 55908db commit 403b409

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed

loopd/daemon.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightninglabs/loop"
1717
"github.com/lightninglabs/loop/lndclient"
1818
"github.com/lightninglabs/loop/looprpc"
19+
"github.com/lightningnetwork/lnd/lntypes"
1920
"google.golang.org/grpc"
2021
)
2122

@@ -68,14 +69,18 @@ func daemon(config *config, lisCfg *listenerCfg) error {
6869
return err
6970
}
7071

72+
swaps := make(map[lntypes.Hash]loop.SwapInfo)
7173
for _, s := range swapsList {
7274
swaps[s.SwapHash] = *s
7375
}
7476

7577
// Instantiate the loopd gRPC server.
7678
server := swapClientServer{
77-
impl: swapClient,
78-
lnd: &lnd.LndServices,
79+
impl: swapClient,
80+
lnd: &lnd.LndServices,
81+
swaps: swaps,
82+
subscribers: make(map[int]chan<- interface{}),
83+
statusChan: make(chan loop.SwapInfo),
7984
}
8085

8186
serverOpts := []grpc.ServerOption{}
@@ -130,8 +135,6 @@ func daemon(config *config, lisCfg *listenerCfg) error {
130135
log.Infof("REST proxy disabled")
131136
}
132137

133-
statusChan := make(chan loop.SwapInfo)
134-
135138
mainCtx, cancel := context.WithCancel(context.Background())
136139
var wg sync.WaitGroup
137140

@@ -141,7 +144,7 @@ func daemon(config *config, lisCfg *listenerCfg) error {
141144
defer wg.Done()
142145

143146
log.Infof("Starting swap client")
144-
err := swapClient.Run(mainCtx, statusChan)
147+
err := swapClient.Run(mainCtx, server.statusChan)
145148
if err != nil {
146149
log.Error(err)
147150
}
@@ -159,25 +162,7 @@ func daemon(config *config, lisCfg *listenerCfg) error {
159162
defer wg.Done()
160163

161164
log.Infof("Waiting for updates")
162-
for {
163-
select {
164-
case swap := <-statusChan:
165-
swapsLock.Lock()
166-
swaps[swap.SwapHash] = swap
167-
168-
for _, subscriber := range subscribers {
169-
select {
170-
case subscriber <- swap:
171-
case <-mainCtx.Done():
172-
return
173-
}
174-
}
175-
176-
swapsLock.Unlock()
177-
case <-mainCtx.Done():
178-
return
179-
}
180-
}
165+
server.processStatusUpdates(mainCtx)
181166
}()
182167

183168
// Start the grpc server.

loopd/start.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,16 @@ import (
77
"os"
88
"path/filepath"
99
"strings"
10-
"sync"
1110

1211
"github.com/jessevdk/go-flags"
1312
"github.com/lightninglabs/loop"
1413
"github.com/lightninglabs/loop/lndclient"
1514
"github.com/lightningnetwork/lnd/build"
16-
"github.com/lightningnetwork/lnd/lntypes"
1715
)
1816

1917
const (
20-
defaultConfTarget = int32(6)
21-
)
22-
23-
var (
18+
defaultConfTarget = int32(6)
2419
defaultConfigFilename = "loopd.conf"
25-
26-
swaps = make(map[lntypes.Hash]loop.SwapInfo)
27-
subscribers = make(map[int]chan<- interface{})
28-
nextSubscriberID int
29-
swapsLock sync.Mutex
3020
)
3121

3222
// RPCConfig holds optional options that can be used to make the loop daemon

loopd/swapclient_server.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"sort"
8+
"sync"
89
"time"
910

11+
"github.com/lightningnetwork/lnd/lntypes"
1012
"github.com/lightningnetwork/lnd/queue"
1113

1214
"github.com/lightninglabs/loop"
@@ -29,8 +31,13 @@ const (
2931

3032
// swapClientServer implements the grpc service exposed by loopd.
3133
type swapClientServer struct {
32-
impl *loop.Client
33-
lnd *lndclient.LndServices
34+
impl *loop.Client
35+
lnd *lndclient.LndServices
36+
swaps map[lntypes.Hash]loop.SwapInfo
37+
subscribers map[int]chan<- interface{}
38+
statusChan chan loop.SwapInfo
39+
nextSubscriberID int
40+
swapsLock sync.Mutex
3441
}
3542

3643
// LoopOut initiates an loop out swap with the given parameters. The call
@@ -162,28 +169,28 @@ func (s *swapClientServer) Monitor(in *looprpc.MonitorRequest,
162169
// Add this subscriber to the global subscriber list. Also create a
163170
// snapshot of all pending and completed swaps within the lock, to
164171
// prevent subscribers from receiving duplicate updates.
165-
swapsLock.Lock()
172+
s.swapsLock.Lock()
166173

167-
id := nextSubscriberID
168-
nextSubscriberID++
169-
subscribers[id] = queue.ChanIn()
174+
id := s.nextSubscriberID
175+
s.nextSubscriberID++
176+
s.subscribers[id] = queue.ChanIn()
170177

171178
var pendingSwaps, completedSwaps []loop.SwapInfo
172-
for _, swap := range swaps {
179+
for _, swap := range s.swaps {
173180
if swap.State.Type() == loopdb.StateTypePending {
174181
pendingSwaps = append(pendingSwaps, swap)
175182
} else {
176183
completedSwaps = append(completedSwaps, swap)
177184
}
178185
}
179186

180-
swapsLock.Unlock()
187+
s.swapsLock.Unlock()
181188

182189
defer func() {
183190
queue.Stop()
184-
swapsLock.Lock()
185-
delete(subscribers, id)
186-
swapsLock.Unlock()
191+
s.swapsLock.Lock()
192+
delete(s.subscribers, id)
193+
s.swapsLock.Unlock()
187194
}()
188195

189196
// Sort completed swaps new to old.
@@ -383,6 +390,36 @@ func (s *swapClientServer) GetLsatTokens(ctx context.Context,
383390
return &looprpc.TokensResponse{Tokens: rpcTokens}, nil
384391
}
385392

393+
// processStatusUpdates reads updates on the status channel and processes them.
394+
//
395+
// NOTE: This must run inside a goroutine as it blocks until the main context
396+
// shuts down.
397+
func (s *swapClientServer) processStatusUpdates(mainCtx context.Context) {
398+
for {
399+
select {
400+
// On updates, refresh the server's in-memory state and inform
401+
// subscribers about the changes.
402+
case swp := <-s.statusChan:
403+
s.swapsLock.Lock()
404+
s.swaps[swp.SwapHash] = swp
405+
406+
for _, subscriber := range s.subscribers {
407+
select {
408+
case subscriber <- swp:
409+
case <-mainCtx.Done():
410+
return
411+
}
412+
}
413+
414+
s.swapsLock.Unlock()
415+
416+
// Server is shutting down.
417+
case <-mainCtx.Done():
418+
return
419+
}
420+
}
421+
}
422+
386423
// validateConfTarget ensures the given confirmation target is valid. If one
387424
// isn't specified (0 value), then the default target is used.
388425
func validateConfTarget(target, defaultTarget int32) (int32, error) {

0 commit comments

Comments
 (0)