123456789101112131415161718192021222324252627282930 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package id
- import (
- "context"
- "fmt"
- "strconv"
- )
- // GetUniqueID 获取唯一id (分布式)
- func GetUniqueID() (uint64, error) {
- if sf == nil {
- return 0, fmt.Errorf("sf is nil.")
- }
- return sf.NextID()
- }
- // GetRequestId 生成request id
- // 如果开启了jaeger trace,则使用其生成的trace id 作为request id
- func GetRequestId(ctx context.Context) string {
- if rid, ok := ctx.Value("RequestId").(string); ok && rid != "" {
- return rid
- }
- id, _ := GetUniqueID()
- newId := strconv.Itoa(int(id))
- ctx = context.WithValue(ctx, "RequestId", newId)
- return newId
- }
|