Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to close the response stream early #124

Open
diyism opened this issue Sep 18, 2022 · 5 comments
Open

ability to close the response stream early #124

diyism opened this issue Sep 18, 2022 · 5 comments
Labels
feature request Enhancement or feature request

Comments

@diyism
Copy link

diyism commented Sep 18, 2022

In nginx-lua/openresty, I can close the http response early (avoid my users waiting) and continue doing things that cost time on my server:

ngx.say("bye bye, my user")
ngx.eof()
local http=require"httpc".create()
http:request{method="GET", url="https://a web page of very long content"}
local data,err=http:read"*a"

Does the algernon web server have such an API? for example "end()":

print("bye bye, my user")
flush()
end()
hc = HTTPClient()
hc:Get("https://a web page of very long content")
@xyproto xyproto added the feature request Enhancement or feature request label Sep 18, 2022
@xyproto
Copy link
Owner

xyproto commented Sep 18, 2022

Thanks for reporting! I think this is a good idea.

@diyism
Copy link
Author

diyism commented Sep 18, 2022

Indeed, I've tested the two mini lua web servers: redbean and mako,
both of them have an async method now:
redbean: jart/cosmopolitan#615 (comment)
mako: RealTimeLogic/BAS#4 (comment)

xyproto added a commit that referenced this issue Sep 27, 2022
@xyproto
Copy link
Owner

xyproto commented Sep 27, 2022

end is already a reserved keyword in Lua, but I added a close() function. It sends a Connection: close header to the connected client, flushes the body and tries to stop Lua functions from writing any more data to the client.

It is also possible to get hold of the net.Conn and then close that one, but it's a tiny bit more involved, since one has to set up a ConnContext field for all involved Server structs that are in use in Algernon.

Please test if the current functionality in the main branch is sufficient to solve this issue. Thanks.

@diyism
Copy link
Author

diyism commented Sep 28, 2022

Thanks for your response.
I've tested it with the following:

$ git clone --depth 1 https://github.com/xyproto/algernon/
$ cd algernon
$ go build -mod=vendor
$ sudo cp algernon /usr/local/bin/
$ mkdir www
$ cd www
$ cat sleep.lua
sleep(3)
print("welcome, my user")
$ cat index.lua
print("bye bye, my user")
close()
GET("http://127.0.0.1:3000/sleep.lua")
$ algernon

then I visit http://127.0.0.1:3000/index.lua
it still cost about 3 seconds to show "bye bye, my user"
it seems there is a problem somewhere.

@xyproto
Copy link
Owner

xyproto commented Sep 28, 2022

Thanks for testing! I'll add code to close the actual connection.

@xyproto xyproto changed the title [Feature Request] to close response stream early ability to close the response stream early Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Enhancement or feature request
Projects
None yet
Development

No branches or pull requests

2 participants