From cd68c8be9fa9891d16dc282d34e5d36adf1817a7 Mon Sep 17 00:00:00 2001
From: Daniel Czerwonk <daniel@dan-nrw.de>
Date: Tue, 26 Jun 2018 22:12:16 +0200
Subject: [PATCH] optimized complexity of method

---
 protocols/bgp/server/fsm_established.go | 28 +++++++++++++------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/protocols/bgp/server/fsm_established.go b/protocols/bgp/server/fsm_established.go
index f496e859..bfdd7073 100644
--- a/protocols/bgp/server/fsm_established.go
+++ b/protocols/bgp/server/fsm_established.go
@@ -227,7 +227,6 @@ func (s *establishedState) updates(u *packet.BGPUpdate) {
 }
 
 func (s *establishedState) processAttributes(attrs *packet.PathAttribute, path *route.Path) {
-	var unknownAttributes *packet.PathAttribute
 	var currentUnknown *packet.PathAttribute
 
 	for pa := attrs; pa != nil; pa = pa.Next {
@@ -248,23 +247,26 @@ func (s *establishedState) processAttributes(attrs *packet.PathAttribute, path *
 		case packet.LargeCommunitiesAttr:
 			path.BGPPath.LargeCommunities = pa.Value.([]packet.LargeCommunity)
 		default:
-			if !pa.Transitive {
-				continue
+			currentUnknown = s.processUnknownAttribute(pa, currentUnknown)
+			if path.BGPPath.UnknownAttributes == nil {
+				path.BGPPath.UnknownAttributes = currentUnknown
 			}
+		}
+	}
+}
 
-			p := pa.Copy()
-			if unknownAttributes == nil {
-				unknownAttributes = p
-				currentUnknown = unknownAttributes
-				continue
-			}
+func (s *establishedState) processUnknownAttribute(attr, current *packet.PathAttribute) *packet.PathAttribute {
+	if !attr.Transitive {
+		return current
+	}
 
-			currentUnknown.Next = p
-			currentUnknown = p
-		}
+	p := attr.Copy()
+	if current == nil {
+		return p
 	}
 
-	path.BGPPath.UnknownAttributes = unknownAttributes
+	current.Next = p
+	return p
 }
 
 func (s *establishedState) keepaliveReceived() (state, string) {
-- 
GitLab