Skip to content

Commit 56871ef

Browse files
committed
modified the locks and unlocks to be consistent
1 parent f9eb1cd commit 56871ef

File tree

1 file changed

+70
-32
lines changed

1 file changed

+70
-32
lines changed

client/nginx.go

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,10 @@ func (client *NginxClient) getWithContext(ctx context.Context, path string, data
965965
}
966966

967967
func (client *NginxClient) post(path string, input interface{}) error {
968-
return client.postWithConext(context.Background(), path, input)
968+
return client.postWithContext(context.Background(), path, input)
969969
}
970970

971-
func (client *NginxClient) postWithConext(ctx context.Context, path string, input interface{}) error {
971+
func (client *NginxClient) postWithContext(ctx context.Context, path string, input interface{}) error {
972972
ctx, cancel := context.WithTimeout(ctx, client.ctxTimeout)
973973
defer cancel()
974974

@@ -1259,156 +1259,183 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
12591259
stats := defaultStats()
12601260
// Collecting initial stats
12611261
initialGroup.Go(func() error {
1262-
mu.Lock()
12631262
endpoints, err := client.GetAvailableEndpointsWithContext(initialCtx)
1264-
mu.Unlock()
12651263
if err != nil {
12661264
return fmt.Errorf("failed to get available Endpoints: %w", err)
12671265
}
1266+
1267+
mu.Lock()
12681268
stats.endpoints = endpoints
1269+
mu.Unlock()
12691270
return nil
12701271
})
12711272

12721273
initialGroup.Go(func() error {
1273-
mu.Lock()
12741274
nginxInfo, err := client.GetNginxInfoWithContext(initialCtx)
1275-
mu.Unlock()
12761275
if err != nil {
12771276
return fmt.Errorf("failed to get NGINX info: %w", err)
12781277
}
1278+
1279+
mu.Lock()
12791280
stats.NginxInfo = *nginxInfo
1281+
mu.Unlock()
1282+
12801283
return nil
12811284
})
12821285

12831286
initialGroup.Go(func() error {
1284-
mu.Lock()
12851287
caches, err := client.GetCachesWithContext(initialCtx)
1286-
mu.Unlock()
12871288
if err != nil {
12881289
return fmt.Errorf("failed to get Caches: %w", err)
12891290
}
1291+
1292+
mu.Lock()
12901293
stats.Caches = *caches
1294+
mu.Unlock()
1295+
12911296
return nil
12921297
})
12931298

12941299
initialGroup.Go(func() error {
1295-
mu.Lock()
12961300
processes, err := client.GetProcessesWithContext(initialCtx)
1297-
mu.Unlock()
12981301
if err != nil {
12991302
return fmt.Errorf("failed to get Process information: %w", err)
13001303
}
1304+
1305+
mu.Lock()
13011306
stats.Processes = *processes
1307+
mu.Unlock()
1308+
13021309
return nil
13031310
})
13041311

13051312
initialGroup.Go(func() error {
1306-
mu.Lock()
13071313
slabs, err := client.GetSlabsWithContext(initialCtx)
1308-
mu.Unlock()
13091314
if err != nil {
13101315
return fmt.Errorf("failed to get Slabs: %w", err)
13111316
}
1317+
1318+
mu.Lock()
13121319
stats.Slabs = *slabs
1320+
mu.Unlock()
1321+
13131322
return nil
13141323
})
13151324

13161325
initialGroup.Go(func() error {
1317-
mu.Lock()
13181326
httpRequests, err := client.GetHTTPRequestsWithContext(initialCtx)
1319-
mu.Unlock()
13201327
if err != nil {
13211328
return fmt.Errorf("failed to get HTTP Requests: %w", err)
13221329
}
1330+
1331+
mu.Lock()
13231332
stats.HTTPRequests = *httpRequests
1333+
mu.Unlock()
1334+
13241335
return nil
13251336
})
13261337

13271338
initialGroup.Go(func() error {
1328-
mu.Lock()
13291339
ssl, err := client.GetSSLWithContext(initialCtx)
1330-
mu.Unlock()
13311340
if err != nil {
13321341
return fmt.Errorf("failed to get SSL: %w", err)
13331342
}
1343+
1344+
mu.Lock()
13341345
stats.SSL = *ssl
1346+
mu.Unlock()
1347+
13351348
return nil
13361349
})
13371350

13381351
initialGroup.Go(func() error {
1339-
mu.Lock()
13401352
serverZones, err := client.GetServerZonesWithContext(initialCtx)
1341-
mu.Unlock()
13421353
if err != nil {
13431354
return fmt.Errorf("failed to get Server Zones: %w", err)
13441355
}
1356+
1357+
mu.Lock()
13451358
stats.ServerZones = *serverZones
1359+
mu.Unlock()
1360+
13461361
return nil
13471362
})
13481363

13491364
initialGroup.Go(func() error {
1350-
mu.Lock()
13511365
upstreams, err := client.GetUpstreamsWithContext(initialCtx)
1352-
mu.Unlock()
13531366
if err != nil {
13541367
return fmt.Errorf("failed to get Upstreams: %w", err)
13551368
}
1369+
1370+
mu.Lock()
13561371
stats.Upstreams = *upstreams
1372+
mu.Unlock()
1373+
13571374
return nil
13581375
})
13591376

13601377
initialGroup.Go(func() error {
1361-
mu.Lock()
13621378
locationZones, err := client.GetLocationZonesWithContext(initialCtx)
1363-
mu.Unlock()
13641379
if err != nil {
13651380
return fmt.Errorf("failed to get Location Zones: %w", err)
13661381
}
1382+
1383+
mu.Lock()
13671384
stats.LocationZones = *locationZones
1385+
mu.Unlock()
1386+
13681387
return nil
13691388
})
13701389

13711390
initialGroup.Go(func() error {
1372-
mu.Lock()
13731391
resolvers, err := client.GetResolversWithContext(initialCtx)
1374-
mu.Unlock()
13751392
if err != nil {
13761393
return fmt.Errorf("failed to get Resolvers: %w", err)
13771394
}
1395+
1396+
mu.Lock()
13781397
stats.Resolvers = *resolvers
1398+
mu.Unlock()
1399+
13791400
return nil
13801401
})
13811402

13821403
initialGroup.Go(func() error {
1383-
mu.Lock()
13841404
httpLimitRequests, err := client.GetHTTPLimitReqsWithContext(initialCtx)
1385-
mu.Unlock()
13861405
if err != nil {
13871406
return fmt.Errorf("failed to get HTTPLimitRequests: %w", err)
13881407
}
1408+
1409+
mu.Lock()
13891410
stats.HTTPLimitRequests = *httpLimitRequests
1411+
mu.Unlock()
1412+
13901413
return nil
13911414
})
13921415

13931416
initialGroup.Go(func() error {
1394-
mu.Lock()
13951417
httpLimitConnections, err := client.GetHTTPConnectionsLimitWithContext(initialCtx)
1396-
mu.Unlock()
13971418
if err != nil {
13981419
return fmt.Errorf("failed to get HTTPLimitConnections: %w", err)
13991420
}
1421+
1422+
mu.Lock()
14001423
stats.HTTPLimitConnections = *httpLimitConnections
1424+
mu.Unlock()
1425+
14011426
return nil
14021427
})
14031428

14041429
initialGroup.Go(func() error {
1405-
mu.Lock()
14061430
workers, err := client.GetWorkersWithContext(initialCtx)
1407-
mu.Unlock()
14081431
if err != nil {
14091432
return fmt.Errorf("failed to get Workers: %w", err)
14101433
}
1434+
1435+
mu.Lock()
14111436
stats.Workers = workers
1437+
mu.Unlock()
1438+
14121439
return nil
14131440
})
14141441

@@ -1421,13 +1448,15 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
14211448
availableStreamGroup, asgCtx := errgroup.WithContext(ctx)
14221449

14231450
availableStreamGroup.Go(func() error {
1424-
mu.Lock()
14251451
streamEndpoints, err := client.GetAvailableStreamEndpointsWithContext(asgCtx)
1426-
mu.Unlock()
14271452
if err != nil {
14281453
return fmt.Errorf("failed to get available Stream Endpoints: %w", err)
14291454
}
1455+
1456+
mu.Lock()
14301457
stats.streamEndpoints = streamEndpoints
1458+
mu.Unlock()
1459+
14311460
return nil
14321461
})
14331462

@@ -1443,9 +1472,11 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
14431472
if err != nil {
14441473
return fmt.Errorf("failed to get streamServerZones: %w", err)
14451474
}
1475+
14461476
mu.Lock()
14471477
stats.StreamServerZones = *streamServerZones
14481478
mu.Unlock()
1479+
14491480
return nil
14501481
})
14511482
}
@@ -1456,6 +1487,7 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
14561487
if err != nil {
14571488
return fmt.Errorf("failed to get StreamUpstreams: %w", err)
14581489
}
1490+
14591491
mu.Lock()
14601492
stats.StreamUpstreams = *streamUpstreams
14611493
mu.Unlock()
@@ -1470,9 +1502,11 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
14701502
if err != nil {
14711503
return fmt.Errorf("failed to get StreamLimitConnections: %w", err)
14721504
}
1505+
14731506
mu.Lock()
14741507
stats.StreamLimitConnections = *streamConnectionsLimit
14751508
mu.Unlock()
1509+
14761510
return nil
14771511
})
14781512

@@ -1481,9 +1515,11 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
14811515
if err != nil {
14821516
return fmt.Errorf("failed to get StreamZoneSync: %w", err)
14831517
}
1518+
14841519
mu.Lock()
14851520
stats.StreamZoneSync = streamZoneSync
14861521
mu.Unlock()
1522+
14871523
return nil
14881524
})
14891525
}
@@ -1502,9 +1538,11 @@ func (client *NginxClient) GetStatsWithContext(ctx context.Context) (*Stats, err
15021538
if err != nil {
15031539
return fmt.Errorf("failed to get connections: %w", err)
15041540
}
1541+
15051542
mu.Lock()
15061543
stats.Connections = *connections
15071544
mu.Unlock()
1545+
15081546
return nil
15091547
})
15101548

0 commit comments

Comments
 (0)