Skip to content

Commit a9ac4f8

Browse files
authored
Merge pull request #180 from elBroom/add-resp-callback
Add transport response callback
2 parents a9c7cfe + 8da5ca7 commit a9ac4f8

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Diff for: conn.go

+7
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser,
277277
c.cancel = nil
278278
return nil, fmt.Errorf("doRequest: transport failed to send a request to ClickHouse: %w", err)
279279
}
280+
281+
if err = callCtxTransportCallback(ctx, req, resp); err != nil {
282+
c.cancel = nil
283+
return nil, fmt.Errorf("doRequest: transport callback: %w", err)
284+
}
285+
280286
if resp.StatusCode != 200 {
281287
msg, err := readResponse(resp)
282288
c.cancel = nil
@@ -287,6 +293,7 @@ func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser,
287293
// response
288294
return nil, newError(string(msg))
289295
}
296+
290297
return resp.Body, nil
291298
}
292299

Diff for: ctx.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package clickhouse
2+
3+
import (
4+
"context"
5+
"net/http"
6+
)
7+
8+
type ctxKey uint8
9+
10+
const (
11+
ctxTransportCallbackKey ctxKey = iota + 1
12+
)
13+
14+
// TransportCallback is a transport response callback. Called before processing the http response.
15+
type TransportCallback func(*http.Request, *http.Response) error
16+
17+
// CtxAddTransportCallback adds callback to work with transport response.
18+
func CtxAddTransportCallback(ctx context.Context, f TransportCallback) context.Context {
19+
return context.WithValue(ctx, ctxTransportCallbackKey, f)
20+
}
21+
22+
func callCtxTransportCallback(ctx context.Context, req *http.Request, resp *http.Response) error {
23+
if f, ok := ctx.Value(ctxTransportCallbackKey).(TransportCallback); ok && f != nil {
24+
return f(req, resp)
25+
}
26+
27+
return nil
28+
}

Diff for: ctx_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package clickhouse
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_CtxAddTransportCallback(t *testing.T) {
14+
var flag bool
15+
ctx := context.Background()
16+
17+
ctx = CtxAddTransportCallback(ctx, func(_ *http.Request, _ *http.Response) error {
18+
flag = true
19+
return nil
20+
})
21+
22+
assert.NoError(t, callCtxTransportCallback(ctx,
23+
httptest.NewRequest(http.MethodGet, "http://localhost", nil), httptest.NewRecorder().Result(),
24+
))
25+
assert.True(t, flag)
26+
}
27+
28+
func Test_CtxAddTransportCallback_err(t *testing.T) {
29+
var flag bool
30+
ctx := context.Background()
31+
32+
ctx = CtxAddTransportCallback(ctx, func(_ *http.Request, _ *http.Response) error {
33+
flag = true
34+
return errors.New("some error")
35+
})
36+
37+
assert.EqualError(t, callCtxTransportCallback(ctx,
38+
httptest.NewRequest(http.MethodGet, "http://localhost", nil), httptest.NewRecorder().Result(),
39+
), "some error")
40+
assert.True(t, flag)
41+
}

0 commit comments

Comments
 (0)