erikdubbelboer / fasthttp

This fork has been deprecated!
MIT License
50 stars 12 forks source link

Servers shuts down after last request #43

Closed dgrr closed 6 years ago

dgrr commented 6 years ago

Hello Erik,

I was testing new Shutdown function and I found that server shuts down after a request perform after Shutdown call. If I press Ctrl+C I must wait a new request to shutdown the server completly. I tried adding s.wg.Done() after this line and works well with the code below but the tests fails (as should do).

package main

import "github.com/erikdubbelboer/fasthttp"
import "os"
import "os/signal"

func hello(ctx *fasthttp.RequestCtx) {
  ctx.WriteString("Hello world!")
}

func main() {
  ch := make(chan os.Signal)
  signal.Notify(ch, os.Interrupt)
  server := &fasthttp.Server{
    Handler: hello,
  }
  go server.ListenAndServe(":8080")
  <-ch
  server.Shutdown()
}
illotum commented 6 years ago

This behaviour is explicitly described in the Shutdown doc. Use Close() if you want an immediate return.

dgrr commented 6 years ago

@illotum no. This is not described in doc. The problem is that I must wait new connection to exit successfully

erikdubbelboer commented 6 years ago

@themester on my linux and mac machines this works fine. What platform are you using? Are you doing any requests or do you press Ctrl+C immediately after starting?

dgrr commented 6 years ago

Hello @erikdubbelboer

I see that works well if you make multiple requests. But if you make one or two the servers waits another connection after Shutdown call. I mean that does not work in all cases. I do not think that is an issue.

erikdubbelboer commented 6 years ago

What platform are you using?

dgrr commented 6 years ago

Gentoo (linux)

erikdubbelboer commented 6 years ago

Can you write a test case or a program that always causes this issue? I can't seem to replicate it.

dgrr commented 6 years ago

http://f.mester.pw/erik.mp4 here you can see the error.

dgrr commented 6 years ago

Okay. I identified the problem. It can be fixed changing this line to case <-time.After(...): But I don't know if it will affect the performance. [edit] I checked it and the server performance is not reduced by this change.

erikdubbelboer commented 6 years ago

That impossible. I'm sure something else is going on. That is just a goroutine that periodically cleans up the worker pool. Nothing will block on that and when main exits the program will just quit no matter if this goroutine is still running.

erikdubbelboer commented 6 years ago

I think your issue is that you still have a keep-alive connection open and the server won't terminate until this is closed. Sending another request forces the connection to close after this. One way to fix this is to set a read or keepalive timeout on the server.

dgrr commented 6 years ago

Aghhh!! That's right. I have keep-alive connections but Chrome does not show Connection field in header and does not show active connections (unlike firefox)