<pre style='margin:0'>
Zero King (l2dy) pushed a commit to branch develop
in repository mpbot-github.
</pre>
<p><a href="https://github.com/macports/mpbot-github/commit/8f83cb7aa124ae246b5166ca75f865b20f35ed30">https://github.com/macports/mpbot-github/commit/8f83cb7aa124ae246b5166ca75f865b20f35ed30</a></p>
<pre style="white-space: pre; background: #F8F8F8"><span style='display:block; white-space:pre;color:#808000;'>commit 8f83cb7aa124ae246b5166ca75f865b20f35ed30
</span>Author: Zero King <l2dy@macports.org>
AuthorDate: Sun May 13 03:47:23 2018 +0000
<span style='display:block; white-space:pre;color:#404040;'> ci: use retryable HTTP client
</span>---
ci/logger/remoteLog.go | 124 +++++++++++++++++++++++++------------------------
1 file changed, 64 insertions(+), 60 deletions(-)
<span style='display:block; white-space:pre;color:#808080;'>diff --git a/ci/logger/remoteLog.go b/ci/logger/remoteLog.go
</span><span style='display:block; white-space:pre;color:#808080;'>index 8b360d6..6649f01 100644
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>--- a/ci/logger/remoteLog.go
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>+++ b/ci/logger/remoteLog.go
</span><span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -2,11 +2,13 @@ package logger
</span>
import (
"bytes"
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ "errors"
</span> "io"
"mime/multipart"
<span style='display:block; white-space:pre;background:#ffe0e0;'>- "net/http"
</span> "net/url"
"os"
<span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ "github.com/hashicorp/go-retryablehttp"
</span> )
var pasteURL = &url.URL{
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -19,83 +21,85 @@ type remoteLogger struct {
</span> logBigFileChan chan *LogFile
parent *Logger
quitChan chan byte
<span style='display:block; white-space:pre;background:#ffe0e0;'>- httpClient *http.Client
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ httpClient *retryablehttp.Client
</span> }
func newRemoteLogger(parent *Logger) *remoteLogger {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return &remoteLogger{
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ r := &remoteLogger{
</span> logBigFileChan: make(chan *LogFile, 4),
parent: parent,
quitChan: make(chan byte),
<span style='display:block; white-space:pre;background:#ffe0e0;'>- httpClient: &http.Client{
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- CheckRedirect: func(req *http.Request, via []*http.Request) error {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return http.ErrUseLastResponse
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- },
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- },
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ httpClient: retryablehttp.NewClient(),
</span> }
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ r.httpClient.Logger = nil
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return r
</span> }
func (r *remoteLogger) run() {
for {
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ var err error
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ var fieldName string
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> select {
case logBigFile := <-r.logBigFileChan:
if logBigFile == nil {
r.parent.LogChan <- &LogText{FieldName: "", Text: nil}
return
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- fileInfo, err := os.Stat(logBigFile.Filename)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if err == nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- for i := 1; i < 3; i++ {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- file, iErr := os.Open(logBigFile.Filename)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if iErr != nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = iErr
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- buf := new(bytes.Buffer)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mimeWriter := multipart.NewWriter(buf)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- writer, iErr := mimeWriter.CreateFormField("paste")
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if iErr != nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = iErr
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- // Max 8 MiB
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- const trimNote = "*log trimmed*\n"
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if fileSize := fileInfo.Size(); fileSize > 8*1024*1024-int64(len(trimNote)) {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- file.Seek(fileSize-8*1024*1024+int64(len(trimNote)), 0)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- io.WriteString(writer, trimNote)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- io.Copy(writer, file)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- file.Close()
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mimeWriter.Close()
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- resp, iErr := r.httpClient.Post(pasteURL.String(), mimeWriter.FormDataContentType(), buf)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if iErr != nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = iErr
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- continue
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- resp.Body.Close()
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- loc := resp.Header.Get("Location")
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if loc == "" {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = iErr
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- continue
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- u, iErr := pasteURL.Parse(loc)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if iErr != nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = iErr
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- r.parent.LogChan <- &LogText{
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- logBigFile.FieldName + "-pastebin",
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- []byte(u.String()),
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- err = nil
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- break
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fieldName = logBigFile.FieldName
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fileInfo, iErr := os.Stat(logBigFile.Filename)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if iErr != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = iErr
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ file, iErr := os.Open(logBigFile.Filename)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if iErr != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = iErr
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ buf := new(bytes.Buffer)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mimeWriter := multipart.NewWriter(buf)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ writer, iErr := mimeWriter.CreateFormField("paste")
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if iErr != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = iErr
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ // Max 8 MiB
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ const trimNote = "*log trimmed*\n"
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if fileSize := fileInfo.Size(); fileSize > 8*1024*1024-int64(len(trimNote)) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ file.Seek(fileSize-8*1024*1024+int64(len(trimNote)), 0)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ io.WriteString(writer, trimNote)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ io.Copy(writer, file)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ file.Close()
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mimeWriter.Close()
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ postForm := bytes.NewReader(buf.Bytes())
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ resp, iErr := r.httpClient.Post(pasteURL.String(), mimeWriter.FormDataContentType(), postForm)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if iErr != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = iErr
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if err != nil {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- r.parent.LogChan <- &LogText{
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- logBigFile.FieldName + "-pastebin-fail",
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- []byte(err.Error()),
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ resp.Body.Close()
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ loc := resp.Header.Get("Location")
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if loc == "" {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = errors.New("missing Location header")
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ u, iErr := pasteURL.Parse(loc)
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if iErr != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ err = iErr
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ break
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ r.parent.LogChan <- &LogText{
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fieldName + "-pastebin",
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ []byte(u.String()),
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if err != nil {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ r.parent.LogChan <- &LogText{
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ fieldName + "-pastebin-fail",
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ []byte(err.Error()),
</span> }
}
}
</pre><pre style='margin:0'>
</pre>