-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_registry.go
120 lines (107 loc) · 3.4 KB
/
schema_registry.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package pulsavro
import (
"bytes"
"encoding/json"
"fmt"
"github.com/linkedin/goavro/v2"
"io"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
)
// SchemaRegistryClientInterface defines the api for all clients interfacing with schema registry
type SchemaRegistryClientInterface interface {
GetSchemaCodecByTopic(string) (*goavro.Codec, error)
CreateSchemaByTopic(string, string) error
}
// SchemaRegistryClient is a basic http client to interact with schema registry
type SchemaRegistryClient struct {
SchemaRegistryConnect []string
httpClient *http.Client
retries int
}
type schemaResponse struct {
Schema string `json:"data"`
}
const (
schemaByTopic = "/admin/v2/schemas/%s/schema"
contentType = "application/json"
timeout = 2 * time.Second
)
// NewSchemaRegistryClient creates a client to talk with the schema registry at the connect string
// By default it will retry failed requests (5XX responses and http errors) len(connect) number of times
func NewSchemaRegistryClient(connect []string) *SchemaRegistryClient {
client := &http.Client{
Timeout: timeout,
}
return &SchemaRegistryClient{connect, client, len(connect)}
}
// NewSchemaRegistryClientWithRetries creates an http client with a configurable amount of retries on 5XX responses
func NewSchemaRegistryClientWithRetries(connect []string, retries int) *SchemaRegistryClient {
client := &http.Client{
Timeout: timeout,
}
return &SchemaRegistryClient{connect, client, retries}
}
// GetSchemaCodecByTopic returns a goavro.Codec by topic
func (client *SchemaRegistryClient) GetSchemaCodecByTopic(topic string) (*goavro.Codec, error) {
resp, err := client.httpCall("GET", fmt.Sprintf(schemaByTopic, topic), nil)
if nil != err {
return nil, err
}
schema, err := parseSchema(resp)
if nil != err {
return nil, err
}
return goavro.NewCodec(schema.Schema)
}
// CreateSchemaByTopic creates a schema for the specified topic
func (client *SchemaRegistryClient) CreateSchemaByTopic(topic string, schema string) error {
buf := &bytes.Buffer{}
err := json.Compact(buf, []byte(schema))
if err != nil {
return err
}
payload := fmt.Sprintf(`{"type": "JSON","schema": "%s","properties": {}}`, strings.ReplaceAll(strings.TrimSpace(buf.String()), `"`, `\"`))
_, err = client.httpCall("POST", fmt.Sprintf(schemaByTopic, topic), strings.NewReader(payload))
return err
}
func parseSchema(str []byte) (*schemaResponse, error) {
var schema = new(schemaResponse)
err := json.Unmarshal(str, &schema)
return schema, err
}
func (client *SchemaRegistryClient) httpCall(method, uri string, payload io.Reader) ([]byte, error) {
nServers := len(client.SchemaRegistryConnect)
offset := rand.Intn(nServers)
for i := 0; ; i++ {
url := fmt.Sprintf("%s%s", client.SchemaRegistryConnect[(i+offset)%nServers], uri)
req, err := http.NewRequest(method, url, payload)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
resp, err := client.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if i < client.retries && (err != nil || retryable(resp)) {
continue
}
if err != nil {
return nil, err
}
if !isOK(resp) {
return nil, newError(resp)
}
return ioutil.ReadAll(resp.Body)
}
}
func retryable(resp *http.Response) bool {
return resp.StatusCode >= 500 && resp.StatusCode < 600
}
func isOK(resp *http.Response) bool {
return resp.StatusCode >= 200 && resp.StatusCode < 400
}