Skip to content
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

Closed
gobwas opened this Issue Sep 4, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@gobwas
Copy link

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 net: export writeBuffers() method net: export writeBuffers() and bufferWriter Sep 4, 2017

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

@funny-falcon

This comment has been minimized.

Copy link
Contributor

funny-falcon commented Sep 4, 2017

Hidden behavior that is difficult to use right.

@odeke-em

This comment has been minimized.

Copy link
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.

Copy link
Author

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.

Copy link
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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

ianlancetaylor commented Dec 18, 2018

After more thought, closing as dup of #21676 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.