Skip to content

Commit

Permalink
Merge pull request #35 from nulab/issue-34/apply-sync.Pool-to-io.Copy
Browse files Browse the repository at this point in the history
Issue 34/apply sync.pool to io.copy
  • Loading branch information
vvatanabe committed Sep 11, 2018
2 parents 90a0fcc + dff1649 commit 0b3c10f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions githttpxfer/githttpxfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"regexp"
"strings"
"sync"
)

var (
Expand Down Expand Up @@ -275,7 +276,9 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) {
return
}

if _, err := io.Copy(stdin, body); err != nil {
bufIn := bufPool.Get().([]byte)
defer bufPool.Put(bufIn)
if _, err := io.CopyBuffer(stdin, body, bufIn); err != nil {
ghx.logger.Error("failed to write the request body to standard input. ", err.Error())
RenderInternalServerError(res.Writer)
return
Expand All @@ -287,17 +290,24 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) {
res.SetContentType(fmt.Sprintf("application/x-git-%s-result", rpc))
res.WriteHeader(http.StatusOK)

if _, err := io.Copy(res.Writer, stdout); err != nil {
bufOut := bufPool.Get().([]byte)
defer bufPool.Put(bufOut)
if _, err := io.CopyBuffer(res.Writer, stdout, bufOut); err != nil {
ghx.logger.Error("failed to write the standard output to response. ", err.Error())
return
}


if err = cmd.Wait(); err != nil {
ghx.logger.Error("specified command fails to run or doesn't complete successfully. ", err.Error())
}
}

var bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 32*1024)
},
}

func (ghx *GitHTTPXfer) getInfoRefs(ctx Context) {
res, req, repoPath := ctx.Response(), ctx.Request(), ctx.RepoPath()

Expand Down

0 comments on commit 0b3c10f

Please sign in to comment.