Skip to content

Commit

Permalink
feat: support insecure (#26)
Browse files Browse the repository at this point in the history
* support secure

* refactor options

* docs

* keepalive variable

* rename UserAgentOption
  • Loading branch information
yuanbohan authored Mar 6, 2024
1 parent a16fc70 commit 08930f5
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 133 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,25 @@ Initiate a Config for Client

```go
cfg := greptime.NewConfig("<host>").
WithPort(4001).
WithAuth("<username>", "<password>").
WithDatabase("<database>")
```

#### Options

##### Secure

```go
cfg.WithInsecure(false) // default insecure=true
```

##### keepalive

```go
cfg.WithKeepalive(time.Second*30, time.Second*5) // keepalive isn't enabled by default
```

### Client

```go
Expand All @@ -40,12 +55,12 @@ cli, err := greptime.NewClient(cfg)

- you can Insert data into GreptimeDB via different style:

- [Table style](#with-table)
- [ORM style](#with-struct-tag)
- [Table style](#table-style)
- [ORM style](#orm-style)

- streaming insert is to Send data into GreptimeDB without waiting for response.

#### With Table
#### Table style

you can define schema via Table and Column, and then AddRow to include the real data you want to write.

Expand Down Expand Up @@ -82,7 +97,7 @@ err := cli.StreamWrite(context.Background(), tbl)
affected, err := cli.CloseStream(ctx)
```

#### With Struct Tag
#### ORM style

If you prefer ORM style, and define column-field relationship via struct field tag, you can try the following way.

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Client struct {

// NewClient helps to create the greptimedb client, which will be responsible write data into GreptimeDB.
func NewClient(cfg *Config) (*Client, error) {
conn, err := grpc.Dial(cfg.GetEndpoint(), cfg.Options().Build()...)
conn, err := grpc.Dial(cfg.endpoint(), cfg.build()...)
if err != nil {
return nil, err
}
Expand Down
63 changes: 41 additions & 22 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ package greptime
import (
"fmt"
"time"

"google.golang.org/grpc"

"github.com/GreptimeTeam/greptimedb-ingester-go/options"
)

// Config is to define how the Client behaves.
Expand All @@ -27,24 +31,26 @@ import (
// you can find them in GreptimeCloud service detail page.
// - Database is the default database the client will operate on.
// But you can change the database in InsertRequest or QueryRequest.
// - DialOptions and CallOptions are for gRPC service.
// You can specify them or leave them empty.
type Config struct {
Host string // no scheme or port included. example: 127.0.0.1
Port int // default: 4001
Username string
Password string
Database string // the default database

keepaliveInterval time.Duration
keepaliveTimeout time.Duration
tls *options.TlsOption
options []grpc.DialOption
}

// NewConfig helps to init Config with host only
func NewConfig(host string) *Config {
return &Config{
Host: host,
Port: 4001,

options: []grpc.DialOption{
options.NewUserAgentOption(version).Build(),
},
}
}

Expand All @@ -60,37 +66,50 @@ func (c *Config) WithDatabase(database string) *Config {
return c
}

// WithAuth helps to specify the Basic Auth username and password
// WithAuth helps to specify the Basic Auth username and password.
// Leave them empty if you are in local environment.
func (c *Config) WithAuth(username, password string) *Config {
c.Username = username
c.Password = password
return c
}

func (c *Config) WithKeepalive(interval, timeout time.Duration) *Config {
c.keepaliveInterval = interval
c.keepaliveTimeout = timeout
// WithKeepalive helps to set the keepalive option.
// - time. After a duration of this time if the client doesn't see any activity it
// pings the server to see if the transport is still alive.
// If set below 10s, a minimum value of 10s will be used instead.
// - timeout. After having pinged for keepalive check, the client waits for a duration
// of Timeout and if no activity is seen even after that the connection is closed.
func (c *Config) WithKeepalive(time, timeout time.Duration) *Config {
keepalive := options.NewKeepaliveOption(time, timeout).Build()
c.options = append(c.options, keepalive)
return c
}

func (c *Config) GetEndpoint() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
// TODO(yuanbohan): support more tls options
func (c *Config) WithInsecure(insecure bool) *Config {
opt := options.NewTlsOption(insecure)
c.tls = &opt
return c
}

func (c *Config) Options() *Options {
if c.keepaliveInterval == 0 && c.keepaliveTimeout == 0 {
return nil
}

keepalive := NewKeepaliveOptions()
// WithDialOption helps to specify the dial option
// which has not been supported by ingester sdk yet.
func (c *Config) WithDialOption(opt grpc.DialOption) *Config {
c.options = append(c.options, opt)
return c
}

if c.keepaliveInterval != 0 {
keepalive.WithInterval(c.keepaliveInterval)
}
func (c *Config) endpoint() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}

if c.keepaliveTimeout != 0 {
keepalive.WithTimeout(c.keepaliveTimeout)
func (c *Config) build() []grpc.DialOption {
if c.tls == nil {
opt := options.NewTlsOption(true)
c.tls = &opt
}

return NewOptions(keepalive)
c.options = append(c.options, c.tls.Build())
return c.options
}
9 changes: 7 additions & 2 deletions examples/object/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ func data() []Monitor {
}

func writeObject() {
resp, err := client.WriteObject(context.Background(), data())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

resp, err := client.WriteObject(ctx, data())
if err != nil {
log.Fatal(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func streamWriteObject() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()

if err := client.StreamWriteObject(ctx, data()); err != nil {
log.Println(err)
}
Expand Down
7 changes: 5 additions & 2 deletions examples/table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,18 @@ func data() *table.Table {
}

func write() {
resp, err := client.Write(context.Background(), data())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
resp, err := client.Write(ctx, data())
if err != nil {
log.Println(err)
}
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
}

func streamWrite() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
if err := client.StreamWrite(ctx, data()); err != nil {
log.Println(err)
}
Expand Down
101 changes: 0 additions & 101 deletions options.go

This file was deleted.

55 changes: 55 additions & 0 deletions options/keepalive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

import (
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)

var (
defaultKeepaliveTime = time.Second * 30
defaultKeepaliveTimeout = time.Second * 5
)

type KeepaliveOption struct {
time time.Duration
timeout time.Duration
}

func NewKeepaliveOption(time, timeout time.Duration) KeepaliveOption {
return KeepaliveOption{
time: time,
timeout: timeout,
}
}

func (opt KeepaliveOption) Build() grpc.DialOption {
param := keepalive.ClientParameters{
PermitWithoutStream: true,
Time: defaultKeepaliveTime,
Timeout: defaultKeepaliveTimeout,
}

if opt.time != 0 {
param.Time = opt.time
}
if opt.timeout != 0 {
param.Timeout = opt.timeout
}
return grpc.WithKeepaliveParams(param)
}
Loading

0 comments on commit 08930f5

Please sign in to comment.