[ARVADOS] created: 4afa9f04a01727ff44598aba871d69cba15340a6

Git user git at public.curoverse.com
Wed Apr 13 18:54:30 EDT 2016


        at  4afa9f04a01727ff44598aba871d69cba15340a6 (commit)


commit 4afa9f04a01727ff44598aba871d69cba15340a6
Author: radhika <radhika at curoverse.com>
Date:   Wed Apr 13 18:39:06 2016 -0400

    8825: an attempt at using CloseNotifier (WIP)

diff --git a/services/keepstore/handler_test.go b/services/keepstore/handler_test.go
index a7675fb..b45ce31 100644
--- a/services/keepstore/handler_test.go
+++ b/services/keepstore/handler_test.go
@@ -511,8 +511,7 @@ func TestDeleteHandler(t *testing.T) {
 	}
 
 	// Unauthenticated request returns PermissionError.
-	var response *httptest.ResponseRecorder
-	response = IssueRequest(unauthReq)
+	response := IssueRequest(unauthReq)
 	ExpectStatusCode(t,
 		"unauthenticated request",
 		PermissionError.HTTPCode,
@@ -805,10 +804,22 @@ func TestTrashHandler(t *testing.T) {
 // Helper functions
 // ====================
 
+type ResponseRecorder struct {
+	*httptest.ResponseRecorder
+	http.CloseNotifier
+}
+
+func (responseRecorder *ResponseRecorder) CloseNotify() <-chan bool {
+	return make(chan bool)
+}
+
 // IssueTestRequest executes an HTTP request described by rt, to a
 // REST router.  It returns the HTTP response to the request.
-func IssueRequest(rt *RequestTester) *httptest.ResponseRecorder {
-	response := httptest.NewRecorder()
+func IssueRequest(rt *RequestTester) *ResponseRecorder {
+	resp := httptest.NewRecorder()
+	rwChan := make(chan http.ResponseWriter, 1)
+	closeNotifier := (<-rwChan).(http.CloseNotifier)
+	response := ResponseRecorder{resp, closeNotifier}
 	body := bytes.NewReader(rt.requestBody)
 	req, _ := http.NewRequest(rt.method, rt.uri, body)
 	if rt.apiToken != "" {
@@ -816,7 +827,7 @@ func IssueRequest(rt *RequestTester) *httptest.ResponseRecorder {
 	}
 	loggingRouter := MakeLoggingRESTRouter()
 	loggingRouter.ServeHTTP(response, req)
-	return response
+	return &response
 }
 
 // ExpectStatusCode checks whether a response has the specified status code,
@@ -825,7 +836,7 @@ func ExpectStatusCode(
 	t *testing.T,
 	testname string,
 	expectedStatus int,
-	response *httptest.ResponseRecorder) {
+	response *ResponseRecorder) {
 	if response.Code != expectedStatus {
 		t.Errorf("%s: expected status %d, got %+v",
 			testname, expectedStatus, response)
@@ -836,11 +847,12 @@ func ExpectBody(
 	t *testing.T,
 	testname string,
 	expectedBody string,
-	response *httptest.ResponseRecorder) {
+	response *ResponseRecorder) {
 	if expectedBody != "" && response.Body.String() != expectedBody {
 		t.Errorf("%s: expected response body '%s', got %+v",
 			testname, expectedBody, response)
 	}
+	response.CloseNotify()
 }
 
 // See #7121
diff --git a/services/keepstore/handlers.go b/services/keepstore/handlers.go
index 043ab69..406ecdb 100644
--- a/services/keepstore/handlers.go
+++ b/services/keepstore/handlers.go
@@ -91,6 +91,9 @@ func GetBlockHandler(resp http.ResponseWriter, req *http.Request) {
 	resp.Header().Set("Content-Length", strconv.Itoa(len(block)))
 	resp.Header().Set("Content-Type", "application/octet-stream")
 	resp.Write(block)
+	go func() {
+		resp.(http.CloseNotifier).CloseNotify()
+	}()
 }
 
 // PutBlockHandler is a HandleFunc to address Put block requests.
diff --git a/services/keepstore/logging_router.go b/services/keepstore/logging_router.go
index 9edfb6e..6d92378 100644
--- a/services/keepstore/logging_router.go
+++ b/services/keepstore/logging_router.go
@@ -18,6 +18,7 @@ type LoggingResponseWriter struct {
 	http.ResponseWriter
 	ResponseBody string
 	sentHdr      time.Time
+	http.CloseNotifier
 }
 
 // WriteHeader writes header to ResponseWriter
@@ -42,6 +43,10 @@ func (loggingWriter *LoggingResponseWriter) Write(data []byte) (int, error) {
 	return loggingWriter.ResponseWriter.Write(data)
 }
 
+func (loggingWriter *LoggingResponseWriter) CloseNotify() <-chan bool {
+	return loggingWriter.CloseNotifier.CloseNotify()
+}
+
 // LoggingRESTRouter is used to add logging capabilities to mux.Router
 type LoggingRESTRouter struct {
 	router *mux.Router
@@ -55,7 +60,7 @@ func MakeLoggingRESTRouter() *LoggingRESTRouter {
 
 func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
 	t0 := time.Now()
-	loggingWriter := LoggingResponseWriter{http.StatusOK, 0, resp, "", zeroTime}
+	loggingWriter := LoggingResponseWriter{http.StatusOK, 0, resp, "", zeroTime, resp.(http.CloseNotifier)}
 	loggingRouter.router.ServeHTTP(&loggingWriter, req)
 	statusText := http.StatusText(loggingWriter.Status)
 	if loggingWriter.Status >= 400 {
@@ -66,5 +71,4 @@ func (loggingRouter *LoggingRESTRouter) ServeHTTP(resp http.ResponseWriter, req
 	tLatency := loggingWriter.sentHdr.Sub(t0)
 	tResponse := now.Sub(loggingWriter.sentHdr)
 	log.Printf("[%s] %s %s %d %.6fs %.6fs %.6fs %d %d \"%s\"", req.RemoteAddr, req.Method, req.URL.Path[1:], req.ContentLength, tTotal.Seconds(), tLatency.Seconds(), tResponse.Seconds(), loggingWriter.Status, loggingWriter.Length, statusText)
-
 }

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list