@@ -631,6 +631,7 @@ func SearchServiceAsCrossLinkedList(ctx consolectx.Context, req *model.ServiceGr
631631 Data : instanceData ,
632632 })
633633 }
634+ edgeKeyMap := make (map [string ]struct {})
634635
635636 // Build edges between consumers and providers
636637 // Only create edges between apps that actually have the service relationship
@@ -642,6 +643,14 @@ func SearchServiceAsCrossLinkedList(ctx consolectx.Context, req *model.ServiceGr
642643 if consumer .Spec .ServiceName != provider .Spec .ServiceName {
643644 continue
644645 }
646+ // If there are two instances, such as p1->c1 and p2->c1, two edges will be generated, which need to be merged.
647+ // Merging logic: Only one edge is kept for identical source and target.
648+ // However, using a loop would result in three levels of nesting. Therefore, an auxiliary map is created for efficient filtering.
649+ edgeKey := consumer .Spec .ConsumerAppName + "->" + provider .Spec .ProviderAppName
650+ if _ , exists := edgeKeyMap [edgeKey ]; exists {
651+ continue
652+ }
653+ edgeKeyMap [edgeKey ] = struct {}{}
645654 edges = append (edges , model.GraphEdge {
646655 Source : consumer .Spec .ConsumerAppName ,
647656 Target : provider .Spec .ProviderAppName ,
@@ -652,8 +661,12 @@ func SearchServiceAsCrossLinkedList(ctx consolectx.Context, req *model.ServiceGr
652661 "providerApp" : provider .Spec .ProviderAppName ,
653662 },
654663 })
664+ } else {
665+ logger .Warnf ("provider spec is nil for provider resource: %s" , provider .Name )
655666 }
656667 }
668+ } else {
669+ logger .Warnf ("consumer spec is nil for consumer resource: %s" , consumer .Name )
657670 }
658671 }
659672
0 commit comments