我用--no-html和--database mysql创建了一个phoenix项目
我们在工作中使用的版本是:
Erlang/OTP 21 [erts-10.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]
Elixir 1.8.0 (compiled with Erlang/OTP 20)我正在做一个项目,作为状态页工具(hund.io)和我们自己的API和报告工具之间的代理,所以我需要一个数据库来存储服务名称+请求的url,并且,当我需要一点与数据库Cachex (https://github.com/whitfin/cachex)分离的时候,将我的数据放到缓存中
当数据被缓存时,使用邮递员请求我的包装器的api需要大约100ms才能获得响应,菲尼克斯的日志显示类似[info] Sent 200 in 1ms之类的内容,但是,我曾经关闭过数据库,邮递员需要整整3秒才能获得响应,菲尼克斯日志仍然会显示[info] Sent 200 in 1ms之类的内容
我确信包装器在这两种情况下都使用缓存数据:
我的代码:
def show(conn, %{"service_name" => service_name}) do
Logger.debug("Top of show func")
case Cachex.get(:proxies, service_name) do
{:ok, nil} ->
Logger.debug("Service #{service_name} not found in cache")
proxy = Health.get_proxy_by_name!(service_name)
Logger.debug("Service #{service_name} found in db")
Cachex.put(:proxies, service_name, proxy)
proxy_with_health = Checker.call_api(proxy)
render(conn, "show.json", proxy_health: proxy_with_health)
{:ok, proxy} ->
Logger.debug("Found service #{service_name} in cache")
proxy_with_health = Checker.call_api(proxy)
render(conn, "show.json", proxy_health: proxy_with_health)
end
end日志:
[info] GET /api/proxies_health/docto
[debug] Processing with StatusWeb.ProxyHealthController.show/2
Parameters: %{"service_name" => "docto"}
Pipelines: [:api]
[debug] Top of show func
[debug] Found service docto in cache对于路由器部分:
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", StatusWeb do
pipe_through :api
resources "/proxies", ProxyController, except: [:new, :edit]
get "/proxies_health", ProxyHealthController, :index
get "/proxies_health/:service_name", ProxyHealthController, :show
end我还在日志中看到两个错误:
[error] MyXQL.Connection (#PID<0.373.0>) failed to connect: ** (DBConnection.ConnectionError) connection refused这看起来“正常”,当应用程序想要重新连接到数据库和这个数据库时,就在处理请求之前(至少根据日志)。
[error] Could not create schema migrations table. This error usually happens due to the following:
* The database does not exist
* The "schema_migrations" table, which Ecto uses for managing
migrations, was defined by another library
* There is a deadlock while migrating (such as using concurrent
indexes with a migration_lock)
To fix the first issue, run "mix ecto.create".
To address the second, you can run "mix ecto.drop" followed by
"mix ecto.create". Alternatively you may configure Ecto to use
another table and/or repository for managing migrations:
config :status, Status.Repo,
migration_source: "some_other_table_for_schema_migrations",
migration_repo: AnotherRepoForSchemaMigrations
The full error report is shown below.
[error] GenServer #PID<0.620.0> terminating
** (DBConnection.ConnectionError) connection refused
(db_connection) lib/db_connection/connection.ex:100: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: MyXQL.Connection在查看How do I detect database connection issues from Elixir Ecto?时,我尝试放入我的Status.Repo文件(repo.ex
backoff_type: :stop但这并没有改变我的问题
发布于 2021-05-11 04:39:51
由于我的代码中,在缓存中找到一个对象(根本)不涉及数据库,我们最终发现问题来自端点的一个插件,该插件应该只存在于开发环境(MyAppWeb/endpoint.ex)中。
if code_reloading? do
plug Phoenix.CodeReloader
plug Phoenix.Ecto.CheckRepoStatus, otp_app: :status
end这是一个插件,它会在每次请求时检查数据库是否正常,迁移是否正在运行等,等待Ecto的答复,在genserver中的超时意识到调用导致了延迟!
修复方法是简单地注释这行
plug Phoenix.Ecto.CheckRepoStatus, otp_app: :statushttps://stackoverflow.com/questions/67404687
复制相似问题