api: semaphore acquire can fail if context is already closed

This commit is contained in:
mappu 2020-06-07 11:31:25 +12:00
parent 63ee2ec4ee
commit e34ab1fd16
1 changed files with 24 additions and 6 deletions

30
api.go
View File

@ -47,7 +47,10 @@ type MarkdownRequest struct {
// repos gets a list of Git repositories in this organisation.
func (this *Application) repos(ctx context.Context) ([]Repo, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+`/repos`, nil)
@ -77,7 +80,10 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) {
// repoFile gets a single file from the default branch of the git repository
// Usually the default branch is `master`.
func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), nil)
@ -105,7 +111,10 @@ func (this *Application) repoFile(ctx context.Context, repo, filename string) ([
}
func (this *Application) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+dir, nil) // n.b. $dir param not escaped
@ -168,7 +177,10 @@ func (this *Application) imageFilesForRepo(ctx context.Context, repo string) ([]
}
func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]string, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/topics`, nil)
@ -197,7 +209,10 @@ func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]stri
// renderMarkdown calls the remote Gitea server's own markdown renderer.
func (this *Application) renderMarkdown(ctx context.Context, repoName string, body string) ([]byte, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
jb, err := json.Marshal(MarkdownRequest{
@ -231,7 +246,10 @@ func (this *Application) renderMarkdown(ctx context.Context, repoName string, bo
// renderMarkdownRaw calls the remote Gitea server's own markdown renderer.
func (this *Application) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte, error) {
this.apiSem.Acquire(ctx, 1)
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, this.cfg.Gitea.URL+`api/v1/markdown/raw`, bytes.NewReader(body))