Open Jorropo opened 4 months ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
opening as a formal proposal to solve #21676, feel free to close a dup
And in case of rehashing arguments from #21676 the problem I am seeing:
I have protocol with <small header> | <big payload>
and a method like this:
func (*T) WritePayload(b []byte) error
Here are the possible solutions today and why they are not that good:
syscall.Syscall6
doing TCP)net.Buffers
it is not composable, in most of my usecases I have a TCPConn wrapped in TLS, HTTP or yamux.bufio.Writer
still do two .Write
call, the header is small enough to be fully buffered, then the payload is written to, it does not fit into the buffer, so bufio.Writer
flushes the header and then pass the payload as-is in a second .Write
call.
.WriteMany
to write the buffered portion and the inbound payload in one call, would be done conditionally with a conditional type assertion but that would be a future proposal if something like this one passes.t.underlying.Write(append(header, payload...))
generate garbagefunc (*T) WritePayload(n uint, func(dest []byte) error) error
)
create callback hell when composed and is not always possible (I don't always know the maximum size of the payload ahead of time)Does defining a dual io.ManyReader
have any known applications?
@jfrech ManyReader
¿ not ManyWriter
?
Idk this is not this proposal feel free to open an other one.
Proposal Details
This proposal aims at making a user-implementable version of
net.Buffers
.Add a new interface to
io
and implement it onnet.*
which implementnet.buffersWriter
:note: this return
int64
because it would be easy to build let's say 16GiB buffer on a 32bits pointer architecture by having the byte arrays alias each other.(*net.Buffers).WriteTo
would also check if the underlying implementation supportsio.ManyWriter
as well asnet.buffersWriter
.