@@ -33,28 +33,21 @@ import (
3333// concurrency tracking.
3434func (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).
10798func (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