freeport.go ×7

Frontier kind: Code frontier

unlabeled · c_89e4b1683fe0

56 tests · 2371 LOC · 121 files · introduces 0 tests · 33 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
7 ranges33 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
356 ranges2371 lines · 121 files · Browse complete extent
All tests (intent)
56 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 33 introduced LOC across 7 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

go.temporal.io/server/common/testing/freeport/freeport.go 33 introduced LOC · 7 ranges

Open complete file

22 // in this regard; on that platform, `SO_REUSEADDR` has a different meaning and
23 // should not be set (setting it may have unpredictable consequences).
24 > func MustGetFreePort() int { freeport.go
25 > port, err := getFreePort("127.0.0.1")
26 > if err != nil {
27 // try ipv6
28 port, err = getFreePort("[::1]")
31 }
32 }
33 > return port freeport.go
34 }
35
36 > func getFreePort(host string) (int, error) { freeport.go
37 > l, err := net.Listen("tcp", host+":0")
38 > if err != nil {
39 return 0, fmt.Errorf("failed to assign a free port: %v", err)
40 }
41 > defer l.Close() freeport.go
42 > port := l.Addr().(*net.TCPAddr).Port
43 >
44 > // On Linux and some BSD variants, ephemeral ports are randomized, and may
45 > // consequently repeat within a short time frame after the listening end
46 > // has been closed. To avoid this, we make a connection to the port, then
47 > // close that connection from the server's side (this is very important),
48 > // which puts the connection in TIME_WAIT state for some time (by default,
49 > // 60s on Linux). While it remains in that state, the OS will not reallocate
50 > // that port number for bind(:0) syscalls, yet we are not prevented from
51 > // explicitly binding to it (thanks to SO_REUSEADDR).
52 > //
53 > // On macOS and Windows, the above technique is not necessary, as the OS
54 > // allocates ephemeral ports sequentially, meaning a port number will only
55 > // be reused after the entire range has been exhausted. Quite the opposite,
56 > // given that these OSes use a significantly smaller range for ephemeral
57 > // ports, making an extra connection just to reserve a port might actually
58 > // be harmful (by hastening ephemeral port exhaustion).
59 > if runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
60 > r, err := net.DialTCP("tcp", nil, l.Addr().(*net.TCPAddr))
61 > if err != nil {
62 return 0, fmt.Errorf("failed to assign a free port: %v", err)
63 }
64 > c, err := l.Accept() freeport.go
65 > if err != nil {
66 return 0, fmt.Errorf("failed to assign a free port: %v", err)
67 }
68 // Closing the socket from the server side
69 > _ = c.Close() freeport.go
70 > defer r.Close()
71 }
72
73 > return port, nil freeport.go
74 }