-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is an upper limit to what is accepted by the platform per request. Make sure we do not go past it when sending data.
- Loading branch information
Showing
4 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Using license identifier: BUSL-1.1 | ||
// Using copyright holder: Mondoo, Inc. | ||
|
||
package iox | ||
|
||
import ( | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
var maxMessageSize = 6 * (1 << 20) | ||
|
||
func ChunkMessages[T proto.Message](sendFunc func([]T) error, onTooLarge func(T, int), items ...T) error { | ||
idx := 0 | ||
for { | ||
buffer := make([]T, 0, len(items)) | ||
|
||
if idx >= len(items) { | ||
break | ||
} | ||
size := 0 | ||
for i := idx; i < len(items); i++ { | ||
msgSize := proto.Size(items[i]) | ||
if msgSize > maxMessageSize { | ||
onTooLarge(items[i], msgSize) | ||
idx++ | ||
continue | ||
} | ||
size += proto.Size(items[i]) | ||
if size > maxMessageSize { | ||
break | ||
} | ||
buffer = append(buffer, items[i]) | ||
idx++ | ||
} | ||
if err := sendFunc(buffer); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Using license identifier: BUSL-1.1 | ||
// Using copyright holder: Mondoo, Inc. | ||
|
||
package iox | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/interop/grpc_testing" | ||
) | ||
|
||
func TestChunkerIgnoreTooLargeMessages(t *testing.T) { | ||
payloads := []*grpc_testing.Payload{ | ||
{ | ||
Body: bytes.Repeat([]byte{0x01}, maxMessageSize+1), | ||
}, | ||
{ | ||
Body: bytes.Repeat([]byte{0x02}, maxMessageSize/2), | ||
}, | ||
} | ||
|
||
var chunks [][]*grpc_testing.Payload | ||
err := ChunkMessages(func(chunk []*grpc_testing.Payload) error { | ||
chunks = append(chunks, chunk) | ||
return nil | ||
}, func(*grpc_testing.Payload, int) {}, payloads...) | ||
require.NoError(t, err) | ||
require.Len(t, chunks, 1) | ||
require.Len(t, chunks[0], 1) | ||
require.Equal(t, payloads[1], chunks[0][0]) | ||
} | ||
|
||
func TestChunker(t *testing.T) { | ||
maxMessageSize = 100 | ||
payloads := []*grpc_testing.Payload{ | ||
{ | ||
Body: bytes.Repeat([]byte{0x01}, maxMessageSize-10), | ||
}, | ||
{ | ||
Body: bytes.Repeat([]byte{0x02}, maxMessageSize-10), | ||
}, | ||
{ | ||
Body: bytes.Repeat([]byte{0x03}, 10), | ||
}, | ||
{ | ||
Body: bytes.Repeat([]byte{0x04}, 10), | ||
}, | ||
} | ||
|
||
var chunks [][]*grpc_testing.Payload | ||
err := ChunkMessages(func(chunk []*grpc_testing.Payload) error { | ||
chunks = append(chunks, chunk) | ||
return nil | ||
}, func(*grpc_testing.Payload, int) {}, payloads...) | ||
require.NoError(t, err) | ||
require.Len(t, chunks, 3) | ||
require.Len(t, chunks[0], 1) | ||
require.Equal(t, payloads[0], chunks[0][0]) | ||
require.Len(t, chunks[1], 1) | ||
require.Equal(t, payloads[1], chunks[1][0]) | ||
require.Len(t, chunks[2], 2) | ||
require.Equal(t, payloads[2], chunks[2][0]) | ||
} |