Skip to content

Commit

Permalink
worker: add memory endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Nov 28, 2023
1 parent 193dcbd commit 50f453e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 6 additions & 0 deletions worker/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions worker/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"sync"

"go.sia.tech/renterd/api"
)

type (
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 50f453e

Please sign in to comment.