-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Modify Handler.finished API to remove assumption all responses are one-shot. #81
Open
redvers
wants to merge
4
commits into
ponylang:main
Choose a base branch
from
redvers:recusive_finished
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use "../../http_server" | ||
use "net" | ||
use "valbytes" | ||
use "debug" | ||
|
||
actor Main | ||
""" | ||
A simple example of how to send your response body gradually. When sending | ||
large responses you don't want the entire payload in memory at the same | ||
time. | ||
""" | ||
new create(env: Env) => | ||
for arg in env.args.values() do | ||
if (arg == "-h") or (arg == "--help") then | ||
_print_help(env) | ||
return | ||
end | ||
end | ||
|
||
let port = try env.args(1)? else "50000" end | ||
let limit = try env.args(2)?.usize()? else 100 end | ||
let host = "localhost" | ||
|
||
// Start the top server control actor. | ||
let server = Server( | ||
TCPListenAuth(env.root), | ||
LoggingServerNotify(env), // notify for server lifecycle events | ||
BackendMaker.create(env) // factory for session-based application backend | ||
where config = ServerConfig( // configuration of Server | ||
where host' = host, | ||
port' = port, | ||
max_concurrent_connections' = limit) | ||
) | ||
// everything is initialized, if all goes well | ||
// the server is listening on the given port | ||
// and thus kept alive by the runtime, as long its listening socket is not | ||
// closed. | ||
|
||
fun _print_help(env: Env) => | ||
env.err.print( | ||
""" | ||
Usage: | ||
|
||
gradual [<PORT> = 50000] [<MAX_CONCURRENT_CONNECTIONS> = 100] | ||
|
||
""" | ||
) | ||
|
||
|
||
class LoggingServerNotify is ServerNotify | ||
""" | ||
Notification class that is notified about | ||
important lifecycle events for the Server | ||
""" | ||
let _env: Env | ||
|
||
new iso create(env: Env) => | ||
_env = env | ||
|
||
fun ref listening(server: Server ref) => | ||
""" | ||
Called when the Server starts listening on its host:port pair via TCP. | ||
""" | ||
try | ||
(let host, let service) = server.local_address().name()? | ||
_env.err.print("connected: " + host + ":" + service) | ||
else | ||
_env.err.print("Couldn't get local address.") | ||
_env.exitcode(1) | ||
server.dispose() | ||
end | ||
|
||
fun ref not_listening(server: Server ref) => | ||
""" | ||
Called when the Server was not able to start listening on its host:port pair via TCP. | ||
""" | ||
_env.err.print("Failed to listen.") | ||
_env.exitcode(1) | ||
|
||
fun ref closed(server: Server ref) => | ||
""" | ||
Called when the Server is closed. | ||
""" | ||
_env.err.print("Shutdown.") | ||
|
||
class BackendMaker is HandlerFactory | ||
""" | ||
Fatory to instantiate a new HTTP-session-scoped backend instance. | ||
""" | ||
let _env: Env | ||
|
||
new val create(env: Env) => | ||
_env = env | ||
|
||
fun apply(session: Session): Handler^ => | ||
BackendHandler.create(_env, session) | ||
|
||
class BackendHandler is Handler | ||
""" | ||
Backend application instance for a single HTTP session. | ||
|
||
Executed on an actor representing the HTTP Session. | ||
That means we have 1 actor per TCP Connection | ||
(to be exact it is 2 as the TCPConnection is also an actor). | ||
""" | ||
let _env: Env | ||
let _session: Session | ||
|
||
var _response: BuildableResponse | ||
var stage: (ExHdrs | ExHello | ExWorld) = ExHdrs | ||
|
||
new ref create(env: Env, session: Session) => | ||
_env = env | ||
_session = session | ||
_response = BuildableResponse(where status' = StatusOK) | ||
|
||
fun ref finished(request_id: RequestID): Bool => | ||
""" | ||
Start processing a request. | ||
|
||
Called when request-line and all headers have been parsed. | ||
Body is not yet parsed, not even received maybe. | ||
|
||
In this example we have a simple State Machine which we | ||
use to demonstrate how replies can be chunked in such a | ||
way as we trade memory efficiency for speed. | ||
|
||
This tradeoff is needed when sending huge files. | ||
|
||
""" | ||
|
||
match stage | ||
| ExHdrs => | ||
var response: BuildableResponse iso = BuildableResponse(where status' = StatusOK) | ||
response.add_header("Content-Type", "text/plain") | ||
response.add_header("Server", "http_server.pony/0.2.1") | ||
response.add_header("Content-Length", "12") | ||
|
||
_session.send_start(consume response, request_id) | ||
stage = ExHello | ||
return false | ||
| ExHello => _session.send_chunk("Hello ", request_id) | ||
stage = ExWorld | ||
return false | ||
| ExWorld => | ||
_session.send_chunk("World!", request_id) | ||
_session.send_finished(request_id) | ||
return true | ||
end | ||
true // Never Reached | ||
|
||
primitive ExHdrs | ||
primitive ExHello | ||
primitive ExWorld |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the commented out?