protobuf: unrevert and fix hyperpb processor#4240
Conversation
| if err := msg.Unmarshal(buf, hyperpb.WithRecordProfile(state.profile, p.opts.Rate)); err != nil { | ||
| return err | ||
| } | ||
| if state.profile != nil && p.seen.Add(1)%p.opts.RecompileInterval == 0 { |
There was a problem hiding this comment.
Bug: Integer division by zero — if RecompileInterval is 0 (the zero value of int64), the modulo operation p.seen.Add(1) % p.opts.RecompileInterval will panic at runtime.
Since ProfilingOptions is an exported struct with no validation, and NewHyperPbDecoder always creates a non-nil profile (line 30), the state.profile != nil guard won't protect against this — the first call to WithDecoded will panic if a zero-value ProfilingOptions{} is passed.
Add a guard (e.g., p.opts.RecompileInterval > 0 &&) before the modulo, or validate RecompileInterval in NewHyperPbDecoder.
|
Commits Review
|
| cachedDecoder = common.NewHyperPbDecoder(msgDesc, common.ProfilingOptions{ | ||
| Rate: 0.01, | ||
| RecompileInterval: 100_000, | ||
| }) | ||
| } | ||
| return cachedDecoder |
There was a problem hiding this comment.
The literal values 0.01 and 100_000 for profiling rate and recompile interval are repeated in three locations across two files (serde_protobuf.go, processor_protobuf.go x2) without named constants. Per the project's Magic Numbers code style rule:
Name all numeric constants. Every literal number in logic must have a clear meaning through a named constant or variable.
Define these as named constants (or a package-level default ProfilingOptions) in common and reference them from all three call sites. This also reduces the risk of the values diverging across locations.
|
Commits Review
|
|
Commits Review LGTM |
This reverts commit 4410665.
The leak comes from the pool holding on to messages that are unused and this keeps old parsers alive. This fixes that issue.
b2733d9 to
95c6253
Compare
| if err := msg.Unmarshal(buf, hyperpb.WithRecordProfile(state.profile, p.opts.Rate)); err != nil { | ||
| return err |
There was a problem hiding this comment.
Missing error wrapping: The unmarshal error is returned bare, but the existing dynamicPbParser.WithDecoded wraps it with message-type context:
return fmt.Errorf("unmarshalling protobuf message: '%v': %w", p.msgType.Descriptor().FullName(), err)Since ProtobufDecoder implementations are interchangeable, error messages should be consistent. Wrap the error here with equivalent context (message type full name) so callers get the same diagnostic information regardless of which decoder is active. This also aligns with the project error handling pattern requiring fmt.Errorf wrapping with %w.
|
Commits Review
|
|
Commits Review LGTM |
RecompileIntervalin hyperpb decoder