Skip to content

Commit

Permalink
feat(fxgcppubsub): Fixed race conditions on avro binary codec (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox authored Jul 8, 2024
1 parent d8256de commit 8d2f8e9
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions fxgcppubsub/codec/avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@ var (

// AvroBinaryCodec is a Codec implementation for encoding and decoding with avro schema in binary format.
type AvroBinaryCodec struct {
api avro.API
schema avro.Schema
}

// NewAvroBinaryCodec returns a new AvroBinaryCodec instance.
func NewAvroBinaryCodec(schemaDefinition string) (*AvroBinaryCodec, error) {
api := avro.Config{}.Freeze()

schema, err := avro.Parse(schemaDefinition)
if err != nil {
return nil, fmt.Errorf("cannot parse avro schema: %w", err)
}

return &AvroBinaryCodec{schema: schema}, nil
return &AvroBinaryCodec{
api: api,
schema: schema,
}, nil
}

// Encode encodes in avro binary format the provided input.
func (c *AvroBinaryCodec) Encode(in any) ([]byte, error) {
out, err := avro.Marshal(c.schema, in)
out, err := c.api.Marshal(c.schema, in)
if err != nil {
return nil, fmt.Errorf("cannot encode avro binary: %w", err)
}
Expand All @@ -40,7 +46,7 @@ func (c *AvroBinaryCodec) Encode(in any) ([]byte, error) {

// Decode decodes from avro binary format the provided input.
func (c *AvroBinaryCodec) Decode(enc []byte, out any) error {
err := avro.Unmarshal(c.schema, enc, out)
err := c.api.Unmarshal(c.schema, enc, out)
if err != nil {
return fmt.Errorf("cannot decode avro binary: %w", err)
}
Expand Down

0 comments on commit 8d2f8e9

Please sign in to comment.