Sorry, you need to enable JavaScript to visit this website.

A simple reverse proxy server in Rust

Do you need help with your Drupal website?

Fill in your email and query below and we can chat.

More info here

Submitted by abhaisasidharan on Thu, 09/24/2020 - 06:38
Why Rust

I have been writing code in rust for a couple of months. I was looking for a programming language that has the least energy consumption, least time to execute, and the smallest memory footprint to build an API first web application. No other features mattered because I have control over other things like learning curve, secure code, etc. I also wanted it to be something exciting that can be used for writing applications that are not just web apps. C has been the most famous "fastest" programming language for as long as I can remember. And Yes! You can write web applications in C.

Finally, I came to the conclusion that it should either be Rust or Go; And I was leaning more toward Rust. People around me kept talking about how Go is the easiest to write code and the most optimized garbage collector blah blah blah. I was very sure that I did not want a garbage collector. I wanted to manage my own memory. I also did not want the overhead caused by garbage collection. I wanted the application I write to be able to run well on a raspberry pi zero. And Rust could do all this extremely well. Apart from that, Rust is the safest language ever. And it's not like when Java says that it's secure. Rust means it and has gone above and beyond to make sure you can only write safe and secure code. It does this because it has one of the most advanced and thorough compilers, that WILL NOT compile your code unless it is safe. I choose Rust. Q.E.D.

This also means the compile-time is extremely slow. For large applications, it can take hours. This will be a huge problem when you are developing web applications. I personally would hate the idea of having to wait even 10 minutes while my project compiles. I am already tired waiting for Acquia pipelines to finish running!

Here is a simple proxy server just in vanilla rust:

I have explained the code the best as I can, feel free to reach out to me for anything by mail or just comment down here. If anyone can help me more to improve this code, it is even more important to reach out, because I learn from wherever I can.

Known Issues:

The code is not non-blocking. There is a lot more code to be written to make it non-blocking. As you can see, streams are stored in Vectors. The max size of a vector that can be used is 32768, but web pages are much larger. So multiple writes and reads are required to do this. But because it is rust, it is still quite fast. And on the browser, you will not notice any difference between this proxy and the actual website.

I am currently re-writing this using hyper.rs and/or tokio.rs so that I can make it non-blocking as well as support https, http/2. Hyper is a really good and correct implementation for easy client/server applications. This is one of the good things about rust; The standard (core) library is very small, because of which there are really good contributed libraries out there. Go has all of this inbuilt and to write the same code in Go would be much easier (probably 30 minutes). I took around 5 hours writing this code. This is only because I wanted to do this in vanilla Rust and I am still learning. If we are using hyper, this will probably take very little time.