mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-05 07:57:00 +08:00
feat: add example
This commit is contained in:
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -9,3 +10,18 @@ func CalculateJitter(baseDelay time.Duration, percent float64) time.Duration {
|
||||
jitter := time.Duration(rand.Float64()*percent*float64(baseDelay)) - time.Duration(percent*float64(baseDelay)/2)
|
||||
return baseDelay + jitter
|
||||
}
|
||||
|
||||
func SizeOf(v any) uintptr {
|
||||
val := reflect.ValueOf(v)
|
||||
if val.Kind() == reflect.Ptr {
|
||||
val = val.Elem()
|
||||
}
|
||||
switch val.Kind() {
|
||||
case reflect.Slice:
|
||||
return uintptr(val.Len()) * val.Type().Elem().Size()
|
||||
case reflect.Map:
|
||||
return uintptr(val.Len()) * (val.Type().Key().Size() + val.Type().Elem().Size())
|
||||
default:
|
||||
return val.Type().Size()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user