Rust in Production

at Terminal.com

Jonathan Reem and Zach Pomerantz

github.com/reem, github.com/zzmp

A bit about us

Terminal.com

  • Fast, simple linux virtualization
  • < 10s snapshot and start time
  • Snapshot disk, RAM, processes
  • In-browser terminal and IDE
    • persistent sessions
    • live collaboration

Rust things we're building (FOSS!)

  • rust thrift implementation
  • rust zookeeper bindings
  • rust netfilter_queue ip filtering

Rust Thrift Implementation

  • Front end C++ code generator
  • Back end rust macros
  • github.com/terminalcloud/thrift
  • also PR at github.com/maximg/thrift

C++ Generator

  • 2k LOC -> 500 LOC
  • Simple, readable output
strukt! {
     name = Many,
     fields = {
         one: i32 => 3,
         two: String => 4,
         three: Vec<Operation> => 9,
     }
}

enom! {
    name = Operation,
    values = [
        Add = 1,
        Sub = 2,
        Clear = 3,
    ],
    default = Sub
}

Rust Macros

  • 300 LOC
    • Structs
    • Enums
    • Services
    • (De)serialization
  • Efficient, low-duplication output
#[macro_export]
macro_rules! strukt {
    (name = $name:ident,
     fields = { $($fname:ident: $fty:ty => $id:expr,)+ }) => {
        #[derive(Debug, Clone, Default)]
        pub struct $name {
            $(pub $fname: Option<$fty>,)+
        }

        impl $crate::protocol::ThriftTyped for $name {
            fn typ() -> $crate::protocol::Type { $crate::protocol::Type::Struct }
        }

Rust Macro (ex)

impl $crate::protocol::Encode for $name {
    fn encode<P, T>(&self, protocol: &mut P, transport: &mut T) -> $crate::Result<()>
    where P: $crate::Protocol, T: $crate::Transport {
        try!(protocol.write_struct_begin(transport, stringify!($name)));

        $(if let Some(ref x) = self.$fname {
            try!(protocol.write_field_begin(transport, stringify!($fname), 
                                            <$fty as ThriftTyped>::typ(), 
                                            $id));
            try!(x.encode(protocol, transport));
            try!(protocol.write_field_end(transport));
        })*

        try!(protocol.write_field_stop(transport));
        try!(protocol.write_struct_end(transport));

        Ok(())
    }
}

Rust Macro (ex)

Rust Zookeeper

  • Bindings to the C API
  • Recent active development
  • Safer, easier to use
  • github.com/terminacloud/rust-zoo

Rust Firewall

  • In use, needs cleanup and
    conversion from C idioms to Rust
  • Bindings to the C netfilter_queue API
    • github.com/terminacloud/libnetfilter_queue
  • Byproducts:
    • github.com/terminacloud/rust-redispool
    • github.com/terminacloud/rust-scheduler

Thanks!

Rust in Production

By Jonathan Reem

Rust in Production

Presentation about the use of Rust in production at terminal.com.

  • 3,371