Skip to content

Commit 27274c2

Browse files
Cache parsed file descriptors (#1128)
* chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * chore: update repo semaphore config * Cache parsed file descriptors Fixes #969 * Clean up import --------- Co-authored-by: Confluent Jenkins Bot <[email protected]>
1 parent 89b6c2c commit 27274c2

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

schemaregistry/serde/protobuf/protobuf.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import (
2222
"io"
2323
"log"
2424
"strings"
25+
"sync"
2526

2627
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry"
28+
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/cache"
2729
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/confluent"
2830
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/confluent/types"
2931
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/serde"
@@ -69,7 +71,9 @@ type Serializer struct {
6971
// Deserializer represents a Protobuf deserializer
7072
type Deserializer struct {
7173
serde.BaseDeserializer
72-
ProtoRegistry *protoregistry.Types
74+
ProtoRegistry *protoregistry.Types
75+
schemaToDescCache cache.Cache
76+
schemaToDescCacheLock sync.RWMutex
7377
}
7478

7579
var _ serde.Serializer = new(Serializer)
@@ -337,8 +341,14 @@ func ignoreFile(name string) bool {
337341

338342
// NewDeserializer creates a Protobuf deserializer for Protobuf-generated objects
339343
func NewDeserializer(client schemaregistry.Client, serdeType serde.Type, conf *DeserializerConfig) (*Deserializer, error) {
340-
s := &Deserializer{}
341-
err := s.ConfigureDeserializer(client, serdeType, &conf.DeserializerConfig)
344+
cache, err := cache.NewLRUCache(1000)
345+
if err != nil {
346+
return nil, err
347+
}
348+
s := &Deserializer{
349+
schemaToDescCache: cache,
350+
}
351+
err = s.ConfigureDeserializer(client, serdeType, &conf.DeserializerConfig)
342352
if err != nil {
343353
return nil, err
344354
}
@@ -405,6 +415,12 @@ func (s *Deserializer) DeserializeInto(topic string, payload []byte, msg interfa
405415
}
406416

407417
func (s *Deserializer) toFileDesc(info schemaregistry.SchemaInfo) (*desc.FileDescriptor, error) {
418+
s.schemaToDescCacheLock.RLock()
419+
value, ok := s.schemaToDescCache.Get(info.Schema)
420+
s.schemaToDescCacheLock.RUnlock()
421+
if ok {
422+
return value.(*desc.FileDescriptor), nil
423+
}
408424
deps := make(map[string]string)
409425
err := serde.ResolveReferences(s.Client, info, deps)
410426
if err != nil {
@@ -433,7 +449,11 @@ func (s *Deserializer) toFileDesc(info schemaregistry.SchemaInfo) (*desc.FileDes
433449
if len(fileDescriptors) != 1 {
434450
return nil, fmt.Errorf("could not resolve schema")
435451
}
436-
return fileDescriptors[0], nil
452+
fd := fileDescriptors[0]
453+
s.schemaToDescCacheLock.Lock()
454+
s.schemaToDescCache.Put(info.Schema, fd)
455+
s.schemaToDescCacheLock.Unlock()
456+
return fd, nil
437457
}
438458

439459
func readMessageIndexes(payload []byte) (int, []int, error) {

0 commit comments

Comments
 (0)