Skip to content

Commit

Permalink
parsed uploaders client during startup
Browse files Browse the repository at this point in the history
* parsed tls configs during startup instead of during upload operation
* reuse http client for uploaders
  • Loading branch information
lordvidex committed Jul 26, 2023
1 parent 8e26313 commit 93bb77d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
29 changes: 4 additions & 25 deletions uploader/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"go.uber.org/zap"

"github.com/lomik/carbon-clickhouse/helper/config"
"github.com/lomik/carbon-clickhouse/helper/stop"
)

Expand Down Expand Up @@ -237,41 +236,21 @@ func (u *Base) insertRowBinary(table string, data io.Reader) error {

q.Set("query", fmt.Sprintf("INSERT INTO %s FORMAT RowBinary", table))
p.RawQuery = q.Encode()
queryUrl := p.String()
queryURL := p.String()

var req *http.Request

if u.config.CompressData {
req, err = http.NewRequest("POST", queryUrl, compress(data))
req, err = http.NewRequest("POST", queryURL, compress(data))
req.Header.Add("Content-Encoding", "gzip")
} else {
req, err = http.NewRequest("POST", queryUrl, data)
req, err = http.NewRequest("POST", queryURL, data)
}

if err != nil {
return err
}

transport := &http.Transport{DisableKeepAlives: true}

if u.config.TLS != nil {
tlsConfig, warns, err := config.ParseClientTLSConfig(u.config.TLS)
if err != nil {
return err
}
if len(warns) > 0 {
u.logger.Warn("insecure options detected, while parsing HTTP Client TLS Config for uploader",
zap.Strings("warnings", warns),
)
}
transport.TLSClientConfig = tlsConfig
}

client := &http.Client{
Timeout: u.config.Timeout.Value(),
Transport: transport,
}
resp, err := client.Do(req)
resp, err := u.config.client.Do(req)
if err != nil {
return err
}
Expand Down
40 changes: 39 additions & 1 deletion uploader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package uploader

import (
"fmt"
"net/http"
"net/url"
"time"

"go.uber.org/zap"

"github.com/lomik/zapwriter"

"github.com/lomik/carbon-clickhouse/helper/config"
)

Expand All @@ -19,11 +25,12 @@ type Config struct {
URL string `toml:"url"`
CacheTTL *config.Duration `toml:"cache-ttl"`
IgnoredPatterns []string `toml:"ignored-patterns,omitempty"` // points, points-reverse
CompressData bool `toml:"compress-data"` //compress data while sending to clickhouse
CompressData bool `toml:"compress-data"` // compress data while sending to clickhouse
IgnoredTaggedMetrics []string `toml:"ignored-tagged-metrics"` // for tagged table; create only `__name__` tag for these metrics and ignore others
Hash string `toml:"hash"` // in index uploader store hash in memory instead of full metric
DisableDailyIndex bool `toml:"disable-daily-index"` // do not calculate and upload daily index to ClickHouse
hashFunc func(string) string `toml:"-"`
client *http.Client `toml:"-"`
}

func (cfg *Config) Parse() error {
Expand All @@ -36,11 +43,42 @@ func (cfg *Config) Parse() error {
}
}

if cfg.Timeout == nil {
cfg.Timeout = &config.Duration{Duration: time.Minute}
}

var known bool
cfg.hashFunc, known = knownHash[cfg.Hash]
if !known {
return fmt.Errorf("unknown hash function %#v", cfg.Hash)
}

cfg.client = &http.Client{
Timeout: cfg.Timeout.Value(),
}
if cfg.TLS != nil {
tlsConfig, warns, err := config.ParseClientTLSConfig(cfg.TLS)
if err != nil {
return err
}
p, err := url.Parse(cfg.URL)
if err != nil {
return err
}
if p.Scheme == "https" {
cfg.client.Transport = &http.Transport{
TLSClientConfig: tlsConfig,
}
} else {
warns = append(warns, "TLS configurations is ignored because scheme is not HTTPS")
}
if len(warns) > 0 {
logger := zapwriter.Logger("config")
logger.Warn("insecure options detected, while parsing HTTP Client TLS Config for uploader",
zap.Strings("warnings", warns),
)
}
}

return nil
}

0 comments on commit 93bb77d

Please sign in to comment.