如何解决Elixir、Phoenix 和 Ecto:当数据库关闭时,提供缓存数据的请求变得非常缓慢从 100 毫秒到 3 秒
我用--no-html和--database MysqL创建了一个凤凰项目
我们在工作中使用的版本是:
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 需要大约 100 毫秒才能获得响应,而 phoenix 的日志会显示类似 [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,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",: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
但它对我的问题没有任何改变
解决方法
作为我的代码,在缓存中找到对象的路径中(根本)不涉及数据库,我们最终发现问题来自端点中的插件,该插件应该只存在于开发环境 (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: :status
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。