From 50f453e3d3bf9821890413ac2422238a0a7e93e6 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 28 Nov 2023 09:43:23 +0100 Subject: [PATCH] worker: add memory endpoint --- api/worker.go | 9 +++++++++ worker/client/client.go | 6 ++++++ worker/memory.go | 11 +++++++++++ worker/worker.go | 8 ++++++++ 4 files changed, 34 insertions(+) diff --git a/api/worker.go b/api/worker.go index 1a5e77558..5b3ea14d1 100644 --- a/api/worker.go +++ b/api/worker.go @@ -51,6 +51,15 @@ type ( Error string `json:"error,omitempty"` } + MemoryResponse struct { + Upload MemoryStatus `json:"upload"` + } + + MemoryStatus struct { + Available uint64 `json:"available"` + Total uint64 `json:"total"` + } + // MigrateSlabResponse is the response type for the /slab/migrate endpoint. MigrateSlabResponse struct { NumShardsMigrated int `json:"numShardsMigrated"` diff --git a/worker/client/client.go b/worker/client/client.go index 8319d5c5c..a5979d124 100644 --- a/worker/client/client.go +++ b/worker/client/client.go @@ -136,6 +136,12 @@ func (c *Client) ID(ctx context.Context) (id string, err error) { return } +// Memory requests the /memory endpoint. +func (c *Client) Memory(ctx context.Context) (resp api.MemoryResponse, err error) { + err = c.c.WithContext(ctx).GET("/memory", &resp) + return +} + // MigrateSlab migrates the specified slab. func (c *Client) MigrateSlab(ctx context.Context, slab object.Slab, set string) (res api.MigrateSlabResponse, err error) { values := make(url.Values) diff --git a/worker/memory.go b/worker/memory.go index 1f408f5d6..7918e69c0 100644 --- a/worker/memory.go +++ b/worker/memory.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "sync" + + "go.sia.tech/renterd/api" ) type ( @@ -37,6 +39,15 @@ func newMemoryManager(maxMemory uint64) (*memoryManager, error) { return mm, nil } +func (mm *memoryManager) Status() api.MemoryStatus { + mm.mu.Lock() + defer mm.mu.Unlock() + return api.MemoryStatus{ + Available: mm.available, + Total: mm.totalAvailable, + } +} + func (mm *memoryManager) AcquireMemory(ctx context.Context, amt uint64) <-chan *acquiredMemory { if amt == 0 { panic("cannot acquire 0 memory") diff --git a/worker/worker.go b/worker/worker.go index 1719ac9de..f28bffaa1 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -1340,6 +1340,12 @@ func (w *worker) idHandlerGET(jc jape.Context) { jc.Encode(w.id) } +func (w *worker) memoryGET(jc jape.Context) { + jc.Encode(api.MemoryResponse{ + Upload: w.uploadManager.mm.Status(), + }) +} + func (w *worker) accountHandlerGET(jc jape.Context) { var hostKey types.PublicKey if jc.DecodeParam("hostkey", &hostKey) != nil { @@ -1409,6 +1415,8 @@ func (w *worker) Handler() http.Handler { "GET /account/:hostkey": w.accountHandlerGET, "GET /id": w.idHandlerGET, + "GET /memory": w.memoryGET, + "GET /rhp/contracts": w.rhpContractsHandlerGET, "POST /rhp/contract/:id/broadcast": w.rhpBroadcastHandler, "POST /rhp/contract/:id/prune": w.rhpPruneContractHandlerPOST,