go.temporal.io/server/common/config/localip.go

79 LOC · 23 covered · 56 uncovered · 5 ranges · 1 concepts · 1 introducers · 1 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

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 related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package config
2
3 // ** This code is copied from tchannel, we would like to not take dependency on tchannel code **
4
5 import (
6 "errors"
7 "net"
8 )
9
10 // scoreAddr scores how likely the given addr is to be a remote address and returns the
11 // IP to use when listening. Any address which receives a negative score should not be used.
12 // Scores are calculated as:
13 // -1 for any unknown IP addreseses.
14 // +300 for IPv4 addresses
15 // +100 for non-local addresses, extra +100 for "up" interaces.
16 > func scoreAddr(iface net.Interface, addr net.Addr) (int, net.IP) { localip.go ×5
17 > var ip net.IP
18 > if netAddr, ok := addr.(*net.IPNet); ok {
19 > ip = netAddr.IP
20 > } else if netIP, ok := addr.(*net.IPAddr); ok {
21 > ip = netIP.IP
22 > } else {
23 > return -1, nil
24 > }
25
26 > var score int localip.go ×5
27 > if ip.To4() != nil {
28 > score += 300
29 > }
30 > if iface.Flags&net.FlagLoopback == 0 && !ip.IsLoopback() {
31 > score += 100
32 > if iface.Flags&net.FlagUp != 0 {
33 > score += 100
34 > }
35 }
36 > return score, ip localip.go ×5
37 }
38
39 // ListenIP returns the IP to bind to in Listen. It tries to find an IP that can be used
40 // by other machines to reach this machine.
41 func ListenIP() (net.IP, error) {
42 interfaces, err := net.Interfaces()
43 if err != nil {
44 return nil, err
45 }
46
47 bestScore := -1
48 var bestIP net.IP
49 // Select the highest scoring IP as the best IP.
50 for _, iface := range interfaces {
51 addrs, err := iface.Addrs()
52 if err != nil {
53 // Skip this interface if there is an error.
54 continue
55 }
56
57 for _, addr := range addrs {
58 score, ip := scoreAddr(iface, addr)
59 if score > bestScore {
60 bestScore = score
61 bestIP = ip
62 }
63 }
64 }
65
66 if bestScore == -1 {
67 return nil, errors.New("no addresses to listen on")
68 }
69
70 return bestIP, nil
71 }
72
73 > func mustParseMAC(s string) net.HardwareAddr { localip.go ×5
74 > addr, err := net.ParseMAC(s)
75 > if err != nil {
76 panic(err)
77 }
78 > return addr localip.go ×5
79 }