Skip to content

Commit 508db5c

Browse files
committed
deleting comments
1 parent c92ef82 commit 508db5c

File tree

4 files changed

+16
-39
lines changed

4 files changed

+16
-39
lines changed

concurrency/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ const (
5757

5858
debounceScaleDownThreshold = 5 // Number of consecutive triggers before scaling down
5959

60-
//
60+
// TODO this comment
6161
AcceptableAverageResponseTime = 100 * time.Millisecond
6262
)

concurrency/metrics.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,37 @@ func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.D
215215
ch.Metrics.ResponseTimeVariability.Lock()
216216
defer ch.Metrics.ResponseTimeVariability.Unlock()
217217

218-
responseTimesLock.Lock() // Ensure safe concurrent access
218+
responseTimesLock.Lock()
219219
responseTimes = append(responseTimes, responseTime)
220220
if len(responseTimes) > 10 {
221-
responseTimes = responseTimes[1:] // Maintain last 10 measurements
221+
responseTimes = responseTimes[1:]
222222
}
223223
responseTimesLock.Unlock()
224224

225225
stdDev := calculateStdDev(responseTimes)
226226
averageResponseTime := calculateAverage(responseTimes)
227227

228-
// Check if conditions suggest a need to scale down
229228
if stdDev > ch.Metrics.ResponseTimeVariability.StdDevThreshold && averageResponseTime > AcceptableAverageResponseTime {
230229
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount++
231230
if ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount >= debounceScaleDownThreshold {
232231
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0
233-
return -1 // Suggest decrease concurrency
232+
return -1
234233
}
235234
} else {
236-
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // Reset counter if conditions are not met
235+
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0
237236
}
238237

239-
// Check if conditions suggest a need to scale up
240238
if stdDev <= ch.Metrics.ResponseTimeVariability.StdDevThreshold && averageResponseTime <= AcceptableAverageResponseTime {
241239
ch.Metrics.ResponseTimeVariability.DebounceScaleUpCount++
242240
if ch.Metrics.ResponseTimeVariability.DebounceScaleUpCount >= debounceScaleDownThreshold {
243241
ch.Metrics.ResponseTimeVariability.DebounceScaleUpCount = 0
244-
return 1 // Suggest increase concurrency
242+
return 1
245243
}
246244
} else {
247-
ch.Metrics.ResponseTimeVariability.DebounceScaleUpCount = 0 // Reset counter if conditions are not met
245+
ch.Metrics.ResponseTimeVariability.DebounceScaleUpCount = 0
248246
}
249247

250-
return 0 // Default to no change
248+
return 0
251249
}
252250

253251
// calculateAverage computes the average response time from a slice of time.Duration values.

concurrency/scale.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import "go.uber.org/zap"
55

66
// ScaleDown reduces the concurrency level by one, down to the minimum limit.
77
func (ch *ConcurrencyHandler) ScaleDown() {
8-
// Lock to ensure thread safety
98
ch.Lock()
109
defer ch.Unlock()
1110

12-
// We must consider the capacity rather than the length of the semaphore channel
1311
currentSize := cap(ch.sem)
1412
if currentSize > MinConcurrency {
1513
newSize := currentSize - 1
@@ -22,7 +20,6 @@ func (ch *ConcurrencyHandler) ScaleDown() {
2220

2321
// ScaleUp increases the concurrency level by one, up to the maximum limit.
2422
func (ch *ConcurrencyHandler) ScaleUp() {
25-
// Lock to ensure thread safety
2623
ch.Lock()
2724
defer ch.Unlock()
2825

@@ -49,19 +46,15 @@ func (ch *ConcurrencyHandler) ScaleUp() {
4946
func (ch *ConcurrencyHandler) ResizeSemaphore(newSize int) {
5047
newSem := make(chan struct{}, newSize)
5148

52-
// Transfer tokens from the old semaphore to the new one.
5349
for {
5450
select {
5551
case token := <-ch.sem:
5652
select {
5753
case newSem <- token:
58-
// Token transferred to new semaphore.
5954
default:
60-
// New semaphore is full, put token back to the old one to allow ongoing operations to complete.
6155
ch.sem <- token
6256
}
6357
default:
64-
// No more tokens to transfer.
6558
close(ch.sem)
6659
ch.sem = newSem
6760
return

concurrency/semaphore.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,21 @@ import (
3333
// concurrency tracking.
3434
func (ch *ConcurrencyHandler) AcquireConcurrencyPermit(ctx context.Context) (context.Context, uuid.UUID, error) {
3535
log := ch.logger
36-
37-
// Start measuring the permit acquisition time.
3836
tokenAcquisitionStart := time.Now()
39-
40-
// Generate a unique request ID for this permit acquisition.
4137
requestID := uuid.New()
4238

43-
// Create a new context with a specified timeout for acquiring the permit.
4439
ctxWithTimeout, cancel := context.WithTimeout(ctx, 10*time.Second)
45-
defer cancel() // Ensure to free up resources by cancelling the context after use.
40+
defer cancel()
4641

4742
select {
48-
case ch.sem <- struct{}{}: // permit acquisition was successful.
49-
// Record the time taken to acquire the permit.
43+
case ch.sem <- struct{}{}:
5044
tokenAcquisitionDuration := time.Since(tokenAcquisitionStart)
51-
ch.trackResourceAcquisition(tokenAcquisitionDuration, requestID) // Track and log metrics.
45+
ch.trackResourceAcquisition(tokenAcquisitionDuration, requestID)
5246

53-
// Create a new context that includes the unique request ID.
5447
ctxWithRequestID := context.WithValue(ctx, RequestIDKey{}, requestID)
5548
return ctxWithRequestID, requestID, nil
5649

57-
case <-ctxWithTimeout.Done(): // Timeout occurred before a permit could be acquired.
50+
case <-ctxWithTimeout.Done():
5851
log.Error("Failed to acquire concurrency permit", zap.Error(ctxWithTimeout.Err()))
5952
return ctx, requestID, ctxWithTimeout.Err()
6053
}
@@ -74,14 +67,12 @@ func (ch *ConcurrencyHandler) trackResourceAcquisition(duration time.Duration, r
7467
ch.Lock()
7568
defer ch.Unlock()
7669

77-
// Record the time taken to acquire the permit and update related metrics.
7870
ch.AcquisitionTimes = append(ch.AcquisitionTimes, duration)
7971
ch.Metrics.Lock()
8072
ch.Metrics.PermitWaitTime += duration
81-
ch.Metrics.TotalRequests++ // Increment the count of total requests handled.
73+
ch.Metrics.TotalRequests++
8274
ch.Metrics.Unlock()
8375

84-
// Calculate and log the current state of permit utilization.
8576
utilizedPermits := len(ch.sem)
8677
availablePermits := cap(ch.sem) - utilizedPermits
8778
ch.logger.Debug("Resource acquired", zap.String("RequestID", requestID.String()), zap.Duration("Duration", duration), zap.Int("UtilizedPermits", utilizedPermits), zap.Int("AvailablePermits", availablePermits))
@@ -105,28 +96,23 @@ func (ch *ConcurrencyHandler) trackResourceAcquisition(duration time.Duration, r
10596
// This usage ensures that the permit is released in a deferred manner at the end of the operation, regardless of
10697
// how the operation exits (normal completion or error path).
10798
func (ch *ConcurrencyHandler) ReleaseConcurrencyPermit(requestID uuid.UUID) {
108-
// Safely remove a permit from the semaphore to make it available for other operations.
10999
select {
110100
case <-ch.sem:
111-
// Continue to process after successfully retrieving a permit from the semaphore.
112101
default:
113-
// Log an error if no permit was available to release, indicating a potential synchronization issue.
114102
ch.logger.Error("Attempted to release a non-existent concurrency permit", zap.String("RequestID", requestID.String()))
115103
return
116104
}
117105

118106
ch.Lock()
119107
defer ch.Unlock()
120108

121-
// Update metrics related to permit release.
122109
ch.Metrics.Lock()
123-
ch.Metrics.TotalRequests-- // Decrement the count of total requests handled, if applicable.
110+
ch.Metrics.TotalRequests--
124111
ch.Metrics.Unlock()
125112

126-
utilizedPermits := len(ch.sem) // Calculate tokens currently in use.
127-
availablePermits := cap(ch.sem) - utilizedPermits // Calculate tokens that are available for use.
113+
utilizedPermits := len(ch.sem)
114+
availablePermits := cap(ch.sem) - utilizedPermits
128115

129-
// Log the release of the concurrency permit for auditing and debugging purposes.
130116
ch.logger.Debug("Released concurrency permit",
131117
zap.String("RequestID", requestID.String()),
132118
zap.Int("UtilizedPermits", utilizedPermits),

0 commit comments

Comments
 (0)