debug.go 1.1 KB

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