forked from kr/httpshutdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.go
More file actions
36 lines (31 loc) · 695 Bytes
/
net.go
File metadata and controls
36 lines (31 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package httpshutdown
import (
"net"
"sync"
)
// listener wraps a net.Listener and lets you wait when for
// all returned connections to be closed.
type listener struct {
net.Listener
w *sync.WaitGroup
}
func (l *listener) Accept() (net.Conn, error) {
l.w.Add(1) // call Add before the event to be waited for (the connection)
c, err := l.Listener.Accept()
if err != nil {
l.w.Done()
return nil, err
}
return &conn{Conn: c, w: l.w}, nil
}
// conn wraps a net.Conn and decrements the WaitGroup
// when the connection is closed.
type conn struct {
net.Conn
w *sync.WaitGroup
once sync.Once
}
func (c *conn) Close() error {
defer c.once.Do(c.w.Done)
return c.Conn.Close()
}