Created
December 31, 2017 18:34
-
-
Save zombiezen/8da741be7aed53d2de82679d52d08446 to your computer and use it in GitHub Desktop.
Closing simple RPC connection in Experience Report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Conn struct { | |
wc io.WriteCloser | |
tasks sync.WaitGroup | |
// ... other internal state ... | |
} | |
func NewConn(rwc io.ReadWriteCloser) *Conn { | |
c := &Conn{wc: rwc} | |
c.tasks.Add(1) | |
go func() { | |
defer c.tasks.Done() | |
c.receive(rwc) | |
}() | |
return c | |
} | |
func (c *Conn) receive(r io.Reader) { | |
for { | |
msg, err := decode(r) | |
if err != nil { | |
return | |
} | |
// ... Process message ... | |
} | |
} | |
func (c *Conn) Close() error { | |
err := c.wc.Close() | |
c.tasks.Wait() | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment