rawhat / mist

gleam HTTP server. because it glistens on a web
Apache License 2.0
260 stars 11 forks source link

Close connections automaticaly? #43

Open eduardvercaemer opened 3 months ago

eduardvercaemer commented 3 months ago

SO the first thing I tried was to use ab to test a simple ping server from an example.

Turns out each request was always taking 10 seconds to complete. I dug into the code and found out the reason. Without explicitly doing |> response.set_header("connection", "close") on each request, the connection will be closed on a timeout of 10 seconds.

Is there a reason for this ? It definitely threw me off for a while since it is not mentioned anywhere.

Relevant code: https://github.com/rawhat/mist/blob/master/src/mist/internal/http/handler.gleam

fn close_or_set_timer(
  resp: response.Response(ResponseData),
  conn: Connection,
  sender: Subject(handler.Message(user_message)),
) -> Result(State, process.ExitReason) {
  // If the handler explicitly says to close the connection, we should
  // probably listen to them
  case response.get_header(resp, "connection") {
    Ok("close") -> {
      let _ = transport.close(conn.transport, conn.socket)
      Error(process.Normal)
    }
    _ -> {
      // TODO:  this should be a configuration
      let timer = process.send_after(sender, 10_000, Internal(Close))
      Ok(State(idle_timer: Some(timer)))
    }
  }
}
rawhat commented 3 months ago

The "issue" here is that ApacheBench is sending an HTTP/1.0 request. It seems like that doesn't support keep-alive. I'll need to handle this particular case (and likely others with HTTP 1.0).

It's definitely not how it should be behaving, but I doubt many things are issuing requests with that version at this point. I'll definitely fix it, but it shouldn't be a problem with basically any other thing.

Thanks for the issue!