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 support for Byte size-based flushing #10

Open
valllabh opened this issue Sep 18, 2023 · 3 comments
Open

Request support for Byte size-based flushing #10

valllabh opened this issue Sep 18, 2023 · 3 comments

Comments

@valllabh
Copy link

Feature Request

Description:

I would like to request support for byte size-based flushing in the buffer package. Currently, the package supports flushing based on a timeout or when the buffer is full based on the number of items. However, there are use cases where it's essential to flush the buffer when it reaches a certain byte size limit.

Use Case:

Imagine a scenario where we're buffering log entries to be written to the buffer and sent to a remote server. It's important to flush the buffer when it reaches a certain size to avoid memory overflows or to ensure that log entries are sent in manageable chunks.

Proposed Solution:

I suggest adding a new configuration option to the Options struct to specify a byte size limit. When this limit is reached, the buffer should automatically trigger a flush. Users should be able to set this byte size limit when creating a new Buffer instance.

Example:

// Create a new buffer with byte size-based flushing.
buffer := buffer.New(buffer.WithByteSizeLimit(1024)) // Flush when the buffer reaches 1 KB.

// Push data into the buffer.
buffer.Push([]byte("Some log entry"))

// The buffer will automatically flush when it reaches 1 KB in size.
@mbenford
Copy link
Collaborator

I'm not sure if that is doable since the buffer holds an array of items that can be anything (currently interface{}). I don't now how to get the overall size in bytes without resorting to unsafe or reflect. Also, even if it was possible to safely get that number it wouldn't be practicable to flush the buffer partially (e.g. at exactly 1KB).

Did you think about a working implementation?

@valllabh
Copy link
Author

valllabh commented Sep 29, 2023

It can be done by keeping a counter of size before the item is added to the buffer. If the (new item size + existing size) exceeds the limit, flush existing items, add a new item to the buffer and reset the existing size. Doesn't have to be precisely (1KB) its a max limit.

@mbenford
Copy link
Collaborator

What do you have in mind to get the size in bytes of each item in order to keep a counter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants