Skip to content

Commit

Permalink
Some minor optimizations to how we format host:port strings. (knative…
Browse files Browse the repository at this point in the history
…#4652)

In most cases we can avoid sprintf, which only for the means of formatting integer runs 4x slower than atoi,
pile in additional memory for parsing the request string, etc.

So do just that.

Also some ports were always used as strings, so define the constant as string, rather than as number.
  • Loading branch information
vagababov authored and knative-prow-robot committed Jul 8, 2019
1 parent 53d92be commit 016f97a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 6 additions & 5 deletions cmd/activator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/knative/serving/cmd/util"
Expand Down Expand Up @@ -81,7 +82,7 @@ const (
breakerMaxConcurrency = 1000

// The port on which autoscaler WebSocket server listens.
autoscalerPort = 8080
autoscalerPort = ":8080"

defaultResyncInterval = 10 * time.Hour
)
Expand Down Expand Up @@ -203,8 +204,8 @@ func main() {
configStore := activatorconfig.NewStore(createdLogger, tracerUpdater)
configStore.WatchConfigs(configMapWatcher)

// Open a websocket connection to the autoscaler
autoscalerEndpoint := fmt.Sprintf("ws://%s.%s.svc.%s:%d", "autoscaler", system.Namespace(), network.GetClusterDomainName(), autoscalerPort)
// Open a WebSocket connection to the autoscaler.
autoscalerEndpoint := fmt.Sprintf("ws://%s.%s.svc.%s%s", "autoscaler", system.Namespace(), network.GetClusterDomainName(), autoscalerPort)
logger.Info("Connecting to autoscaler at", autoscalerEndpoint)
statSink := websocket.NewDurableSendingConnection(autoscalerEndpoint, logger)
go statReporter(statSink, stopCh, statChan, logger)
Expand Down Expand Up @@ -250,8 +251,8 @@ func main() {
}

servers := map[string]*http.Server{
"http1": network.NewServer(fmt.Sprintf(":%d", networking.BackendHTTPPort), ah),
"h2c": network.NewServer(fmt.Sprintf(":%d", networking.BackendHTTP2Port), ah),
"http1": network.NewServer(":"+strconv.Itoa(networking.BackendHTTPPort), ah),
"h2c": network.NewServer(":"+strconv.Itoa(networking.BackendHTTP2Port), ah),
}

errCh := make(chan error, len(servers))
Expand Down
9 changes: 5 additions & 4 deletions cmd/queue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func initEnv() {
servingRevision = util.GetRequiredEnvOrFatal("SERVING_REVISION", logger)
servingService = os.Getenv("SERVING_SERVICE") // KService is optional
userTargetPort = util.MustParseIntEnvOrFatal("USER_PORT", logger)
userTargetAddress = fmt.Sprintf("127.0.0.1:%d", userTargetPort)
userTargetAddress = "127.0.0.1:" + strconv.Itoa(userTargetPort)
userContainerName = util.GetRequiredEnvOrFatal("USER_CONTAINER_NAME", logger)

enableVarLogCollection, _ = strconv.ParseBool(os.Getenv("ENABLE_VAR_LOG_COLLECTION")) // Optional, default is false
Expand Down Expand Up @@ -354,7 +354,7 @@ func main() {
}, time.Now())

adminServer := &http.Server{
Addr: fmt.Sprintf(":%d", networking.QueueAdminPort),
Addr: ":" + strconv.Itoa(networking.QueueAdminPort),
Handler: createAdminHandlers(),
}

Expand Down Expand Up @@ -384,8 +384,9 @@ func main() {
if metricsSupported {
composedHandler = pushRequestMetricHandler(composedHandler, requestCountM, responseTimeInMsecM)
}
logger.Infof("Queue-proxy will listen on port %d", queueServingPort)
server := network.NewServer(fmt.Sprintf(":%d", queueServingPort), composedHandler)
qSP := strconv.Itoa(queueServingPort)
logger.Info("Queue-proxy will listen on port ", qSP)
server := network.NewServer(":"+qSP, composedHandler)

errChan := make(chan error, 2)
defer close(errChan)
Expand Down
5 changes: 2 additions & 3 deletions pkg/activator/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -316,9 +317,7 @@ func (a *activationHandler) serviceHostName(rev *v1alpha1.Revision, serviceName
return "", errors.New("revision needs external HTTP port")
}

serviceFQDN := network.GetServiceHostname(serviceName, rev.Namespace)

return fmt.Sprintf("%s:%d", serviceFQDN, port), nil
return network.GetServiceHostname(serviceName, rev.Namespace) + ":" + strconv.Itoa(port), nil
}

func sendError(err error, w http.ResponseWriter) {
Expand Down

0 comments on commit 016f97a

Please sign in to comment.