You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: httpclient/httpclient_concurrency_management.go
+55-2Lines changed: 55 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ import (
13
13
"go.uber.org/zap"
14
14
)
15
15
16
-
//------ Constants and Data Structures:
16
+
// Constants and Data Structures:
17
17
18
18
const (
19
19
MaxConcurrency=10// Maximum allowed concurrent requests
@@ -31,9 +31,62 @@ type ConcurrencyManager struct {
31
31
lastTokenAcquisitionTime time.Time
32
32
}
33
33
34
+
// requestIDKey is type used as a key for storing and retrieving
35
+
// request-specific identifiers from a context.Context object. This private
36
+
// type ensures that the key is distinct and prevents accidental value
37
+
// retrieval or conflicts with other context keys. The value associated
38
+
// with this key in a context is typically a UUID that uniquely identifies
39
+
// a request being processed by the ConcurrencyManager, allowing for
40
+
// fine-grained control and tracking of concurrent HTTP requests.
34
41
typerequestIDKeystruct{}
35
42
36
-
//------ Constructor and Helper Functions:
43
+
// Functions:
44
+
45
+
// AcquireConcurrencyToken attempts to acquire a token from the ConcurrencyManager to manage the number of concurrent requests.
46
+
// This function is designed to ensure that the HTTP client adheres to predefined concurrency limits, preventing an excessive number of simultaneous requests.
47
+
// It creates a new context with a timeout to avoid indefinite blocking in case the concurrency limit is reached.
48
+
// Upon successfully acquiring a token, it records the time taken to acquire the token and updates performance metrics accordingly.
49
+
// The function then adds the acquired request ID to the context, which can be used for tracking and managing individual requests.
50
+
//
51
+
// Parameters:
52
+
// - ctx: The parent context from which the new context with timeout will be derived. This allows for proper request cancellation and timeout handling.
53
+
// - log: An instance of a logger (conforming to the logger.Logger interface), used to log relevant information and errors during the token acquisition process.
54
+
//
55
+
// Returns:
56
+
// - A new context containing the acquired request ID, which should be passed to subsequent operations requiring concurrency control.
57
+
// - An error if the token could not be acquired within the timeout period or due to any other issues encountered by the ConcurrencyManager.
58
+
//
59
+
// Usage:
60
+
// This function should be called before making an HTTP request that needs to be controlled for concurrency.
61
+
// The returned context should be used for the HTTP request to ensure it is associated with the acquired concurrency token.
Copy file name to clipboardExpand all lines: httpclient/httpclient_request.go
+25-49Lines changed: 25 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -14,50 +14,20 @@ import (
14
14
"go.uber.org/zap"
15
15
)
16
16
17
-
// AcquireConcurrencyToken attempts to acquire a token from the ConcurrencyManager to manage the number of concurrent requests.
18
-
// This function is designed to ensure that the HTTP client adheres to predefined concurrency limits, preventing an excessive number of simultaneous requests.
19
-
// It creates a new context with a timeout to avoid indefinite blocking in case the concurrency limit is reached.
20
-
// Upon successfully acquiring a token, it records the time taken to acquire the token and updates performance metrics accordingly.
21
-
// The function then adds the acquired request ID to the context, which can be used for tracking and managing individual requests.
17
+
// updatePerformanceMetrics updates the client's performance metrics by recording the duration
18
+
// of the HTTP request and incrementing the total request count. This function is thread-safe
19
+
// and uses a mutex to synchronize updates to the performance metrics.
22
20
//
23
21
// Parameters:
24
-
// - ctx: The parent context from which the new context with timeout will be derived. This allows for proper request cancellation and timeout handling.
25
-
// - log: An instance of a logger (conforming to the logger.Logger interface), used to log relevant information and errors during the token acquisition process.
22
+
// - duration: The time duration it took for an HTTP request to complete.
26
23
//
27
-
// Returns:
28
-
// - A new context containing the acquired request ID, which should be passed to subsequent operations requiring concurrency control.
29
-
// - An error if the token could not be acquired within the timeout period or due to any other issues encountered by the ConcurrencyManager.
30
-
//
31
-
// Usage:
32
-
// This function should be called before making an HTTP request that needs to be controlled for concurrency.
33
-
// The returned context should be used for the HTTP request to ensure it is associated with the acquired concurrency token.
0 commit comments