@@ -33,28 +33,21 @@ import (
33
33
// concurrency tracking.
34
34
func (ch * ConcurrencyHandler ) AcquireConcurrencyPermit (ctx context.Context ) (context.Context , uuid.UUID , error ) {
35
35
log := ch .logger
36
-
37
- // Start measuring the permit acquisition time.
38
36
tokenAcquisitionStart := time .Now ()
39
-
40
- // Generate a unique request ID for this permit acquisition.
41
37
requestID := uuid .New ()
42
38
43
- // Create a new context with a specified timeout for acquiring the permit.
44
39
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 ()
46
41
47
42
select {
48
- case ch .sem <- struct {}{}: // permit acquisition was successful.
49
- // Record the time taken to acquire the permit.
43
+ case ch .sem <- struct {}{}:
50
44
tokenAcquisitionDuration := time .Since (tokenAcquisitionStart )
51
- ch .trackResourceAcquisition (tokenAcquisitionDuration , requestID ) // Track and log metrics.
45
+ ch .trackResourceAcquisition (tokenAcquisitionDuration , requestID )
52
46
53
- // Create a new context that includes the unique request ID.
54
47
ctxWithRequestID := context .WithValue (ctx , RequestIDKey {}, requestID )
55
48
return ctxWithRequestID , requestID , nil
56
49
57
- case <- ctxWithTimeout .Done (): // Timeout occurred before a permit could be acquired.
50
+ case <- ctxWithTimeout .Done ():
58
51
log .Error ("Failed to acquire concurrency permit" , zap .Error (ctxWithTimeout .Err ()))
59
52
return ctx , requestID , ctxWithTimeout .Err ()
60
53
}
@@ -74,14 +67,12 @@ func (ch *ConcurrencyHandler) trackResourceAcquisition(duration time.Duration, r
74
67
ch .Lock ()
75
68
defer ch .Unlock ()
76
69
77
- // Record the time taken to acquire the permit and update related metrics.
78
70
ch .AcquisitionTimes = append (ch .AcquisitionTimes , duration )
79
71
ch .Metrics .Lock ()
80
72
ch .Metrics .PermitWaitTime += duration
81
- ch .Metrics .TotalRequests ++ // Increment the count of total requests handled.
73
+ ch .Metrics .TotalRequests ++
82
74
ch .Metrics .Unlock ()
83
75
84
- // Calculate and log the current state of permit utilization.
85
76
utilizedPermits := len (ch .sem )
86
77
availablePermits := cap (ch .sem ) - utilizedPermits
87
78
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
105
96
// This usage ensures that the permit is released in a deferred manner at the end of the operation, regardless of
106
97
// how the operation exits (normal completion or error path).
107
98
func (ch * ConcurrencyHandler ) ReleaseConcurrencyPermit (requestID uuid.UUID ) {
108
- // Safely remove a permit from the semaphore to make it available for other operations.
109
99
select {
110
100
case <- ch .sem :
111
- // Continue to process after successfully retrieving a permit from the semaphore.
112
101
default :
113
- // Log an error if no permit was available to release, indicating a potential synchronization issue.
114
102
ch .logger .Error ("Attempted to release a non-existent concurrency permit" , zap .String ("RequestID" , requestID .String ()))
115
103
return
116
104
}
117
105
118
106
ch .Lock ()
119
107
defer ch .Unlock ()
120
108
121
- // Update metrics related to permit release.
122
109
ch .Metrics .Lock ()
123
- ch .Metrics .TotalRequests -- // Decrement the count of total requests handled, if applicable.
110
+ ch .Metrics .TotalRequests --
124
111
ch .Metrics .Unlock ()
125
112
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
128
115
129
- // Log the release of the concurrency permit for auditing and debugging purposes.
130
116
ch .logger .Debug ("Released concurrency permit" ,
131
117
zap .String ("RequestID" , requestID .String ()),
132
118
zap .Int ("UtilizedPermits" , utilizedPermits ),
0 commit comments