@@ -19,11 +19,9 @@ import (
19
19
"crypto/tls"
20
20
"fmt"
21
21
"io/fs"
22
- "net"
23
22
"net/http"
24
23
"net/url"
25
24
"slices"
26
- "time"
27
25
28
26
"github.com/twmb/franz-go/pkg/sr"
29
27
@@ -32,26 +30,7 @@ import (
32
30
33
31
// Client is used to make requests to a schema registry.
34
32
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
55
34
}
56
35
57
36
// NewClient creates a new schema registry client.
@@ -61,55 +40,18 @@ func NewClient(
61
40
tlsConf * tls.Config ,
62
41
mgr * service.Resources ,
63
42
) (* Client , error ) {
64
- u , err := url .Parse (urlStr )
43
+ _ , err := url .Parse (urlStr )
65
44
if err != nil {
66
45
return nil , fmt .Errorf ("failed to parse url: %w" , err )
67
46
}
68
47
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 ) }))
104
49
if err != nil {
105
50
return nil , fmt .Errorf ("failed to init client: %w" , err )
106
51
}
107
52
108
53
return & Client {
109
- clientSR : clientSR ,
110
- SchemaRegistryBaseURL : u ,
111
- requestSigner : reqSigner ,
112
- mgr : mgr ,
54
+ Client : clientSR ,
113
55
}, nil
114
56
}
115
57
@@ -119,7 +61,7 @@ func (c *Client) GetSchemaByID(ctx context.Context, id int, includeDeleted bool)
119
61
ctx = sr .WithParams (ctx , sr .ShowDeleted )
120
62
}
121
63
122
- schema , err := c .clientSR .SchemaByID (ctx , id )
64
+ schema , err := c .Client .SchemaByID (ctx , id )
123
65
if err != nil {
124
66
return sr.Schema {}, fmt .Errorf ("schema %d not found by registry: %s" , id , err )
125
67
}
@@ -132,12 +74,12 @@ func (c *Client) GetSubjectsBySchemaID(ctx context.Context, id int, includeDelet
132
74
ctx = sr .WithParams (ctx , sr .ShowDeleted )
133
75
}
134
76
135
- return c .clientSR .SubjectsByID (ctx , id )
77
+ return c .Client .SubjectsByID (ctx , id )
136
78
}
137
79
138
80
// GetLatestSchemaVersionForSchemaIDAndSubject gets the latest version of a schema by its global identifier scoped to the provided subject.
139
81
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 )
141
83
if err != nil {
142
84
return - 1 , fmt .Errorf ("failed to fetch schema versions for ID %d and subject %q" , id , subject )
143
85
}
@@ -166,10 +108,10 @@ func (c *Client) GetSchemaBySubjectAndVersion(ctx context.Context, subject strin
166
108
var schema sr.SubjectSchema
167
109
var err error
168
110
if version != nil {
169
- schema , err = c .clientSR .SchemaByVersion (ctx , subject , * version )
111
+ schema , err = c .Client .SchemaByVersion (ctx , subject , * version )
170
112
} else {
171
113
// 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 )
173
115
}
174
116
if err != nil {
175
117
return sr.SubjectSchema {}, err
@@ -180,7 +122,7 @@ func (c *Client) GetSchemaBySubjectAndVersion(ctx context.Context, subject strin
180
122
181
123
// GetMode returns the mode of the Schema Registry instance.
182
124
func (c * Client ) GetMode (ctx context.Context ) (string , error ) {
183
- res := c .clientSR .Mode (ctx )
125
+ res := c .Client .Mode (ctx )
184
126
// There will be one and only one element in the response.
185
127
if res [0 ].Err != nil {
186
128
return "" , fmt .Errorf ("request failed: %s" , res [0 ].Err )
@@ -195,7 +137,7 @@ func (c *Client) GetSubjects(ctx context.Context, includeDeleted bool) ([]string
195
137
ctx = sr .WithParams (ctx , sr .ShowDeleted )
196
138
}
197
139
198
- return c .clientSR .Subjects (ctx )
140
+ return c .Client .Subjects (ctx )
199
141
}
200
142
201
143
// GetVersionsForSubject returns the versions for a given subject.
@@ -204,12 +146,12 @@ func (c *Client) GetVersionsForSubject(ctx context.Context, subject string, incl
204
146
ctx = sr .WithParams (ctx , sr .ShowDeleted )
205
147
}
206
148
207
- return c .clientSR .SubjectVersions (ctx , subject )
149
+ return c .Client .SubjectVersions (ctx , subject )
208
150
}
209
151
210
152
// CreateSchema creates a new schema for the given subject.
211
153
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 )
213
155
if err != nil {
214
156
return - 1 , fmt .Errorf ("failed to create schema for subject %q: %s" , subject , err )
215
157
}
0 commit comments