@@ -203,6 +203,18 @@ func queueNotification[T any](sub subscriber, recvChan chan T, ntfn T) {
203203 }
204204}
205205
206+ func dropNotification [T any ](sub subscriber , recvChan chan T , ntfn T ,
207+ description string ) {
208+
209+ select {
210+ case recvChan <- ntfn :
211+ case <- sub .subCtx .Done ():
212+ default :
213+ log .Debugf ("Dropping %s notification for slow subscriber" ,
214+ description )
215+ }
216+ }
217+
206218// SubscribeReservations subscribes to the reservation notifications.
207219func (m * Manager ) SubscribeReservations (ctx context.Context ,
208220) <- chan * swapserverrpc.ServerReservationNotification {
@@ -470,6 +482,64 @@ func (m *Manager) subscribeNotifications(ctx context.Context) error {
470482 }
471483}
472484
485+ func staticLoopInRiskDecisionName (accepted bool ) string {
486+ if accepted {
487+ return "accepted"
488+ }
489+
490+ return "rejected"
491+ }
492+
493+ func (m * Manager ) handleStaticLoopInRiskDecision (ctx context.Context ,
494+ swapHashBytes []byte , accepted bool , notifType NotificationType ,
495+ cacheDecision func (lntypes.Hash ), notifySubscriber func (subscriber )) {
496+
497+ decision := staticLoopInRiskDecisionName (accepted )
498+
499+ var (
500+ swapHash lntypes.Hash
501+ hasSwapHash bool
502+ )
503+ if swapHashBytes != nil {
504+ hash , err := lntypes .MakeHash (swapHashBytes )
505+ if err != nil {
506+ log .Warnf ("Received invalid static loop in risk " +
507+ "%s notification: %v" , decision , err )
508+ } else {
509+ swapHash = hash
510+ hasSwapHash = true
511+ }
512+ }
513+
514+ if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
515+ err := m .cfg .PersistStaticLoopInRiskDecision (
516+ ctx , swapHash , accepted ,
517+ )
518+ if err != nil {
519+ log .Errorf ("Unable to persist static loop in risk " +
520+ "%s notification: %v" , decision , err )
521+ return
522+ }
523+ }
524+
525+ m .Lock ()
526+ defer m .Unlock ()
527+
528+ if hasSwapHash {
529+ cacheDecision (swapHash )
530+ }
531+
532+ for _ , sub := range m .subscribers [notifType ] {
533+ if ! hasSwapHash || sub .swapHash == nil ||
534+ * sub .swapHash != swapHash {
535+
536+ continue
537+ }
538+
539+ notifySubscriber (sub )
540+ }
541+ }
542+
473543// handleNotification handles an incoming notification from the server,
474544// forwarding it to the appropriate subscribers.
475545func (m * Manager ) handleNotification (ctx context.Context , ntfn * swapserverrpc.
@@ -512,117 +582,55 @@ func (m *Manager) handleNotification(ctx context.Context, ntfn *swapserverrpc.
512582 // We'll forward the static loop in risk accepted notification to the
513583 // subscriber for the matching swap.
514584 riskAcceptedNtfn := ntfn .GetStaticLoopInRiskAccepted ()
515- var (
516- swapHash lntypes.Hash
517- hasSwapHash bool
518- )
585+ var swapHashBytes []byte
519586 if riskAcceptedNtfn != nil {
520- hash , err := lntypes .MakeHash (riskAcceptedNtfn .SwapHash )
521- if err != nil {
522- log .Warnf ("Received invalid static loop in risk " +
523- "accepted notification: %v" , err )
524- } else {
525- swapHash = hash
526- hasSwapHash = true
527- }
587+ swapHashBytes = riskAcceptedNtfn .SwapHash
528588 }
529589
530- if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
531- err := m .cfg .PersistStaticLoopInRiskDecision (
532- ctx , swapHash , true ,
533- )
534- if err != nil {
535- log .Errorf ("Unable to persist static loop in " +
536- "risk accepted notification: %v" , err )
537- return
538- }
539- }
540-
541- m .Lock ()
542- defer m .Unlock ()
543-
544- if hasSwapHash {
545- m .staticLoopInRiskAccepted [swapHash ] =
546- riskAcceptedNtfn
547- delete (m .staticLoopInRiskRejected , swapHash )
548- }
549-
550- for _ , sub := range m .subscribers [NotificationTypeStaticLoopInRiskAccepted ] { // nolint: lll
551- if ! hasSwapHash || sub .swapHash == nil ||
552- * sub .swapHash != swapHash {
553-
554- continue
555- }
556-
557- recvChan := sub .recvChan .(chan * swapserverrpc.
558- ServerStaticLoopInRiskAcceptedNotification )
559-
560- select {
561- case recvChan <- riskAcceptedNtfn :
562- case <- sub .subCtx .Done ():
563- default :
564- log .Debugf ("Dropping static loop in risk " +
565- "accepted notification for slow subscriber" )
566- }
567- }
590+ m .handleStaticLoopInRiskDecision (
591+ ctx , swapHashBytes , true ,
592+ NotificationTypeStaticLoopInRiskAccepted ,
593+ func (swapHash lntypes.Hash ) {
594+ m .staticLoopInRiskAccepted [swapHash ] =
595+ riskAcceptedNtfn
596+ delete (m .staticLoopInRiskRejected , swapHash )
597+ },
598+ func (sub subscriber ) {
599+ recvChan := sub .recvChan .(chan * swapserverrpc.
600+ ServerStaticLoopInRiskAcceptedNotification )
601+ dropNotification (
602+ sub , recvChan , riskAcceptedNtfn ,
603+ "static loop in risk accepted" ,
604+ )
605+ },
606+ )
568607
569608 case * swapserverrpc.SubscribeNotificationsResponse_StaticLoopInRiskRejected : // nolint: lll
570609 // We'll forward the static loop in risk rejected notification to the
571610 // subscriber for the matching swap.
572611 riskRejectedNtfn := ntfn .GetStaticLoopInRiskRejected ()
573- var (
574- swapHash lntypes.Hash
575- hasSwapHash bool
576- )
612+ var swapHashBytes []byte
577613 if riskRejectedNtfn != nil {
578- hash , err := lntypes .MakeHash (riskRejectedNtfn .SwapHash )
579- if err != nil {
580- log .Warnf ("Received invalid static loop in risk " +
581- "rejected notification: %v" , err )
582- } else {
583- swapHash = hash
584- hasSwapHash = true
585- }
614+ swapHashBytes = riskRejectedNtfn .SwapHash
586615 }
587616
588- if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
589- err := m .cfg .PersistStaticLoopInRiskDecision (
590- ctx , swapHash , false ,
591- )
592- if err != nil {
593- log .Errorf ("Unable to persist static loop in " +
594- "risk rejected notification: %v" , err )
595- return
596- }
597- }
598-
599- m .Lock ()
600- defer m .Unlock ()
601-
602- if hasSwapHash {
603- m .staticLoopInRiskRejected [swapHash ] =
604- riskRejectedNtfn
605- delete (m .staticLoopInRiskAccepted , swapHash )
606- }
607-
608- for _ , sub := range m .subscribers [NotificationTypeStaticLoopInRiskRejected ] { // nolint: lll
609- if ! hasSwapHash || sub .swapHash == nil ||
610- * sub .swapHash != swapHash {
611-
612- continue
613- }
614-
615- recvChan := sub .recvChan .(chan * swapserverrpc.
616- ServerStaticLoopInRiskRejectedNotification )
617-
618- select {
619- case recvChan <- riskRejectedNtfn :
620- case <- sub .subCtx .Done ():
621- default :
622- log .Debugf ("Dropping static loop in risk " +
623- "rejected notification for slow subscriber" )
624- }
625- }
617+ m .handleStaticLoopInRiskDecision (
618+ ctx , swapHashBytes , false ,
619+ NotificationTypeStaticLoopInRiskRejected ,
620+ func (swapHash lntypes.Hash ) {
621+ m .staticLoopInRiskRejected [swapHash ] =
622+ riskRejectedNtfn
623+ delete (m .staticLoopInRiskAccepted , swapHash )
624+ },
625+ func (sub subscriber ) {
626+ recvChan := sub .recvChan .(chan * swapserverrpc.
627+ ServerStaticLoopInRiskRejectedNotification )
628+ dropNotification (
629+ sub , recvChan , riskRejectedNtfn ,
630+ "static loop in risk rejected" ,
631+ )
632+ },
633+ )
626634
627635 case * swapserverrpc.SubscribeNotificationsResponse_UnfinishedSwap : // nolint: lll
628636 // We'll forward the unfinished swap notification to all
0 commit comments