首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elixir,Phoenix和Ecto :当数据库关闭时,缓存数据的请求变得非常慢(从100ms到3秒)

Elixir,Phoenix和Ecto :当数据库关闭时,缓存数据的请求变得非常慢(从100ms到3秒)
EN

Stack Overflow用户
提问于 2021-05-05 23:53:52
回答 1查看 201关注 0票数 0

我用--no-html和--database mysql创建了一个phoenix项目

我们在工作中使用的版本是:

代码语言:javascript
复制
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之类的内容

我确信包装器在这两种情况下都使用缓存数据:

我的代码:

代码语言:javascript
复制
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

日志:

代码语言:javascript
复制
[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

对于路由器部分:

代码语言:javascript
复制
 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

我还在日志中看到两个错误:

代码语言:javascript
复制
[error] MyXQL.Connection (#PID<0.373.0>) failed to connect: ** (DBConnection.ConnectionError) connection refused

这看起来“正常”,当应用程序想要重新连接到数据库和这个数据库时,就在处理请求之前(至少根据日志)。

代码语言:javascript
复制
[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

代码语言:javascript
复制
backoff_type: :stop

但这并没有改变我的问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 04:39:51

由于我的代码中,在缓存中找到一个对象(根本)不涉及数据库,我们最终发现问题来自端点的一个插件,该插件应该只存在于开发环境(MyAppWeb/endpoint.ex)中。

代码语言:javascript
复制
   if code_reloading? do
      plug Phoenix.CodeReloader
      plug Phoenix.Ecto.CheckRepoStatus, otp_app: :status
  end

这是一个插件,它会在每次请求时检查数据库是否正常,迁移是否正在运行等,等待Ecto的答复,在genserver中的超时意识到调用导致了延迟!

修复方法是简单地注释这行

代码语言:javascript
复制
          plug Phoenix.Ecto.CheckRepoStatus, otp_app: :status
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67404687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档