You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/nazasync/goid.go

34 lines
779 B
Go

// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package nazasync
import (
"bytes"
"errors"
"runtime"
"strconv"
)
// NOTICE copy from https://github.com/golang/net/blob/master/http2/gotrack.go
var ErrObtainGoroutineID = errors.New("nazasync: obtain current goroutine id failed")
func CurGoroutineID() (int64, error) {
var goroutineSpace = []byte("goroutine ")
b := make([]byte, 128)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, goroutineSpace)
i := bytes.IndexByte(b, ' ')
if i < 0 {
return -1, ErrObtainGoroutineID
}
return strconv.ParseInt(string(b[:i]), 10, 64)
}