Writing vmods in Rust
Guillaume Quintard
guillaume.quintard at gmail.com
Mon Dec 6 01:32:27 UTC 2021
Hi everyone,
I've been working for a while on a little project that ended up taking
quite a chunk of my time, and while it's nowhere near done, I feel like
it's time to show the current progress. *In other words: that stuff ain't
ready, don't put it in prod.*
Here we go: we can *build vmods using pure rust*.
For example, this guys:
https://github.com/gquintard/varnish-rs/tree/main/examples
And the crates `varnish` and `varnish-sys`, that make this possible have
been published on crates.io, and we have documentation!
https://docs.rs/varnish/latest/varnish/
Without trying to start a language war, there are a few benefits to using
Rust over C:
- the language is designed to prevent a few classes of annoying bugs, and
the compiler is very good at explaining where you went wrong
- a lot of the C practices (assert everywhere, miniobj, etc.) are basically
baked into Rust
- the barrier between C and Rust is razor-thin, with almost zero overhead
cost (curse you, null-terminated strings!)
- the tooling is absolutely stellar
- string manipulation is sane (yeah, easy stab at C, but I had to do it)
What you get at the moment:
- few dependencies: you only need cargo, python3 and the libvarnish dev
files to get going
- write the same vmod.vcc as you would for C, the boilerplate is generated
for you
- building is literally just one command: `cargo build`
- automatic type translation so you can write pure rust, and never need to
see the `VCL` objects under their C form (
https://docs.rs/varnish/latest/varnish/vcl/convert/index.html)
- support for PRIVobjects (
https://github.com/gquintard/varnish-rs/blob/main/examples/vmod_object/vmod.vcc
and
https://github.com/gquintard/varnish-rs/blob/main/examples/vmod_object/src/lib.rs
)
- you can iterate through and modify the HTTP objects easily (
https://docs.rs/varnish/latest/varnish/vcl/ctx/struct.Ctx.html)
What you don't get for now:
- a comprehensive API, some bits are still missing, like
WS_ReserveAll/WS_Release, but because of the type conversion, you can
possibly live without it
- a stable API. I'm heavily working on it, and things will change twenty
times before the next Varnish release (but it doesn't matter as the crates
are version-locked)
- something based solely on the public library (libvarnishapi), I
reimplemented some stuff using private bits to avoid copies
- all the types. I only implemented a subset of VCL types, but fret not,
the others will come.
- a sensible boilerplate code generation, the current approach involves
embedding a python script that imports `vmodtool.py` and running it from
rust. But there's a plan to fix that
In case you want to try it, I've also created a minimal example repo that
you can play with:
git clone https://github.com/gquintard/vmod-rs-template.git
cd vmod-rs-template
cargo build
cargo test
I will continue working on expanding the API, documentation and will start
filing issues to come up with a roadmap. If you are interested in helping,
have questions or feedback, please do ping me, I'd love to hear from you.
Cheers!
--
Guillaume Quintard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20211205/00240bac/attachment-0001.html>
More information about the varnish-misc
mailing list