[ARVADOS] created: f3c31e7c71f076f5feafbb3f14e210f4d0de9012

Git user git at public.curoverse.com
Mon Nov 21 14:23:53 EST 2016


        at  f3c31e7c71f076f5feafbb3f14e210f4d0de9012 (commit)


commit f3c31e7c71f076f5feafbb3f14e210f4d0de9012
Author: Tom Clegg <tom at curoverse.com>
Date:   Mon Nov 21 14:22:59 2016 -0500

    10484: Report current client count.

diff --git a/sdk/go/httpserver/request_limiter.go b/sdk/go/httpserver/request_limiter.go
index 178ffb9..ee35f47 100644
--- a/sdk/go/httpserver/request_limiter.go
+++ b/sdk/go/httpserver/request_limiter.go
@@ -4,18 +4,42 @@ import (
 	"net/http"
 )
 
+// RequestCounter is an http.Handler that tracks the number of
+// requests in progress.
+type RequestCounter interface {
+	http.Handler
+
+	// Current() returns the number of requests in progress.
+	Current() int
+
+	// Max() returns the maximum number of concurrent requests
+	// that will be accepted.
+	Max() int
+}
+
 type limiterHandler struct {
 	requests chan struct{}
 	handler  http.Handler
 }
 
-func NewRequestLimiter(maxRequests int, handler http.Handler) http.Handler {
+// NewRequestLimiter returns a RequestCounter that delegates up to
+// maxRequests at a time to the given handler, and responds 503 to all
+// incoming requests beyond that limit.
+func NewRequestLimiter(maxRequests int, handler http.Handler) RequestCounter {
 	return &limiterHandler{
 		requests: make(chan struct{}, maxRequests),
 		handler:  handler,
 	}
 }
 
+func (h *limiterHandler) Current() int {
+	return len(h.requests)
+}
+
+func (h *limiterHandler) Max() int {
+	return cap(h.requests)
+}
+
 func (h *limiterHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
 	select {
 	case h.requests <- struct{}{}:
diff --git a/services/keepstore/handlers.go b/services/keepstore/handlers.go
index ed0d8d7..adaaa36 100644
--- a/services/keepstore/handlers.go
+++ b/services/keepstore/handlers.go
@@ -24,14 +24,20 @@ import (
 	"sync"
 	"time"
 
+	"git.curoverse.com/arvados.git/sdk/go/httpserver"
 	log "github.com/Sirupsen/logrus"
 )
 
-// MakeRESTRouter returns a new mux.Router that forwards all Keep
-// requests to the appropriate handlers.
-//
-func MakeRESTRouter() *mux.Router {
+type router struct {
+	*mux.Router
+	limiter httpserver.RequestCounter
+}
+
+// MakeRESTRouter returns a new router that forwards all Keep requests
+// to the appropriate handlers.
+func MakeRESTRouter() *router {
 	rest := mux.NewRouter()
+	rtr := &router{Router: rest}
 
 	rest.HandleFunc(
 		`/{hash:[0-9a-f]{32}}`, GetBlockHandler).Methods("GET", "HEAD")
@@ -48,10 +54,10 @@ func MakeRESTRouter() *mux.Router {
 	rest.HandleFunc(`/index/{prefix:[0-9a-f]{0,32}}`, IndexHandler).Methods("GET", "HEAD")
 
 	// Internals/debugging info (runtime.MemStats)
-	rest.HandleFunc(`/debug.json`, DebugHandler).Methods("GET", "HEAD")
+	rest.HandleFunc(`/debug.json`, rtr.DebugHandler).Methods("GET", "HEAD")
 
 	// List volumes: path, device number, bytes used/avail.
-	rest.HandleFunc(`/status.json`, StatusHandler).Methods("GET", "HEAD")
+	rest.HandleFunc(`/status.json`, rtr.StatusHandler).Methods("GET", "HEAD")
 
 	// Replace the current pull queue.
 	rest.HandleFunc(`/pull`, PullHandler).Methods("PUT")
@@ -66,7 +72,7 @@ func MakeRESTRouter() *mux.Router {
 	// 400 Bad Request.
 	rest.NotFoundHandler = http.HandlerFunc(BadRequestHandler)
 
-	return rest
+	return rtr
 }
 
 // BadRequestHandler is a HandleFunc to address bad requests.
@@ -259,17 +265,19 @@ type volumeStatusEnt struct {
 
 // NodeStatus struct
 type NodeStatus struct {
-	Volumes    []*volumeStatusEnt
-	BufferPool PoolStatus
-	PullQueue  WorkQueueStatus
-	TrashQueue WorkQueueStatus
+	Volumes         []*volumeStatusEnt
+	BufferPool      PoolStatus
+	PullQueue       WorkQueueStatus
+	TrashQueue      WorkQueueStatus
+	RequestsCurrent int
+	RequestsMax     int
 }
 
 var st NodeStatus
 var stLock sync.Mutex
 
 // DebugHandler addresses /debug.json requests.
-func DebugHandler(resp http.ResponseWriter, req *http.Request) {
+func (rtr *router) DebugHandler(resp http.ResponseWriter, req *http.Request) {
 	type debugStats struct {
 		MemStats runtime.MemStats
 	}
@@ -282,9 +290,9 @@ func DebugHandler(resp http.ResponseWriter, req *http.Request) {
 }
 
 // StatusHandler addresses /status.json requests.
-func StatusHandler(resp http.ResponseWriter, req *http.Request) {
+func (rtr *router) StatusHandler(resp http.ResponseWriter, req *http.Request) {
 	stLock.Lock()
-	readNodeStatus(&st)
+	rtr.readNodeStatus(&st)
 	jstat, err := json.Marshal(&st)
 	stLock.Unlock()
 	if err == nil {
@@ -297,7 +305,7 @@ func StatusHandler(resp http.ResponseWriter, req *http.Request) {
 }
 
 // populate the given NodeStatus struct with current values.
-func readNodeStatus(st *NodeStatus) {
+func (rtr *router) readNodeStatus(st *NodeStatus) {
 	vols := KeepVM.AllReadable()
 	if cap(st.Volumes) < len(vols) {
 		st.Volumes = make([]*volumeStatusEnt, len(vols))
@@ -320,6 +328,10 @@ func readNodeStatus(st *NodeStatus) {
 	st.BufferPool.Len = bufs.Len()
 	st.PullQueue = getWorkQueueStatus(pullq)
 	st.TrashQueue = getWorkQueueStatus(trashq)
+	if rtr.limiter != nil {
+		st.RequestsCurrent = rtr.limiter.Current()
+		st.RequestsMax = rtr.limiter.Max()
+	}
 }
 
 // return a WorkQueueStatus for the given queue. If q is nil (which
diff --git a/services/keepstore/keepstore.go b/services/keepstore/keepstore.go
index d82c7d0..5414795 100644
--- a/services/keepstore/keepstore.go
+++ b/services/keepstore/keepstore.go
@@ -150,9 +150,10 @@ func main() {
 	KeepVM = MakeRRVolumeManager(theConfig.Volumes)
 
 	// Middleware stack: logger, MaxRequests limiter, method handlers
-	http.Handle("/", &LoggingRESTRouter{
-		router: httpserver.NewRequestLimiter(theConfig.MaxRequests, MakeRESTRouter()),
-	})
+	router := MakeRESTRouter()
+	limiter := httpserver.NewRequestLimiter(theConfig.MaxRequests, router)
+	router.limiter = limiter
+	http.Handle("/", &LoggingRESTRouter{router: limiter})
 
 	// Set up a TCP listener.
 	listener, err := net.Listen("tcp", theConfig.Listen)

commit 372a67d4364d4776aaa8a5ae9a4dd0ac16a0c524
Merge: e5fd39a 6e38822
Author: Tom Clegg <tom at curoverse.com>
Date:   Mon Nov 21 13:39:50 2016 -0500

    Merge branch '10473-keepstore-logrus' closes #10473


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


hooks/post-receive
-- 




More information about the arvados-commits mailing list