questdb / go-questdb-client

Golang client for QuestDB's Influx Line Protocol
Apache License 2.0
47 stars 9 forks source link

Bad address on attempt to create sender leads to hang w/o error #24

Closed JoeHCQ1 closed 8 months ago

JoeHCQ1 commented 11 months ago

The Bug

If you try to create a sender with a bad URL the code hangs indefinitely. I think it should return an error, perhaps after a configurable timeout.

To Replicate

You should be able to replicate this behavior by running the following code:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    qdb "github.com/questdb/go-questdb-client/v2"
    "io"
    "log"
    "net/http"
    "os"
)

func main() {
    fmt.Printf("Attempting to connect...")
    ctx := context.TODO()

    sender, err := qdb.NewLineSender(
        ctx,
        qdb.WithAddress("trial-instance-xxxxxxxx.ilp.xxxx.questdb.com:3xxxx"), // <== If this is wrong the program will hang
        qdb.WithBufferCapacity(4096),
    )
    if err != nil {
        log.Fatal(fmt.Errorf("error: Failed to connect to QDB: %w", err))
        return
    }
    //defer sender.Close()
    // Make sure to close the sender on exit to release resources.
    defer func(sender *qdb.LineSender) {
        err := sender.Close()
        if err != nil {
            log.Printf("Error closing sender to QuestDB - may cause resource leak: %v", err)
        }
    }(sender)

    fmt.Printf("Connection made")
}

For Others Looking For Connection URL:

For anyone else trying to connect to your cloud trial instance, poke around your trial instance webpage a bit, but not in the web-console. You'll find the correct URL in places where they talk about adding data to the DB. The port is not 9009, unlike the local hello-world install.

JoeHCQ1 commented 11 months ago

Similarly, I get no error when my authentication fails. It doesn't hang, but it just hands me back the sender w/o indications there was a problem.

puzpuzpuz commented 8 months ago

Sorry for the late reply. I've tried creating a sender when there is no QDB server running and immediately got the following:

$ go run main.go 
2024/03/22 09:06:18 failed to connect to server: dial tcp 127.0.0.1:9009: connect: connection refused
exit status 1

Closing this issue for now. Feel free to reopen if the issue persists.

Off-topic: v3 shipped HTTP sender which includes better error propagation, automatic retries, and allows explicit control over transactions.