Skip to content

Commit

Permalink
Merge pull request #52 from catchpoint/rate_limiting
Browse files Browse the repository at this point in the history
rate limiting added
  • Loading branch information
taranlabana authored Sep 3, 2024
2 parents 4db0ef3 + e8de2b1 commit 0d40632
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
43 changes: 43 additions & 0 deletions catchpoint/catchpoint_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -212,6 +213,36 @@ type Test struct {
AdvancedSettings AdvancedSetting `json:"advancedSettings"`
}

const (
rateLimit = 7 // 7 requests per second
bucketSize = 7 // same as rate limit
requestInterval = time.Second / rateLimit
)

var (
tokens = make(chan struct{}, bucketSize)
)

func init() {
// Fill the bucket with initial tokens
for i := 0; i < bucketSize; i++ {
tokens <- struct{}{}
}

// Refill tokens at a rate of 7 per second
go func() {
ticker := time.NewTicker(requestInterval)
defer ticker.Stop()
for range ticker.C {
select {
case tokens <- struct{}{}:
default:
// The bucket is full; discard new tokens
}
}
}()
}

func createJson(config TestConfig) string {

//Set properties
Expand Down Expand Up @@ -288,6 +319,9 @@ func getTest(apiToken string, testId string) (*Test, string, error) {
TraceId string `json:"traceId"`
}

// Consume a token before proceeding
<-tokens

var response Response
var responseStatus = ""
getURL := catchpointTestURI + "/" + testId + "?showInheritedProperties=false"
Expand Down Expand Up @@ -332,6 +366,9 @@ func createTest(apiToken string, jsonPayload string) (string, string, string, er
TraceId string `json:"traceId"`
}

// Consume a token before proceeding
<-tokens

var response Response
var postBody = []byte(jsonPayload)
var responseBody = ""
Expand Down Expand Up @@ -374,6 +411,9 @@ func deleteTest(apiToken string, testId string) (string, string, bool, error) {
TraceId string `json:"traceId"`
}

// Consume a token before proceeding
<-tokens

deleteURL := catchpointTestURI + "/" + testId
var response Response
var responseBody = ""
Expand Down Expand Up @@ -757,6 +797,9 @@ func updateTest(apiToken string, testId string, jsonPayload string) (string, str
TraceId string `json:"traceId"`
}

// Consume a token before proceeding
<-tokens

updateURL := catchpointTestURI + "/" + testId
var jsonPatchDocument = []byte(jsonPayload)
var response Response
Expand Down
1 change: 0 additions & 1 deletion catchpoint/resource_web_test_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,6 @@ func resourceTestRead(d *schema.ResourceData, m interface{}) error {
return nil
}
log.Printf("[DEBUG] Response Code from Catchpoint API: " + respStatus)
log.Printf("[DEBUG] testXXXXXXXXXXXXXXXXXXX : %#v", test)

testNew := flattenTest(test)

Expand Down
11 changes: 9 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# v1.3.0

ENHANCEMENT

* A minor change to handle bulk requests by limiting the processing of number of requests to 7 per second has context menu.

# v1.2.0

BUG FIXES

* Fixed a minor bug to detect changes correctly for optional fields on running "terraform apply" command.


# v1.0.1

Catchpoint internal testing:

* Bug 187721: [Terraform] Import command fails when the alert settings are set to Override
* Bug 188869: [Terraform] Update operation in terraform without any changes, displays 1 change applied message
* import command fails when the alert settings are set to Override
* Update operation in terraform without any changes, displays 1 change applied message

# v1.0.0

Expand Down

0 comments on commit 0d40632

Please sign in to comment.