0

We have an elixir (version 1.8.1) web application running inside docker (Server version 1.12.6, Client version 18.09.2) containers within a rancher cluster (Running within EC2, using the AMI Rancheros 1.4.0). We are using the phoenix framework (Version 1.3).

I implemented a simple html cache using the file system. It's a small plug which checks if the html file for the url requested exists and returns the file if it does. If not, it registers a function to run before the response is sent to save the html response to the cache.

It works very well with response times of 4ms and below in case of a cache hit. BUT it seems the plug did introduce a memory leak. The memory the docker container uses grows over time, depending on the amount of traffic the web app receives. If I have a simple crawler going through the side, the memory grows by about 1MB/Minute.

Interestingly, this does not happen locally on my dev machine but in our staging and production environment.

Here is the full plug:

defmodule PagesWeb.Plugs.Cache do
  @moduledoc false

  import Plug.Conn

  def init(default), do: default

  def call(
        %Plug.Conn{method: "GET", query_string: query_string, request_path: request_path} = conn,
        _default
      ) do
    case page_cached?(request_path, query_string) do
      true ->
        conn
        |> put_resp_header("x-phoenix-cache", "true")
        |> put_resp_header("content-type", "text/html; charset=utf-8")
        |> send_file(200, "priv/static/html#{uri_to_filepath(request_path, query_string)}")
        |> halt()

      false ->
        conn
        |> Plug.Conn.register_before_send(&PagesWeb.Plugs.Cache.save_html_to_cache/1)
        |> put_resp_header("x-phoenix-cache", "false")
    end
  end

  def call(conn, _default) do
    conn
  end

  def save_html_to_cache(
        %Plug.Conn{request_path: request_path, query_string: query_string, resp_body: resp_body} =
          conn
      ) do
    case conn.status do
      200 ->
        html_file = uri_to_filepath(request_path, query_string)
        File.mkdir_p(Path.dirname("priv/static/html#{html_file}"))
        File.write("priv/static/html#{html_file}", resp_body)
        conn

      _ ->
        conn
    end
  end

  def read_cached_page(url, query_string) do
    case File.open("priv/static/html#{uri_to_filepath(url, query_string)}", [:read, :utf8]) do
      {:ok, file} ->
        content = IO.read(file, :all)
        File.close(file)
        content

      {:error, _} ->
        :err
    end
  end

  def page_cached?(url, query_string) do
    File.exists?("priv/static/html#{uri_to_filepath(url, query_string)}", [:raw])
  end

  defp uri_to_filepath(url, query_string) do
    query_string =
      case query_string do
        "" -> ""
        qs -> "-#{qs}"
      end

    case url do
      "/" -> "/index.html"
      path -> "#{path}#{query_string}.html"
    end
  end
end
Sgoettschkes
  • 13,141
  • 5
  • 60
  • 78

0 Answers0