Skip to content

Instantly share code, notes, and snippets.

@stuartnelson3
Last active July 18, 2017 22:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartnelson3/88b70ffa1aa8d8c0e0d703d0574acc09 to your computer and use it in GitHub Desktop.
Save stuartnelson3/88b70ffa1aa8d8c0e0d703d0574acc09 to your computer and use it in GitHub Desktop.
borrowing + lifetime issues
[package]
name = "rust_learning"
version = "0.1.0"
authors = ["Stuart Nelson <stuartnelson3@gmail.com>"]
[dependencies]
prometheus = "0.2"
extern crate prometheus;
use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder};
use std::net::{TcpListener, TcpStream};
use std::io::Write;
use std::thread;
fn handle_client(stream: TcpStream) {
// Turn this into a closure that accepts the prometheus::Registry
// I'll make a different function or something that increments the prometheus::Counter.
}
fn main() {
let listener = TcpListener::bind("0.0.0.0:5000").unwrap();
// Create a Counter.
let counter_opts = Opts::new("test_counter", "test counter help");
let counter = Counter::with_opts(counter_opts).unwrap();
// Create a Registry and register Counter.
let r = Registry::new();
r.register(Box::new(counter.clone())).unwrap();
// accept connections and process them serially
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
thread::spawn(|| {
// handle_client(stream);
counter.inc();
let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_familys = r.gather();
encoder.encode(&metric_familys, &mut buffer).unwrap();
stream.write(&buffer);
});
}
Err(e) => { /* connection failed */ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment