server: stats endpoint (#468)

fixes #389
This commit is contained in:
C Cirello
2017-01-03 21:39:29 +01:00
committed by GitHub
parent 90092d2e8f
commit c48bd95fa6
6 changed files with 65 additions and 0 deletions

View File

@@ -30,6 +30,8 @@ type Runner struct {
availableMem int64
usedMem int64
usedMemMutex sync.RWMutex
stats
}
var (

46
api/runner/stats.go Normal file
View File

@@ -0,0 +1,46 @@
package runner
import "sync"
type stats struct {
mu sync.Mutex
queue uint64
running uint64
complete uint64
}
type Stats struct {
Queue uint64
Running uint64
Complete uint64
}
func (s *stats) Enqueue() {
s.mu.Lock()
s.queue++
s.mu.Unlock()
}
func (s *stats) Start() {
s.mu.Lock()
s.queue--
s.running++
s.mu.Unlock()
}
func (s *stats) Complete() {
s.mu.Lock()
s.running--
s.complete++
s.mu.Unlock()
}
func (s *stats) Stats() Stats {
var stats Stats
s.mu.Lock()
stats.Running = s.running
stats.Complete = s.complete
stats.Queue = s.queue
s.mu.Unlock()
return stats
}

View File

@@ -96,10 +96,12 @@ func StartWorkers(ctx context.Context, rnr *Runner, tasks <-chan task.Request) {
continue
}
rnr.Start()
select {
case <-ctx.Done():
return
case p <- task:
rnr.Complete()
}
}
}
@@ -344,6 +346,8 @@ func (hc *htfn) serve(ctx context.Context) {
func runTaskReq(rnr *Runner, wg *sync.WaitGroup, t task.Request) {
defer wg.Done()
rnr.Start()
defer rnr.Complete()
result, err := rnr.Run(t.Ctx, t.Config)
select {
case t.Response <- task.Response{result, err}:

View File

@@ -180,6 +180,7 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun
Timeout: time.Duration(found.Timeout) * time.Second,
}
s.Runner.Enqueue()
switch found.Type {
case "async":
// Read payload

View File

@@ -133,6 +133,7 @@ func (s *Server) bindHandlers() {
engine.GET("/", handlePing)
engine.GET("/version", handleVersion)
engine.GET("/stats", s.handleStats)
v1 := engine.Group("/v1")
{

11
api/server/stats.go Normal file
View File

@@ -0,0 +1,11 @@
package server
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Server) handleStats(c *gin.Context) {
c.JSON(http.StatusOK, s.Runner.Stats())
}