-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (92 loc) · 2.07 KB
/
Copy pathmain.go
File metadata and controls
115 lines (92 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-FileCopyrightText: 2021 Dinko Korunic
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"net/netip"
"os"
"runtime"
"runtime/pprof"
"sync"
"github.com/KimMachineGun/automemlimit/memlimit"
)
const (
mapSize = 4096
subMapSize = 8
hostChanSize = 2048
maxMemRatio = 0.9
)
var (
GitTag = ""
GitCommit = ""
GitDirty = ""
BuildTime = ""
)
func main() {
_, _ = memlimit.SetGoMemLimitWithOpts(
memlimit.WithRatio(maxMemRatio),
memlimit.WithProvider(
memlimit.ApplyFallback(
memlimit.FromCgroup,
memlimit.FromSystem,
),
),
)
zones, server, cidrList := parseFlags()
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating CPU profile: %v\n", err)
} else {
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Error starting CPU profile: %v\n", err)
}
defer pprof.StopCPUProfile()
}
}
if *memProfile != "" {
f, err := os.Create(*memProfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error trying to create memory profile: %v\n", err)
} else {
defer f.Close()
defer func() {
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Fprintf(os.Stderr, "Error writing memory profile: %v\n", err)
}
}()
}
}
_ = setNofile()
ranger, doCIDR := rangerInit(cidrList)
hostChan := make(chan HostEntry, hostChanSize)
entries := make(HostMap, mapSize)
keys := make([]netip.Addr, 0, mapSize)
var wgMon, wgWrk sync.WaitGroup
// sole owner of entries/keys; lock-free via channel
wgMon.Go(func() {
writeHostEntries(hostChan, &keys, entries)
})
semAXFR := make(chan struct{}, *maxTransfers)
for _, zone := range zones {
if server == "" {
wgWrk.Go(func() {
processLocalZone(zone, doCIDR, ranger, hostChan)
})
} else {
wgWrk.Add(1)
semAXFR <- struct{}{}
go func() {
defer wgWrk.Done()
processRemoteZone(zone, server, doCIDR, ranger, hostChan)
<-semAXFR
}()
}
}
wgWrk.Wait()
close(hostChan)
wgMon.Wait()
displayHostEntries(keys, entries)
}