Skip to content

Commit

Permalink
Return error from ParseRemainingChunksFromNonce
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaCharlie committed Oct 2, 2024
1 parent 14b0d8e commit 8aa8b4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions ads/ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ads
import (
"encoding/binary"
"encoding/hex"
"errors"
"log/slog"
"sync"
"time"
Expand Down Expand Up @@ -258,23 +259,29 @@ func LookupStreamTypeByRPCMethod(rpcMethod string) (StreamType, bool) {
}
}

var (
invalidNonceEncodingErr = errors.New("nonce isn't in hex encoding")
invalidNonceLengthErr = errors.New("decoded nonce did not have expected length")
)

// ParseRemainingChunksFromNonce checks whether the Diderot server implementation chunked the delta
// responses because not all resources could fit in the same response without going over the default
// max gRPC message size of 4MB. A nonce from Diderot always starts with the 64-bit nanosecond
// timestamp of when the response was generated on the server. Then the number of remaining chunks as
// a 32-bit integer. The sequence of integers is binary encoded with [binary.BigEndian] then hex
// encoded. If the given nonce does not match the expected format, this function simply returns 0, as
// it means the nonce was not created by Diderot server implementation, and therefore does not
// contain the expected information.
func ParseRemainingChunksFromNonce(nonce string) (remainingChunks int) {
// encoded. If the given nonce does not match the expected format, this function simply returns 0
// along with an error describing why it does not match. If the error isn't nil, it means the nonce
// was not created by a Diderot server implementation, and therefore does not contain the expected
// information.
func ParseRemainingChunksFromNonce(nonce string) (remainingChunks int, err error) {
decoded, err := hex.DecodeString(nonce)
if err != nil {
return 0
return 0, invalidNonceEncodingErr
}

if len(decoded) != 12 {
return 0
return 0, invalidNonceLengthErr
}

return int(binary.BigEndian.Uint32(decoded[8:12]))
return int(binary.BigEndian.Uint32(decoded[8:12])), nil
}
2 changes: 1 addition & 1 deletion internal/server/handlers_delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type deltaSender struct {
queuedUpdates []queuedResourceUpdate
// The minimum size an encoded chunk will serialize to, in bytes. Used to check whether a given
// update can _ever_ be sent, and as the initial size of a chunk. Note that this value only depends
// on utils.MaxNonceLength and the length of typeURL.
// on utils.NonceLength and the length of typeURL.
minChunkSize int
}

Expand Down

0 comments on commit 8aa8b4b

Please sign in to comment.