Skip to content

Just an example illustrating how to use std.http from within zap (dangerous!)

License

Notifications You must be signed in to change notification settings

zigzap/mini-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mini Proxy Example

This is just an example to demonstrate that you can make HTTP requests inside your request handlers, although I strongly advise against it. It blocks your valuable worker thread for a time you have no control over.

Running the demo

  1. Have some other server running on port 8000
$ python -m http.server
  1. Start this server
$ zig build run
  1. Browse the directory indirectly
$ curl http://localhost:3000/
$ curl http://localhost:3000/src/main.zig

# etc

Why is this dangerouse

The reasons I advise against depending on HTTP requests directly from within your request handler, are briefly:

  • you make a 3rd party server's latency your own.
  • if for some (malicious) reason the remote server sends its response really, really slowly, you might find yourself in a slow loris situation, eventually starving ALL your worker threads, rendering your server "dead" or unresponsive.

What would be a better approach?

  • use the worker threads (request handlers) to dispatch the work to some other thread (pool) that executes potentially long-running tasks, and immediately return a handle or an ID that identifies the request.
  • have your clients poll your server to query whether the potentially long-running task with the provided handle has yielded a result yet.
  • if the long-running task has completed, return the result upon the next poll.

Essentially, make potentially long-running tasks execute asynchronously and you'll "never" risk having your entire server blocked by slow external resources.

If you're brave

There is SOME support for pausing and resuming request handlers in facil.io. So, basically, you can http_pause() your request, have some other thread http_resume() it, check if a response can be sent, and decide whether to http_pause() it again or use http_send_...() / http_finish() on it.

There's no zap wrapping of this functionality yet, but if you're fine with using facil.io's data structures directly, you can already use it via zap.http_pause(), etc.

About

Just an example illustrating how to use std.http from within zap (dangerous!)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published