types.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "strconv"
  19. "strings"
  20. )
  21. const (
  22. MB = 1024 * 1024
  23. KB = 1024
  24. BandwidthLimitModeClient = "client"
  25. BandwidthLimitModeServer = "server"
  26. )
  27. type BandwidthQuantity struct {
  28. s string // MB or KB
  29. i int64 // bytes
  30. }
  31. func NewBandwidthQuantity(s string) (BandwidthQuantity, error) {
  32. q := BandwidthQuantity{}
  33. err := q.UnmarshalString(s)
  34. if err != nil {
  35. return q, err
  36. }
  37. return q, nil
  38. }
  39. func MustBandwidthQuantity(s string) BandwidthQuantity {
  40. q := BandwidthQuantity{}
  41. err := q.UnmarshalString(s)
  42. if err != nil {
  43. panic(err)
  44. }
  45. return q
  46. }
  47. func (q *BandwidthQuantity) Equal(u *BandwidthQuantity) bool {
  48. if q == nil && u == nil {
  49. return true
  50. }
  51. if q != nil && u != nil {
  52. return q.i == u.i
  53. }
  54. return false
  55. }
  56. func (q *BandwidthQuantity) String() string {
  57. return q.s
  58. }
  59. func (q *BandwidthQuantity) UnmarshalString(s string) error {
  60. s = strings.TrimSpace(s)
  61. if s == "" {
  62. return nil
  63. }
  64. var (
  65. base int64
  66. f float64
  67. err error
  68. )
  69. switch {
  70. case strings.HasSuffix(s, "MB"):
  71. base = MB
  72. fstr := strings.TrimSuffix(s, "MB")
  73. f, err = strconv.ParseFloat(fstr, 64)
  74. if err != nil {
  75. return err
  76. }
  77. case strings.HasSuffix(s, "KB"):
  78. base = KB
  79. fstr := strings.TrimSuffix(s, "KB")
  80. f, err = strconv.ParseFloat(fstr, 64)
  81. if err != nil {
  82. return err
  83. }
  84. default:
  85. return errors.New("unit not support")
  86. }
  87. q.s = s
  88. q.i = int64(f * float64(base))
  89. return nil
  90. }
  91. func (q *BandwidthQuantity) UnmarshalJSON(b []byte) error {
  92. if len(b) == 4 && string(b) == "null" {
  93. return nil
  94. }
  95. var str string
  96. err := json.Unmarshal(b, &str)
  97. if err != nil {
  98. return err
  99. }
  100. return q.UnmarshalString(str)
  101. }
  102. func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
  103. return []byte("\"" + q.s + "\""), nil
  104. }
  105. func (q *BandwidthQuantity) Bytes() int64 {
  106. return q.i
  107. }