create ?buf_len ?syscall ?buffer_age_limit fd
creates a new writer. The file descriptor fd
should not be in use for writing by anything else.
By default, a write system call occurs at the end of a cycle in which bytes were written. One can supply ~syscall:(`Periodic span)
to get better performance. This batches writes together, doing the write system call periodically according to the supplied span.
A writer can asynchronously fail if the underlying write syscall returns an error, e.g., EBADF
, EPIPE
, ECONNRESET
, ....
buffer_age_limit
specifies how backed up you can get before raising an exception. The default is `Unlimited
for files, and 2 minutes for other kinds of file descriptors. You can supply `Unlimited
to turn off buffer-age checks.
raise_when_consumer_leaves
specifies whether the writer should raise an exception when the consumer receiving bytes from the writer leaves, i.e., in Unix, the write syscall returns EPIPE
or ECONNRESET
. If not raise_when_consumer_leaves
, then the writer will silently drop all writes after the consumer leaves, and the writer will eventually fail with a writer-buffer-older-than error if the application remains open long enough.
line_ending
determines how newline
and write_line
terminate lines by default. If line_ending = Unix
then end of line is "\n"
; if line_ending = Dos
then end of line is "\r\n"
. Note that line_ending = Dos
is not equivalent to opening the file in text mode because any "\n" characters being printed by other means (e.g., write
"\n"
) are still written verbatim (in Unix style).
time_source
is useful in tests to trigger buffer_age_limit
-related conditions, or simply to have the result of (for example) flushed_time_ns
agree with your test's synthetic time. It is also used to schedule the `Periodic
syscalls.
buf_len
specifies the initial size of the internal buffer. This buffer will be automatically resized up if more data is written than the buffer has room for, e.g. using the write*
functions. Note that buffers at least 128 KiB in size will be allocated with mmap (see bigstring_unix_stubs.c
); buffers smaller than that will go on the C heap directly, which can cause C heap fragmentation in programs that allocate lots of buffers, e.g. RPC servers, possibly resulting in unintuitive higher overall program memory usage.