Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session_timeout bug fix & Local Build Process fixup #119

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.13-alpine AS build
FROM golang:1.16-alpine AS build

RUN apk add --update zstd-static zstd-dev make gcc musl-dev git
RUN apk add --no-cache --update zstd-static zstd-dev make gcc musl-dev git libc6-compat
RUN go get golang.org/x/lint/golint
RUN mkdir -p /go/src/github.com/Vertamedia/chproxy
WORKDIR /go/src/github.com/Vertamedia/chproxy
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ clean:

release-build:
@echo "Ver: $(BUILD_TAG), OPTS: $(BUILD_OPTS)"
GOOS=linux GOARCH=amd64 go build $(BUILD_OPTS)
rm chproxy-linux-amd64-*.tar.gz
@GOOS=linux GOARCH=amd64 go build $(BUILD_OPTS)
@if [ -f chproxy-linux-amd64-*.tar.gz ]; then rm chproxy-linux-amd64-*.tar.gz; fi
tar czf chproxy-linux-amd64-$(BUILD_TAG).tar.gz chproxy

release: format lint test clean release-build
Expand Down
22 changes: 19 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -285,21 +286,36 @@ func TestServe(t *testing.T) {
startHTTP,
},
{
"http POST request with session id",
"http POST request with session_id and session_timeout",
"testdata/http-session-id.yml",
func(t *testing.T) {
sessionName := "name"
sessionTimeout := 900
req, err := http.NewRequest("POST",
"http://127.0.0.1:9090/?query_id=45395792-a432-4b92-8cc9-536c14e1e1a9&extremes=0&session_id=default-session-id233",
"http://127.0.0.1:9090/?query_id=45395792-a432-4b92-8cc9-536c14e1e1a9&extremes=0&session_id="+sessionName+"&session_timeout="+strconv.Itoa(sessionTimeout),
bytes.NewBufferString("SELECT * FROM system.numbers LIMIT 10"))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;") // This makes it work

checkErr(t, err)
resp, err := http.DefaultClient.Do(req)
checkErr(t, err)

if resp.StatusCode != http.StatusOK || resp.StatusCode != http.StatusOK && resp.Header.Get("X-Clickhouse-Server-Session-Id") == "" {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d; expected: %d", resp.StatusCode, http.StatusOK)
}

// verify correctness of session_id
_sessionName := resp.Header.Get("X-Clickhouse-Server-Session-Id")
if _sessionName != sessionName {
t.Fatalf("unexpected value of X-Clickhouse-Server-Session-Id: %s; expected: %s", _sessionName, sessionName)
}

// verify correctness of session_id
_sessionTimeout, _ := strconv.Atoi(resp.Header.Get("X-Clickhouse-Server-Session-Timeout"))
if _sessionTimeout != sessionTimeout {
t.Fatalf("unexpected value of X-Clickhouse-Server-Session-Timeout: %d; expected: %d", _sessionTimeout, sessionTimeout)
}

resp.Body.Close()
},
startHTTP,
Expand Down
5 changes: 5 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (rp *reverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("X-ClickHouse-Server-Session-Id", s.sessionId)
}

// publish session_timeout if needed
if s.sessionId != "" {
rw.Header().Set("X-ClickHouse-Server-Session-Timeout", strconv.Itoa(s.sessionTimeout))
}

if s.user.cache == nil {
rp.proxyRequest(s, srw, srw, req)
} else {
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func getSessionId(req *http.Request) string {
func getSessionTimeout(req *http.Request) int {
params := req.URL.Query()
sessionTimeout, err := strconv.Atoi(params.Get("session_timeout"))
if err != nil && sessionTimeout > 0 {
if err == nil && sessionTimeout > 0 {
return sessionTimeout
}
return 60
Expand Down