Conf42 Golang 2022 - Online

HTTP Server on random available port in Go

Video size:

Abstract

Imagine you want to start an HTTP server without specifying the port. For example, a temporary server for testing. How do you do that?

The answer is straightforward. Specify the port number to 0 like:

l, err := net.Listen(""tcp"", "":0"")

It’s an easy rule provided by the net package, but do you understand how Go binds a random port allocation. I’ll give you a clear understanding of what Go does inside the net package.

The presentation contains the following topics.

  • net, net/http packages
  • File descriptor
  • TCP, UDP
  • System calls

I think this presentation will give audiences not only a knowledge of Go, but also one of system calls.

Summary

  • Kazgir is a backend engineer at autify. Autify is a no code AI powered software testing automation platform. Today he talks about how go works to bind a port and listen for the port when starting an HTTP.
  • An HTTP server binds an available port dynamically and listens to it. Knowing the internal specification will help you make more profound design decisions. This talk will give you the basic knowledge to answer these questions.

Transcript

This transcript was autogenerated. To make changes, submit a PR.
What if you could work with some of the world's most innovative companies, all from the comfort of a remote workplace? Andela has matched thousands of technologists across the globe to their next career adventure. We're empowering new talent worldwide, from Sao Paulo to Egypt and Lagos to Warsaw. Now the future of work is yours to create. Anytime, anywhere. The world is at your fingertips. This is Andela. Hi there, my name is Kazgir. I've been calling go for nearly four years now. Today I'd like to talk about how go works to bind a port and listen for the port when starting an HTTP. HTTP HTTP HTTP HTTP HTTP. HTTP. HTTP. HTTP. HTTP HTTP server on random available port inside of standard libraries. Net and syscall. I hope you'll find it helps you understand HTTP and TCP programming in go. First, let me introduce myself. I am a backend engineer at autify. Autify is a no code AI powered software testing automation platform. You can find me on Twitter. I've been publishing blogs and presentation materials on this Twitter account. Please follow me and check it out. Let me explain autify just a little more. First, autify allows anyone to automate testing with a no code solution. Otifi records the user's action on the service they want to test and converts it into a test scenario. Second, AI updates test scenario accordingly. As the application changes, it dramatically reduce maintenance cost for the users. We have two services, autify for web and OT, five for mobile. We are taking democrats from you. Okay, let's get down to the main topic. Let's say you need to implement an HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP. HTTP server on random available port an HTTP server or HTTP server. In this case, the code does not specify a port where the servers land. An HTTP server binds an available port dynamically and listens to it. How would you implement this? The answer is straightforward. Here is a sample code with a minimum number of lines. Look at the second argument of the. Net list function. In the second line, I passed dell to the second argument. This way, the port assigned changes each time the server runs. As you see in this slide, the go document of the. Net package describes the behavior. If the port in the address parameter is empty or dell, a port number is automatically chosen. Some standard libraries invoke the listen function in this way. For example, we can use the HTTP test library to start our server for testing, but internally the new server function called net listen function, specifically the new local listener. An unexported function in the HTTP test library invokes netlistentcp function with the port zero. It's an excellent and easy interface for us so that you can use this capability easily. However, what I want to describe in this talk is the specification and the internal implementation of the. Net package by leading go code. Knowing the internal specification will help you make more profound design decisions. For example, what kind of infrastructure is suitable to deploy this HTTP server? What is the range of network ports that the server will bind? This talk will give you the basic knowledge to answer these questions. Let's have a look at the signature of netless function. The first argument network specifying the network type the network must be Tcp tcp four, tcp six, Unix or Unix packet. Regarding TCP, what you should specify depends on whether the server instance supports IP version four or IP version six. TCP four means IP version four only, and TcP six means IP version six only. TcP indicates either IP version four or version six. Then let's move on. The inside of. Net listen function here is a diagram visualizing the relevant parts. The two main libraries involves. Net and syscall. Subsequent slides will provide more detail. First, the listen function uses a. Net listen or listen config extract a helpful tip for your daily go life is that if you want to pass the context on the application call side, use listen config instead of listen. Then next there are listen function of listen config stack called default resolver which resolves the network IP address. Specifically invokes a method resolve address list. It invokes many functions, but the following method is relevant for setting the port. It is lookup port lookup port looks up the port for the given network. For example, if you provide a port zillow to this method, it returns zillow. It is an exported function of the. Net library so that you can check the behavior directly. The main point is that it does not specify food port a server will run on, but leave it at zero. Next listen invokes a sys listener that contains listened parameters and configuration. When the network protocol is TCP, it invokes the listen TCP function. Then next listen TcP invokes net internal socket, which returns a network file descriptor ready for asynchronous I O communication using the network puller. This function mainly handles low layer handling such as OS socket types and system calls, and so on. This code gives sock slim as a socket type. OS layer technology, such as a Linux main page defines several socket types. When you start an HTTP server, the network type is tcp and go program uses a socket type SoC stolim, a stolen oriented socket type. Otherwise, when you start a UDP server, it uses a socket type sock d glam. It is a datagram oriented socket type like this Google codes that perform network communications internally select the corresponding socket type. Then the internal socket function invokes the function that bundles necessary system calls according to the socket type. For example, when the socket type is SoC stolen, it invokes the listen stolen function which handles listening to storing oriented connections such as TCP. It has been a long journey, but here we'll get to the heart to answer our original question. How does go implement to bind one available port? Dynamically, the function executes three system calls in order bind, listen and get sock name bind assigns a socket address specified by the given address to the socket referred to by the file descriptor. Listen marks the socket referred to by the file descriptor as a passive socket. That is, the socket is ready to accept incoming requests using another system calls. Accept Getsocnab returns the current socket address. We got to the answer the rule over random port is the specification of the binds of each operating operating system. For example, if you start a server process on a Linux server, the system called binds will bind to an aheminal port. An ahemular port is also calls a dynamic port, as is a port that is used for only a short period of the duration of a communication session. You can see the port range from the bank specification. For example, if you use Windows Server 2003, support range is from 1025 to 5000. We will learn that when you want to launch HTTP HTTP HTTP HTTP HTTP HTTP HTTP HTTP server on random available port. The three things first, you'd better confirm the bind specification. Second, you'd better check if your infrastructure lecturer can use an ehminal port. For example, suppose you use AWS Fargate, a fully managed container orchestration service as an infrastructure. In that case you can't binds a Hemine report because automatic assign reserves them. Third, you'd better check the range of a Hemine report considering a network firewall setting may be necessary if you use this capability or remote server. You can see more detail in this article. Please check it out. That is for me. Thank you for your watching.
...

Kazuki Higashiguchi

Software Engineer @ Autify

Kazuki Higashiguchi's LinkedIn account Kazuki Higashiguchi's twitter account



Awesome tech events for

Priority access to all content

Video hallway track

Community chat

Exclusive promotions and giveaways