go-libsql: failed to execute query - Hrana STREAM_EXPIRED

Hey Guys,

I am getting the following error after ~10s of the connection to the DB being alive:

failed to execute query 
...
error code = 1: Error executing statement: Hrana: `api error: `{\"message\":\"The stream has expired due to inactivity\",\"code\":\"STREAM_EXPIRED\"}

Makes this client more or less unusable (don’t want to create a new connection on each request…). Would also be nice to see relative paths working with the client for the embedded replicas.

Thanks!

Repo to reproduce: https://github.com/juliuslipp/libsql-weirdness

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (15 by maintainers)

Most upvoted comments

@haaawk, this is still an issue and not fixed in the CGO version, but works and is not an issue for the non-CGO version. Is this intentional?

We just restarted development on a product that was supposed to use turso on the server side, and we just hit this issue on day 2 of development. I double checked against the examples to see if the original devs missed something, but their code aligns with the go example client code.

The issue is still present in this library (github.com/tursodatabase/go-libsql), but is not present in the github.com/tursodatabase/libsql-client-go/libsql library. This means that if we have a “embedded replica” connection for one of our libsql connections, we cannot use the same library to fallback to “remote-only”, or reuse the connection code for a “remote-only” connection to another db.

We’ve tested it both with turso dev and docker libsql and we get the same Hrana: api error: STREAM_EXPIRED error.

Let me know if you need a github to reproduce this.

Just had the same problem - the DB was empty after restarting. Let me try to replicate this…

You’re opening embedded replicas incorrectly. Instead of doing:

        connector, err := libsql.NewEmbeddedReplicaConnector(absPath, config.DatabaseUrl, *config.DatabaseAuthToken)
...
	db, err := sqlx.Open("libsql", "file://"+absPath)

the correct way is to do:

        connector, err := libsql.NewEmbeddedReplicaConnector(absPath, config.DatabaseUrl, *config.DatabaseAuthToken)
        ...
        db := sql.OpenDB(connector)

What you did was opening embedded replica file with a local only client and that makes it impossible for embedded replicas to work correctly.

If you care about sqlx then you can do:

db := sqlx.NewDb(sql.OpenDB(connector), "libsql")

There are two options:

  1. Limit connection pool to 1 connection + Ping -> DB.SetMaxIdleConns(1)
  2. Limit connection idle lifetime to 9s and then you don’t need Ping at all -> DB.SetConnMaxIdleTime(9s)

So the NewEmbeddedClient would look like:

func NewEmbeddedClient(config ClientConfig) (*Client, error) {
	absPath, _ := filepath.Abs("./sqlite.db")
	connector, err := libsql.NewEmbeddedReplicaConnectorWithAutoSync(absPath, config.DatabaseUrl, *config.DatabaseAuthToken, config.SyncInterval)
	if err != nil {
		return nil, err
	}

	db := sqlx.NewDb(sql.OpenDB(connector), "libsql")

	err = db.Ping()
	if err != nil {
		return nil, err
	}

	return db, nil
}

You are right. Last time I tried the auto-sync, it didn’t work. That was a few versions ago though.

Ok let me try it.

You’re opening embedded replicas incorrectly. Instead of doing:

        connector, err := libsql.NewEmbeddedReplicaConnector(absPath, config.DatabaseUrl, *config.DatabaseAuthToken)
...
	db, err := sqlx.Open("libsql", "file://"+absPath)

the correct way is to do:

        connector, err := libsql.NewEmbeddedReplicaConnector(absPath, config.DatabaseUrl, *config.DatabaseAuthToken)
        ...
        db := sql.OpenDB(connector)

What you did was opening embedded replica file with a local only client and that makes it impossible for embedded replicas to work correctly.

Just a comment about syncing embedded replicas: If you do NewEmbeddedReplicaConnectorWithAutoSync instead of NewEmbeddedReplicaConnector then the sync will be done periodically automatically and you don’t need all the:

	go func() {
		for true {
			_ = connector.Sync()
			time.Sleep(config.SyncInterval)
		}
	}()