mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Add io.Closer guidelines (#29387)
				
					
				
			Co-authored-by: Yarden Shoham <git@yardenshoham.com>
This commit is contained in:
		| @@ -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. | 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"` | 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 | ### Important Gotchas | ||||||
|  |  | ||||||
| - Never write `x.Update(exemplar)` without an explicit `WHERE` clause: | - Never write `x.Update(exemplar)` without an explicit `WHERE` clause: | ||||||
|   | |||||||
| @@ -115,6 +115,10 @@ func (r *BlameReader) NextPart() (*BlamePart, error) { | |||||||
|  |  | ||||||
| // Close BlameReader - don't run NextPart after invoking that | // Close BlameReader - don't run NextPart after invoking that | ||||||
| func (r *BlameReader) Close() error { | func (r *BlameReader) Close() error { | ||||||
|  | 	if r.bufferedReader == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	err := <-r.done | 	err := <-r.done | ||||||
| 	r.bufferedReader = nil | 	r.bufferedReader = nil | ||||||
| 	_ = r.reader.Close() | 	_ = r.reader.Close() | ||||||
|   | |||||||
| @@ -88,16 +88,17 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) { | |||||||
| } | } | ||||||
|  |  | ||||||
| // Close this repository, in particular close the underlying gogitStorage if this is not nil | // 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 { | 	if repo == nil || repo.gogitStorage == nil { | ||||||
| 		return | 		return nil | ||||||
| 	} | 	} | ||||||
| 	if err := repo.gogitStorage.Close(); err != nil { | 	if err := repo.gogitStorage.Close(); err != nil { | ||||||
| 		gitealog.Error("Error closing storage: %v", err) | 		gitealog.Error("Error closing storage: %v", err) | ||||||
| 	} | 	} | ||||||
|  | 	repo.gogitStorage = nil | ||||||
| 	repo.LastCommitCache = nil | 	repo.LastCommitCache = nil | ||||||
| 	repo.tagCache = nil | 	repo.tagCache = nil | ||||||
| 	return | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // GoGitRepo gets the go-git repo representation | // GoGitRepo gets the go-git repo representation | ||||||
|   | |||||||
| @@ -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 { | 	if repo == nil { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| @@ -123,5 +123,5 @@ func (repo *Repository) Close() (err error) { | |||||||
| 	} | 	} | ||||||
| 	repo.LastCommitCache = nil | 	repo.LastCommitCache = nil | ||||||
| 	repo.tagCache = nil | 	repo.tagCache = nil | ||||||
| 	return err | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -92,7 +92,7 @@ func (i *Indexer) Ping(_ context.Context) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (i *Indexer) Close() { | func (i *Indexer) Close() { | ||||||
| 	if i == nil { | 	if i == nil || i.Indexer == nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -87,8 +87,5 @@ func (i *Indexer) Close() { | |||||||
| 	if i == nil { | 	if i == nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if i.Client == nil { |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	i.Client = nil | 	i.Client = nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -149,6 +149,7 @@ func (b *FileBackedBuffer) Close() error { | |||||||
| 	if b.file != nil { | 	if b.file != nil { | ||||||
| 		err := b.file.Close() | 		err := b.file.Close() | ||||||
| 		os.Remove(b.file.Name()) | 		os.Remove(b.file.Name()) | ||||||
|  | 		b.file = nil | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user