Categorygithub.com/questdb/go-questdb-client/v3
modulepackage
3.2.0
Repository: https://github.com/questdb/go-questdb-client.git
Documentation: pkg.go.dev

# README

GoDoc reference

go-questdb-client

Golang client for QuestDB's Influx Line Protocol (ILP) over HTTP and TCP. This library makes it easy to insert data into QuestDB.

The library requires Go 1.19 or newer.

Features:

  • Context-aware API.
  • Optimized for batch writes.
  • Supports TLS encryption and ILP authentication.
  • Automatic write retries and connection reuse for ILP over HTTP.
  • Tested against QuestDB 7.3.10 and newer versions.

New in v3:

  • Supports ILP over HTTP using the same client semantics

Documentation is available here.

Quickstart

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	qdb "github.com/questdb/go-questdb-client/v3"
)

func main() {
	ctx := context.TODO()
	// Connect to QuestDB running locally.
	sender, err := qdb.LineSenderFromConf(ctx, "http::addr=localhost:9000;")
	if err != nil {
		log.Fatal(err)
	}
	// Make sure to close the sender on exit to release resources.
	defer sender.Close(ctx)

	// Send a few ILP messages.
	tradedTs, err := time.Parse(time.RFC3339, "2022-08-06T15:04:05.123456Z")
	if err != nil {
		log.Fatal(err)
	}
	err = sender.
		Table("trades_go").
		Symbol("pair", "USDGBP").
		Symbol("type", "buy").
		Float64Column("traded_price", 0.83).
		Float64Column("limit_price", 0.84).
		Int64Column("qty", 100).
		At(ctx, tradedTs)
	if err != nil {
		log.Fatal(err)
	}

	tradedTs, err = time.Parse(time.RFC3339, "2022-08-06T15:04:06.987654Z")
	if err != nil {
		log.Fatal(err)
	}
	err = sender.
		Table("trades_go").
		Symbol("pair", "GBPJPY").
		Symbol("type", "sell").
		Float64Column("traded_price", 135.97).
		Float64Column("limit_price", 0.84).
		Int64Column("qty", 400).
		At(ctx, tradedTs)
	if err != nil {
		log.Fatal(err)
	}

	// Make sure that the messages are sent over the network.
	err = sender.Flush(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

To connect via TCP, set the configuration string to:

	// ...
	sender, err := qdb.LineSenderFromConf(ctx, "tcp::addr=localhost:9009;")
	// ...

Pooled Line Senders

Warning: Experimental feature designed for use with HTTP senders ONLY

Version 3 of the client introduces a LineSenderPool, which provides a mechanism to pool previously-used LineSenders so they can be reused without having to allocate and instantiate new senders.

A LineSenderPool is thread-safe and can be used to concurrently obtain senders across multiple goroutines.

Since LineSenders must be used in a single-threaded context, a typical pattern is to Acquire a sender from a LineSenderPool at the beginning of a goroutine and use a deferred execution block to Close the sender at the end of the goroutine.

Here is an example of the LineSenderPool Acquire, Release, and Close semantics:

package main

import (
	"context"

	qdb "github.com/questdb/go-questdb-client/v3"
)

func main() {
	ctx := context.TODO()

	pool := qdb.PoolFromConf("http::addr=localhost:9000")
	defer func() {
		err := pool.Close(ctx)
		if err != nil {
			panic(err)
		}
	}()

	sender, err := pool.Sender(ctx)
	if err != nil {
		panic(err)
	}

	sender.Table("prices").
		Symbol("ticker", "AAPL").
		Float64Column("price", 123.45).
		AtNow(ctx)

	// Close call returns the sender back to the pool
	if err := sender.Close(ctx); err != nil {
		panic(err)
	}
}

Migration from v2

v2 code example:

package main

import (
	"context"

	qdb "github.com/questdb/go-questdb-client/v2"
)

func main() {
	// Connect to QuestDB running on 127.0.0.1:9009 (ILP/TCP)
	sender, err := qdb.NewLineSender(context.TODO())
	// ...
	defer sender.Close()
	// ...
}

Migrated v3 code:

package main

import (
	"context"

	qdb "github.com/questdb/go-questdb-client/v3"
)

func main() {
	// Connect to QuestDB running on 127.0.0.1:9000 (ILP/HTTP)
	sender, err := qdb.NewLineSender(context.TODO(), qdb.WithHTTP())
	// Alternatively, you can use the LineSenderFromConf function:
	// sender, err := qdb.LineSenderFromConf(ctx, "http::addr=localhost:9000;")
	// ...
	// or you can export the "http::addr=localhost:9000;" config string to
	// the QDB_CLIENT_CONF environment variable and use the LineSenderFromEnv function:
	// sender, err := qdb.LineSenderFromEnv(ctx)
	// ...
	defer sender.Close(context.TODO())
	// ...
}

Note that the migrated code uses the HTTP sender instead of the TCP one.

Community

If you need help, have additional questions or want to provide feedback, you may find in our Community Forum. You can also sign up to our mailing list to get notified of new releases.

# Packages

No description provided by the author

# Functions

LineSenderFromConf creates a LineSender using the QuestDB config string format.
LineSenderFromEnv creates a LineSender with a config string defined by the QDB_CLIENT_CONF environment variable.
NewInvalidConfigStrError creates new InvalidConfigStrError.
NewLineSender creates new InfluxDB Line Protocol (ILP) sender.
NewRetryTimeoutError returns a new RetryTimeoutError error.
PoolFromConf instantiates a new LineSenderPool with a QuestDB configuration string.
PoolFromOptions instantiates a new LineSenderPool using programmatic options.
WithAddress sets address to connect to.
WithAuth sets token (private key) used for ILP authentication.
WithAutoFlushDisabled turns off auto-flushing behavior.
WithAutoFlushInterval the interval at which the Sender automatically flushes its buffer.
WithAutoFlushRows sets the number of buffered rows that must be breached in order to trigger an auto-flush.
WithBasicAuth sets a Basic authentication header for ILP requests over HTTP.
WithBearerToken sets a Bearer token Authentication header for ILP requests.
WithFileNameLimit sets maximum file name length in chars allowed by the server.
WithHttp enables ingestion over HTTP protocol.
WithHttpTransport sets the client's http transport to the passed pointer instead of the global transport.
WithInitBufferSize sets the desired initial buffer capacity in bytes to be used when sending ILP messages.
WithMaxBufferSize sets the maximum buffer capacity in bytes to be used when sending ILP messages.
WithMaxSenders sets the maximum number of senders in the pool.
WithMinThroughput is used in combination with request_timeout to set the timeout of an ILP request.
WithRequestTimeout is used in combination with request_min_throughput to set the timeout of an ILP request.
WithRetryTimeout is the cumulative maximum duration spend in retries.
WithTcp enables ingestion over TCP protocol.
WithTls enables TLS connection encryption.
WithTlsInsecureSkipVerify enables TLS connection encryption, but skips server certificate verification.

# Structs

HttpError is a server-sent error message.
InvalidConfigStrError is error indicating invalid config string.
LineSenderPool wraps a mutex-protected slice of [LineSender].
RetryTimeoutError is error indicating failed flush retry attempt.

# Interfaces

LineSender allows you to insert rows into QuestDB by sending ILP messages over HTTP or TCP protocol.

# Type aliases

LineSenderOption defines line sender config option.
LineSenderPoolOption defines line sender pool config option.