Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with not closed file descriptor #29

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Parser struct {
blockWrappers map[string]func(*gonginx.Directive) gonginx.IDirective
directiveWrappers map[string]func(*gonginx.Directive) gonginx.IDirective
commentBuffer []string
file *os.File
}

// WithSameOptions copy options from another parser
Expand Down Expand Up @@ -87,6 +88,7 @@ func NewParser(filePath string, opts ...Option) (*Parser, error) {
l := newLexer(bufio.NewReader(f))
l.file = filePath
p := NewParserFromLexer(l, opts...)
p.file = f
return p, nil
}

Expand Down Expand Up @@ -152,10 +154,12 @@ func (p *Parser) followingTokenIs(t token.Type) bool {

// Parse the gonginx.
func (p *Parser) Parse() *gonginx.Config {
return &gonginx.Config{
c := &gonginx.Config{
FilePath: p.lexer.file, //TODO: set filepath here,
Block: p.parseBlock(),
}
_ = p.Close()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer was read at this point, so it's time to close file.

return c
}

// ParseBlock parse a block statement
Expand Down Expand Up @@ -345,3 +349,11 @@ func (p *Parser) wrapHTTP(directive *gonginx.Directive) *gonginx.HTTP {
func (p *Parser) parseUpstreamServer(directive *gonginx.Directive) *gonginx.UpstreamServer {
return gonginx.NewUpstreamServer(directive)
}

// Close closes the file handler and releases the resources
func (p *Parser) Close() (err error) {
if p.file != nil {
err = p.file.Close()
}
return err
}
Comment on lines +354 to +359
Copy link
Contributor Author

@berejant berejant Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide interface to Close parser (for case when Parse was not closed; useful to use like
defer parser.Close())

3 changes: 2 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ func TestParser_ParseUpstream(t *testing.T) {

func TestParser_ParseFromFile(t *testing.T) {
t.Parallel()
_, err := NewParser("../full-example/nginx.conf")
p, err := NewParser("../full-example/nginx.conf")
assert.NilError(t, err)
assert.Assert(t, p.file != nil, "file must be non-nil")
_, err2 := NewParser("../full-example/nginx.conf-not-found")
assert.ErrorContains(t, err2, "no such file or directory")
}
Expand Down
Loading