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

     1  package option
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/dustin/go-humanize"
     7  )
     8  
     9  type DebugOptions struct {
    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  BytesLength `json:"memory_limit,omitempty"`
    16  	OOMKiller    *bool       `json:"oom_killer,omitempty"`
    17  }
    18  
    19  type BytesLength int64
    20  
    21  func (l BytesLength) MarshalJSON() ([]byte, error) {
    22  	return json.Marshal(humanize.IBytes(uint64(l)))
    23  }
    24  
    25  func (l *BytesLength) UnmarshalJSON(bytes []byte) error {
    26  	var valueInteger int64
    27  	err := json.Unmarshal(bytes, &valueInteger)
    28  	if err == nil {
    29  		*l = BytesLength(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.ParseBytes(valueString)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	*l = BytesLength(parsedValue)
    42  	return nil
    43  }