New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: export writeBuffers() and buffersWriter #21756

Open
gobwas opened this Issue Sep 4, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@gobwas

gobwas commented Sep 4, 2017

Hello!

Looking at net.Buffers.WriteTo() implementation one can find that it uses type assertion to non-exported type net.buffersWriter. The proposal is to export this type and methods which implement it.

Two things:

  1. When someone wants to wrap net.Conn to get some i/o statistics, say:
type ConnWatcher struct {
    c net.Conn
    n int64
}
func (c *ConnWatcher) Write(p []byte) (int, error) {
    n, err := c.conn.Write(p)
    c.n += int64(n)
    return n, err
}

It becomes impossible to use writev() syscall – ConnWatcher has no way to implement non-exported net.buffersWriter interface.

  1. When someone wants to wrap concrete *net.TCPConn (rare case, but possible) to decorate some write behaviour:
type CipherConn struct {
    *net.TCPConn                                
    Encode func([]byte) []byte
}
func (c CipherConn) Write(p []byte) (int, error) {
    return c.TCPConn.Write(c.Encode(p))
}

// Somewhere in the application.
var bs net.Buffers
var cc CipherConn
bs.WriteTo(cc) 

The last line of the example above bs.WriteTo(cc) will produce cc.TCPConn.writeBuffers(bs) call, sending raw data without encoding.

Regards,
Sergey.

@gobwas gobwas changed the title from net: export writeBuffers() method to net: export writeBuffers() and bufferWriter Sep 4, 2017

@gobwas gobwas changed the title from net: export writeBuffers() and bufferWriter to net: export writeBuffers() and buffersWriter Sep 4, 2017

@funny-falcon

This comment has been minimized.

Contributor

funny-falcon commented Sep 4, 2017

Hidden behavior that is difficult to use right.

@odeke-em

This comment has been minimized.

Member

odeke-em commented Sep 4, 2017

Looks similar to #21676 in which @zombiezen talks about how frustrating it is that writeBuffers is unexported.

@gobwas

This comment has been minimized.

gobwas commented May 8, 2018

Sorry, for open/reopen spam ) Should I close it as a duplicate or keep it opened until Go1.11?

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented May 8, 2018

The two seem slightly different, let's keep them both for now.

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment