Skip to content

How to write to a TcpStream without lots of write()s? #13

@njam

Description

@njam

I'm currently using the Json drain to write to a TCP port like this:

    let writer = TcpStream::connect("localhost:5000").unwrap();
    let drain = slog_json::Json::default(writer);
    let logger = slog::Logger::root(Mutex::new(drain).map(slog::Fuse), o!());

The problem with this approach is that the JSON serializer will call write() on the TCP connection for every token ({, ", msg, ", : etc.). I believe this results in quite a performance hit.

I'm thinking to use a BufWriter around the TcpStream to allow buffering of the individual writes. To ensure that every log message is sent through the wire I'd want to call flush() after each message though.
Do you think that's a good solution?
I will open a pull request to add the flush() call if you agree.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions