github.com/sagernet/sing-box@v1.9.0-rc.20/option/debug.go (about)

     1  package option
     2  
     3  import (
     4  	"github.com/sagernet/sing-box/common/humanize"
     5  	"github.com/sagernet/sing/common/json"
     6  )
     7  
     8  type DebugOptions struct {
     9  	Listen       string      `json:"listen,omitempty"`
    10  	GCPercent    *int        `json:"gc_percent,omitempty"`
    11  	MaxStack     *int        `json:"max_stack,omitempty"`
    12  	MaxThreads   *int        `json:"max_threads,omitempty"`
    13  	PanicOnFault *bool       `json:"panic_on_fault,omitempty"`
    14  	TraceBack    string      `json:"trace_back,omitempty"`
    15  	MemoryLimit  MemoryBytes `json:"memory_limit,omitempty"`
    16  	OOMKiller    *bool       `json:"oom_killer,omitempty"`
    17  }
    18  
    19  type MemoryBytes uint64
    20  
    21  func (l MemoryBytes) MarshalJSON() ([]byte, error) {
    22  	return json.Marshal(humanize.MemoryBytes(uint64(l)))
    23  }
    24  
    25  func (l *MemoryBytes) UnmarshalJSON(bytes []byte) error {
    26  	var valueInteger int64
    27  	err := json.Unmarshal(bytes, &valueInteger)
    28  	if err == nil {
    29  		*l = MemoryBytes(valueInteger)
    30  		return nil
    31  	}
    32  	var valueString string
    33  	err = json.Unmarshal(bytes, &valueString)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	parsedValue, err := humanize.ParseMemoryBytes(valueString)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	*l = MemoryBytes(parsedValue)
    42  	return nil
    43  }