Skip to content

Commit d4e4986

Browse files
committed
update at 2024-08-09 17:18:56
1 parent 8ca04a5 commit d4e4986

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

pkg/clb/listener.go

-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"sync"
87

98
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
109
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
@@ -65,22 +64,6 @@ func GetListenerByPort(ctx context.Context, region, lbId string, port int64, pro
6564

6665
const TkePodListenerName = "TKE-DEDICATED-POD"
6766

68-
var (
69-
lock sync.Mutex
70-
lbLockMap = make(map[string]*sync.Mutex)
71-
)
72-
73-
func getLbLock(lbId string) *sync.Mutex {
74-
lock.Lock()
75-
defer lock.Unlock()
76-
mux, ok := lbLockMap[lbId]
77-
if !ok {
78-
mux = &sync.Mutex{}
79-
lbLockMap[lbId] = mux
80-
}
81-
return mux
82-
}
83-
8467
func CreateListener(ctx context.Context, region string, req *clb.CreateListenerRequest) (id string, err error) {
8568
req.ListenerNames = []*string{common.StringPtr(TkePodListenerName)}
8669
client := GetClient(region)

pkg/clb/lock.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package clb
2+
3+
import "sync"
4+
5+
var (
6+
lock sync.Mutex
7+
lbLockMap = make(map[string]*sync.Mutex)
8+
)
9+
10+
func getLbLock(lbId string) *sync.Mutex {
11+
lock.Lock()
12+
defer lock.Unlock()
13+
mux, ok := lbLockMap[lbId]
14+
if !ok {
15+
mux = &sync.Mutex{}
16+
lbLockMap[lbId] = mux
17+
}
18+
return mux
19+
}

pkg/clb/target.go

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ func (t Target) String() string {
103103
}
104104

105105
func DeregisterAllTargets(ctx context.Context, region, lbId, listenerId string) error {
106+
mux := getLbLock(lbId)
107+
mux.Lock()
108+
defer mux.Unlock()
106109
queryReq := clb.NewDescribeTargetsRequest()
107110
queryReq.LoadBalancerId = &lbId
108111
queryReq.ListenerIds = []*string{&listenerId}
@@ -129,6 +132,9 @@ func DeregisterAllTargets(ctx context.Context, region, lbId, listenerId string)
129132
}
130133

131134
func DeregisterTargetsForListener(ctx context.Context, region, lbId, listenerId string, targets ...Target) error {
135+
mux := getLbLock(lbId)
136+
mux.Lock()
137+
defer mux.Unlock()
132138
clbTargets := getClbTargets(targets)
133139
req := clb.NewDeregisterTargetsRequest()
134140
req.LoadBalancerId = &lbId
@@ -153,6 +159,9 @@ func getClbTargets(targets []Target) (clbTargets []*clb.Target) {
153159
}
154160

155161
func RegisterTargets(ctx context.Context, region, lbId, listenerId string, targets ...Target) error {
162+
mux := getLbLock(lbId)
163+
mux.Lock()
164+
defer mux.Unlock()
156165
clbTargets := getClbTargets(targets)
157166
req := clb.NewRegisterTargetsRequest()
158167
req.LoadBalancerId = &lbId

0 commit comments

Comments
 (0)