From d4638d4c4b0bd181e4fc31813470fca5e40ca6f8 Mon Sep 17 00:00:00 2001 From: Anton Nesterov Date: Fri, 23 Aug 2024 07:03:54 +0200 Subject: [PATCH] [fix] use response controller to flush the buffer --- README.md | 4 ++-- dal/__test__/builder.test.ts | 4 ++-- dal/__test__/srv/go.mod | 10 ++++++---- dal/__test__/srv/go.sum | 18 ++++++++---------- dal/__test__/srv/test.sqlite | Bin 0 -> 8192 bytes pkg/handler/query_handler.go | 13 ++++++------- 6 files changed, 24 insertions(+), 25 deletions(-) create mode 100644 dal/__test__/srv/test.sqlite diff --git a/README.md b/README.md index 4f7a231..d518196 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ **Data Access Layer** DAL is a proxy layer for SQL databases with a MongoDB inspired query interface. -It can be used as a Go or NodeJS package (requires compiler). +It can be used as a Go or NodeJS package (requires compiler). It is modular and allows to create your own proxy and apply custom middlewares. _Notes:_ @@ -126,5 +126,5 @@ The client uses a light builder and messagepack over http. It is relatively easy ## License -While in alpha stage the project is free for research purposes. +While in alpha stage the project is free for research purposes. Later it will be released under MIT-like license with AI/dataset exclusion terms. diff --git a/dal/__test__/builder.test.ts b/dal/__test__/builder.test.ts index 211ff65..59b60f8 100644 --- a/dal/__test__/builder.test.ts +++ b/dal/__test__/builder.test.ts @@ -22,7 +22,7 @@ test("Rows iter, no format", async () => { }) .Rows(); for await (const row of rows) { - console.log(row); + //console.log(row); expect(row.length).toBe(3); } expect(true).toBe(true); @@ -32,7 +32,7 @@ test("Rows iter, format", async () => { const dal = new DAL(options); const rows = dal.In("test t").Find({}).As(DTO).Rows(); for await (const row of rows) { - console.log(row); + //console.log(row); //expect(row.id).toBe(1); } expect(true).toBe(true); diff --git a/dal/__test__/srv/go.mod b/dal/__test__/srv/go.mod index 3c88777..84826c2 100644 --- a/dal/__test__/srv/go.mod +++ b/dal/__test__/srv/go.mod @@ -2,6 +2,8 @@ module srv go 1.22.6 +replace github.com/nesterow/dal/pkg/handler => ../../../pkg/handler + require ( github.com/mattn/go-sqlite3 v1.14.22 github.com/nesterow/dal/pkg/adapter v0.0.0-20240820192515-7a408c994181 @@ -9,10 +11,10 @@ require ( ) require ( - github.com/nesterow/dal/pkg/builder v0.0.0-20240820191021-7dcd046e6ca0 // indirect - github.com/nesterow/dal/pkg/filters v0.0.0-20240820191021-7dcd046e6ca0 // indirect - github.com/nesterow/dal/pkg/proto v0.0.0-20240820191021-7dcd046e6ca0 // indirect - github.com/nesterow/dal/pkg/utils v0.0.0-20240820191021-7dcd046e6ca0 // indirect + github.com/nesterow/dal/pkg/builder v0.0.0-20240820192515-7a408c994181 // indirect + github.com/nesterow/dal/pkg/filters v0.0.0-20240820192515-7a408c994181 // indirect + github.com/nesterow/dal/pkg/proto v0.0.0-20240820192515-7a408c994181 // indirect + github.com/nesterow/dal/pkg/utils v0.0.0-20240820192515-7a408c994181 // indirect github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/tinylib/msgp v1.2.0 // indirect diff --git a/dal/__test__/srv/go.sum b/dal/__test__/srv/go.sum index ae90b56..4aca4bf 100644 --- a/dal/__test__/srv/go.sum +++ b/dal/__test__/srv/go.sum @@ -2,16 +2,14 @@ github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/nesterow/dal/pkg/adapter v0.0.0-20240820192515-7a408c994181 h1:wwWMAG/Uu/layF1wu6dJiUej0zisaVQRfyTNqjJpxac= github.com/nesterow/dal/pkg/adapter v0.0.0-20240820192515-7a408c994181/go.mod h1:TCkoNn14Hgl+iVENr73GHaDVEuAg8KizH0oXaV2qMKs= -github.com/nesterow/dal/pkg/builder v0.0.0-20240820191021-7dcd046e6ca0 h1:S37CEWRRLdeLA3V8J4XEa8CmmO4Z+auifYf1GfNRo64= -github.com/nesterow/dal/pkg/builder v0.0.0-20240820191021-7dcd046e6ca0/go.mod h1:EmozdaZEPYE2+4qpK1cw0eIS8yx9LcrVRKstvq2dgCg= -github.com/nesterow/dal/pkg/filters v0.0.0-20240820191021-7dcd046e6ca0 h1:2CDPDKwR+PSGCI6dMbSqI5Zf8Ugg9nY2CMN87BDZ1Xg= -github.com/nesterow/dal/pkg/filters v0.0.0-20240820191021-7dcd046e6ca0/go.mod h1:1wKJbE/13P756EjkLU8xzbdhR+dJYfn6Vlw4J2jFJaA= -github.com/nesterow/dal/pkg/handler v0.0.0-20240820192515-7a408c994181 h1:dAzXf3pEYEKTF+WHJxmfy81XLPJ7BcFy3MEYYsLeyQE= -github.com/nesterow/dal/pkg/handler v0.0.0-20240820192515-7a408c994181/go.mod h1:yxMmRLpnj8PmBylCcVr0PJwpy2uLwNjsMFk36kGNWOE= -github.com/nesterow/dal/pkg/proto v0.0.0-20240820191021-7dcd046e6ca0 h1:p2VAOo7aMmFehU17S7aZ3tiwIMEjQUuzLHTNO4jQHWA= -github.com/nesterow/dal/pkg/proto v0.0.0-20240820191021-7dcd046e6ca0/go.mod h1:okcJheB01HZrsNryT2Hun1DjYI7Hj/+/oD6+4PVW3TU= -github.com/nesterow/dal/pkg/utils v0.0.0-20240820191021-7dcd046e6ca0 h1:CYDcRDYrXXD8fQdkXbbPMJvvLnBy4nI36BrRMQEsWUo= -github.com/nesterow/dal/pkg/utils v0.0.0-20240820191021-7dcd046e6ca0/go.mod h1:0XWmrxZfyoEAZoCOxP3quEOP7ZNK2Lhvtpeux8C74mw= +github.com/nesterow/dal/pkg/builder v0.0.0-20240820192515-7a408c994181 h1:Ex2XjUB7o4IRSWcgh8qgac8JQ2gbn6M+jUfom4zZccY= +github.com/nesterow/dal/pkg/builder v0.0.0-20240820192515-7a408c994181/go.mod h1:mVt1QOFEeGLdi+ZbMLzVw3LVtFB2tYWxZRouhkOThJU= +github.com/nesterow/dal/pkg/filters v0.0.0-20240820192515-7a408c994181 h1:Vi6v1T68/oayGfoX8k/FpgvfMsCUqxMsowTHKXY70VE= +github.com/nesterow/dal/pkg/filters v0.0.0-20240820192515-7a408c994181/go.mod h1:6aUr+2+D+184FTSXgziW6r1bvcKTY+Ie3TW4iET0g6I= +github.com/nesterow/dal/pkg/proto v0.0.0-20240820192515-7a408c994181 h1:HcGM9fagbbGa0r/6TRrg+lzRbW79sOr6yTczYkO1miw= +github.com/nesterow/dal/pkg/proto v0.0.0-20240820192515-7a408c994181/go.mod h1:MUS8KEmlNqjUuhNyK4TOkUwjT9893jmNm++ukVz2hzU= +github.com/nesterow/dal/pkg/utils v0.0.0-20240820192515-7a408c994181 h1:b/D2uq18D6CzAh+NUyD1cM7s/q/aQZEpXy57iKyzg8k= +github.com/nesterow/dal/pkg/utils v0.0.0-20240820192515-7a408c994181/go.mod h1:0XWmrxZfyoEAZoCOxP3quEOP7ZNK2Lhvtpeux8C74mw= github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 h1:jYi87L8j62qkXzaYHAQAhEapgukhenIMZRBKTNRLHJ4= github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/dal/__test__/srv/test.sqlite b/dal/__test__/srv/test.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..8467090a263d61dc6f4d65842e2b6ac870c50599 GIT binary patch literal 8192 zcmeI$F-rq66bJB^Yl^+0M3BSvjS5GXegW?!Nb%HqMrg+zy+Xlya2G_!j((JWA!lcI zcauWbb{77RJo0}b({D?j9*dQ$dD*mU>v&A3Bm`YC6H(uHqAt2-Aux-NBQG@C3h zG=~$uSS@&RZ}g2Wd0I~HvU1M1dYns~RGne6QF4Y&QU-hHDeLz6~ z0uX=z1Rwwb2tWV=5P$##An;EF22#?Qs%%>gdLb>F#>SDPM7gGO@3GiL|6B0!2kM|M A#{d8T literal 0 HcmV?d00001 diff --git a/pkg/handler/query_handler.go b/pkg/handler/query_handler.go index 67e8c4f..ddeb248 100644 --- a/pkg/handler/query_handler.go +++ b/pkg/handler/query_handler.go @@ -63,19 +63,17 @@ func QueryHandler(db adapter.DBAdapter) http.Handler { } defer rows.Close() + w.Header().Set("Connection", "Keep-Alive") w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Content-Type", "application/x-msgpack") - flusher, ok := w.(http.Flusher) - if !ok { - http.Error(w, "expected http.ResponseWriter to be an http.Flusher", http.StatusInternalServerError) - return - } columns, _ := rows.Columns() types, _ := rows.ColumnTypes() cols, _ := proto.MarshalRow(columns) w.Write(cols) - flusher.Flush() + + rc := http.NewResponseController(w) + rc.Flush() for rows.Next() { data := make([]interface{}, len(columns)) @@ -86,7 +84,8 @@ func QueryHandler(db adapter.DBAdapter) http.Handler { rows.Scan(data...) cols, _ := proto.MarshalRow(data) w.Write(cols) - flusher.Flush() + rc.Flush() } + }) }