Skip to content

Commit

Permalink
Merge pull request #31 from thiagokokada/raw-reader-should-be-raw
Browse files Browse the repository at this point in the history
request: do not append '/j' in RawRequest()
  • Loading branch information
thiagokokada authored Sep 3, 2024
2 parents 482f29b + 5f0b2a8 commit 4f4c86e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
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

0 comments on commit 4f4c86e

Please sign in to comment.