@@ -30,6 +30,7 @@ import (
3030 "github.com/mohanson/daze/lib/doa"
3131 "github.com/mohanson/daze/lib/lru"
3232 "github.com/mohanson/daze/lib/pretty"
33+ "github.com/mohanson/daze/lib/rate"
3334)
3435
3536// ============================================================================
@@ -1042,18 +1043,6 @@ func OpenFile(name string) (io.ReadCloser, error) {
10421043 return os .Open (name )
10431044}
10441045
1045- // RandomReader is a simple random number generator. Note that it is not cryptographically secure, but for daze, the
1046- // randomness it provides is enough.
1047- type RandomReader struct {}
1048-
1049- // Read implements io.Reader.
1050- func (r * RandomReader ) Read (p []byte ) (int , error ) {
1051- for i := range len (p ) {
1052- p [i ] = byte (rand .Uint64 ())
1053- }
1054- return len (p ), nil
1055- }
1056-
10571046// The PrettyReader struct represents a custom reader that keeps track of read bytes and prints progress.
10581047type PrettyReader struct {
10591048 E uint64 // Total number of bytes read so far
@@ -1072,12 +1061,66 @@ func (r *PrettyReader) Read(p []byte) (int, error) {
10721061 return n , err
10731062}
10741063
1064+ // RandomReader is a simple random number generator. Note that it is not cryptographically secure, but for daze, the
1065+ // randomness it provides is enough.
1066+ type RandomReader struct {}
1067+
1068+ // Read implements io.Reader.
1069+ func (r * RandomReader ) Read (p []byte ) (int , error ) {
1070+ for i := range len (p ) {
1071+ p [i ] = byte (rand .Uint64 ())
1072+ }
1073+ return len (p ), nil
1074+ }
1075+
1076+ // RateConn wraps a net.Conn with a per-conn and a rate limiter.
1077+ type RateConn struct {
1078+ Conn io.ReadWriteCloser
1079+ Rate * rate.Limiter
1080+ }
1081+
1082+ // Close closes the connection.
1083+ func (r * RateConn ) Close () error {
1084+ return r .Conn .Close ()
1085+ }
1086+
1087+ // Read reads up to len(p) bytes into p.
1088+ func (r * RateConn ) Read (p []byte ) (int , error ) {
1089+ n , err := r .Conn .Read (p )
1090+ r .Rate .WaitN (context .Background (), n )
1091+ return n , err
1092+ }
1093+
1094+ // Write writes len(p) bytes from p to the underlying data stream.
1095+ func (r * RateConn ) Write (p []byte ) (int , error ) {
1096+ n , err := r .Conn .Write (p )
1097+ r .Rate .WaitN (context .Background (), n )
1098+ return n , err
1099+ }
1100+
10751101// Salt converts the stupid password passed in by the user to 32-sized byte array.
10761102func Salt (s string ) []byte {
10771103 h := sha256 .Sum256 ([]byte (s ))
10781104 return h [:]
10791105}
10801106
1107+ // SizeParser converts a string like "1K", "1M", or "1G" to bytes as uint64. It expects the input string to end with a
1108+ // unit (K, M, or G) and panics if the unit is invalid. The number part can be a float (e.g., "1.5M") and is converted
1109+ // to bytes based on the unit.
1110+ func SizeParser (s string ) uint64 {
1111+ f := doa .Try (strconv .ParseFloat (s [:len (s )- 1 ], 64 ))
1112+ u := strings .ToLower (s [len (s )- 1 :])
1113+ switch u {
1114+ case "k" :
1115+ return uint64 (f * 1024 )
1116+ case "m" :
1117+ return uint64 (f * 1024 * 1024 )
1118+ case "g" :
1119+ return uint64 (f * 1024 * 1024 * 1024 )
1120+ }
1121+ panic ("unreachable" )
1122+ }
1123+
10811124// ============================================================================
10821125// ___ ___ ___ ___
10831126// /\ \ /\ \ /\ \ /\ \
0 commit comments