Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use runtimex to replace choleraehyq/pid #1347

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CREDITS
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
github.com/stretchr/testify
github.com/choleraehyq/pid
github.com/stretchr/testify
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ require (
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b
github.com/bytedance/mockey v1.2.7
github.com/bytedance/sonic v1.11.6
github.com/choleraehyq/pid v0.0.18
github.com/cloudwego/configmanager v0.2.2
github.com/cloudwego/dynamicgo v0.2.4
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/frugal v0.1.15
github.com/cloudwego/localsession v0.0.2
github.com/cloudwego/netpoll v0.6.0
github.com/cloudwego/runtimex v0.1.0
github.com/cloudwego/thriftgo v0.3.6
github.com/golang/mock v1.6.0
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/choleraehyq/pid v0.0.18 h1:O7LLxPoOyt3YtonlCC8BmNrF9P6Hc8B509UOqlPSVhw=
github.com/choleraehyq/pid v0.0.18/go.mod h1:uhzeFgxJZWQsZulelVQZwdASxQ9TIPZYL4TPkQMtL/U=
joway marked this conversation as resolved.
Show resolved Hide resolved
github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY=
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
Expand Down Expand Up @@ -53,6 +52,8 @@ github.com/cloudwego/localsession v0.0.2 h1:N9/IDtCPj1fCL9bCTP+DbXx3f40YjVYWcwkJ
github.com/cloudwego/localsession v0.0.2/go.mod h1:kiJxmvAcy4PLgKtEnPS5AXed3xCiXcs7Z+KBHP72Wv8=
github.com/cloudwego/netpoll v0.6.0 h1:JRMkrA1o8k/4quxzg6Q1XM+zIhwZsyoWlq6ef+ht31U=
github.com/cloudwego/netpoll v0.6.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/runtimex v0.1.0 h1:HG+WxWoj5/CDChDZ7D99ROwvSMkuNXAqt6hnhTTZDiI=
github.com/cloudwego/runtimex v0.1.0/go.mod h1:23vL/HGV0W8nSCHbe084AgEBdDV4rvXenEUMnUNvUd8=
github.com/cloudwego/thriftgo v0.2.11/go.mod h1:dAyXHEmKXo0LfMCrblVEY3mUZsdeuA5+i0vF5f09j7E=
github.com/cloudwego/thriftgo v0.3.6 h1:gHHW8Ag3cAEQ/awP4emTJiRPr5yQjbANhcsmV8/Epbw=
github.com/cloudwego/thriftgo v0.3.6/go.mod h1:29ukiySoAMd0vXMYIduAY9dph/7dmChvOS11YLotFb8=
Expand Down
10 changes: 5 additions & 5 deletions pkg/utils/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ package utils
import (
"errors"
"runtime"

goid "github.com/choleraehyq/pid"
)

// ErrRingFull means the ring is full.
var ErrRingFull = errors.New("ring is full")

// Deprecated: it's not used by kitex anymore.
joway marked this conversation as resolved.
Show resolved Hide resolved
// NewRing creates a ringbuffer with fixed size.
func NewRing(size int) *Ring {
if size <= 0 {
Expand All @@ -52,6 +51,7 @@ func NewRing(size int) *Ring {
return r
}

// Deprecated: it's not used by kitex anymore.
// Ring implements a fixed size hash list to manage data
type Ring struct {
length int
Expand All @@ -64,7 +64,7 @@ func (r *Ring) Push(obj interface{}) error {
return r.rings[0].Push(obj)
}

idx := goid.GetPid() % r.length
idx := GoroutineID() % r.length
for i := 0; i < r.length; i, idx = i+1, (idx+1)%r.length {
err := r.rings[idx].Push(obj)
if err == nil {
Expand All @@ -80,7 +80,7 @@ func (r *Ring) Pop() interface{} {
return r.rings[0].Pop()
}

idx := goid.GetPid() % r.length
idx := GoroutineID() % r.length
for i := 0; i < r.length; i, idx = i+1, (idx+1)%r.length {
obj := r.rings[idx].Pop()
if obj != nil {
Expand All @@ -94,7 +94,7 @@ func (r *Ring) Pop() interface{} {
func (r *Ring) Dump() interface{} {
m := &ringDump{}
dumpList := make([]*ringDump, 0, r.length)
idx := goid.GetPid() % r.length
idx := GoroutineID() % r.length
for i := 0; i < r.length; i, idx = i+1, (idx+1)%r.length {
curDump := &ringDump{}
r.rings[idx].Dump(curDump)
Expand Down
34 changes: 34 additions & 0 deletions pkg/utils/runtimex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"github.com/bytedance/gopkg/lang/fastrand"
"github.com/cloudwego/runtimex"
)

// GoroutineID return current G's ID, it will fall back to get a random int
func GoroutineID() int {
joway marked this conversation as resolved.
Show resolved Hide resolved
joway marked this conversation as resolved.
Show resolved Hide resolved
gid, err := runtimex.GID()
joway marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
gid = fastrand.Int()
if gid < 0 {
gid = -gid
}
}
return gid
}
30 changes: 30 additions & 0 deletions pkg/utils/runtimex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"testing"

"github.com/cloudwego/runtimex"
)

func TestRuntimeXCompatibility(t *testing.T) {
_, err := runtimex.GID()
if err != nil {
t.Fatal("Your runtimex package is not compatible with current Go runtime version !!!!!!!!!")
}
}
Loading