Skip to content

Commit

Permalink
Implement ping and disconnect callbacks for DBConnection.
Browse files Browse the repository at this point in the history
* Proper error handling on connect.

* Implement ping and disconnect.

* Update Dgraph to v1.0.6.

* Bump version.
  • Loading branch information
ospaarmann committed Jul 31, 2019
1 parent 3c5ae3d commit 90b28fc
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 44 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.2.0-beta.3] - 2019-07-31

- Test against Dgraph `v1.0.16`
- Remove dependency `gun`
- Implement ping callback
- Implement disconnect callback

## [0.2.0-beta.2] - 2019-03-28

- Downgrade poison to `v3.1.0`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

**ExDgraph is functional but I would be careful using it in production. If you want to help, please drop me a message. Any help is greatly appreciated!**

ExDgraph is a gRPC based client for the [Dgraph](https://github.com/dgraph-io/dgraph) database. It uses the [DBConnection](https://hexdocs.pm/db_connection/DBConnection.html) behaviour to support transactions and connection pooling via [Poolboy](https://github.com/devinus/poolboy). Works with Dgraph v1.0.13 (latest).
ExDgraph is a gRPC based client for the [Dgraph](https://github.com/dgraph-io/dgraph) database. It uses the [DBConnection](https://hexdocs.pm/db_connection/DBConnection.html) behaviour to support transactions and connection pooling via [Poolboy](https://github.com/devinus/poolboy). Works with Dgraph v1.0.16 (latest).

> Dgraph is an open source, horizontally scalable and distributed graph database, providing ACID transactions, consistent replication and linearizable reads. [...] Dgraph's goal is to provide Google production level scale and throughput, with low enough latency to be serving real time user queries, over terabytes of structured data. ([Source](https://github.com/dgraph-io/dgraph))
Expand Down Expand Up @@ -31,7 +31,7 @@ Add the package `ex_dgraph` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ex_dgraph, "~> 0.2.0-beta.2"}
{:ex_dgraph, "~> 0.2.0-beta.3"}
]
end
```
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.2"
services:
zero:
image: dgraph/dgraph:v1.0.13
image: dgraph/dgraph:v1.0.16
volumes:
- type: bind
source: ./dgraph_data
Expand All @@ -14,7 +14,7 @@ services:
restart: on-failure
command: dgraph zero --my=zero:5080
server:
image: dgraph/dgraph:v1.0.13
image: dgraph/dgraph:v1.0.16
volumes:
- type: bind
source: ./dgraph_data
Expand All @@ -27,7 +27,7 @@ services:
restart: on-failure
command: dgraph alpha --my=server:7080 --lru_mb=2048 --zero=zero:5080
ratel:
image: dgraph/dgraph:v1.0.13
image: dgraph/dgraph:v1.0.16
volumes:
- type: bind
source: ./dgraph_data
Expand Down
4 changes: 2 additions & 2 deletions lib/exdgraph.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ExDgraph do
@moduledoc """
ExDgraph is a gRPC based client for the Dgraph database. It uses the DBConnection behaviour to support transactions and connection pooling via Poolboy. Works with Dgraph v1.0.13 (latest).
ExDgraph is a gRPC based client for the Dgraph database. It uses the DBConnection behaviour to support transactions and connection pooling via Poolboy. Works with Dgraph v1.0.16 (latest).
## Installation
Expand All @@ -9,7 +9,7 @@ defmodule ExDgraph do
```elixir
def deps do
[
{:ex_dgraph, "~> 0.2.0-beta.2"}
{:ex_dgraph, "~> 0.2.0-beta.3"}
]
end
```
Expand Down
12 changes: 11 additions & 1 deletion lib/exdgraph/error.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
defmodule ExDgraph.Error do
defexception message: "Unknown exception in ExDgraph."
@moduledoc """
Dgraph or connection error are wrapped in ExDgraph.Error.
"""
defexception [:reason, :action]

@type t :: %ExDgraph.Error{}

@impl true
def message(%{action: action, reason: reason}) do
"#{action} failed with #{inspect(reason)}"
end
end
67 changes: 34 additions & 33 deletions lib/exdgraph/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ defmodule ExDgraph.Protocol do
require Logger

alias ExDgraph.Api
alias ExDgraph.{Exception, MutationStatement, OperationStatement, QueryStatement}
alias ExDgraph.{Error, Exception, MutationStatement, OperationStatement, QueryStatement}

@doc "Callback for DBConnection.connect/1"
@impl true
def connect(_opts) do
host = to_charlist(ExDgraph.config(:hostname))
port = ExDgraph.config(:port)
Expand All @@ -27,43 +27,56 @@ defmodule ExDgraph.Protocol do
{:ok, channel} ->
{:ok, channel}

_ ->
Logger.error("ExDgraph: Connection Failed")
{:error, ExDgraph.Error}
# TODO: Proper error handling
{:error, reason} ->
{:error, %Error{action: :connect, reason: reason}}
end
end

@doc "Callback for DBConnection.checkout/1"
@impl true
def checkout(state) do
{:ok, state}
# TODO: Proper checkout. Only placeholder callback
end

@doc "Callback for DBConnection.checkin/1"
@impl true
def checkin(state) do
{:ok, state}
# TODO: Proper checkout. Only placeholder callback
end

@doc "Callback for DBConnection.disconnect/1"
def disconnect(_err, _state) do
:ok
@impl true
def disconnect(_error, state) do
case GRPC.Stub.disconnect(state) do
{:ok, _} -> :ok
{:error, _reason} -> :ok
end
end

@impl true
def ping(%{adapter_payload: %{conn_pid: conn_pid}} = channel) do
# check if the server is up and wait 5s seconds before disconnect
stream = :gun.head(conn_pid, "/")
response = :gun.await(conn_pid, stream, 5_000)

# return based on response
case response do
{:response, :fin, 200, _} -> {:ok, channel}
{:error, reason} -> {:disconnect, reason, channel}
_ -> :ok
end
end

@doc "Callback for DBConnection.handle_begin/1"
@impl true
def handle_begin(_opts, _state) do
end

@doc "Callback for DBConnection.handle_rollback/1"
@impl true
def handle_rollback(_opts, _state) do
end

@doc "Callback for DBConnection.handle_commit/1"
@impl true
def handle_commit(_opts, _state) do
end

@doc "Callback for DBConnection.handle_execute/1"
@impl true
def handle_execute(query, params, opts, channel) do
# only try to reconnect if the error is about the broken connection
with {:disconnect, _, _} <- execute(query, params, opts, channel) do
Expand Down Expand Up @@ -92,22 +105,7 @@ defmodule ExDgraph.Protocol do
end
end

def handle_info({:gun_up, _pid, _protocol}, state) do
Logger.debug(fn ->
[inspect(__MODULE__), ?\s, inspect(self()), " received gun_up from server"]
end)

{:ok, state}
end

def handle_info({:gun_down, _pid, _protocol, _level, _, _}, state) do
Logger.debug(fn ->
[inspect(__MODULE__), ?\s, inspect(self()), " received gun_down from server"]
end)

{:ok, state}
end

@impl true
def handle_info(msg, state) do
Logger.error(fn ->
[inspect(__MODULE__), ?\s, inspect(self()), " received unexpected message: " | inspect(msg)]
Expand All @@ -119,6 +117,7 @@ defmodule ExDgraph.Protocol do
defp execute(%QueryStatement{statement: statement}, _params, _, channel) do
request = ExDgraph.Api.Request.new(query: statement)
timeout = ExDgraph.config(:timeout)

case ExDgraph.Api.Dgraph.Stub.query(channel, request, timeout: timeout) do
{:ok, res} ->
{:ok, res, channel}
Expand Down Expand Up @@ -161,6 +160,7 @@ defmodule ExDgraph.Protocol do
) do
operation = Api.Operation.new(drop_all: drop_all, schema: schema, drop_attr: drop_attr)
timeout = ExDgraph.config(:timeout)

case ExDgraph.Api.Dgraph.Stub.alter(channel, operation, timeout: timeout) do
{:ok, res} ->
{:ok, res, channel}
Expand All @@ -175,6 +175,7 @@ defmodule ExDgraph.Protocol do

defp do_mutate(channel, request) do
timeout = ExDgraph.config(:timeout)

case ExDgraph.Api.Dgraph.Stub.mutate(channel, request, timeout: timeout) do
{:ok, res} ->
{:ok, res, channel}
Expand Down
4 changes: 1 addition & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExDgraph.MixProject do
def project do
[
app: :ex_dgraph,
version: "0.2.0-beta.2",
version: "0.2.0-beta.3",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -32,7 +32,6 @@ defmodule ExDgraph.MixProject do
[
applications: [
:logger,
:poolboy,
:db_connection,
:retry,
:grpc
Expand All @@ -44,7 +43,6 @@ defmodule ExDgraph.MixProject do
defp deps do
[
{:grpc, "~> 0.3.1"},
{:gun, "1.3.0"},
{:protobuf, "~> 0.6.1"},
{:poison, "~> 3.1"},
{:poolboy, "~> 1.5.2"},
Expand Down

0 comments on commit 90b28fc

Please sign in to comment.