0ceanSlim / grain

Go Relay Architecture for Implementing Nostr 🌾
MIT License
12 stars 1 forks source link

Resource Limit Planning #3

Closed 0ceanSlim closed 3 months ago

0ceanSlim commented 3 months ago

This issue contains a list of new files and additions to plan implementation of configurable resource limits

0ceanSlim commented 3 months ago

config.yml

resource_limits:
  cpu_cores: 2          # Limit the number of CPU cores the application can use
  memory_mb: 1024       # Cap the maximum amount of RAM in MB the application can use
  heap_size_mb: 512     # Set a limit on the Go garbage collector's heap size in MB
  max_goroutines: 100   # Limit the maximum number of concurrently running Go routines
0ceanSlim commented 3 months ago

config/types/resourceLimits.go

package config

type ResourceLimits struct {
    CPUCores      int           `yaml:"cpu_cores"`
    MemoryMB      int           `yaml:"memory_mb"`
    HeapSizeMB    int           `yaml:"heap_size_mb"`
        MaxGoroutines      int      `yaml:"max_goroutines"`
}

config/types/serverConfig.go

package config

type ServerConfig struct {
    // other existing fields...
    ResourceLimits ResourceLimits `yaml:"resource_limits"`
}
0ceanSlim commented 3 months ago

server/utils/applyResourceLimits.go

package utils

import (
    "log"
    "runtime"
    "runtime/debug"
    "sync"
    "time"
)

var (
    maxGoroutinesChan   chan struct{}
    wg                  sync.WaitGroup
    goroutineQueue      []func()
    goroutineQueueMutex sync.Mutex
)

func ApplyResourceLimits(cfg *configTypes.ResourceLimits) {
    // Set CPU cores
    runtime.GOMAXPROCS(cfg.CPUCores)

    // Set maximum heap size
    if cfg.HeapSizeMB > 0 {
        debug.SetMemoryLimit(uint64(cfg.HeapSizeMB) * 1024 * 1024)
        log.Printf("Heap size limited to %d MB\n", cfg.HeapSizeMB)
    }

    // Start monitoring memory usage
    if cfg.MemoryMB > 0 {
        go monitorMemoryUsage(cfg.MemoryMB)
        log.Printf("Max memory usage limited to %d MB\n", cfg.MemoryMB)
    }

    // Set maximum number of Go routines
    if cfg.MaxGoroutines > 0 {
        maxGoroutinesChan = make(chan struct{}, cfg.MaxGoroutines)
        log.Printf("Max goroutines limited to %d\n", cfg.MaxGoroutines)
    }
}

// SafeGoRoutine starts a goroutine with limit enforcement
func SafeGoRoutine(f func()) {
    // By default, all routines are considered non-critical
    goroutineQueueMutex.Lock()
    goroutineQueue = append(goroutineQueue, f)
    goroutineQueueMutex.Unlock()
    attemptToStartGoroutine()
}

func attemptToStartGoroutine() {
    goroutineQueueMutex.Lock()
    defer goroutineQueueMutex.Unlock()

    if len(goroutineQueue) > 0 {
        select {
        case maxGoroutinesChan <- struct{}{}:
            wg.Add(1)
            go func(f func()) {
                defer func() {
                    wg.Done()
                    <-maxGoroutinesChan
                    attemptToStartGoroutine()
                }()
                f()
            }(goroutineQueue[0])

            // Remove the started goroutine from the queue
            goroutineQueue = goroutineQueue[1:]

        default:
            // If the channel is full, consider dropping the oldest non-critical goroutine
            dropOldestNonCriticalGoroutine()
        }
    }
}

func dropOldestNonCriticalGoroutine() {
    goroutineQueueMutex.Lock()
    defer goroutineQueueMutex.Unlock()

    if len(goroutineQueue) > 0 {
        log.Println("Dropping the oldest non-critical goroutine to free resources.")
        goroutineQueue = goroutineQueue[1:]
        attemptToStartGoroutine()
    }
}

func WaitForGoroutines() {
    wg.Wait()
}
0ceanSlim commented 3 months ago

Apply the resource limits in the for loop of the main function

main.go

...
for {
        wg.Add(1)
        cfg, err := config.LoadConfig("config.yml")
        if err != nil {
            log.Fatal("Error loading config: ", err)
        }

        // Apply resource limits
        utils.ApplyResourceLimits(&cfg.ResourceLimits)

//rest of code
0ceanSlim commented 3 months ago

I think if I'm going to do this. I want ApplyUnixResourceLimits and ApplyWindowsResourceLimits

Then in the main function, check for os and apply the appropriate resource limits.

0ceanSlim commented 3 months ago

I think I want to only do configs that can be done everywhere and avoid more complexity for now

but I have an idea for database storage limit and tie that into purging at a certain limit. make a notification on frontend so users see it. Here you can solicit to unpaid users that old notes are being deleted unless you're a paid subscriber.

I think I will just do cpu cores and mem heap size. maybe look into more bandwidth stuff

0ceanSlim commented 3 months ago

resource limits implemented with goroutine limits as well. Removed os dependent limits