surrealdb / surrealdb.go

SurrealDB SDK for Golang
https://surrealdb.com
Apache License 2.0
231 stars 62 forks source link

Connection Pooling / Surreal.Instance #61

Closed roku-on-it closed 1 year ago

roku-on-it commented 1 year ago

I can see there is a Surreal.Instance in JavaScript API and in rust there is Surreal::init() in the Rust API but I could not see the equivalent of it neither in the Docs nor the source code.

How can one avoid creating new connections everytime and use a static struct to use the same connection or even implement connection pool?

plally commented 1 year ago

There is no connection pooling in this library currently, You can create a connection and reuse it for as long as you want. The closest thing to init() would be surrealdb.New

You can find an example of creating a db instance and signing in here https://surrealdb.com/docs/integration/libraries/golang

Once you have a db instance you can save it on any struct you want and pass that around to other parts of your application, reusing it for the lifetime of your application. You could also create a global variable and put the db instance on that, but I wouldn't recommend that.

roku-on-it commented 1 year ago

Thanks for answering. Are there any plans to implement a pool to SurrealDB? (Not just for Go but for the other languages too).

Currently, I'm holding N size of WS connections in a dynamic array and getting the connection to execute and give the connection back to the pool by popping/pushing (I believe this is happening in constant time)

ElecTwix commented 1 year ago

Thanks for answering. Are there any plans to implement a pool to SurrealDB? (Not just for Go but for the other languages too).

Currently, I'm holding N size of WS connections in a dynamic array and getting the connection to execute and give the connection back to the pool by popping/pushing (I believe this is happening in constant time)

like @plally said you can use a slice of db to use as a pool I think there is no need for any init() func

roku-on-it commented 1 year ago

I see. Thanks.

toadslop commented 9 months ago

I built this connection pool implementation for Rust: https://crates.io/crates/surreal_bb8. It has some limitations that are pending some changes that will be published in Surreal 1.1.0, but it should work well enough now.