Skip to content

Commit

Permalink
Merge pull request #180 from elBroom/add-resp-callback
Browse files Browse the repository at this point in the history
Add transport response callback
  • Loading branch information
vladlutkov authored May 14, 2024
2 parents a9c7cfe + 8da5ca7 commit a9ac4f8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser,
c.cancel = nil
return nil, fmt.Errorf("doRequest: transport failed to send a request to ClickHouse: %w", err)
}

if err = callCtxTransportCallback(ctx, req, resp); err != nil {
c.cancel = nil
return nil, fmt.Errorf("doRequest: transport callback: %w", err)
}

if resp.StatusCode != 200 {
msg, err := readResponse(resp)
c.cancel = nil
Expand All @@ -287,6 +293,7 @@ func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser,
// response
return nil, newError(string(msg))
}

return resp.Body, nil
}

Expand Down
28 changes: 28 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package clickhouse

import (
"context"
"net/http"
)

type ctxKey uint8

const (
ctxTransportCallbackKey ctxKey = iota + 1
)

// TransportCallback is a transport response callback. Called before processing the http response.
type TransportCallback func(*http.Request, *http.Response) error

// CtxAddTransportCallback adds callback to work with transport response.
func CtxAddTransportCallback(ctx context.Context, f TransportCallback) context.Context {
return context.WithValue(ctx, ctxTransportCallbackKey, f)
}

func callCtxTransportCallback(ctx context.Context, req *http.Request, resp *http.Response) error {
if f, ok := ctx.Value(ctxTransportCallbackKey).(TransportCallback); ok && f != nil {
return f(req, resp)
}

return nil
}
41 changes: 41 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package clickhouse

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_CtxAddTransportCallback(t *testing.T) {
var flag bool
ctx := context.Background()

ctx = CtxAddTransportCallback(ctx, func(_ *http.Request, _ *http.Response) error {
flag = true
return nil
})

assert.NoError(t, callCtxTransportCallback(ctx,
httptest.NewRequest(http.MethodGet, "http://localhost", nil), httptest.NewRecorder().Result(),
))
assert.True(t, flag)
}

func Test_CtxAddTransportCallback_err(t *testing.T) {
var flag bool
ctx := context.Background()

ctx = CtxAddTransportCallback(ctx, func(_ *http.Request, _ *http.Response) error {
flag = true
return errors.New("some error")
})

assert.EqualError(t, callCtxTransportCallback(ctx,
httptest.NewRequest(http.MethodGet, "http://localhost", nil), httptest.NewRecorder().Result(),
), "some error")
assert.True(t, flag)
}

0 comments on commit a9ac4f8

Please sign in to comment.