Skip to content

Commit 1219971

Browse files
committed
GT-593 External versioning
1 parent 60bfa00 commit 1219971

File tree

6 files changed

+97
-47
lines changed

6 files changed

+97
-47
lines changed

v2/connection/connection.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ type ArangoDBConfiguration struct {
4646
DriverFlags []string
4747

4848
// Compression is used to enable compression between client and server
49-
Compression *Compression
49+
Compression *CompressionConfig
5050
}
5151

52-
// Compression is used to enable compression for the connection
53-
type Compression struct {
54-
// Compression is used to enable compression for the requests
52+
// CompressionConfig is used to enable compression for the connection
53+
type CompressionConfig struct {
54+
// CompressionConfig is used to enable compression for the requests
5555
CompressionType CompressionType
5656

5757
// ResponseCompressionEnabled is used to enable compression for the responses (requires server side adjustments)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package connection
22+
23+
import (
24+
"compress/gzip"
25+
"compress/zlib"
26+
"io"
27+
28+
"github.com/pkg/errors"
29+
30+
"github.com/arangodb/go-driver/v2/log"
31+
)
32+
33+
type Compression interface {
34+
ApplyRequestHeaders(r Request)
35+
ApplyRequestCompression(r *httpRequest, rootWriter io.Writer) (io.WriteCloser, error)
36+
}
37+
38+
type compression struct {
39+
config *CompressionConfig
40+
}
41+
42+
func newCompression(config *CompressionConfig) Compression {
43+
return &compression{
44+
config: config,
45+
}
46+
}
47+
48+
func (g compression) ApplyRequestHeaders(r Request) {
49+
if g.config != nil && g.config.ResponseCompressionEnabled {
50+
if g.config.CompressionType == "gzip" {
51+
r.AddHeader("Accept-Encoding", "gzip")
52+
} else if g.config.CompressionType == "deflate" {
53+
r.AddHeader("Accept-Encoding", "deflate")
54+
}
55+
}
56+
}
57+
58+
func (g compression) ApplyRequestCompression(r *httpRequest, rootWriter io.Writer) (io.WriteCloser, error) {
59+
config := g.config
60+
61+
if config != nil && config.RequestCompressionEnabled {
62+
if config.CompressionType == "gzip" {
63+
r.headers["Content-Encoding"] = "gzip"
64+
65+
gzipWriter, err := gzip.NewWriterLevel(rootWriter, config.RequestCompressionLevel)
66+
if err != nil {
67+
log.Errorf(err, "error creating gzip writer")
68+
return nil, err
69+
}
70+
return gzipWriter, nil
71+
} else if config.CompressionType == "deflate" {
72+
r.headers["Content-Encoding"] = "deflate"
73+
74+
zlibWriter, err := zlib.NewWriterLevel(rootWriter, config.RequestCompressionLevel)
75+
if err != nil {
76+
log.Errorf(err, "error creating zlib writer")
77+
return nil, err
78+
}
79+
80+
return zlibWriter, nil
81+
} else {
82+
return nil, errors.Errorf("unsupported compression type: %s", config.CompressionType)
83+
}
84+
}
85+
86+
return nil, nil
87+
}

v2/connection/connection_http_internal.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (j *httpConnection) bodyReadFunc(decoder Decoder, req *httpRequest, stream
311311
if !stream {
312312
return func() (io.Reader, error) {
313313
b := bytes.NewBuffer([]byte{})
314-
compressedWriter, err := j.applyCompression(req, b)
314+
compressedWriter, err := newCompression(j.config.Compression).ApplyRequestCompression(req, b)
315315
if err != nil {
316316
log.Errorf(err, "error applying compression")
317317
return nil, err
@@ -335,7 +335,7 @@ func (j *httpConnection) bodyReadFunc(decoder Decoder, req *httpRequest, stream
335335
return func() (io.Reader, error) {
336336
reader, writer := io.Pipe()
337337

338-
compressedWriter, err := j.applyCompression(req, writer)
338+
compressedWriter, err := newCompression(j.config.Compression).ApplyRequestCompression(req, writer)
339339
if err != nil {
340340
log.Errorf(err, "error applying compression")
341341
return nil, err
@@ -361,34 +361,3 @@ func (j *httpConnection) bodyReadFunc(decoder Decoder, req *httpRequest, stream
361361
}
362362
}
363363
}
364-
365-
func (j *httpConnection) applyCompression(req *httpRequest, rootWriter io.Writer) (io.WriteCloser, error) {
366-
compression := j.config.Compression
367-
368-
if compression != nil && compression.RequestCompressionEnabled {
369-
if compression.CompressionType == "gzip" {
370-
req.headers["Content-Encoding"] = "gzip"
371-
372-
gzipWriter, err := gzip.NewWriterLevel(rootWriter, compression.RequestCompressionLevel)
373-
if err != nil {
374-
log.Errorf(err, "error creating gzip writer")
375-
return nil, err
376-
}
377-
return gzipWriter, nil
378-
} else if compression.CompressionType == "deflate" {
379-
req.headers["Content-Encoding"] = "deflate"
380-
381-
zlibWriter, err := zlib.NewWriterLevel(rootWriter, compression.RequestCompressionLevel)
382-
if err != nil {
383-
log.Errorf(err, "error creating zlib writer")
384-
return nil, err
385-
}
386-
387-
return zlibWriter, nil
388-
} else {
389-
return nil, errors.Errorf("unsupported compression type: %s", compression.CompressionType)
390-
}
391-
}
392-
393-
return nil, nil
394-
}

v2/connection/modifiers.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,7 @@ func applyArangoDBConfiguration(config ArangoDBConfiguration, ctx context.Contex
104104
}
105105
}
106106

107-
if config.Compression != nil && config.Compression.ResponseCompressionEnabled {
108-
if config.Compression.CompressionType == "gzip" {
109-
r.AddHeader("Accept-Encoding", "gzip")
110-
} else if config.Compression.CompressionType == "deflate" {
111-
r.AddHeader("Accept-Encoding", "deflate")
112-
}
113-
}
107+
newCompression(config.Compression).ApplyRequestHeaders(r)
114108

115109
return nil
116110
}

v2/tests/call_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func Test_Compression_Builtin(t *testing.T) {
116116

117117
for _, tc := range testCases {
118118
config := client.Connection().GetConfiguration()
119-
config.Compression = &connection.Compression{
119+
config.Compression = &connection.CompressionConfig{
120120
CompressionType: tc.compression,
121121
RequestCompressionEnabled: tc.request,
122122
RequestCompressionLevel: tc.level,
@@ -158,7 +158,7 @@ func Test_Compression_Raw(t *testing.T) {
158158

159159
for _, tc := range testCases {
160160
config := client.Connection().GetConfiguration()
161-
config.Compression = &connection.Compression{
161+
config.Compression = &connection.CompressionConfig{
162162
CompressionType: tc.compression,
163163
RequestCompressionEnabled: tc.request,
164164
RequestCompressionLevel: tc.level,

v2/tests/run_wrap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func WrapConnectionFactory(t *testing.T, w WrapperConnectionFactory, wo ...WrapO
161161
func applyCompression(conn connection.Connection) {
162162
if os.Getenv("ENABLE_DATABASE_EXTRA_FEATURES") == "true" {
163163
cmp := conn.GetConfiguration()
164-
cmp.Compression = &connection.Compression{
164+
cmp.Compression = &connection.CompressionConfig{
165165
CompressionType: connection.RequestCompressionTypeDeflate,
166166
RequestCompressionLevel: 9,
167167
ResponseCompressionEnabled: true,

0 commit comments

Comments
 (0)