mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
multi: consume and log sever state updates
This commit is contained in:
parent
a6539b6adb
commit
cd2b08aec6
8 changed files with 819 additions and 65 deletions
52
updates.go
Normal file
52
updates.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package loop
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/lightninglabs/loop/swap"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
)
|
||||
|
||||
// subscribeAndLogUpdates subscribes to updates for a swap and logs them. This
|
||||
// function will block, so should run as a goroutine. Note that our subscription
|
||||
// does not survive server restarts; we will simply not have update logs if the
|
||||
// server restarts during swap execution.
|
||||
func subscribeAndLogUpdates(ctx context.Context, hash lntypes.Hash,
|
||||
log *swap.PrefixLog, subscribe func(context.Context,
|
||||
lntypes.Hash) (<-chan *ServerUpdate, <-chan error, error)) {
|
||||
|
||||
subscribeChan, errChan, err := subscribe(ctx, hash)
|
||||
if err != nil {
|
||||
log.Errorf("could not get swap subscription: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
// Consume any updates and log them.
|
||||
case update := <-subscribeChan:
|
||||
log.Infof("Server update: %v received, "+
|
||||
"timestamp: %v", update.State, update.Timestamp)
|
||||
|
||||
// If we get an error from the server, we check whether it is
|
||||
// due to server exit, or restart, and log this information
|
||||
// for the client. Otherwise, we just log non-nil errors.
|
||||
case err := <-errChan:
|
||||
switch err {
|
||||
case errServerSubscriptionComplete:
|
||||
log.Infof("swap subscription: %v", err)
|
||||
|
||||
case nil:
|
||||
|
||||
default:
|
||||
log.Errorf("swap subscription error: %v", err)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
// Exit if our context is cancelled.
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue