It turned out that it wasn’t a problem with rounding in Lua script. Lua script gets already rounded values.
Moreover, it’s not a problem with rounding at all. Unrounded seconds might be used, and it would be possible to receive a 429 response with Retry-After: 0
anyway.
Here is the fix for the main problem:
https://github.com/discourse/discourse/pull/11691
And here is the fix for an additional problem that can occasionally cause the same error:
https://github.com/discourse/discourse/pull/11692