forked from dangkaka/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cachedSchemaRegistry.go
97 lines (84 loc) · 3.73 KB
/
cachedSchemaRegistry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package kafka
import (
"github.com/linkedin/goavro/v2"
"sync"
)
// CachedSchemaRegistryClient is a schema registry client that will cache some data to improve performance
type CachedSchemaRegistryClient struct {
SchemaRegistryClient *SchemaRegistryClient
schemaCache map[int]*goavro.Codec
schemaCacheLock sync.RWMutex
schemaIdCache map[string]int
schemaIdCacheLock sync.RWMutex
}
func NewCachedSchemaRegistryClient(connect []string) *CachedSchemaRegistryClient {
SchemaRegistryClient := NewSchemaRegistryClient(connect)
return &CachedSchemaRegistryClient{SchemaRegistryClient: SchemaRegistryClient, schemaCache: make(map[int]*goavro.Codec), schemaIdCache: make(map[string]int)}
}
func NewCachedSchemaRegistryClientWithRetries(connect []string, retries int) *CachedSchemaRegistryClient {
SchemaRegistryClient := NewSchemaRegistryClientWithRetries(connect, retries)
return &CachedSchemaRegistryClient{SchemaRegistryClient: SchemaRegistryClient, schemaCache: make(map[int]*goavro.Codec), schemaIdCache: make(map[string]int)}
}
// GetSchema will return and cache the codec with the given id
func (client *CachedSchemaRegistryClient) GetSchema(id int) (*goavro.Codec, error) {
client.schemaCacheLock.RLock()
cachedResult := client.schemaCache[id]
client.schemaCacheLock.RUnlock()
if nil != cachedResult {
return cachedResult, nil
}
codec, err := client.SchemaRegistryClient.GetSchema(id)
if err != nil {
return nil, err
}
client.schemaCacheLock.Lock()
client.schemaCache[id] = codec
client.schemaCacheLock.Unlock()
return codec, nil
}
// GetSubjects returns a list of subjects
func (client *CachedSchemaRegistryClient) GetSubjects() ([]string, error) {
return client.SchemaRegistryClient.GetSubjects()
}
// GetVersions returns a list of all versions of a subject
func (client *CachedSchemaRegistryClient) GetVersions(subject string) ([]int, error) {
return client.SchemaRegistryClient.GetVersions(subject)
}
// GetSchemaByVersion returns the codec for a specific version of a subject
func (client *CachedSchemaRegistryClient) GetSchemaByVersion(subject string, version int) (*goavro.Codec, error) {
return client.SchemaRegistryClient.GetSchemaByVersion(subject, version)
}
// GetLatestSchema returns the highest version schema for a subject
func (client *CachedSchemaRegistryClient) GetLatestSchema(subject string) (*goavro.Codec, error) {
return client.SchemaRegistryClient.GetLatestSchema(subject)
}
// CreateSubject will return and cache the id with the given codec
func (client *CachedSchemaRegistryClient) CreateSubject(subject string, codec *goavro.Codec) (int, error) {
schemaJson := codec.Schema()
client.schemaIdCacheLock.RLock()
cachedResult, found := client.schemaIdCache[schemaJson]
client.schemaIdCacheLock.RUnlock()
if found {
return cachedResult, nil
}
id, err := client.SchemaRegistryClient.CreateSubject(subject, codec)
if err != nil {
return 0, err
}
client.schemaIdCacheLock.Lock()
client.schemaIdCache[schemaJson] = id
client.schemaIdCacheLock.Unlock()
return id, nil
}
// IsSchemaRegistered checks if a specific codec is already registered to a subject
func (client *CachedSchemaRegistryClient) IsSchemaRegistered(subject string, codec *goavro.Codec) (int, error) {
return client.SchemaRegistryClient.IsSchemaRegistered(subject, codec)
}
// DeleteSubject deletes the subject, should only be used in development
func (client *CachedSchemaRegistryClient) DeleteSubject(subject string) error {
return client.SchemaRegistryClient.DeleteSubject(subject)
}
// DeleteVersion deletes the a specific version of a subject, should only be used in development.
func (client *CachedSchemaRegistryClient) DeleteVersion(subject string, version int) error {
return client.SchemaRegistryClient.DeleteVersion(subject, version)
}