-
Notifications
You must be signed in to change notification settings - Fork 204
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
Add CMux.Close() to shutdown server #69
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f6608f
add cmux.Close() function
7556a72
Remove use of mutex around done chan
3046316
use param for recieve only chan
jan25 e0463e7
fix blocking select on donec
jan25 381bcc5
Add mutex around channel close
jan25 da9feff
Add TestClose
jan25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package cmux | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
|
@@ -61,6 +62,9 @@ func (e errListenerClosed) Timeout() bool { return false } | |
// listener is closed. | ||
var ErrListenerClosed = errListenerClosed("mux: listener closed") | ||
|
||
// ErrServerClosed is returned from muxListener.Accept when mux server is closed. | ||
var ErrServerClosed = errors.New("mux: server closed") | ||
|
||
// for readability of readTimeout | ||
var noTimeout time.Duration | ||
|
||
|
@@ -93,6 +97,8 @@ type CMux interface { | |
// Serve starts multiplexing the listener. Serve blocks and perhaps | ||
// should be invoked concurrently within a go routine. | ||
Serve() error | ||
// Closes cmux server and stops accepting any connections on listener | ||
Close() | ||
// HandleError registers an error handler that handles listener errors. | ||
HandleError(ErrorHandler) | ||
// sets a timeout for the read of matchers | ||
|
@@ -108,9 +114,10 @@ type cMux struct { | |
root net.Listener | ||
bufLen int | ||
errh ErrorHandler | ||
donec chan struct{} | ||
sls []matchersListener | ||
readTimeout time.Duration | ||
donec chan struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to put things that are protected by a mutex underneath the mutex. |
||
mu sync.Mutex | ||
} | ||
|
||
func matchersToMatchWriters(matchers []Matcher) []MatchWriter { | ||
|
@@ -133,6 +140,7 @@ func (m *cMux) MatchWithWriters(matchers ...MatchWriter) net.Listener { | |
ml := muxListener{ | ||
Listener: m.root, | ||
connc: make(chan net.Conn, m.bufLen), | ||
donec: make(chan struct{}), | ||
} | ||
m.sls = append(m.sls, matchersListener{ss: matchers, l: ml}) | ||
return ml | ||
|
@@ -146,7 +154,7 @@ func (m *cMux) Serve() error { | |
var wg sync.WaitGroup | ||
|
||
defer func() { | ||
close(m.donec) | ||
m.closeDoneChans() | ||
wg.Wait() | ||
|
||
for _, sl := range m.sls { | ||
|
@@ -204,6 +212,30 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) { | |
} | ||
} | ||
|
||
func (m *cMux) Close() { | ||
m.closeDoneChans() | ||
} | ||
|
||
func (m *cMux) closeDoneChans() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
select { | ||
case <-m.donec: | ||
// Already closed. Don't close again | ||
default: | ||
close(m.donec) | ||
jan25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
for _, sl := range m.sls { | ||
select { | ||
case <-sl.l.donec: | ||
// Already closed. Don't close again | ||
default: | ||
close(sl.l.donec) | ||
} | ||
} | ||
} | ||
|
||
func (m *cMux) HandleError(h ErrorHandler) { | ||
m.errh = h | ||
} | ||
|
@@ -223,14 +255,19 @@ func (m *cMux) handleErr(err error) bool { | |
type muxListener struct { | ||
net.Listener | ||
connc chan net.Conn | ||
donec chan struct{} | ||
} | ||
|
||
func (l muxListener) Accept() (net.Conn, error) { | ||
c, ok := <-l.connc | ||
if !ok { | ||
return nil, ErrListenerClosed | ||
select { | ||
case c, ok := <-l.connc: | ||
if !ok { | ||
return nil, ErrListenerClosed | ||
} | ||
return c, nil | ||
case <-l.donec: | ||
return nil, ErrServerClosed | ||
} | ||
return c, nil | ||
} | ||
|
||
// MuxConn wraps a net.Conn and provides transparent sniffing of connection data. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try and use
Close() error
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error
can inform user about multipleClose()
calls. Are there other cases whenerror
can be useful?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The principle interest is in satisfying the
interface { Close() error }
(i.e. theio.Closer
interface) more than anything else.For instance,
net.Listener
for instance implementsio.Closer
.But it’s definitely not anything critical, just expected that
Close()
returns an error.