zackattackz

zackattackz

DBConnection Implementation via HTTP

Hello!

I am currently working on an implementation of an Ecto Adapter for XTDB v2.x. 2.x is not a production-ready version, but I just wanted to try to make this project as a way to learn more about Elixir and XTDB as well.

So first of course I would need to create a DB driver for Elixir. XTDB has an HTTP API so I figured I will implement the driver using that as the protocol.

To create a driver, it seems standard that you should implement the DBConnection.Connection behavior. I just have a few struggles with this.

1.) From looking at other DBConnection implementations (MyXQL, Postgrex), they all seem to implement their database-specific protocols via TCP. Then DBConnection is somehow able to keep these TCP connections alive (via pinging?). My question is, how would one implement a DBConnection for an HTTP based protocol, specifically for the ping callback. To my understanding, HTTP is stateless, so I don’t think there is a realistic way to implement ping for this. Should I just leave it undefined and force :idle_interval = 0? Which brings me to my next question…
2.) Is there anything to be gained from implementing DBConnection for an HTTP based protocol? The main value-gain from implementing DBConnection seems to be that it is able to pool and maintain open TCP connections. I don’t know if there are other benefits, but if this is not applicable to HTTP, then should I bother with implementing it in the first place?

Sorry if these are ill-informed questions, as my knowledge of network protocols is very low. Any advice is appreciated, thank you!

Most Liked

al2o3cr

al2o3cr

There’s “keepalive” in HTTP/1.1 that lets a connection be used for multiple sequential requests.

You’d need to check if XTDB supports it, though.

The goal of connection pooling for this is largely around reducing the number of SSL handshakes required - every time a new connection is opened it takes a bunch of network round-trips and cryptographic calculations.

ruslandoga

ruslandoga

:wave: @zackattackz

I implemented an HTTP driver for ClickHouse and DBConnection was very useful.

  1. You can GET /status for handle_ping, it would probably be enough to keep the connection alive.

  2. In addition to pooling and keeping connections alive, DBConnection provides a way to make them re-connect with backoff in case things go bad.

Just make sure to use an HTTP client without a pool!

Last Post!

zackattackz

zackattackz

Looking more into this it seems that HTTP 1.1 keeps connections alive by default, so pinging some known endpoint periodically, like @ruslandoga does in their Clickhouse driver, should be enough to keep the connection alive.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement