@@ -205,6 +205,18 @@ func queueNotification[T any](sub subscriber, recvChan chan T, ntfn T) {
205205 }
206206}
207207
208+ func dropNotification [T any ](sub subscriber , recvChan chan T , ntfn T ,
209+ description string ) {
210+
211+ select {
212+ case recvChan <- ntfn :
213+ case <- sub .subCtx .Done ():
214+ default :
215+ log .Debugf ("Dropping %s notification for slow subscriber" ,
216+ description )
217+ }
218+ }
219+
208220// SubscribeReservations subscribes to the reservation notifications.
209221func (m * Manager ) SubscribeReservations (ctx context.Context ,
210222) <- chan * swapserverrpc.ServerReservationNotification {
@@ -472,6 +484,63 @@ func (m *Manager) subscribeNotifications(ctx context.Context) error {
472484 }
473485}
474486
487+ func staticLoopInRiskDecisionName (accepted bool ) string {
488+ if accepted {
489+ return "accepted"
490+ }
491+
492+ return "rejected"
493+ }
494+
495+ func (m * Manager ) handleStaticLoopInRiskDecision (ctx context.Context ,
496+ swapHashBytes []byte , accepted bool , notifType NotificationType ,
497+ cacheDecision func (lntypes.Hash ), notifySubscriber func (subscriber )) {
498+
499+ decision := staticLoopInRiskDecisionName (accepted )
500+
501+ var (
502+ swapHash lntypes.Hash
503+ hasSwapHash bool
504+ )
505+ if swapHashBytes != nil {
506+ hash , err := lntypes .MakeHash (swapHashBytes )
507+ if err != nil {
508+ log .Warnf ("Received invalid static loop in risk " +
509+ "%s notification: %v" , decision , err )
510+ } else {
511+ swapHash = hash
512+ hasSwapHash = true
513+ }
514+ }
515+
516+ if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
517+ err := m .cfg .PersistStaticLoopInRiskDecision (
518+ ctx , swapHash , accepted ,
519+ )
520+ if err != nil {
521+ log .Errorf ("Unable to persist static loop in risk " +
522+ "%s notification: %v" , decision , err )
523+ }
524+ }
525+
526+ m .Lock ()
527+ defer m .Unlock ()
528+
529+ if hasSwapHash {
530+ cacheDecision (swapHash )
531+ }
532+
533+ for _ , sub := range m .subscribers [notifType ] {
534+ if ! hasSwapHash || sub .swapHash == nil ||
535+ * sub .swapHash != swapHash {
536+
537+ continue
538+ }
539+
540+ notifySubscriber (sub )
541+ }
542+ }
543+
475544// handleNotification handles an incoming notification from the server,
476545// forwarding it to the appropriate subscribers.
477546func (m * Manager ) handleNotification (ctx context.Context , ntfn * swapserverrpc.
@@ -514,115 +583,55 @@ func (m *Manager) handleNotification(ctx context.Context, ntfn *swapserverrpc.
514583 // We'll forward the static loop in risk accepted notification to the
515584 // subscriber for the matching swap.
516585 riskAcceptedNtfn := ntfn .GetStaticLoopInRiskAccepted ()
517- var (
518- swapHash lntypes.Hash
519- hasSwapHash bool
520- )
586+ var swapHashBytes []byte
521587 if riskAcceptedNtfn != nil {
522- hash , err := lntypes .MakeHash (riskAcceptedNtfn .SwapHash )
523- if err != nil {
524- log .Warnf ("Received invalid static loop in risk " +
525- "accepted notification: %v" , err )
526- } else {
527- swapHash = hash
528- hasSwapHash = true
529- }
530- }
531-
532- if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
533- err := m .cfg .PersistStaticLoopInRiskDecision (
534- ctx , swapHash , true ,
535- )
536- if err != nil {
537- log .Errorf ("Unable to persist static loop in " +
538- "risk accepted notification: %v" , err )
539- }
588+ swapHashBytes = riskAcceptedNtfn .SwapHash
540589 }
541590
542- m .Lock ()
543- defer m .Unlock ()
544-
545- if hasSwapHash {
546- m .staticLoopInRiskAccepted [swapHash ] =
547- riskAcceptedNtfn
548- delete (m .staticLoopInRiskRejected , swapHash )
549- }
550-
551- for _ , sub := range m .subscribers [NotificationTypeStaticLoopInRiskAccepted ] { // nolint: lll
552- if ! hasSwapHash || sub .swapHash == nil ||
553- * sub .swapHash != swapHash {
554-
555- continue
556- }
557-
558- recvChan := sub .recvChan .(chan * swapserverrpc.
559- ServerStaticLoopInRiskAcceptedNotification )
560-
561- select {
562- case recvChan <- riskAcceptedNtfn :
563- case <- sub .subCtx .Done ():
564- default :
565- log .Debugf ("Dropping static loop in risk " +
566- "accepted notification for slow subscriber" )
567- }
568- }
591+ m .handleStaticLoopInRiskDecision (
592+ ctx , swapHashBytes , true ,
593+ NotificationTypeStaticLoopInRiskAccepted ,
594+ func (swapHash lntypes.Hash ) {
595+ m .staticLoopInRiskAccepted [swapHash ] =
596+ riskAcceptedNtfn
597+ delete (m .staticLoopInRiskRejected , swapHash )
598+ },
599+ func (sub subscriber ) {
600+ recvChan := sub .recvChan .(chan * swapserverrpc.
601+ ServerStaticLoopInRiskAcceptedNotification )
602+ dropNotification (
603+ sub , recvChan , riskAcceptedNtfn ,
604+ "static loop in risk accepted" ,
605+ )
606+ },
607+ )
569608
570609 case * swapserverrpc.SubscribeNotificationsResponse_StaticLoopInRiskRejected : // nolint: lll
571610 // We'll forward the static loop in risk rejected notification to the
572611 // subscriber for the matching swap.
573612 riskRejectedNtfn := ntfn .GetStaticLoopInRiskRejected ()
574- var (
575- swapHash lntypes.Hash
576- hasSwapHash bool
577- )
613+ var swapHashBytes []byte
578614 if riskRejectedNtfn != nil {
579- hash , err := lntypes .MakeHash (riskRejectedNtfn .SwapHash )
580- if err != nil {
581- log .Warnf ("Received invalid static loop in risk " +
582- "rejected notification: %v" , err )
583- } else {
584- swapHash = hash
585- hasSwapHash = true
586- }
615+ swapHashBytes = riskRejectedNtfn .SwapHash
587616 }
588617
589- if hasSwapHash && m .cfg .PersistStaticLoopInRiskDecision != nil {
590- err := m .cfg .PersistStaticLoopInRiskDecision (
591- ctx , swapHash , false ,
592- )
593- if err != nil {
594- log .Errorf ("Unable to persist static loop in " +
595- "risk rejected notification: %v" , err )
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- }
618+ m .handleStaticLoopInRiskDecision (
619+ ctx , swapHashBytes , false ,
620+ NotificationTypeStaticLoopInRiskRejected ,
621+ func (swapHash lntypes.Hash ) {
622+ m .staticLoopInRiskRejected [swapHash ] =
623+ riskRejectedNtfn
624+ delete (m .staticLoopInRiskAccepted , swapHash )
625+ },
626+ func (sub subscriber ) {
627+ recvChan := sub .recvChan .(chan * swapserverrpc.
628+ ServerStaticLoopInRiskRejectedNotification )
629+ dropNotification (
630+ sub , recvChan , riskRejectedNtfn ,
631+ "static loop in risk rejected" ,
632+ )
633+ },
634+ )
626635
627636 case * swapserverrpc.SubscribeNotificationsResponse_UnfinishedSwap : // nolint: lll
628637 // We'll forward the unfinished swap notification to all
0 commit comments