-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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 goroutinelock implements goroutine locks. | ||
package goroutinelock | ||
|
||
import "sync" | ||
|
||
// GoroutineWg is used to implement goroutine locks. | ||
var GoroutineWg = &sync.WaitGroup{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2021 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 goroutinelock "github.com/cloudwego/kitex/internal/utils/goroutine_lock" | ||
|
||
// GoroutineLock locks the goroutine so that graceful shutdown will wait until the lock is released | ||
func GoroutineLock() { | ||
goroutinelock.GoroutineWg.Add(1) | ||
} | ||
|
||
// GoroutineUnlock unlocks the goroutine to allow graceful shutdown to continue. | ||
// NOTE: This function should be executed using defer to avoid panic in the middle and causing the lock to not be | ||
// released. | ||
func GoroutineUnlock() { | ||
goroutinelock.GoroutineWg.Done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2021 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_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cloudwego/kitex/internal/utils/goroutine_lock" | ||
"github.com/cloudwego/kitex/pkg/utils" | ||
) | ||
|
||
func TestGoroutineLockAndUnlock(t *testing.T) { | ||
t.Parallel() | ||
startTime := time.Now() | ||
go func() { | ||
utils.GoroutineLock() | ||
time.Sleep(time.Second) | ||
utils.GoroutineUnlock() | ||
}() | ||
goroutinelock.GoroutineWg.Wait() | ||
diff := time.Since(startTime) | ||
if diff < time.Second { | ||
t.Errorf("Expect diff >= 1s, get %v", diff) | ||
} | ||
} |