mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Reset input buffer on retry
Retries were previously sending empty bodies to the server.
This commit is contained in:
		@@ -539,10 +539,10 @@ func (r *Request) Body(obj interface{}) *Request {
 | 
				
			|||||||
			return r
 | 
								return r
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		glog.V(8).Infof("Request Body: %s", string(data))
 | 
							glog.V(8).Infof("Request Body: %s", string(data))
 | 
				
			||||||
		r.body = bytes.NewBuffer(data)
 | 
							r.body = bytes.NewReader(data)
 | 
				
			||||||
	case []byte:
 | 
						case []byte:
 | 
				
			||||||
		glog.V(8).Infof("Request Body: %s", string(t))
 | 
							glog.V(8).Infof("Request Body: %s", string(t))
 | 
				
			||||||
		r.body = bytes.NewBuffer(t)
 | 
							r.body = bytes.NewReader(t)
 | 
				
			||||||
	case io.Reader:
 | 
						case io.Reader:
 | 
				
			||||||
		r.body = t
 | 
							r.body = t
 | 
				
			||||||
	case runtime.Object:
 | 
						case runtime.Object:
 | 
				
			||||||
@@ -556,7 +556,7 @@ func (r *Request) Body(obj interface{}) *Request {
 | 
				
			|||||||
			return r
 | 
								return r
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		glog.V(8).Infof("Request Body: %s", string(data))
 | 
							glog.V(8).Infof("Request Body: %s", string(data))
 | 
				
			||||||
		r.body = bytes.NewBuffer(data)
 | 
							r.body = bytes.NewReader(data)
 | 
				
			||||||
		r.SetHeader("Content-Type", r.content.ContentType)
 | 
							r.SetHeader("Content-Type", r.content.ContentType)
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		r.err = fmt.Errorf("unknown type used for body: %+v", obj)
 | 
							r.err = fmt.Errorf("unknown type used for body: %+v", obj)
 | 
				
			||||||
@@ -823,6 +823,15 @@ func (r *Request) request(fn func(*http.Request, *http.Response)) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			retries++
 | 
								retries++
 | 
				
			||||||
			if seconds, wait := checkWait(resp); wait && retries < maxRetries {
 | 
								if seconds, wait := checkWait(resp); wait && retries < maxRetries {
 | 
				
			||||||
 | 
									if seeker, ok := r.body.(io.Seeker); ok && r.body != nil {
 | 
				
			||||||
 | 
										_, err := seeker.Seek(0, 0)
 | 
				
			||||||
 | 
										if err != nil {
 | 
				
			||||||
 | 
											glog.V(4).Infof("Could not retry request, can't Seek() back to beginning of body for %T", r.body)
 | 
				
			||||||
 | 
											fn(req, resp)
 | 
				
			||||||
 | 
											return true
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				glog.V(4).Infof("Got a Retry-After %s response for attempt %d to %v", seconds, retries, url)
 | 
									glog.V(4).Infof("Got a Retry-After %s response for attempt %d to %v", seconds, retries, url)
 | 
				
			||||||
				r.backoffMgr.Sleep(time.Duration(seconds) * time.Second)
 | 
									r.backoffMgr.Sleep(time.Duration(seconds) * time.Second)
 | 
				
			||||||
				return false
 | 
									return false
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -874,6 +874,13 @@ func TestCheckRetryHandles429And5xx(t *testing.T) {
 | 
				
			|||||||
	count := 0
 | 
						count := 0
 | 
				
			||||||
	ch := make(chan struct{})
 | 
						ch := make(chan struct{})
 | 
				
			||||||
	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 | 
						testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 | 
				
			||||||
 | 
							data, err := ioutil.ReadAll(req.Body)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								t.Fatalf("unable to read request body: %v", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if !bytes.Equal(data, []byte(strings.Repeat("abcd", 1000))) {
 | 
				
			||||||
 | 
								t.Fatalf("retry did not send a complete body: %s", data)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		t.Logf("attempt %d", count)
 | 
							t.Logf("attempt %d", count)
 | 
				
			||||||
		if count >= 4 {
 | 
							if count >= 4 {
 | 
				
			||||||
			w.WriteHeader(http.StatusOK)
 | 
								w.WriteHeader(http.StatusOK)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user