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

Support int64 in arguments #165

Merged
merged 3 commits into from
Feb 20, 2025

Conversation

tomas-novotny
Copy link
Contributor

@tomas-novotny tomas-novotny commented Feb 18, 2025

When consuming stream queue, you send starting numeric offset value as header argument.

$client = new Client();
$channel = $client->channel();

$channel->consume(consumeCallback(...), "stream-queue-name", arguments: [
    'x-stream-offset' => 2_147_958_060,
]);

When offset is larger then limit of int32, it starts returning messages from the beginning of the queue due to offset overflow because now any int is encoded like this:

$buffer->appendUint8(Constants::FIELD_LONG_INT);
$buffer->appendInt32($value);

The simplest solution is to use int64 instead, which works for any int from PHP code, or (as in this PR) at least support int64 with int32 as the default for smaller numbers.

$buffer->appendUint8(Constants::FIELD_LONG_LONG_INT);
$buffer->appendInt64($value);

@WyriHaximus WyriHaximus added this to the v0.6.0 milestone Feb 18, 2025
@tomas-novotny
Copy link
Contributor Author

If preferred, I can change it to only encode int values as int64. This will make the code cleaner.

@WyriHaximus
Copy link
Collaborator

If preferred, I can change it to only encode int values as int64. This will make the code cleaner.

That would make sense tbh. Was going to ask if this impacts #139, and that this obviously requires 64bit PHP. (Which I assume pretty much everyone uses these days.)

@tomas-novotny
Copy link
Contributor Author

tomas-novotny commented Feb 19, 2025

I don't know how this relates to #139...

It doesn't actually require 64-bit PHP, it checks for 64-bit support here and on 32-bit system encodes large numbers in halves here.

So it should not break anything to use

$buffer->appendUint8(Constants::FIELD_LONG_LONG_INT);
$buffer->appendInt64($value);

or (because on 32-bit system you are unable to set larger int number than int32 anyway)

if (PHP_INT_SIZE === 4) {
    $buffer->appendUint8(Constants::FIELD_LONG_INT);
    $buffer->appendInt32($value);
} else {
    $buffer->appendUint8(Constants::FIELD_LONG_LONG_INT);
    $buffer->appendInt64($value);
}

@WyriHaximus
Copy link
Collaborator

I don't know how this relates to #139...

Good, wasn't this expecting it to be but wanted to make sure.

It doesn't actually require 64-bit PHP, it checks for 64-bit support here and on 32-bit system encodes large numbers in halves here.

So it should not break anything to use

Fair, and on 32bit systems you can't pass an integer requiring to use appendInt64 over appendInt32 anyway.

if (PHP_INT_SIZE === 4) {
    $buffer->appendUint8(Constants::FIELD_LONG_INT);
    $buffer->appendInt32($value);
} else {
    $buffer->appendUint8(Constants::FIELD_LONG_LONG_INT);
    $buffer->appendInt64($value);
}

This one works for me, just to be slightly more on the safe side.

Copy link
Collaborator

@WyriHaximus WyriHaximus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for putting the final touches on this PR 👍

@WyriHaximus WyriHaximus merged commit c05d3aa into jakubkulhan:0.6.x Feb 20, 2025
110 checks passed
@tomas-novotny tomas-novotny deleted the protocol-writer-int64 branch February 20, 2025 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants