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 2022/10/24 20:59:16 UTC

[GitHub] [trafficcontrol] rob05c commented on a diff in pull request #7096: Add health client parent health

rob05c commented on code in PR #7096:
URL: https://github.com/apache/trafficcontrol/pull/7096#discussion_r1003762350


##########
tc-health-client/sar/ephemeralportholder.go:
##########
@@ -0,0 +1,77 @@
+package sar
+
+/*
+ * 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"
+	"net"
+	"strconv"
+)
+
+// EphemeralPortHolder serves 2 purposes: it gets an ephemeral TCP port, and it holds
+// onto the port so the OS doesn't assign it to any other app.
+//
+// It listens on :0 thereby getting a socket on an ephemeral port.
+// It continues listening but never reading from the socket until Close is called.
+type EphemeralPortHolder struct {
+	listener net.Listener
+	port     int
+}
+
+// GetAndHoldEphemeralPort gets an ephemeral port, and listens on it to prevent
+// the OS assigning the port to other apps.
+// Close must be called on the returned EphemeralPortHolder to stop listening.
+func GetAndHoldEphemeralPort(addr string) (*EphemeralPortHolder, error) {
+	addr += ":0"
+	listener, err := net.Listen("tcp", addr)
+	if err != nil {
+		return nil, errors.New("listening: " + err.Error())
+	}
+
+	// get the port now, so EphemeralPortHolder.Port() doesn't need to return an error
+
+	listenAddr := listener.Addr().String()
+	ipPort := SplitLast(listenAddr, ":")
+	if len(ipPort) < 1 {
+		return nil, errors.New("malformed addr '" + listenAddr + "', should have been ip:port") // should never happen
+	}
+	portStr := ipPort[1]
+	port, err := strconv.Atoi(portStr)
+	if err != nil {
+		return nil, errors.New("malformed addr '" + listenAddr + "' port was not an integer") // should never happen
+	}
+	if port > 65535 || port < 0 {

Review Comment:
   The number is technically the same, but "the last valid port number" is semantically different than "the last number a uint16 can store." We could make a constant, but it seems a bit overkill for just a port check, I'd prefer to just keep the literal for now



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@trafficcontrol.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org