Skip to content

Commit e0fa03a

Browse files
authored
Max open files (#628)
* adds max_open_files flag * adds doc of max_open_files flag * amends commmit
1 parent 6545203 commit e0fa03a

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Please notice that if no particular configuration is provided, `revive` will beh
191191

192192
### Command Line Flags
193193

194-
`revive` accepts three command line parameters:
194+
`revive` accepts the following command line parameters:
195195

196196
- `-config [PATH]` - path to config file in TOML format, defaults to `$HOME/revive.toml` if present.
197197
- `-exclude [PATTERN]` - pattern for files/directories/packages to be excluded for linting. You can specify the files you want to exclude for linting either as package name (i.e. `github.com/mgechev/revive`), list them as individual files (i.e. `file.go`), directories (i.e. `./foo/...`), or any combination of the three.
@@ -203,7 +203,10 @@ Please notice that if no particular configuration is provided, `revive` will beh
203203
- `friendly` - outputs the failures when found. Shows summary of all the failures.
204204
- `stylish` - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others.
205205
- `checkstyle` - outputs the failures in XML format compatible with that of Java's [Checkstyle](https://checkstyle.org/).
206-
- `-set_exit_status` - set exit status to 1 if any issues are found, overwrites errorCode and warningCode in config.
206+
- `-max_open_files` - maximum number of open files at the same time. Defaults to unlimited.
207+
- `-set_exit_status` - set exit status to 1 if any issues are found, overwrites `errorCode` and `warningCode` in config.
208+
- `-version` - get revive version.
209+
207210

208211
### Sample Invocations
209212

lint/linter.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,34 @@ type disabledIntervalsMap = map[string][]DisabledInterval
1818

1919
// Linter is used for linting set of files.
2020
type Linter struct {
21-
reader ReadFile
21+
reader ReadFile
22+
fileReadTokens chan struct{}
2223
}
2324

2425
// New creates a new Linter
25-
func New(reader ReadFile) Linter {
26-
return Linter{reader: reader}
26+
func New(reader ReadFile, maxOpenFiles int) Linter {
27+
var fileReadTokens chan struct{}
28+
if maxOpenFiles > 0 {
29+
fileReadTokens = make(chan struct{}, maxOpenFiles)
30+
}
31+
return Linter{
32+
reader: reader,
33+
fileReadTokens: fileReadTokens,
34+
}
35+
}
36+
37+
func (l Linter) readFile(path string) (result []byte, err error) {
38+
if l.fileReadTokens != nil {
39+
// "take" a token by writing to the channel.
40+
// It will block if no more space in the channel's buffer
41+
l.fileReadTokens <- struct{}{}
42+
defer func() {
43+
// "free" a token by reading from the channel
44+
<-l.fileReadTokens
45+
}()
46+
}
47+
48+
return l.reader(path)
2749
}
2850

2951
var (
@@ -62,7 +84,7 @@ func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config,
6284
mu: sync.Mutex{},
6385
}
6486
for _, filename := range filenames {
65-
content, err := l.reader(filename)
87+
content, err := l.readFile(filename)
6688
if err != nil {
6789
return err
6890
}

main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func main() {
5959

6060
revive := lint.New(func(file string) ([]byte, error) {
6161
return ioutil.ReadFile(file)
62-
})
62+
}, maxOpenFiles)
6363

6464
lintingRules, err := config.GetLintingRules(conf)
6565
if err != nil {
@@ -155,6 +155,7 @@ var (
155155
help bool
156156
versionFlag bool
157157
setExitStatus bool
158+
maxOpenFiles int
158159
)
159160

160161
var originalUsage = flag.Usage
@@ -204,11 +205,12 @@ func init() {
204205

205206
// command line help strings
206207
const (
207-
configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (i.e. -config myconf.toml)"
208-
excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)"
209-
formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)"
210-
versionUsage = "get revive version"
211-
exitStatusUsage = "set exit status to 1 if any issues are found, overwrites errorCode and warningCode in config"
208+
configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (i.e. -config myconf.toml)"
209+
excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)"
210+
formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)"
211+
versionUsage = "get revive version"
212+
exitStatusUsage = "set exit status to 1 if any issues are found, overwrites errorCode and warningCode in config"
213+
maxOpenFilesUsage = "maximum number of open files at the same time"
212214
)
213215

214216
defaultConfigPath := buildDefaultConfigPath()
@@ -218,6 +220,7 @@ func init() {
218220
flag.StringVar(&formatterName, "formatter", "", formatterUsage)
219221
flag.BoolVar(&versionFlag, "version", false, versionUsage)
220222
flag.BoolVar(&setExitStatus, "set_exit_status", false, exitStatusUsage)
223+
flag.IntVar(&maxOpenFiles, "max_open_files", 0, maxOpenFilesUsage)
221224
flag.Parse()
222225

223226
// Output build info (version, commit, date and builtBy)

test/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
4444
func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Rule, config map[string]lint.RuleConfig) error {
4545
l := lint.New(func(file string) ([]byte, error) {
4646
return ioutil.ReadFile(baseDir + file)
47-
})
47+
}, 0)
4848

4949
ps, err := l.Lint([][]string{{fi.Name()}}, rules, lint.Config{
5050
Rules: config,
@@ -66,7 +66,7 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru
6666
func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
6767
l := lint.New(func(file string) ([]byte, error) {
6868
return ioutil.ReadFile(baseDir + file)
69-
})
69+
}, 0)
7070

7171
ins := parseInstructions(t, fi.Name(), src)
7272
if ins == nil {

0 commit comments

Comments
 (0)