memguardian

package
v0.0.91 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 7 Imported by: 1

README

Mem Guardian Usage Guide

Environment Variables

  • MEMGUARDIAN: Enable or disable memguardian. Set to 1 to enable
  • MEMGUARDIAN_MAX_RAM_RATIO: Maximum ram ratio from 1 to 100
  • MEMGUARDIAN_MAX_RAM: Maximum amount of RAM (in size units ex: 10gb)
  • MEMGUARDIAN_INTERVAL: detection interval (with unit ex: 30s)

How to Use

  1. Set the environment variables as per your requirements.
export MEMGUARDIAN=1
export MEMGUARDIAN_MAX_RAM_RATIO=75 # default
export MEMGUARDIAN_MAX_RAM=6Gb # optional
export MEMGUARDIAN_INTERVAL=30s # default
  1. Run your Go application. The profiler will start automatically if MEMGUARDIAN is set to 1.

Documentation

Overview

memguardian is a package that provides a simple RAM memory control mechanism once activated it sets an internal atomic boolean when the RAM usage exceed in absolute terms the warning ratio, for passive indirect check or invoke an optional callback for reactive backpressure

Index

Constants

View Source
const (
	MemGuardianEnabled            = "MEMGUARDIAN"
	MemGuardianMaxUsedRamRatioENV = "MEMGUARDIAN_MAX_RAM_RATIO"
	MemGuardianMaxUsedMemoryENV   = "MEMGUARDIAN_MAX_RAM"
	MemGuardianIntervalENV        = "MEMGUARDIAN_INTERVAL"
)

Variables

View Source
var (
	DefaultInterval        time.Duration
	DefaultMaxUsedRamRatio float64

	DefaultMemGuardian *MemGuardian
)

Functions

func UsedRam

func UsedRam() (ratio float64, used uint64, err error)

Calculate the system absolute ratio of used RAM

Types

type MemGuardian

type MemGuardian struct {
	Warning atomic.Bool
	// contains filtered or unexported fields
}

func New

func New(options ...MemGuardianOption) (*MemGuardian, error)

New mem guadian instance with user defined options

func (*MemGuardian) Close

func (mg *MemGuardian) Close()

Close and stops the instance

func (*MemGuardian) Run

func (mg *MemGuardian) Run(ctx context.Context) error

Run the instance monitor (cancel using the Stop method or context parameter)

type MemGuardianOption

type MemGuardianOption func(*MemGuardian) error

func WitInterval

func WitInterval(d time.Duration) MemGuardianOption

WithInterval defines the ticker interval of the memory monitor

func WithCallback

func WithCallback(f func()) MemGuardianOption

WithCallback defines an optional callback if the warning ration is exceeded

func WithMaxRamAmountWarning

func WithMaxRamAmountWarning(maxRam string) MemGuardianOption

WithMaxRamAmountWarning defines the max amount of used RAM in bytes threshold of the warning state (and optional callback invocation)

func WithMaxRamRatioWarning

func WithMaxRamRatioWarning(ratio float64) MemGuardianOption

WithMaxRamRatioWarning defines the ratio (1-100) threshold of the warning state (and optional callback invocation)

type SysInfo

type SysInfo struct {
	Uptime int64

	SharedRam uint64
	BufferRam uint64
	TotalSwap uint64
	FreeSwap  uint64
	Unit      uint64
	// contains filtered or unexported fields
}

func GetSysInfo

func GetSysInfo() (*SysInfo, error)

func (*SysInfo) FreeRam

func (si *SysInfo) FreeRam() uint64

func (*SysInfo) TotalRam

func (si *SysInfo) TotalRam() uint64

func (*SysInfo) UsedPercent

func (si *SysInfo) UsedPercent() float64

func (*SysInfo) UsedRam

func (si *SysInfo) UsedRam() uint64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL