dev vouch dev on at. thats about it atvouch.dev
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

use split read/write repo architecture

Luna c55b1e2c 7a605f41

+85 -23
+1
appview/config/test.exs
··· 6 6 atvouch_endpoint: "https://localhost:123" 7 7 8 8 config :atvouch, Atvouch.Repo, pool: Ecto.Adapters.SQL.Sandbox 9 + config :atvouch, start_replicas: false 9 10 10 11 config :atvouch, :tap, 11 12 uri: "ws://localhost:123/channel",
+15 -3
appview/lib/atvouch/application.ex
··· 7 7 8 8 children = 9 9 [ 10 - Atvouch.Repo, 11 - {Bandit, plug: Atvouch.Router, port: port, ip: {127, 0, 0, 1}} 12 - ] ++ tap_children() 10 + Atvouch.Repo 11 + ] ++ 12 + replica_children() ++ 13 + [ 14 + {Bandit, plug: Atvouch.Router, port: port, ip: {127, 0, 0, 1}} 15 + ] ++ tap_children() 13 16 14 17 15 18 opts = [strategy: :one_for_one, name: Atvouch.Supervisor] 16 19 Supervisor.start_link(children, opts) 20 + end 21 + 22 + defp replica_children do 23 + if Application.get_env(:atvouch, :start_replicas, true) do 24 + %{read_replicas: replicas} = Atvouch.Repo.repo_spec() 25 + Enum.map(replicas, & &1) 26 + else 27 + [] 28 + end 17 29 end 18 30 19 31 defp tap_children do
+65 -16
appview/lib/atvouch/repo.ex
··· 1 - defmodule Atvouch.Repo do 2 - use Ecto.Repo, 3 - otp_app: :atvouch, 4 - adapter: Ecto.Adapters.SQLite3 1 + defmodule Atvouch.Repo.Base do 2 + defmacro __using__(opts) do 3 + quote bind_quoted: [opts: opts] do 4 + use Ecto.Repo, 5 + otp_app: :atvouch, 6 + adapter: Ecto.Adapters.SQLite3, 7 + pool_size: 1 8 + 9 + @read_replicas opts[:read_replicas] 10 + 11 + def repo_spec do 12 + %{read_replicas: @read_replicas} 13 + end 14 + 15 + def replica() do 16 + Enum.random(@read_replicas) 17 + end 18 + 19 + def replica(identifier) 20 + when is_number(identifier) or is_bitstring(identifier) or is_atom(identifier) do 21 + @read_replicas |> Enum.at(rem(identifier |> :erlang.phash2(), length(@read_replicas))) 22 + end 23 + 24 + for repo <- @read_replicas do 25 + default_dynamic_repo = 26 + if Mix.env() == :test do 27 + opts[:primary] 28 + else 29 + repo 30 + end 31 + 32 + defmodule repo do 33 + use Ecto.Repo, 34 + otp_app: :atvouch, 35 + adapter: Ecto.Adapters.SQLite3, 36 + pool_size: 1, 37 + read_only: true, 38 + default_dynamic_repo: default_dynamic_repo 39 + end 40 + end 5 41 6 - def init(_type, config) do 7 - config = 8 - config 9 - |> Keyword.put_new(:after_connect, fn conn -> 10 - Exqlite.Sqlite3.execute(conn, "PRAGMA journal_mode = WAL") 11 - Exqlite.Sqlite3.execute(conn, "PRAGMA busy_timeout = 5000") 12 - Exqlite.Sqlite3.execute(conn, "PRAGMA synchronous = NORMAL") 13 - Exqlite.Sqlite3.execute(conn, "PRAGMA cache_size = -6000") 14 - Exqlite.Sqlite3.execute(conn, "PRAGMA foreign_keys = true") 15 - Exqlite.Sqlite3.execute(conn, "PRAGMA temp_store = memory") 16 - end) 42 + def init(_type, config) do 43 + config = 44 + config 45 + |> Keyword.put_new(:after_connect, fn conn -> 46 + Exqlite.Sqlite3.execute(conn, "PRAGMA journal_mode = WAL") 47 + Exqlite.Sqlite3.execute(conn, "PRAGMA busy_timeout = 5000") 48 + Exqlite.Sqlite3.execute(conn, "PRAGMA synchronous = NORMAL") 49 + Exqlite.Sqlite3.execute(conn, "PRAGMA cache_size = -6000") 50 + Exqlite.Sqlite3.execute(conn, "PRAGMA foreign_keys = true") 51 + Exqlite.Sqlite3.execute(conn, "PRAGMA temp_store = memory") 52 + end) 17 53 18 - {:ok, config} 54 + {:ok, config} 55 + end 56 + end 19 57 end 20 58 end 59 + 60 + defmodule Atvouch.Repo do 61 + use Atvouch.Repo.Base, 62 + primary: Atvouch.Repo, 63 + read_replicas: [ 64 + Atvouch.Repo.Replica1, 65 + Atvouch.Repo.Replica2, 66 + Atvouch.Repo.Replica3, 67 + Atvouch.Repo.Replica4 68 + ] 69 + end
+4 -4
appview/lib/atvouch/vouch.ex
··· 42 42 end 43 43 44 44 def one(at_uri) do 45 - Atvouch.Repo.get(__MODULE__, at_uri) 45 + Atvouch.Repo.replica().get(__MODULE__, at_uri) 46 46 end 47 47 48 48 def all_from_did(did) do 49 49 import Ecto.Query 50 50 from(v in __MODULE__, where: v.creator_did == ^did) 51 - |> Atvouch.Repo.all() 51 + |> Atvouch.Repo.replica().all() 52 52 end 53 53 54 54 def all_targeting_did(did) do 55 55 import Ecto.Query 56 56 from(v in __MODULE__, where: v.target_did == ^did) 57 - |> Atvouch.Repo.all() 57 + |> Atvouch.Repo.replica().all() 58 58 end 59 59 60 60 def paginated(limit, offset) do 61 61 import Ecto.Query 62 62 63 63 from(v in __MODULE__, order_by: [asc: v.at_uri], limit: ^limit, offset: ^offset) 64 - |> Atvouch.Repo.all() 64 + |> Atvouch.Repo.replica().all() 65 65 end 66 66 end