-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
125 lines (111 loc) · 3.06 KB
/
listener.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package multilistener
import (
"fmt"
"net"
)
// Listener implements a net.Listener interface but multiplexes connections
// from multiple listeners.
type Listener struct {
listeners []net.Listener
closing chan struct{}
conns chan acceptResults
}
type acceptResults struct {
conn net.Conn
err error
}
var _ net.Listener = &Listener{}
// New creates an instance of a Listener using the given listeners. You must
// pass at least one listener. The new listener object listens for new connection
// on all the given listeners.
func New(listeners ...net.Listener) (*Listener, error) {
if len(listeners) == 0 {
return nil, fmt.Errorf("multilistener requires at least 1 listener")
}
n := &Listener{
listeners: listeners,
closing: make(chan struct{}),
conns: make(chan acceptResults),
}
for i := range n.listeners {
go n.loop(n.listeners[i])
}
return n, nil
}
// Addr returns the address of the first listener the multi-listener is using.
// The address of other listeners are not available.
func (l *Listener) Addr() net.Addr {
return l.listeners[0].Addr()
}
// Close will close the multi-listener by iterating over its listeners and calling
// Close() on each one. If an error is encountered, it is returned. If multiple
// errors are encountered they are returned in a MutiError. Close will also shut down
// the background goroutines that are calling Accept() on the underlying listeners.
//
// Calling Close() more than once will cause it to panic.
func (l *Listener) Close() error {
close(l.closing)
var errors []error
for i := range l.listeners {
err := l.listeners[i].Close()
if err != nil {
errors = append(errors, err)
}
}
switch len(errors) {
case 0:
return nil
case 1:
return errors[0]
}
return &MultiError{Errors: errors}
}
// MultiError is a wrapper around a slice of errors that implements the error interface.
type MultiError struct {
Errors []error
}
// Error concats the Error() messages of the underlying errors
func (m *MultiError) Error() string {
if len(m.Errors) == 0 {
return ""
}
s := "errors: "
for _, e := range m.Errors {
s += e.Error() + ", "
}
return s
}
// loop continually accepts connections from the given listener. It forwards the result
// of the .Accept() method to a channel on the listener. When a user of the Listener object
// calls Accept(), it receives a value from that channel. Closing the listener will cause
// this loop to exit.
func (l *Listener) loop(listener net.Listener) {
for {
conn, err := listener.Accept()
r := acceptResults{
conn: conn,
err: err,
}
select {
case l.conns <- r:
case <-l.closing:
if r.err == nil {
r.conn.Close()
}
return
}
}
}
// Accept will wait for a result from calling Accept from the underlying listeners.
// It will return an error if the multi-listener is closed.
func (l *Listener) Accept() (net.Conn, error) {
select {
case acceptResult, ok := <-l.conns:
if ok {
return acceptResult.conn, acceptResult.err
}
return nil, fmt.Errorf("closed conn channel")
case <-l.closing:
return nil, fmt.Errorf("listener is closed")
}
}