11 Star 11 Fork 0

Gitee 极速下载 / goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
xray.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
Nitin 提交于 2019-03-07 14:26 . Godoc updates for transport pkgs (#2028)
// Package xray contains the AWS X-Ray segment document type populated by the
// transport-specific X-Ray middleware.
package xray
import (
"context"
"crypto/rand"
"fmt"
"net"
"sync"
"time"
)
const (
// SegKey is the request context key used to store the segments if any.
SegKey contextKey = iota + 1
)
type (
// private type used to define context keys.
contextKey int
)
// Connect creates a goroutine to periodically re-dial a connection, so the
// hostname can be re-resolved if the IP changes. Returns a func that provides
// the latest Conn value.
func Connect(ctx context.Context, renewPeriod time.Duration, dial func() (net.Conn, error)) (func() net.Conn, error) {
var (
err error
// guard access to c
mu sync.RWMutex
c net.Conn
)
// get an initial connection
if c, err = dial(); err != nil {
return nil, err
}
// periodically re-dial
go func() {
ticker := time.NewTicker(renewPeriod)
for {
select {
case <-ticker.C:
newConn, err := dial()
if err != nil {
continue // we don't have anything better to replace `c` with
}
mu.Lock()
c = newConn
mu.Unlock()
case <-ctx.Done():
return
}
}
}()
return func() net.Conn {
mu.RLock()
defer mu.RUnlock()
return c
}, nil
}
// NewID is a span ID creation algorithm which produces values that are
// compatible with AWS X-Ray.
func NewID() string {
b := make([]byte, 8)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
// NewTraceID is a trace ID creation algorithm which produces values that are
// compatible with AWS X-Ray.
func NewTraceID() string {
b := make([]byte, 12)
rand.Read(b)
return fmt.Sprintf("%d-%x-%s", 1, time.Now().Unix(), fmt.Sprintf("%x", b))
}
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助

53164aa7 5694891 3bd8fe86 5694891