You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/04/12 21:49:25 UTC

[GitHub] [trafficcontrol] mitchell852 commented on a change in pull request #3422: Adds max origin connections to ds api endpoints (v14) and TP and adds the value to hdr_rw_xml-id.config OR hdr_rw_mid_xml-id.config files

mitchell852 commented on a change in pull request #3422: Adds max origin connections to ds api endpoints (v14) and TP and adds the value to hdr_rw_xml-id.config OR hdr_rw_mid_xml-id.config files
URL: https://github.com/apache/trafficcontrol/pull/3422#discussion_r275074549
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/ats/headerrewrite.go
 ##########
 @@ -0,0 +1,201 @@
+package ats
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+	"errors"
+	"github.com/apache/trafficcontrol/lib/go-tc"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+	"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/deliveryservice"
+	"github.com/jmoiron/sqlx"
+	"math"
+	"net/http"
+	"regexp"
+	"strconv"
+)
+
+func GetEdgeHeaderRewriteDotConfig(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn-name-or-id"}, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
+
+	cdnName, userErr, sysErr, errCode := getCDNNameFromNameOrID(inf.Tx.Tx, inf.Params["cdn-name-or-id"])
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+		return
+	}
+
+	text, err := headerComment(inf.Tx.Tx, "CDN "+cdnName)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_xml-id.config text: "+err.Error()))
+		return
+	}
+
+	ds, err := getDeliveryService(inf.Tx, inf.Params["xml-id"])
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_mid_xml-id.config text: "+err.Error()))
+		return
+	}
+
+	maxOriginConnections := *ds.MaxOriginConnections
+
+	dsType, err := deliveryservice.GetDeliveryServiceType(*ds.ID, inf.Tx.Tx)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_xml-id.config text: "+err.Error()))
+		return
+	}
+	usesMids := dsType.UsesMidCache()
+
+	// write a header rewrite rule if maxOriginConnections > 0 and the ds does NOT use mids
+	if maxOriginConnections > 0 && !usesMids {
+		dsOnlineEdgeCount, err := getOnlineDSEdgeCount(inf.Tx, *ds.ID)
+		if err != nil {
+			api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting ds server count: "+err.Error()))
+			return
+		}
+		maxOriginConnectionsPerEdge := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineEdgeCount)))
+		if ds.EdgeHeaderRewrite == nil {
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerEdge) + " [L]"
+		} else {
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerEdge) + "\n"
+
+		}
+	}
+
+	// write the contents of ds.EdgeHeaderRewrite to hdr_rw_xml-id.config replacing any instances of __RETURN__ (surrounded by spaces or not) with \n
+	if ds.EdgeHeaderRewrite != nil {
+		var re = regexp.MustCompile(`\s*__RETURN__\s*`)
+		text += re.ReplaceAllString(*ds.EdgeHeaderRewrite, "\n")
+	}
+
+	text += "\n"
+
+	w.Header().Set("Content-Type", "text/plain")
+	w.Write([]byte(text))
+}
+
+func GetMidHeaderRewriteDotConfig(w http.ResponseWriter, r *http.Request) {
+	inf, userErr, sysErr, errCode := api.NewInfo(r, []string{"cdn-name-or-id"}, nil)
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+		return
+	}
+	defer inf.Close()
+
+	cdnName, userErr, sysErr, errCode := getCDNNameFromNameOrID(inf.Tx.Tx, inf.Params["cdn-name-or-id"])
+	if userErr != nil || sysErr != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+		return
+	}
+
+	text, err := headerComment(inf.Tx.Tx, "CDN "+cdnName)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_mid_xml-id.config text: "+err.Error()))
+		return
+	}
+
+	ds, err := getDeliveryService(inf.Tx, inf.Params["xml-id"])
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_mid_xml-id.config text: "+err.Error()))
+		return
+	}
+
+	maxOriginConnections := *ds.MaxOriginConnections
+
+	dsType, err := deliveryservice.GetDeliveryServiceType(*ds.ID, inf.Tx.Tx)
+	if err != nil {
+		api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting hdr_rw_mid_xml-id.config text: "+err.Error()))
+		return
+	}
+	usesMids := dsType.UsesMidCache()
+
+	// write a header rewrite rule if maxOriginConnections > 0 and the ds DOES use mids
+	if maxOriginConnections > 0 && usesMids {
+		dsOnlineMidCount, err := getOnlineDSMidCount(inf.Tx, *ds.ID)
+		if err != nil {
+			api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, nil, errors.New("getting ds server count: "+err.Error()))
+			return
+		}
+		maxOriginConnectionsPerMid := int(math.Round(float64(maxOriginConnections) / float64(dsOnlineMidCount)))
+		if ds.MidHeaderRewrite == nil {
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerMid) + " [L]"
+		} else {
+			text += "cond %{REMAP_PSEUDO_HOOK}\nset-config proxy.config.http.origin_max_connections " + strconv.Itoa(maxOriginConnectionsPerMid) + "\n"
+
+		}
+	}
+
+	// write the contents of ds.MidHeaderRewrite to hdr_rw_mid_xml-id.config replacing any instances of __RETURN__ (surrounded by spaces or not) with \n
+	if ds.MidHeaderRewrite != nil {
+		var re = regexp.MustCompile(`\s*__RETURN__\s*`)
 
 Review comment:
   i started down the path of strings.ReplaceAll but changed it because sometimes there are spaces around the `__RETURN__` and sometimes there are not.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services