Browse Source

调整请求超时时间

pull/40/head
Sydonian 1 year ago
parent
commit
d5c921dee6
3 changed files with 115 additions and 2 deletions
  1. +1
    -1
      pkgs/mq/client.go
  2. +1
    -1
      sdks/storage/models.go
  3. +113
    -0
      utils/time2/measurement.go

+ 1
- 1
pkgs/mq/client.go View File

@@ -300,7 +300,7 @@ func (c *RabbitMQTransport) Close() error {

// 发送消息并等待回应。因为无法自动推断出TResp的类型,所以将其放在第一个手工填写,之后的TBody可以自动推断出来
func Request[TSvc any, TReq MessageBody, TResp MessageBody](_ func(svc TSvc, msg TReq) (TResp, *CodeMessage), cli RoundTripper, req TReq, opts ...RequestOption) (TResp, error) {
opt := RequestOption{Timeout: time.Second * 15}
opt := RequestOption{Timeout: time.Second * 15, KeepAlive: true}
if len(opts) > 0 {
opt = opts[0]
}


+ 1
- 1
sdks/storage/models.go View File

@@ -71,7 +71,7 @@ func (b *RepRedundancy) Value() (driver.Value, error) {
return serder.ObjectToJSONEx[Redundancy](b)
}

var DefaultECRedundancy = *NewECRedundancy(2, 3, 1024*1024*5)
var DefaultECRedundancy = *NewECRedundancy(3, 5, 1024*1024*5)

type ECRedundancy struct {
serder.Metadata `union:"ec"`


+ 113
- 0
utils/time2/measurement.go View File

@@ -0,0 +1,113 @@
package time2

import (
"fmt"
"path"
"runtime"
"strings"
"time"
)

type Measurement struct {
startTime time.Time
lastPointTime time.Time
printer func(string)
on bool
title string
}

func NewMeasurement(printer func(string)) Measurement {
return Measurement{
printer: printer,
}
}

func (m *Measurement) Begin(on bool, title ...string) {
if m == nil {
return
}

m.on = on
m.title = strings.Join(title, ".")

if on {
m.startTime = time.Now()
m.lastPointTime = m.startTime

_, file, line, ok := runtime.Caller(1)

titlePart := ""
if m.title != "" {
titlePart = fmt.Sprintf(":%s", m.title)
}

if ok {
m.printer(fmt.Sprintf("[BEGIN%v]<%v:%v>", titlePart, path.Base(file), line))
} else {
m.printer(fmt.Sprintf("[BEGIN%v]<UNKNOWN>", titlePart))
}
}
}

func (m *Measurement) Point(desc ...string) {
if m == nil {
return
}

if m.on {
m.printer(m.makePointString(strings.Join(desc, ".")))
}
}

func (m *Measurement) makePointString(desc string) string {
last := m.lastPointTime
now := time.Now()
m.lastPointTime = now

_, file, line, ok := runtime.Caller(2)

titlePart := ""
if m.title != "" {
titlePart = fmt.Sprintf("(%s)", m.title)
}

if desc != "" {
desc = fmt.Sprintf("@%s", desc)
}

if ok {
return fmt.Sprintf("%v {%v/%v} %v<%v:%v>", titlePart, now.Sub(last), now.Sub(m.startTime), desc, path.Base(file), line)
}

return fmt.Sprintf("{%v/%v}%v<UNKNOWN>", now.Sub(last), now.Sub(m.startTime), desc)
}

func (m *Measurement) End(descs ...string) {
if m == nil {
return
}

if m.on {
last := m.lastPointTime
now := time.Now()
m.lastPointTime = now

_, file, line, ok := runtime.Caller(1)

titlePart := ""
if m.title != "" {
titlePart = fmt.Sprintf(":%s", m.title)
}

desc := strings.Join(descs, ".")
if desc != "" {
desc = fmt.Sprintf("@%s", desc)
}

if ok {
m.printer(fmt.Sprintf("[END%v] {%v/%v} %v<%v:%v>", titlePart, now.Sub(last), now.Sub(m.startTime), desc, path.Base(file), line))
} else {
m.printer(fmt.Sprintf("[END%v] {%v/%v} %v<UNKNOWN>", titlePart, now.Sub(last), now.Sub(m.startTime), desc))
}
}
}

Loading…
Cancel
Save