Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request: do not append '/j' in RawRequest() #31

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var reqHeader = []byte{'j', '/'}
var reqSep = []byte{' ', ';'}

func prepareRequest(buf *bytes.Buffer, command string, param string) int {
buf.Write(reqHeader)
buf.WriteString(command)
buf.WriteByte(reqSep[0])
buf.WriteString(param)
Expand All @@ -39,6 +40,10 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
// misuse since this function is internal
panic("empty command")
}

// Buffer that will store the temporary prepared request
buf := bytes.NewBuffer(nil)

switch len(params) {
case 0:
if len(command)+len(reqHeader) > bufSize {
Expand All @@ -49,7 +54,8 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
command,
)
}
requests = append(requests, []byte(command))
buf.Write(reqHeader)
buf.WriteString(command)
case 1:
request := command + " " + params[0]
if len(request)+len(reqHeader) > bufSize {
Expand All @@ -60,10 +66,9 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
request,
)
}
requests = append(requests, []byte(request))
buf.Write(reqHeader)
buf.WriteString(request)
default:
buf := bytes.NewBuffer(nil)

// Add [[BATCH]] to the buffer
buf.WriteString(batch)
// Initialise current length of buffer
Expand Down Expand Up @@ -102,9 +107,10 @@ func prepareRequests(command string, params []string) (requests []RawRequest, er
// Add the contents of the request to the buffer
curLen = prepareRequest(buf, command, param)
}
// Append any remaining buffer content to requests array
requests = append(requests, buf.Bytes())
}
// Append any remaining buffer content to requests array
requests = append(requests, buf.Bytes())

return requests, nil
}

Expand Down Expand Up @@ -253,7 +259,6 @@ func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, er
}

// Send the request to the socket
request = append(reqHeader, request...)
if len(request) > bufSize {
return nil, fmt.Errorf(
"request too big (%d>%d): %s",
Expand Down
10 changes: 5 additions & 5 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func TestPrepareRequests(t *testing.T) {
params []string
expected []string
}{
{"command", nil, []string{"command"}},
{"command", []string{"param0"}, []string{"command param0"}},
{"command", []string{"param0", "param1"}, []string{"[[BATCH]]command param0;command param1;"}},
{"command", nil, []string{"j/command"}},
{"command", []string{"param0"}, []string{"j/command param0"}},
{"command", []string{"param0", "param1"}, []string{"[[BATCH]]j/command param0;j/command param1;"}},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("tests_%s-%s", tt.command, tt.params), func(t *testing.T) {
Expand All @@ -102,8 +102,8 @@ func TestPrepareRequestsMass(t *testing.T) {
{"command", genParams("very big param list", 100), 1},
{"command", genParams("very big param list", 500), 2},
{"command", genParams("very big param list", 1000), 4},
{"command", genParams("very big param list", 5000), 18},
{"command", genParams("very big param list", 10000), 35},
{"command", genParams("very big param list", 5000), 19},
{"command", genParams("very big param list", 10000), 37},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("mass_tests_%s-%d", tt.command, len(tt.params)), func(t *testing.T) {
Expand Down
Loading