forked from mdlayher/vsock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
148 additions
and
54 deletions.
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
language: go | ||
go: | ||
- 1.x | ||
- 1.11.x | ||
os: | ||
- linux | ||
sudo: required | ||
|
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
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//+build go1.12,linux | ||
|
||
package vsock | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
func (lfd *sysListenFD) accept4(flags int) (int, unix.Sockaddr, error) { | ||
// In Go 1.12+, we make use of runtime network poller integration to allow | ||
// net.Listener.Accept to be unblocked by a call to net.Listener.Close. | ||
rc, err := lfd.f.SyscallConn() | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
|
||
var ( | ||
newFD int | ||
sa unix.Sockaddr | ||
) | ||
|
||
doErr := rc.Read(func(fd uintptr) bool { | ||
newFD, sa, err = unix.Accept4(int(fd), flags) | ||
|
||
switch err { | ||
case unix.EAGAIN, unix.ECONNABORTED: | ||
// Return false to let the poller wait for readiness. See the | ||
// source code for internal/poll.FD.RawRead for more details. | ||
// | ||
// When the socket is in non-blocking mode, we might see EAGAIN if | ||
// the socket is not ready for reading. | ||
// | ||
// In addition, the network poller's accept implementation also | ||
// deals with ECONNABORTED, in case a socket is closed before it is | ||
// pulled from our listen queue. | ||
return false | ||
default: | ||
// No error or some unrecognized error, treat this Read operation | ||
// as completed. | ||
return true | ||
} | ||
}) | ||
if doErr != nil { | ||
return 0, nil, doErr | ||
} | ||
|
||
return newFD, sa, err | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//+build !go1.12,linux | ||
|
||
package vsock | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func (lfd *sysListenFD) accept4(flags int) (int, unix.Sockaddr, error) { | ||
// In Go 1.11, accept on the raw file descriptor directly, because lfd.f | ||
// may be attached to the runtime network poller, forcing this call to block | ||
// even if Close is called. | ||
return unix.Accept4(lfd.fd, flags) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Package vsutil provides added functionality for package vsock-internal use. | ||
package vsutil | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
// Accept blocks until a single connection is accepted by the net.Listener, and | ||
// then closes the net.Listener. If timeout is non-zero, the listener will be | ||
// closed after the timeout expires, even if no connection was accepted. | ||
func Accept(l net.Listener, timeout time.Duration) (net.Conn, error) { | ||
defer l.Close() | ||
|
||
// This function accomodates both Go1.12+ and Go1.11- functionality to allow | ||
// net.Listener.Accept to be canceled by net.Listener.Close. | ||
// | ||
// If a timeout is set, set up a timer to close the listener and either: | ||
// - Go1.12+: unblock the call to Accept | ||
// - Go1.11 : eventually halt the loop due to closed file descriptor | ||
cancel := func() {} | ||
if timeout != 0 { | ||
timer := time.AfterFunc(timeout, func() { _ = l.Close() }) | ||
cancel = func() { timer.Stop() } | ||
} | ||
|
||
for { | ||
c, err := l.Accept() | ||
if err != nil { | ||
if nerr, ok := err.(net.Error); ok && nerr.Temporary() { | ||
time.Sleep(250 * time.Millisecond) | ||
continue | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
// Got a connection, stop the timer. | ||
cancel() | ||
return c, nil | ||
} | ||
} |
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