Skip to content

Commit 389389d

Browse files
authored
Merge pull request #167 from deploymenttheory/dev
Introduced a dynamic sized semaphore into http client
2 parents 556aa0b + 1d54732 commit 389389d

File tree

6 files changed

+251
-254
lines changed

6 files changed

+251
-254
lines changed

concurrency/const.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// concurrency/const.go
2+
package concurrency
3+
4+
import "time"
5+
6+
const (
7+
// MaxAcceptableTTFB represents the maximum acceptable Time to First Byte (TTFB) in milliseconds.
8+
// TTFB is the time taken for the server to start sending the first byte of data in response to a request.
9+
// Adjustments in concurrency will be made if the TTFB exceeds this threshold.
10+
MaxAcceptableTTFB = 300 * time.Millisecond
11+
12+
// MaxAcceptableThroughput represents the maximum acceptable network throughput in bytes per second.
13+
// Throughput is the amount of data transferred over the network within a specific time interval.
14+
// Adjustments in concurrency will be made if the network throughput exceeds this threshold.
15+
MaxAcceptableThroughput = 5 * 1024 * 1024
16+
17+
// MaxAcceptableResponseTimeVariability represents the maximum acceptable variability in response times.
18+
// It is used as a threshold to dynamically adjust concurrency based on fluctuations in response times.
19+
MaxAcceptableResponseTimeVariability = 500 * time.Millisecond
20+
21+
// ErrorRateThreshold represents the threshold for error rate above which concurrency will be adjusted.
22+
// Error rate is calculated as (TotalRateLimitErrors + 5xxErrors) / TotalRequests.
23+
// Adjustments in concurrency will be made if the error rate exceeds this threshold. A threshold of 0.1 (or 10%) is common.
24+
ErrorRateThreshold = 0.1
25+
)

concurrency/dynamic_token_adjustment.go

Lines changed: 0 additions & 199 deletions
This file was deleted.

concurrency/handler.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@ type ConcurrencyHandler struct {
2727

2828
// ConcurrencyMetrics captures various metrics related to managing concurrency for the client's interactions with the API.
2929
type ConcurrencyMetrics struct {
30-
TotalRequests int64
31-
TotalRetries int64
32-
TotalRateLimitErrors int64
33-
TotalResponseTime time.Duration
34-
TokenWaitTime time.Duration
35-
Lock sync.Mutex // Protects performance metrics fields
30+
TotalRequests int64 // Total number of requests made
31+
TotalRetries int64 // Total number of retry attempts
32+
TotalRateLimitErrors int64 // Total number of rate limit errors encountered
33+
TokenWaitTime time.Duration // Total time spent waiting for tokens
34+
TTFB struct { // Metrics related to Time to First Byte (TTFB)
35+
Total time.Duration // Total Time to First Byte (TTFB) for all requests
36+
Count int64 // Count of requests used for calculating TTFB
37+
Lock sync.Mutex // Lock for TTFB metrics
38+
}
39+
Throughput struct { // Metrics related to network throughput
40+
Total float64 // Total network throughput for all requests
41+
Count int64 // Count of requests used for calculating throughput
42+
Lock sync.Mutex // Lock for throughput metrics/
43+
}
44+
ResponseTimeVariability struct { // Metrics related to response time variability
45+
Total time.Duration // Total response time for all requests
46+
Average time.Duration // Average response time across all requests
47+
Variance float64 // Variance of response times
48+
Count int64 // Count of responses used for calculating response time variability
49+
Lock sync.Mutex // Lock for response time variability metrics
50+
StdDevThreshold float64 // Maximum acceptable standard deviation for adjusting concurrency
51+
}
52+
ResponseCodeMetrics struct {
53+
ErrorRate float64 // Error rate calculated as (TotalRateLimitErrors + 5xxErrors) / TotalRequests
54+
Lock sync.Mutex // Lock for response code metrics
55+
}
56+
Lock sync.Mutex // Lock for overall metrics fields
3657
}
3758

3859
// NewConcurrencyHandler initializes a new ConcurrencyHandler with the given

0 commit comments

Comments
 (0)