-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
text/template: shut down lexing goroutine on error
When a parse error occurred, the lexing goroutine would lay idle. It's not likely a problem but if the program is for some reason accepting badly formed data repeatedly, it's wasteful. The solution is easy: Just drain the input on error. We know this will succeed because the input is always a string and is therefore guaranteed finite. With debugging prints in the package tests I've shown this is effective, shutting down 79 goroutines that would otherwise linger, out of 123 total. Fixes #10574. Change-Id: I8aa536e327b219189a7e7f604a116fa562ae1c39 Reviewed-on: https://go-review.googlesource.com/9658 Reviewed-by: Russ Cox <[email protected]>
- Loading branch information
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,12 +167,23 @@ func (l *lexer) errorf(format string, args ...interface{}) stateFn { | |
} | ||
|
||
// nextItem returns the next item from the input. | ||
// Called by the parser, not in the lexing goroutine. | ||
func (l *lexer) nextItem() item { | ||
item := <-l.items | ||
l.lastPos = item.pos | ||
return item | ||
} | ||
|
||
// drain drains the output so the lexing goroutine will exit. | ||
// Called by the parser, not in the lexing goroutine. | ||
func (l *lexer) drain() { | ||
if l == nil { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
robpike
Author
Contributor
|
||
return | ||
} | ||
for range l.items { | ||
} | ||
} | ||
|
||
// lex creates a new scanner for the input string. | ||
func lex(name, input, left, right string) *lexer { | ||
if left == "" { | ||
|
@@ -197,6 +208,7 @@ func (l *lexer) run() { | |
for l.state = lexText; l.state != nil; { | ||
l.state = l.state(l) | ||
} | ||
close(l.items) | ||
} | ||
|
||
// state functions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
how l could be nil here?