Skip to content

Commit 45791ef

Browse files
committed
fix: sync performance degradation caused by lock competition
1 parent c7a09b1 commit 45791ef

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

internal/adc/client/client.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import (
3939
)
4040

4141
type Client struct {
42-
mu sync.Mutex
42+
syncMu sync.Mutex
43+
mu sync.Mutex
4344
*cache.Store
4445

4546
executor ADCExecutor
@@ -74,9 +75,7 @@ type Task struct {
7475

7576
func (d *Client) Update(ctx context.Context, args Task) error {
7677
d.mu.Lock()
77-
defer d.mu.Unlock()
7878
deleteConfigs := d.ConfigManager.Update(args.Key, args.Configs)
79-
8079
for _, config := range deleteConfigs {
8180
if err := d.Store.Delete(config.Name, args.ResourceTypes, args.Labels); err != nil {
8281
log.Errorw("failed to delete resources from store",
@@ -96,6 +95,7 @@ func (d *Client) Update(ctx context.Context, args Task) error {
9695
return err
9796
}
9897
}
98+
d.mu.Unlock()
9999

100100
if len(deleteConfigs) > 0 {
101101
err := d.sync(ctx, Task{
@@ -140,8 +140,6 @@ func (d *Client) UpdateConfig(ctx context.Context, args Task) error {
140140

141141
func (d *Client) Delete(ctx context.Context, args Task) error {
142142
d.mu.Lock()
143-
defer d.mu.Unlock()
144-
145143
configs := d.ConfigManager.Get(args.Key)
146144
d.ConfigManager.Delete(args.Key)
147145

@@ -154,6 +152,7 @@ func (d *Client) Delete(ctx context.Context, args Task) error {
154152
return err
155153
}
156154
}
155+
d.mu.Unlock()
157156

158157
return d.sync(ctx, Task{
159158
Labels: args.Labels,
@@ -183,8 +182,8 @@ func (d *Client) DeleteConfig(ctx context.Context, args Task) error {
183182
}
184183

185184
func (c *Client) Sync(ctx context.Context) (map[string]types.ADCExecutionErrors, error) {
186-
c.mu.Lock()
187-
defer c.mu.Unlock()
185+
c.syncMu.Lock()
186+
defer c.syncMu.Unlock()
188187
log.Debug("syncing all resources")
189188

190189
configs := c.ConfigManager.List()
@@ -194,16 +193,12 @@ func (c *Client) Sync(ctx context.Context) (map[string]types.ADCExecutionErrors,
194193
return nil, nil
195194
}
196195

197-
cfg := map[string]adctypes.Config{}
198-
for _, config := range configs {
199-
cfg[config.Name] = config
200-
}
201-
202-
log.Debugw("syncing resources with multiple configs", zap.Any("configs", cfg))
196+
log.Debugw("syncing resources with multiple configs", zap.Any("configs", configs))
203197

204198
failedMap := map[string]types.ADCExecutionErrors{}
205199
var failedConfigs []string
206-
for name, config := range cfg {
200+
for _, config := range configs {
201+
name := config.Name
207202
resources, err := c.GetResources(name)
208203
if err != nil {
209204
log.Errorw("failed to get resources from store", zap.String("name", name), zap.Error(err))

internal/adc/client/executor.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ type DefaultADCExecutor struct {
5252
}
5353

5454
func (e *DefaultADCExecutor) Execute(ctx context.Context, mode string, config adctypes.Config, args []string) error {
55-
e.Lock()
56-
defer e.Unlock()
57-
5855
return e.runADC(ctx, mode, config, args)
5956
}
6057

@@ -225,7 +222,6 @@ type ADCServerOpts struct {
225222

226223
// HTTPADCExecutor implements ADCExecutor interface using HTTP calls to ADC Server
227224
type HTTPADCExecutor struct {
228-
sync.Mutex
229225
httpClient *http.Client
230226
serverURL string
231227
}
@@ -242,9 +238,6 @@ func NewHTTPADCExecutor(serverURL string, timeout time.Duration) *HTTPADCExecuto
242238

243239
// Execute implements the ADCExecutor interface using HTTP calls
244240
func (e *HTTPADCExecutor) Execute(ctx context.Context, mode string, config adctypes.Config, args []string) error {
245-
e.Lock()
246-
defer e.Unlock()
247-
248241
return e.runHTTPSync(ctx, mode, config, args)
249242
}
250243

0 commit comments

Comments
 (0)