Skip to content

Commit ad0a34b

Browse files
Add io.Closer guidelines (#29387)
Co-authored-by: Yarden Shoham <[email protected]>
1 parent 0676bf5 commit ad0a34b

File tree

7 files changed

+16
-9
lines changed

7 files changed

+16
-9
lines changed

docs/content/contributing/guidelines-backend.en-us.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ i.e. `services/user`, `models/repository`.
101101
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.
102102
i.e. `import user_service "code.gitea.io/gitea/services/user"`
103103

104+
### Implementing `io.Closer`
105+
106+
If a type implements `io.Closer`, calling `Close` multiple times must not fail or `panic` but return an error or `nil`.
107+
104108
### Important Gotchas
105109

106110
- Never write `x.Update(exemplar)` without an explicit `WHERE` clause:

modules/git/blame.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {
115115

116116
// Close BlameReader - don't run NextPart after invoking that
117117
func (r *BlameReader) Close() error {
118+
if r.bufferedReader == nil {
119+
return nil
120+
}
121+
118122
err := <-r.done
119123
r.bufferedReader = nil
120124
_ = r.reader.Close()

modules/git/repo_base_gogit.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,17 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
8888
}
8989

9090
// Close this repository, in particular close the underlying gogitStorage if this is not nil
91-
func (repo *Repository) Close() (err error) {
91+
func (repo *Repository) Close() error {
9292
if repo == nil || repo.gogitStorage == nil {
93-
return
93+
return nil
9494
}
9595
if err := repo.gogitStorage.Close(); err != nil {
9696
gitealog.Error("Error closing storage: %v", err)
9797
}
98+
repo.gogitStorage = nil
9899
repo.LastCommitCache = nil
99100
repo.tagCache = nil
100-
return
101+
return nil
101102
}
102103

103104
// GoGitRepo gets the go-git repo representation

modules/git/repo_base_nogogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError
103103
}
104104
}
105105

106-
func (repo *Repository) Close() (err error) {
106+
func (repo *Repository) Close() error {
107107
if repo == nil {
108108
return nil
109109
}
@@ -123,5 +123,5 @@ func (repo *Repository) Close() (err error) {
123123
}
124124
repo.LastCommitCache = nil
125125
repo.tagCache = nil
126-
return err
126+
return nil
127127
}

modules/indexer/internal/bleve/indexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (i *Indexer) Ping(_ context.Context) error {
9292
}
9393

9494
func (i *Indexer) Close() {
95-
if i == nil {
95+
if i == nil || i.Indexer == nil {
9696
return
9797
}
9898

modules/indexer/internal/meilisearch/indexer.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,5 @@ func (i *Indexer) Close() {
8787
if i == nil {
8888
return
8989
}
90-
if i.Client == nil {
91-
return
92-
}
9390
i.Client = nil
9491
}

modules/util/filebuffer/file_backed_buffer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (b *FileBackedBuffer) Close() error {
149149
if b.file != nil {
150150
err := b.file.Close()
151151
os.Remove(b.file.Name())
152+
b.file = nil
152153
return err
153154
}
154155
return nil

0 commit comments

Comments
 (0)