rstudio / platform-lib

Shared Go Libraries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Large Message Chunking for rsnotify

jonyoder opened this issue · comments

In some situations, notifications sent with rsnotify may exceed the 8000-byte Postgres NOTIFY limit. We should consider support message chunking in the Postgres (pgx) implementations.

We don't want to encourage abuse of notifications, and notifications, in general, should remain small in size. However, it is ideal to avoid blocking errors in cases where situational parameters cause larger than normal messages. For example, if you add many sources, repos, and subscriptions to Package Manager, it's possible to exceed the 8000-byte limit, although we've rarely encountered this issue.

Rough Specification

Sending

When a message is serialized to JSON bytes for sending, if the size exceeds a specific limit, split the message into x-byte chunks and add a prefix:

01/03:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n
<first chunk>

Since the new header, including the newline, is 43 bytes, we can chunk the message data into 7957-byte chunks. We don't currently provide a message sender implementation, so we'll probably need to add that as well.

Receiving

When processing incoming messages, if the message beginning matches the chunk header, use the range value (01/03) to determine the number of expected chunks, which are cached in memory and reassembled in order before being sent to any subscribers.

Limits

If a message exceeds a specified number of chunks, err when sending. We can make this limit configurable, or we can use a hard-coded limit like 10. I recommend keeping the default low since notifications are not ideal for very large messages.

This is done.