Skip to content

Commit 8ddf70d

Browse files
committed
removed key for lock
1 parent 7a832b6 commit 8ddf70d

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

concurrency/handler.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,42 @@ type ConcurrencyHandler struct {
1313
sem chan struct{}
1414
logger *zap.SugaredLogger
1515
AcquisitionTimes []time.Duration
16-
lock sync.Mutex
1716
lastTokenAcquisitionTime time.Time
1817
Metrics *ConcurrencyMetrics
18+
sync.Mutex
1919
}
2020

2121
// ConcurrencyMetrics captures various metrics related to managing concurrency for the client's interactions with the API.
2222
type ConcurrencyMetrics struct {
23-
TotalRequests int64 // Total number of requests made
24-
TotalRetries int64 // Total number of retry attempts
25-
TotalRateLimitErrors int64 // Total number of rate limit errors encountered
26-
PermitWaitTime time.Duration // Total time spent waiting for tokens
27-
TTFB struct { // Metrics related to Time to First Byte (TTFB)
28-
Total time.Duration // Total Time to First Byte (TTFB) for all requests
29-
Count int64 // Count of requests used for calculating TTFB
30-
Lock sync.Mutex // Lock for TTFB metrics
23+
TotalRequests int64
24+
TotalRetries int64
25+
TotalRateLimitErrors int64
26+
PermitWaitTime time.Duration
27+
sync.Mutex
28+
TTFB struct {
29+
Total time.Duration
30+
Count int64
31+
sync.Mutex
3132
}
32-
Throughput struct { // Metrics related to network throughput
33-
Total float64 // Total network throughput for all requests
34-
Count int64 // Count of requests used for calculating throughput
35-
Lock sync.Mutex // Lock for throughput metrics/
33+
Throughput struct {
34+
Total float64
35+
Count int64
36+
sync.Mutex
3637
}
37-
ResponseTimeVariability struct { // Metrics related to response time variability
38-
Total time.Duration // Total response time for all requests
39-
Average time.Duration // Average response time across all requests
40-
Variance float64 // Variance of response times
41-
Count int64 // Count of responses used for calculating response time variability
42-
Lock sync.Mutex // Lock for response time variability metrics
43-
StdDevThreshold float64 // Maximum acceptable standard deviation for adjusting concurrency
44-
DebounceScaleDownCount int // Counter to manage scale down actions after consecutive triggers
45-
DebounceScaleUpCount int // Counter to manage scale up actions after consecutive triggers
38+
ResponseTimeVariability struct {
39+
Total time.Duration
40+
Average time.Duration
41+
Variance float64
42+
Count int64
43+
sync.Mutex
44+
StdDevThreshold float64
45+
DebounceScaleDownCount int
46+
DebounceScaleUpCount int
4647
}
4748
ResponseCodeMetrics struct {
48-
ErrorRate float64 // Error rate calculated as (TotalRateLimitErrors + 5xxErrors) / TotalRequests
49-
Lock sync.Mutex // Lock for response code metrics
49+
ErrorRate float64
50+
sync.Mutex
5051
}
51-
Lock sync.Mutex // Lock for overall metrics fields
5252
}
5353

5454
// NewConcurrencyHandler initializes a new ConcurrencyHandler with the given

concurrency/metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func (ch *ConcurrencyHandler) MonitorRateLimitHeaders(resp *http.Response) int {
156156
func (ch *ConcurrencyHandler) MonitorServerResponseCodes(resp *http.Response) int {
157157
statusCode := resp.StatusCode
158158

159-
ch.Metrics.Lock.Lock()
160-
defer ch.Metrics.Lock.Unlock()
159+
ch.Metrics.Lock()
160+
defer ch.Metrics.Unlock()
161161

162162
if statusCode >= 200 && statusCode < 300 {
163163
// Reset error counts for successful responses
@@ -212,8 +212,8 @@ var responseTimesLock sync.Mutex
212212
// - (1) to suggest an increase in concurrency,
213213
// - (0) to indicate no change needed.
214214
func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.Duration) int {
215-
ch.Metrics.ResponseTimeVariability.Lock.Lock()
216-
defer ch.Metrics.ResponseTimeVariability.Lock.Unlock()
215+
ch.Metrics.ResponseTimeVariability.Lock()
216+
defer ch.Metrics.ResponseTimeVariability.Unlock()
217217

218218
responseTimesLock.Lock() // Ensure safe concurrent access
219219
responseTimes = append(responseTimes, responseTime)

concurrency/scale.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import "go.uber.org/zap"
66
// ScaleDown reduces the concurrency level by one, down to the minimum limit.
77
func (ch *ConcurrencyHandler) ScaleDown() {
88
// Lock to ensure thread safety
9-
ch.lock.Lock()
10-
defer ch.lock.Unlock()
9+
ch.Lock()
10+
defer ch.Unlock()
1111

1212
// We must consider the capacity rather than the length of the semaphore channel
1313
currentSize := cap(ch.sem)
@@ -23,8 +23,8 @@ func (ch *ConcurrencyHandler) ScaleDown() {
2323
// ScaleUp increases the concurrency level by one, up to the maximum limit.
2424
func (ch *ConcurrencyHandler) ScaleUp() {
2525
// Lock to ensure thread safety
26-
ch.lock.Lock()
27-
defer ch.lock.Unlock()
26+
ch.Lock()
27+
defer ch.Unlock()
2828

2929
currentSize := cap(ch.sem)
3030
if currentSize < MaxConcurrency {

concurrency/semaphore.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ func (ch *ConcurrencyHandler) AcquireConcurrencyPermit(ctx context.Context) (con
7171
// This method locks the concurrency handler to safely update shared metrics and logs detailed
7272
// information about the permit acquisition for debugging and monitoring purposes.
7373
func (ch *ConcurrencyHandler) trackResourceAcquisition(duration time.Duration, requestID uuid.UUID) {
74-
ch.lock.Lock()
75-
defer ch.lock.Unlock()
74+
ch.Lock()
75+
defer ch.Unlock()
7676

7777
// Record the time taken to acquire the permit and update related metrics.
7878
ch.AcquisitionTimes = append(ch.AcquisitionTimes, duration)
79-
ch.Metrics.Lock.Lock()
79+
ch.Metrics.Lock()
8080
ch.Metrics.PermitWaitTime += duration
8181
ch.Metrics.TotalRequests++ // Increment the count of total requests handled.
82-
ch.Metrics.Lock.Unlock()
82+
ch.Metrics.Unlock()
8383

8484
// Calculate and log the current state of permit utilization.
8585
utilizedPermits := len(ch.sem)
@@ -115,13 +115,13 @@ func (ch *ConcurrencyHandler) ReleaseConcurrencyPermit(requestID uuid.UUID) {
115115
return
116116
}
117117

118-
ch.lock.Lock()
119-
defer ch.lock.Unlock()
118+
ch.Lock()
119+
defer ch.Unlock()
120120

121121
// Update metrics related to permit release.
122-
ch.Metrics.Lock.Lock()
122+
ch.Metrics.Lock()
123123
ch.Metrics.TotalRequests-- // Decrement the count of total requests handled, if applicable.
124-
ch.Metrics.Lock.Unlock()
124+
ch.Metrics.Unlock()
125125

126126
utilizedPermits := len(ch.sem) // Calculate tokens currently in use.
127127
availablePermits := cap(ch.sem) - utilizedPermits // Calculate tokens that are available for use.

0 commit comments

Comments
 (0)