EncodeAgentIDChunks encodes agent IDs into chunks that fit within the PostgreSQL NOTIFY 8KB payload size limit. Each UUID is base64-encoded (without padding) and concatenated into a single byte slice per chunk.
(agentIDs []uuid.UUID)
| 33 | // PostgreSQL NOTIFY 8KB payload size limit. Each UUID is base64-encoded |
| 34 | // (without padding) and concatenated into a single byte slice per chunk. |
| 35 | func EncodeAgentIDChunks(agentIDs []uuid.UUID) ([][]byte, error) { |
| 36 | chunks := make([][]byte, 0, (len(agentIDs)+maxAgentIDsPerChunk-1)/maxAgentIDsPerChunk) |
| 37 | |
| 38 | for i := 0; i < len(agentIDs); i += maxAgentIDsPerChunk { |
| 39 | end := i + maxAgentIDsPerChunk |
| 40 | if end > len(agentIDs) { |
| 41 | end = len(agentIDs) |
| 42 | } |
| 43 | |
| 44 | chunk := agentIDs[i:end] |
| 45 | |
| 46 | // Build payload by base64-encoding each UUID (without padding) and |
| 47 | // concatenating them. This is UTF-8 safe for PostgreSQL NOTIFY. |
| 48 | payload := make([]byte, len(chunk)*UUIDBase64Size) |
| 49 | for i, agentID := range chunk { |
| 50 | err := EncodeAgentID(agentID, payload[i*UUIDBase64Size:(i+1)*UUIDBase64Size]) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | } |
| 55 | chunks = append(chunks, payload) |
| 56 | } |
| 57 | |
| 58 | return chunks, nil |
| 59 | } |