Giter Club home page Giter Club logo

astra's Introduction

Astra

Crate Github Docs

A blocking HTTP server built on top of hyper.

use astra::{Body, Response, Server};

fn main() {
    Server::bind("localhost:3000")
        .serve(|_req, _info| Response::new(Body::new("Hello World!")))
        .expect("serve failed");
}

How Does It Work?

Hyper is built on async I/O and depends on it to run correctly. To avoid depending on a large crate like Tokio, Astra runs a small evented I/O loop on a background thread and dispatches connections to it's own worker pool. The difference is that instead of tasks yielding to a userspace runtime like Tokio, they yield to the operating system. This means that request handlers can use standard I/O primitives without worrying about blocking the runtime:

use astra::{Body, ResponseBuilder, Server};
use std::time::Duration;

fn main() {
    Server::bind("localhost:3000")
        .serve(|_req, _info| {
            // Putting the worker thread to sleep will allow
            // other workers to run.
            std::thread::sleep(Duration::from_secs(1));

            // Regular blocking I/O is fine too!
            let body = std::fs::read_to_string("index.html").unwrap();

            ResponseBuilder::new()
                .header("Content-Type", "text/html")
                .body(Body::new(body))
                .unwrap()
        })
        .expect("serve failed");
}

Features

Astra supports both HTTP/1 and HTTP/2 with most the configuration options that Hyper exposes. Features that depend on Tokio however, such as http2_keep_alive_while_idle, are not supported and blocked on better hyper support.

Astra is currently an HTTP server library only. The client API is unimplemented.

Security

One of the issues with blocking I/O is that it is susceptible to attacks such as Slowloris. Because of this, it is important to run your server behind an async reverse proxy such as Nginx. You were likely going to doing this anyways for TLS support, but it is something to keep in mind.

But Is It Fast?

Many of the references you'll find about thread-per-request performance are very outdated, often referencing bottlenecks from a time where C10k was peak scale. Since then, thread creation has gotten significantly cheaper, and context switching overhead has been reduced drastically. Modern OS schedulers are much better than they are given credit for, and it is now very feasible to serve upwards of tens of thousands of concurrent connections using blocking I/O.

In naive "Hello World" style HTTP benchmarks, Astra is likely to lag behind Tokio. This is partly because Astra has to pay the cost of both threading and async I/O to be compatible with Hyper. However, as more work is done per request, especially pure blocking I/O, the difference diminishes. As always, you should measure your own use case, but Astra's performance may surprise you.

That being said, one of Astra's main use cases is running a lightweight server with minimal dependencies, and avoiding the complexity that comes with async, so any potential performance tradeoffs might not be be relevant.

astra's People

Contributors

dpc avatar fatfingers23 avatar ibraheemdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

astra's Issues

Consider TLS support?

This project looks very interesting; I'm writing a CLI app that needs to serve one single request (for OAuth), and in principle I'd like to avoid having to spin up Tokio and go full async just for that, and Astra fits the bill. But of course I must have TLS, and whereas for a normal server I'd just use a reverse proxy I can't do that here. Would you be willing to offer built-in TLS?

Feature suggestion: client address of requests

I'm in the process of migrating some code from using tiny_http to astra, but there's one piece of functionality I've been relying upon (mainly for logging) that I don't think astra supports at the moment: getting the client address of a request.

In tiny_http, that's provided by its Request type remote_addr(), and plain hyper gives access to it through MakeService (see this commit).

I'd imagine this would require a breaking change to astra::Service to accept a second argument or an entirely new type of Request. I'm happy to push a PR if this sounds like a good idea.

Testing

Along with the overall lack of integration tests, some of the utilities Body::wrap_stream, BodyReader would benefit from unit tests.

End-goal featureset, roadmap and goals

Hi!

In my free time I wanted to play with htmx in Rust, which turned out into usual spare-time yak-shaving, trying to glue together some personal "web stack".

I'm somewhat of an "async-sceptic" - in a sense that I believe that in its current form async brings more pain than benefit for majority of projects.

So for web server I seem to have two choices: there's rouille and now there's astra. I used rouille before, for similar reasons, it was OK. But I like astra's hyper-based yet-blocking model, and so far it feels kind nice to work with.

As I'm playing with different functionality, I feel some pain points. E.g. I now want to handle sessions, deal with cookies, possibly forms, etc. Hyper itself does not seem to help much with these.

So I wonder - if astra planning to remain rather bare minimum wrapper around hyper, or is the a room for some "conveniences" like cookie and form parsing, and possibly others - either directly, or in some companion/recommended libraries?

Do you have any plans already or are you satisfied with the current state?

Serving static HTML etc files

Hi! How can I use astra to serve an HTML file? Thank you!

For example, how can I serve the rust docs using Astra? Thank you!

`Arc` the `Service` internally is not optimal

let service = Arc::new(service);

Service already requires Send + Sync + 'static so it's rather pointless. Instead S: Service + Clone, and the user can Arc (or not) the S internally.

In my case I need to use Arc internally in my code (because reasons), and now it will be effectively Arc<Arc<Stuff>> and it can't be avoided, so my inner micro-optimizator is bothering me.

Hyper 1.0

Upgrade to Hyper 1.0, look into what extra features we can implement now (timers, etc.).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.