Просмотр исходного кода

重写 TryLock 的实现,修复 #706

Signed-off-by: allan716 <[email protected]>
allan716 2 лет назад
Родитель
Сommit
7e19acb6d1

+ 2 - 2
internal/backend/controllers/base/controller_base.go

@@ -14,7 +14,7 @@ import (
 
 type ControllerBase struct {
 	fileDownloader   *file_downloader.FileDownloader
-	proxyCheckLocker lock.Lock
+	proxyCheckLocker *lock.Lock
 	restartSignal    chan interface{}
 	preJob           *pre_job.PreJob
 }
@@ -40,5 +40,5 @@ func (cb *ControllerBase) ErrorProcess(c *gin.Context, funcName string, err erro
 }
 
 func (cb *ControllerBase) Close() {
-	cb.proxyCheckLocker.Close()
+
 }

+ 1 - 2
internal/backend/controllers/v1/controller_base.go

@@ -26,7 +26,7 @@ type ControllerBase struct {
 	videoListHelper                     *video_list_helper.VideoListHelper
 	hslCenter                           *hls_center.Center
 	videoScanAndRefreshHelperIsRunning  bool
-	videoScanAndRefreshHelperLocker     lock.Lock
+	videoScanAndRefreshHelperLocker     *lock.Lock
 	videoScanAndRefreshHelperErrMessage string
 	restartSignal                       chan interface{}
 }
@@ -61,7 +61,6 @@ func (cb *ControllerBase) GetPathUrlMap() map[string]string {
 func (cb *ControllerBase) Close() {
 	cb.cronHelper.Stop()
 	cb.videoScanAndRefreshHelper.Cancel()
-	cb.videoScanAndRefreshHelperLocker.Close()
 }
 
 func (cb *ControllerBase) GetVersion() string {

+ 3 - 3
pkg/lock/example_test.go

@@ -1,11 +1,11 @@
-//go:build !windows && !plan9
-// +build !windows,!plan9
-
 package lock
 
+import "sync"
+
 func ExampleNewLock() {
 	var l = NewLock()
 	var wg sync.WaitGroup
+	var counter int
 	for i := 0; i < 10; i++ {
 		wg.Add(1)
 		go func() {

+ 18 - 18
pkg/lock/lock.go

@@ -1,34 +1,34 @@
 package lock
 
+import "sync"
+
 // Lock try lock
 type Lock struct {
-	c chan struct{}
+	isLocked bool
+	ll       sync.Mutex
 }
 
 // NewLock generate a try lock
-func NewLock() Lock {
+func NewLock() *Lock {
 	var l Lock
-	l.c = make(chan struct{}, 1)
-	l.c <- struct{}{}
-	return l
+	l.isLocked = false
+	return &l
 }
 
 // Lock try lock, return lock result
-func (l Lock) Lock() bool {
-	lockResult := false
-	select {
-	case <-l.c:
-		lockResult = true
-	default:
+func (l *Lock) Lock() bool {
+	l.ll.Lock()
+	defer l.ll.Unlock()
+	if l.isLocked {
+		return false
 	}
-	return lockResult
+	l.isLocked = true
+	return true
 }
 
 // Unlock , Unlock the try lock
-func (l Lock) Unlock() {
-	l.c <- struct{}{}
-}
-
-func (l Lock) Close() {
-	close(l.c)
+func (l *Lock) Unlock() {
+	l.ll.Lock()
+	defer l.ll.Unlock()
+	l.isLocked = false
 }

+ 27 - 0
pkg/lock/lock_test.go

@@ -0,0 +1,27 @@
+package lock
+
+import (
+	"sync"
+	"testing"
+)
+
+func TestNewLock(t *testing.T) {
+	var l = NewLock()
+	var wg sync.WaitGroup
+	var counter int
+	for i := 0; i < 10; i++ {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			if l.Lock() == false {
+				// log error
+				println("lock failed")
+				return
+			}
+			counter++
+			println("current counter", counter)
+			l.Unlock()
+		}()
+	}
+	wg.Wait()
+}