Skip to content

Commit 18822a0

Browse files
authored
Merge pull request #30 from yuuki/setrlimit_nofile
avoid too many open files error
2 parents 00e4da3 + 5c36702 commit 18822a0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cli.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/signal"
1111
"strconv"
12+
"syscall"
1213
"text/tabwriter"
1314
"time"
1415

@@ -106,6 +107,11 @@ func (c *CLI) Run(args []string) int {
106107
return exitCodeErr
107108
}
108109

110+
if err := setRLimitNoFile(); err != nil {
111+
fmt.Fprintf(c.errStream, "%v", err)
112+
return exitCodeErr
113+
}
114+
109115
if watch == 0 { // no watch option
110116
return c.run(processes, numeric, json, filter)
111117
}
@@ -191,6 +197,26 @@ func (c *CLI) PrintHostFlowsAsJSON(flows tcpflow.HostFlows) error {
191197
return nil
192198
}
193199

200+
// setRLimitNoFile avoids too many open files error.
201+
func setRLimitNoFile() error {
202+
var rLimit syscall.Rlimit
203+
204+
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
205+
if err != nil {
206+
return xerrors.Errorf("could not get rlimit: %w", err)
207+
}
208+
209+
if rLimit.Cur < rLimit.Max {
210+
rLimit.Cur = rLimit.Max
211+
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
212+
if err != nil {
213+
return xerrors.Errorf("could not set rlimit: %w", err)
214+
}
215+
}
216+
217+
return nil
218+
}
219+
194220
var helpText = `Usage: lstf [options]
195221
196222
Print TCP flows between localhost and other hosts

0 commit comments

Comments
 (0)