Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor approximate template size in when batching
Browse files Browse the repository at this point in the history
Won't be exact but will err on the side of caution since the template file's data will be _slightly_ bigger than the empty template.
colmsnowplow committed Jul 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a80ea8d commit 01a5aba
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 13 additions & 8 deletions pkg/target/http.go
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@ type HTTPTarget struct {
messageByteLimit int

requestTemplate *template.Template
approxTmplSize int
}

func checkURL(str string) error {
@@ -155,11 +156,13 @@ func newHTTPTarget(
client := createHTTPClient(oAuth2ClientID, oAuth2ClientSecret, oAuth2TokenURL, oAuth2RefreshToken, transport)
client.Timeout = time.Duration(requestTimeout) * time.Second

requestTemplate, err := loadRequestTemplate(templateFile)

approxTmplSize, requestTemplate, err := loadRequestTemplate(templateFile)
if err != nil {
return nil, err
}
if approxTmplSize >= requestByteLimit || approxTmplSize >= messageByteLimit {
return nil, errors.New("target error: Byte limit must be larger than template size")
}

return &HTTPTarget{
client: client,
@@ -176,19 +179,21 @@ func newHTTPTarget(
messageByteLimit: messageByteLimit,

requestTemplate: requestTemplate,
approxTmplSize: approxTmplSize,
}, nil
}

func loadRequestTemplate(templateFile string) (*template.Template, error) {
func loadRequestTemplate(templateFile string) (int, *template.Template, error) {
if templateFile != "" {
content, err := os.ReadFile(templateFile)

if err != nil {
return nil, err
return 0, nil, err
}
return parseRequestTemplate(string(content))
tmpl, err := parseRequestTemplate(string(content))
return len(content), tmpl, err
}
return nil, nil
return 0, nil, nil
}

func parseRequestTemplate(templateContent string) (*template.Template, error) {
@@ -295,8 +300,8 @@ func (ht *HTTPTarget) Write(messages []*models.Message) (*models.TargetWriteResu
chunks, oversized := models.GetChunkedMessages(
messages,
ht.requestMaxMessages,
ht.messageByteLimit,
ht.requestByteLimit,
ht.messageByteLimit-ht.approxTmplSize,
ht.requestByteLimit-ht.approxTmplSize,
)

sent := []*models.Message{}
3 changes: 2 additions & 1 deletion pkg/target/http_test.go
Original file line number Diff line number Diff line change
@@ -159,14 +159,15 @@ func TestHTTP_RetrieveHeaders(t *testing.T) {
t.Run(tt.Name, func(t *testing.T) {
testTargetConfig := &HTTPTargetConfig{
HTTPURL: "http://test",
MessageByteLimit: 1048576,
RequestByteLimit: 1048576,
RequestTimeoutInSeconds: 5,
ContentType: "application/json",
DynamicHeaders: tt.Dynamic,
}
testTarget, err := HTTPTargetConfigFunction(testTargetConfig)
if err != nil {
t.Fatalf("failed to create test target")
t.Fatalf("failed to create test target: " + err.Error())
}

out := testTarget.retrieveHeaders(tt.Msg)

0 comments on commit 01a5aba

Please sign in to comment.