diff --git a/pkgs/mq/client.go b/pkgs/mq/client.go index 078cdc2..8709347 100644 --- a/pkgs/mq/client.go +++ b/pkgs/mq/client.go @@ -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] } diff --git a/sdks/storage/models.go b/sdks/storage/models.go index 9c60ce1..577479b 100644 --- a/sdks/storage/models.go +++ b/sdks/storage/models.go @@ -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"` diff --git a/utils/time2/measurement.go b/utils/time2/measurement.go new file mode 100644 index 0000000..f5f77c9 --- /dev/null +++ b/utils/time2/measurement.go @@ -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]", 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", 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", titlePart, now.Sub(last), now.Sub(m.startTime), desc)) + } + } +}