-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisterner.go
53 lines (41 loc) · 1016 Bytes
/
listerner.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package grpcmock
import (
"net"
"sync"
"go.nhat.io/grpcmock/must"
)
var _ net.Listener = (*listener)(nil)
type listener struct {
upstream net.Listener
signal chan struct{}
sendSignal sync.Once
}
// Accept waits for and returns the next connection to the listener.
func (l *listener) Accept() (net.Conn, error) {
l.sendSignal.Do(func() {
close(l.signal)
})
return l.upstream.Accept()
}
// Close closes the listener.
func (l *listener) Close() error {
return l.upstream.Close()
}
// Addr returns the listener's network address.
func (l *listener) Addr() net.Addr {
return l.upstream.Addr()
}
func newListenerByAddr(addr string) func() (net.Listener, func() error) {
return func() (net.Listener, func() error) {
l, err := net.Listen("tcp", addr)
must.NotFail(err)
return l, l.Close
}
}
func newListenerWithReadySignal(upstream net.Listener) (net.Listener, <-chan struct{}) {
signal := make(chan struct{})
return &listener{
upstream: upstream,
signal: signal,
}, signal
}