- 更新 README.md 中的项目结构说明 - 添加 pprof 性能分析工具的使用说明 - 更新 build.bat 文件,增加 proto 文件编译命令 - 升级 go-logr/logr 依赖至 v1.3.0 - 降级 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc 依赖至 v1.16.0 - 降级 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp 依赖至 v1.16.0 - 升级 go.opentelemetry.io/otel/trace 依赖至 v1.20.0 - 移除 logic/main.go 中的冗余代码 - 重构 logic/server.go 中的 Start 函数 - 更新 login/main.go 文件
82 lines
1.3 KiB
Go
82 lines
1.3 KiB
Go
package jsonrpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"math/rand"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
type param struct {
|
|
data []byte // from unmarshal
|
|
|
|
v reflect.Value // to marshal
|
|
}
|
|
|
|
func (p *param) UnmarshalJSON(raw []byte) error {
|
|
p.data = make([]byte, len(raw))
|
|
copy(p.data, raw)
|
|
return nil
|
|
}
|
|
|
|
func (p *param) MarshalJSON() ([]byte, error) {
|
|
if p.v.Kind() == reflect.Invalid {
|
|
return p.data, nil
|
|
}
|
|
|
|
return json.Marshal(p.v.Interface())
|
|
}
|
|
|
|
// processFuncOut finds value and error Outs in function
|
|
func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) {
|
|
errOut = -1 // -1 if not found
|
|
valOut = -1
|
|
n = funcType.NumOut()
|
|
|
|
switch n {
|
|
case 0:
|
|
case 1:
|
|
if funcType.Out(0) == errorType {
|
|
errOut = 0
|
|
} else {
|
|
valOut = 0
|
|
}
|
|
case 2:
|
|
valOut = 0
|
|
errOut = 1
|
|
if funcType.Out(1) != errorType {
|
|
panic("expected error as second return value")
|
|
}
|
|
default:
|
|
errstr := fmt.Sprintf("too many return values: %s", funcType)
|
|
panic(errstr)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
type backoff struct {
|
|
minDelay time.Duration
|
|
maxDelay time.Duration
|
|
}
|
|
|
|
func (b *backoff) next(attempt int) time.Duration {
|
|
if attempt < 0 {
|
|
return b.minDelay
|
|
}
|
|
|
|
minf := float64(b.minDelay)
|
|
durf := minf * math.Pow(1.5, float64(attempt))
|
|
durf = durf + rand.Float64()*minf
|
|
|
|
delay := time.Duration(durf)
|
|
|
|
if delay > b.maxDelay {
|
|
return b.maxDelay
|
|
}
|
|
|
|
return delay
|
|
}
|