Skip to content

Commit

Permalink
Merge pull request #114 from go-faster/feat/tls
Browse files Browse the repository at this point in the history
feat(client): support TLS
  • Loading branch information
ernado authored Jun 3, 2022
2 parents b2fde1e + 66e6092 commit 404fa64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ arr.Row(0) // ["foo", "bar", "baz"]
- [ ] Improved i/o timeout handling
- [ ] Close connection on context cancellation in all cases
- [ ] Ensure that reads can't block forever
- [ ] TLS
- [x] TLS
- [ ] API UX Improvements (with 1.18 generics)
- [x] Enum
- [x] LowCardinality
Expand Down
39 changes: 33 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch

import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -302,9 +303,12 @@ type Options struct {
User string // "default"
Password string // blank string by default
Compression Compression // disabled by default
Dialer Dialer // defaults to net.Dialer
Settings []Setting // none by default

Dialer Dialer // defaults to net.Dialer
DialTimeout time.Duration // defaults to 1s
TLS *tls.Config // no TLS is used by default

// Additional OpenTelemetry instrumentation that will capture query body
// and other parameters.
//
Expand All @@ -319,10 +323,11 @@ type Options struct {

// Defaults for connection.
const (
DefaultDatabase = "default"
DefaultUser = "default"
DefaultHost = "127.0.0.1"
DefaultPort = 9000
DefaultDatabase = "default"
DefaultUser = "default"
DefaultHost = "127.0.0.1"
DefaultPort = 9000
DefaultDialTimeout = 1 * time.Second
)

func (o *Options) setDefaults() {
Expand All @@ -338,8 +343,13 @@ func (o *Options) setDefaults() {
if o.Address == "" {
o.Address = net.JoinHostPort(DefaultHost, strconv.Itoa(DefaultPort))
}
if o.DialTimeout == 0 {
o.DialTimeout = DefaultDialTimeout
}
if o.Dialer == nil {
o.Dialer = &net.Dialer{}
o.Dialer = &net.Dialer{
Timeout: o.DialTimeout,
}
}
if o.MeterProvider == nil {
o.MeterProvider = nonrecording.NewNoopMeterProvider()
Expand Down Expand Up @@ -458,6 +468,23 @@ func Dial(ctx context.Context, opt Options) (c *Client, err error) {
}()
}

if opt.TLS != nil {
netDialer := &net.Dialer{
Timeout: opt.DialTimeout,
}
if opt.Dialer != nil {
d, ok := opt.Dialer.(*net.Dialer)
if !ok {
return nil, errors.Errorf("tls dialer should be *net.Dialer, got %T", opt.Dialer)
}
netDialer = d
}
opt.Dialer = &tls.Dialer{
NetDialer: netDialer,
Config: opt.TLS,
}
}

conn, err := opt.Dialer.DialContext(ctx, "tcp", opt.Address)
if err != nil {
return nil, errors.Wrap(err, "dial")
Expand Down

0 comments on commit 404fa64

Please sign in to comment.