Skip to content

Commit e6a224d

Browse files
authored
Update Schema Registry client (#3135)
This is a follow-up to twmb/franz-go#867. Since `sr.URLs()` from franz-go doesn't validate URLs during client initialisation, I decided to leave our `url.Parse()` in there because this way we can catch malformed URLs on startup, before any HTTP requests are attempted. Also update the changelog. We forgot to do it in #3134 Signed-off-by: Mihai Todor <todormihai@gmail.com>
1 parent e0de935 commit e6a224d

File tree

7 files changed

+18
-83
lines changed

7 files changed

+18
-83
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99

1010
- New `mysql_cdc` input supporting change data capture (CDC) from MySQL. (@rockwotj, @le-vlad)
1111
- Field `instance_id` added to `kafka`, `kafka_franz`, `ockam_kafka`, `redpanda`, `redpanda_common`, and `redpanda_migrator` inputs. (@rockwotj)
12+
- Fields `rebalance_timeout`, `session_timeout` and `heartbeat_interval` added to the `kafka_franz`, `redpanda`, `redpanda_common`, `redpanda_migrator` and `ockam_kafka` inputs. (@rockwotj)
1213

1314
## 4.45.1 - 2025-01-17
1415

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ require (
127127
github.com/twmb/franz-go v1.18.0
128128
github.com/twmb/franz-go/pkg/kadm v1.13.0
129129
github.com/twmb/franz-go/pkg/kmsg v1.9.0
130-
github.com/twmb/franz-go/pkg/sr v1.2.0
130+
github.com/twmb/franz-go/pkg/sr v1.3.0
131131
github.com/vmihailenco/msgpack/v5 v5.4.1
132132
github.com/xdg-go/scram v1.1.2
133133
github.com/xeipuuv/gojsonschema v1.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,8 +1889,8 @@ github.com/twmb/franz-go/pkg/kadm v1.13.0 h1:bJq4C2ZikUE2jh/wl9MtMTQ/kpmnBgVFh8X
18891889
github.com/twmb/franz-go/pkg/kadm v1.13.0/go.mod h1:VMvpfjz/szpH9WB+vGM+rteTzVv0djyHFimci9qm2C0=
18901890
github.com/twmb/franz-go/pkg/kmsg v1.9.0 h1:JojYUph2TKAau6SBtErXpXGC7E3gg4vGZMv9xFU/B6M=
18911891
github.com/twmb/franz-go/pkg/kmsg v1.9.0/go.mod h1:CMbfazviCyY6HM0SXuG5t9vOwYDHRCSrJJyBAe5paqg=
1892-
github.com/twmb/franz-go/pkg/sr v1.2.0 h1:zYr0Ly7KLFfeCGaSr8teN6LvAVeYVrZoUsyyPHTYB+M=
1893-
github.com/twmb/franz-go/pkg/sr v1.2.0/go.mod h1:gpd2Xl5/prkj3gyugcL+rVzagjaxFqMgvKMYcUlrpDw=
1892+
github.com/twmb/franz-go/pkg/sr v1.3.0 h1:UlXpZ2suGgylzQBUb6Wn1jzqVShoPGzt7BbixznJ4qo=
1893+
github.com/twmb/franz-go/pkg/sr v1.3.0/go.mod h1:gpd2Xl5/prkj3gyugcL+rVzagjaxFqMgvKMYcUlrpDw=
18941894
github.com/uptrace/bun v1.1.12 h1:sOjDVHxNTuM6dNGaba0wUuz7KvDE1BmNu9Gqs2gJSXQ=
18951895
github.com/uptrace/bun v1.1.12/go.mod h1:NPG6JGULBeQ9IU6yHp7YGELRa5Agmd7ATZdz4tGZ6z0=
18961896
github.com/uptrace/bun/dialect/pgdialect v1.1.12 h1:m/CM1UfOkoBTglGO5CUTKnIKKOApOYxkcP2qn0F9tJk=

internal/impl/confluent/processor_schema_registry_decode_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ basic_auth:
7373
require.NoError(t, err)
7474

7575
e, err := newSchemaRegistryDecoderFromConfig(conf, service.MockResources())
76-
if e != nil {
77-
assert.Equal(t, test.expectedBaseURL, e.client.SchemaRegistryBaseURL.String())
78-
}
79-
8076
if err == nil {
8177
_ = e.Close(context.Background())
8278
}

internal/impl/confluent/processor_schema_registry_encode_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ subject: foo
106106
require.NoError(t, err)
107107

108108
e, err := newSchemaRegistryEncoderFromConfig(conf, service.MockResources())
109-
if e != nil {
110-
assert.Equal(t, test.expectedBaseURL, e.client.SchemaRegistryBaseURL.String())
111-
}
112-
113109
if err == nil {
114110
_ = e.Close(context.Background())
115111
}

internal/impl/confluent/sr/client.go

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ import (
1919
"crypto/tls"
2020
"fmt"
2121
"io/fs"
22-
"net"
2322
"net/http"
2423
"net/url"
2524
"slices"
26-
"time"
2725

2826
"github.com/twmb/franz-go/pkg/sr"
2927

@@ -32,26 +30,7 @@ import (
3230

3331
// Client is used to make requests to a schema registry.
3432
type Client struct {
35-
SchemaRegistryBaseURL *url.URL
36-
clientSR *sr.Client
37-
requestSigner func(f fs.FS, req *http.Request) error
38-
mgr *service.Resources
39-
}
40-
41-
type roundTripper struct {
42-
reqSigner func(req *http.Request) error
43-
*http.Transport
44-
}
45-
46-
func (rt *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
47-
// This is naughty, but it's probably fine...
48-
// The `RoundTrip` docs state that "RoundTrip should not modify the request, except for consuming and closing the Request's Body."
49-
// This is because the following code https://github.com/golang/go/blob/e25b913127ac8ba26c4ecc39288c7f8781f4ef5d/src/net/http/client.go#L246-L252
50-
// already tries to set the `Authorization` header if `req.URL.User` is already set, but `reqSigner` replicates the same functionality anyway.
51-
if err := rt.reqSigner(req); err != nil {
52-
return nil, err
53-
}
54-
return rt.Transport.RoundTrip(req)
33+
Client *sr.Client
5534
}
5635

5736
// NewClient creates a new schema registry client.
@@ -61,55 +40,18 @@ func NewClient(
6140
tlsConf *tls.Config,
6241
mgr *service.Resources,
6342
) (*Client, error) {
64-
u, err := url.Parse(urlStr)
43+
_, err := url.Parse(urlStr)
6544
if err != nil {
6645
return nil, fmt.Errorf("failed to parse url: %w", err)
6746
}
6847

69-
reqSignerWrapped := func(req *http.Request) error { return reqSigner(mgr.FS(), req) }
70-
71-
// Timeout copied from https://github.com/twmb/franz-go/blob/cea7aa5d803781e5f0162187795482ba1990c729/pkg/sr/client.go#L73
72-
hClient := &http.Client{Timeout: 5 * time.Second}
73-
if c, ok := http.DefaultTransport.(*http.Transport); ok {
74-
cloned := c.Clone()
75-
cloned.TLSClientConfig = tlsConf
76-
hClient.Transport = &roundTripper{
77-
reqSigner: reqSignerWrapped,
78-
Transport: cloned,
79-
}
80-
} else {
81-
hClient.Transport = &roundTripper{
82-
reqSigner: reqSignerWrapped,
83-
// Copied from https://github.com/twmb/franz-go/blob/cea7aa5d803781e5f0162187795482ba1990c729/pkg/sr/clientopt.go#L48-L68
84-
// TODO: Why are we setting `MaxIdleConnsPerHost: 100`? It's not set in `http.DefaultTransport`.
85-
// Note: `http.DefaultMaxIdleConnsPerHost` is 2.
86-
Transport: &http.Transport{
87-
Proxy: http.ProxyFromEnvironment,
88-
DialContext: (&net.Dialer{
89-
Timeout: 30 * time.Second,
90-
KeepAlive: 30 * time.Second,
91-
}).DialContext,
92-
TLSClientConfig: tlsConf,
93-
ForceAttemptHTTP2: true,
94-
MaxIdleConns: 100,
95-
MaxIdleConnsPerHost: 100,
96-
IdleConnTimeout: 90 * time.Second,
97-
TLSHandshakeTimeout: 10 * time.Second,
98-
ExpectContinueTimeout: 1 * time.Second,
99-
},
100-
}
101-
}
102-
103-
clientSR, err := sr.NewClient(sr.HTTPClient(hClient), sr.URLs(urlStr))
48+
clientSR, err := sr.NewClient(sr.URLs(urlStr), sr.PreReq(func(req *http.Request) error { return reqSigner(mgr.FS(), req) }))
10449
if err != nil {
10550
return nil, fmt.Errorf("failed to init client: %w", err)
10651
}
10752

10853
return &Client{
109-
clientSR: clientSR,
110-
SchemaRegistryBaseURL: u,
111-
requestSigner: reqSigner,
112-
mgr: mgr,
54+
Client: clientSR,
11355
}, nil
11456
}
11557

@@ -119,7 +61,7 @@ func (c *Client) GetSchemaByID(ctx context.Context, id int, includeDeleted bool)
11961
ctx = sr.WithParams(ctx, sr.ShowDeleted)
12062
}
12163

122-
schema, err := c.clientSR.SchemaByID(ctx, id)
64+
schema, err := c.Client.SchemaByID(ctx, id)
12365
if err != nil {
12466
return sr.Schema{}, fmt.Errorf("schema %d not found by registry: %s", id, err)
12567
}
@@ -132,12 +74,12 @@ func (c *Client) GetSubjectsBySchemaID(ctx context.Context, id int, includeDelet
13274
ctx = sr.WithParams(ctx, sr.ShowDeleted)
13375
}
13476

135-
return c.clientSR.SubjectsByID(ctx, id)
77+
return c.Client.SubjectsByID(ctx, id)
13678
}
13779

13880
// GetLatestSchemaVersionForSchemaIDAndSubject gets the latest version of a schema by its global identifier scoped to the provided subject.
13981
func (c *Client) GetLatestSchemaVersionForSchemaIDAndSubject(ctx context.Context, id int, subject string) (versionID int, err error) {
140-
svs, err := c.clientSR.SchemaVersionsByID(ctx, id)
82+
svs, err := c.Client.SchemaVersionsByID(ctx, id)
14183
if err != nil {
14284
return -1, fmt.Errorf("failed to fetch schema versions for ID %d and subject %q", id, subject)
14385
}
@@ -166,10 +108,10 @@ func (c *Client) GetSchemaBySubjectAndVersion(ctx context.Context, subject strin
166108
var schema sr.SubjectSchema
167109
var err error
168110
if version != nil {
169-
schema, err = c.clientSR.SchemaByVersion(ctx, subject, *version)
111+
schema, err = c.Client.SchemaByVersion(ctx, subject, *version)
170112
} else {
171113
// Setting version to -1 will return the latest schema.
172-
schema, err = c.clientSR.SchemaByVersion(ctx, subject, -1)
114+
schema, err = c.Client.SchemaByVersion(ctx, subject, -1)
173115
}
174116
if err != nil {
175117
return sr.SubjectSchema{}, err
@@ -180,7 +122,7 @@ func (c *Client) GetSchemaBySubjectAndVersion(ctx context.Context, subject strin
180122

181123
// GetMode returns the mode of the Schema Registry instance.
182124
func (c *Client) GetMode(ctx context.Context) (string, error) {
183-
res := c.clientSR.Mode(ctx)
125+
res := c.Client.Mode(ctx)
184126
// There will be one and only one element in the response.
185127
if res[0].Err != nil {
186128
return "", fmt.Errorf("request failed: %s", res[0].Err)
@@ -195,7 +137,7 @@ func (c *Client) GetSubjects(ctx context.Context, includeDeleted bool) ([]string
195137
ctx = sr.WithParams(ctx, sr.ShowDeleted)
196138
}
197139

198-
return c.clientSR.Subjects(ctx)
140+
return c.Client.Subjects(ctx)
199141
}
200142

201143
// GetVersionsForSubject returns the versions for a given subject.
@@ -204,12 +146,12 @@ func (c *Client) GetVersionsForSubject(ctx context.Context, subject string, incl
204146
ctx = sr.WithParams(ctx, sr.ShowDeleted)
205147
}
206148

207-
return c.clientSR.SubjectVersions(ctx, subject)
149+
return c.Client.SubjectVersions(ctx, subject)
208150
}
209151

210152
// CreateSchema creates a new schema for the given subject.
211153
func (c *Client) CreateSchema(ctx context.Context, subject string, schema sr.Schema) (int, error) {
212-
ss, err := c.clientSR.CreateSchema(ctx, subject, schema)
154+
ss, err := c.Client.CreateSchema(ctx, subject, schema)
213155
if err != nil {
214156
return -1, fmt.Errorf("failed to create schema for subject %q: %s", subject, err)
215157
}

internal/impl/kafka/enterprise/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func deleteSubject(t *testing.T, url string, subject string, hardDelete bool) {
282282
deleteMode = franz_sr.HardDelete
283283
}
284284

285-
_, err = client.DeleteSubject(context.Background(), subject, franz_sr.DeleteHow(deleteMode))
285+
_, err = client.DeleteSubject(context.Background(), subject, deleteMode)
286286
require.NoError(t, err)
287287
}
288288

0 commit comments

Comments
 (0)