-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement(hy2): support human-readable bandwidth configuration
Fixes #665
- Loading branch information
Showing
6 changed files
with
128 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
* Copyright (c) 2024, daeuniverse Organization <[email protected]> | ||
*/ | ||
|
||
package bandwidth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// UnitValue is a wrapper for uint64 to support custom unmarshaler. | ||
type UnitValue uint64 | ||
|
||
// UnmarshalText parses a string into a UnitValue. | ||
func (b *UnitValue) UnmarshalText(text []byte) error { | ||
value, err := ConvBandwidth(string(text)) | ||
if err != nil { | ||
return err | ||
} | ||
*b = UnitValue(value) | ||
return nil | ||
} | ||
|
||
// Uint64 returns the underlying uint64 value. | ||
func (b UnitValue) Uint64() uint64 { | ||
return uint64(b) | ||
} | ||
|
||
// /////// Code reference from https://github.com/apernet/hysteria/blob/21ea2a0/app/internal/utils/bpsconv.go | ||
const ( | ||
Byte = 1 | ||
Kilobyte = Byte * 1000 | ||
Megabyte = Kilobyte * 1000 | ||
Gigabyte = Megabyte * 1000 | ||
Terabyte = Gigabyte * 1000 | ||
) | ||
|
||
// StringToBps converts a string to a bandwidth value in bytes per second. | ||
// E.g. "100 Mbps", "512 kbps", "1g" are all valid. | ||
func StringToBps(s string) (uint64, error) { | ||
s = strings.ToLower(strings.TrimSpace(s)) | ||
spl := 0 | ||
for i, c := range s { | ||
if c < '0' || c > '9' { | ||
spl = i | ||
break | ||
} | ||
} | ||
if spl == 0 { | ||
// No unit or no value | ||
return 0, errors.New("invalid format") | ||
} | ||
v, err := strconv.ParseUint(s[:spl], 10, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
unit := strings.TrimSpace(s[spl:]) | ||
|
||
switch strings.ToLower(unit) { | ||
case "b", "bps": | ||
return v * Byte / 8, nil | ||
case "k", "kb", "kbps": | ||
return v * Kilobyte / 8, nil | ||
case "m", "mb", "mbps": | ||
return v * Megabyte / 8, nil | ||
case "g", "gb", "gbps": | ||
return v * Gigabyte / 8, nil | ||
case "t", "tb", "tbps": | ||
return v * Terabyte / 8, nil | ||
default: | ||
return 0, errors.New("unsupported unit") | ||
} | ||
} | ||
|
||
// ConvBandwidth handles both string and int types for bandwidth. | ||
// When using string, it will be parsed as a bandwidth string with units. | ||
// When using int, it will be parsed as a raw bandwidth in bytes per second. | ||
// It does NOT support float types. | ||
func ConvBandwidth(bw interface{}) (uint64, error) { | ||
switch bwT := bw.(type) { | ||
case string: | ||
return StringToBps(bwT) | ||
case int: | ||
return uint64(bwT), nil | ||
default: | ||
return 0, fmt.Errorf("invalid type %T for bandwidth", bwT) | ||
} | ||
} | ||
|
||
// /////// reference end |