2021-02-11 05:28:32 +08:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8/internal/proto"
|
|
|
|
"github.com/go-redis/redis/v8/internal/util"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Sleep(ctx context.Context, dur time.Duration) error {
|
2021-04-23 08:08:53 +08:00
|
|
|
_, span := StartSpan(ctx, "time.Sleep")
|
|
|
|
defer span.End()
|
2021-02-11 05:28:32 +08:00
|
|
|
|
2021-04-23 08:08:53 +08:00
|
|
|
t := time.NewTimer(dur)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2021-02-11 05:28:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func ToLower(s string) string {
|
|
|
|
if isLower(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, len(s))
|
|
|
|
for i := range b {
|
|
|
|
c := s[i]
|
|
|
|
if c >= 'A' && c <= 'Z' {
|
|
|
|
c += 'a' - 'A'
|
|
|
|
}
|
|
|
|
b[i] = c
|
|
|
|
}
|
|
|
|
return util.BytesToString(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLower(s string) bool {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
c := s[i]
|
|
|
|
if c >= 'A' && c <= 'Z' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
var tracer = otel.Tracer("github.com/go-redis/redis")
|
|
|
|
|
2021-04-23 08:08:53 +08:00
|
|
|
func StartSpan(ctx context.Context, name string) (context.Context, trace.Span) {
|
2021-02-11 05:28:32 +08:00
|
|
|
if span := trace.SpanFromContext(ctx); !span.IsRecording() {
|
2021-04-23 08:08:53 +08:00
|
|
|
return ctx, span
|
2021-02-11 05:28:32 +08:00
|
|
|
}
|
2021-04-23 08:08:53 +08:00
|
|
|
return tracer.Start(ctx, name)
|
2021-02-11 05:28:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func RecordError(ctx context.Context, span trace.Span, err error) error {
|
|
|
|
if err != proto.Nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|