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

Add io.Closer guidelines #29387

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions docs/content/contributing/guidelines-backend.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ i.e. `services/user`, `models/repository`.
Since there are some packages which use the same package name, it is possible that you find packages like `modules/user`, `models/user`, and `services/user`. When these packages are imported in one Go file, it's difficult to know which package we are using and if it's a variable name or an import name. So, we always recommend to use import aliases. To differ from package variables which are commonly in camelCase, just use **snake_case** for import aliases.
i.e. `import user_service "code.gitea.io/gitea/services/user"`

### Implementing `io.Closer`

If a type implements `io.Closer`, calling `Close` multiple times must not fail or `panic` but return an error or `nil`.

### Important Gotchas

- Never write `x.Update(exemplar)` without an explicit `WHERE` clause:
Expand Down
4 changes: 4 additions & 0 deletions modules/git/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {

// Close BlameReader - don't run NextPart after invoking that
func (r *BlameReader) Close() error {
if r.bufferedReader == nil {
return nil
}

err := <-r.done
r.bufferedReader = nil
_ = r.reader.Close()
Expand Down
5 changes: 3 additions & 2 deletions modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@
}

// Close this repository, in particular close the underlying gogitStorage if this is not nil
func (repo *Repository) Close() (err error) {
func (repo *Repository) Close() error {
if repo == nil || repo.gogitStorage == nil {
return

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / backend

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough return values

Check failure on line 93 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

not enough return values
KN4CK3R marked this conversation as resolved.
Show resolved Hide resolved
}
if err := repo.gogitStorage.Close(); err != nil {
gitealog.Error("Error closing storage: %v", err)
}
repo.gogitStorage = nil
repo.LastCommitCache = nil
repo.tagCache = nil
return
return nil
}

// GoGitRepo gets the go-git repo representation
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
}
}

func (repo *Repository) Close() (err error) {
func (repo *Repository) Close() error {
if repo == nil {
return nil
}
Expand All @@ -123,5 +123,5 @@ func (repo *Repository) Close() (err error) {
}
repo.LastCommitCache = nil
repo.tagCache = nil
return err
return nil
}
2 changes: 1 addition & 1 deletion modules/indexer/internal/bleve/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (i *Indexer) Ping(_ context.Context) error {
}

func (i *Indexer) Close() {
if i == nil {
if i == nil || i.Indexer == nil {
return
}

Expand Down
3 changes: 0 additions & 3 deletions modules/indexer/internal/meilisearch/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,5 @@ func (i *Indexer) Close() {
if i == nil {
return
}
if i.Client == nil {
return
}
i.Client = nil
}
1 change: 1 addition & 0 deletions modules/util/filebuffer/file_backed_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (b *FileBackedBuffer) Close() error {
if b.file != nil {
err := b.file.Close()
os.Remove(b.file.Name())
b.file = nil
return err
}
return nil
Expand Down
Loading