Skip to content

Commit

Permalink
chore: use runtimex to replace choleraehyq/pid
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed May 13, 2024
1 parent 43aad35 commit 9558cd3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
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=
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
8 changes: 3 additions & 5 deletions pkg/utils/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package utils
import (
"errors"
"runtime"

goid "github.com/choleraehyq/pid"
)

// ErrRingFull means the ring is full.
Expand Down Expand Up @@ -64,7 +62,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 +78,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 +92,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
30 changes: 30 additions & 0 deletions pkg/utils/runtimex.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 (
"github.com/bytedance/gopkg/lang/fastrand"
"github.com/cloudwego/runtimex"
)

func GoroutineID() int {
gid, err := runtimex.GID()
if err != nil {
gid = fastrand.Int()
}
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 !!!!!!!!!")
}
}

0 comments on commit 9558cd3

Please sign in to comment.