You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

  1. package loop
  2. import (
  3. "context"
  4. "github.com/lightninglabs/loop/swap"
  5. "github.com/lightningnetwork/lnd/lntypes"
  6. )
  7. // subscribeAndLogUpdates subscribes to updates for a swap and logs them. This
  8. // function will block, so should run as a goroutine. Note that our subscription
  9. // does not survive server restarts; we will simply not have update logs if the
  10. // server restarts during swap execution.
  11. func subscribeAndLogUpdates(ctx context.Context, hash lntypes.Hash,
  12. log *swap.PrefixLog, subscribe func(context.Context,
  13. lntypes.Hash) (<-chan *ServerUpdate, <-chan error, error)) {
  14. subscribeChan, errChan, err := subscribe(ctx, hash)
  15. if err != nil {
  16. log.Errorf("could not get swap subscription: %v", err)
  17. return
  18. }
  19. for {
  20. select {
  21. // Consume any updates and log them.
  22. case update := <-subscribeChan:
  23. log.Infof("Server update: %v received, "+
  24. "timestamp: %v", update.State, update.Timestamp)
  25. // If we get an error from the server, we check whether it is
  26. // due to server exit, or restart, and log this information
  27. // for the client. Otherwise, we just log non-nil errors.
  28. case err := <-errChan:
  29. switch err {
  30. case errServerSubscriptionComplete:
  31. log.Infof("swap subscription: %v", err)
  32. case nil:
  33. default:
  34. log.Errorf("swap subscription error: %v", err)
  35. }
  36. return
  37. // Exit if our context is cancelled.
  38. case <-ctx.Done():
  39. return
  40. }
  41. }
  42. }