Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
ee4c727c
提交
ee4c727c
authored
1月 20, 2015
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update reuseport for the check
上级
121061b6
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
119 行增加
和
3 行删除
+119
-3
Godeps.json
Godeps/Godeps.json
+1
-1
.travis.yml
..._workspace/src/github.com/jbenet/go-reuseport/.travis.yml
+1
-2
available_unix.go
...pace/src/github.com/jbenet/go-reuseport/available_unix.go
+90
-0
impl_windows.go
...kspace/src/github.com/jbenet/go-reuseport/impl_windows.go
+7
-0
interface.go
...workspace/src/github.com/jbenet/go-reuseport/interface.go
+20
-0
没有找到文件。
Godeps/Godeps.json
浏览文件 @
ee4c727c
...
...
@@ -160,7 +160,7 @@
},
{
"ImportPath"
:
"github.com/jbenet/go-reuseport"
,
"Rev"
:
"
1e1968c4744fef51234e83f015aa0187b4bd796b
"
"Rev"
:
"
a2e454f12a99b8898c41f9dcebae6c35dc3efa3a
"
},
{
"ImportPath"
:
"github.com/jbenet/go-sockaddr/net"
,
...
...
Godeps/_workspace/src/github.com/jbenet/go-reuseport/.travis.yml
浏览文件 @
ee4c727c
language
:
go
go
:
-
1.2
-
1.3
-
1.4
-
release
-
tip
script
:
-
go test -v ./...
-
go test -
race -cpu=5 -
v ./...
Godeps/_workspace/src/github.com/jbenet/go-reuseport/available_unix.go
0 → 100644
浏览文件 @
ee4c727c
// +build darwin freebsd dragonfly netbsd openbsd linux
package
reuseport
import
(
"sync"
"sync/atomic"
"syscall"
"time"
)
// checker is a struct to gather the availability check fields + funcs.
// we use atomic ints because this is potentially a really hot function call.
type
checkerT
struct
{
avail
int32
// atomic int managed by set/isAvailable()
check
int32
// atomic int managed by has/checked()
mu
sync
.
Mutex
// synchonizes the actual check
}
// the static location of the vars.
var
checker
checkerT
func
(
c
*
checkerT
)
isAvailable
()
bool
{
return
atomic
.
LoadInt32
(
&
c
.
avail
)
!=
0
}
func
(
c
*
checkerT
)
setIsAvailable
(
b
bool
)
{
if
b
{
atomic
.
StoreInt32
(
&
c
.
avail
,
1
)
}
else
{
atomic
.
StoreInt32
(
&
c
.
avail
,
0
)
}
}
func
(
c
*
checkerT
)
hasChecked
()
bool
{
return
atomic
.
LoadInt32
(
&
c
.
check
)
!=
0
}
func
(
c
*
checkerT
)
setHasChecked
(
b
bool
)
{
if
b
{
atomic
.
StoreInt32
(
&
c
.
check
,
1
)
}
else
{
atomic
.
StoreInt32
(
&
c
.
check
,
0
)
}
}
// Available returns whether or not SO_REUSEPORT is available in the OS.
// It does so by attepting to open a tcp listener, setting the option, and
// checking ENOPROTOOPT on error. After checking, the decision is cached
// for the rest of the process run.
func
available
()
bool
{
if
checker
.
hasChecked
()
{
return
checker
.
isAvailable
()
}
// synchronize, only one should check
checker
.
mu
.
Lock
()
defer
checker
.
mu
.
Unlock
()
// we blocked. someone may have been gotten this.
if
checker
.
hasChecked
()
{
return
checker
.
isAvailable
()
}
// there may be fluke reasons to fail to add a listener.
// so we give it 5 shots. if not, give up and call it not avail.
for
i
:=
0
;
i
<
5
;
i
++
{
// try to listen at tcp port 0.
l
,
err
:=
listenStream
(
"tcp"
,
"127.0.0.1:0"
)
if
err
==
nil
{
// no error? available.
checker
.
setIsAvailable
(
true
)
checker
.
setHasChecked
(
true
)
l
.
Close
()
// Go back to the Shadow!
return
true
}
if
errno
,
ok
:=
err
.
(
syscall
.
Errno
);
ok
{
if
errno
==
syscall
.
ENOPROTOOPT
{
break
// :( that's all folks.
}
}
// not an errno? or not ENOPROTOOPT? retry.
<-
time
.
After
(
20
*
time
.
Millisecond
)
// wait a bit
}
checker
.
setIsAvailable
(
false
)
checker
.
setHasChecked
(
true
)
return
false
}
Godeps/_workspace/src/github.com/jbenet/go-reuseport/impl_windows.go
浏览文件 @
ee4c727c
...
...
@@ -13,3 +13,10 @@ func listen(network, address string) (net.Listener, error) {
func
dial
(
dialer
net
.
Dialer
,
network
,
address
string
)
(
net
.
Conn
,
error
)
{
return
dialer
.
Dial
(
network
,
address
)
}
// on windows, we just use the regular functions. sources
// vary on this-- some claim port reuse behavior is on by default
// on some windows systems. So we try. may the force be with you.
func
available
()
bool
{
return
true
}
Godeps/_workspace/src/github.com/jbenet/go-reuseport/interface.go
浏览文件 @
ee4c727c
...
...
@@ -20,9 +20,18 @@ package reuseport
import
(
"errors"
"net"
"syscall"
"time"
)
// Available returns whether or not SO_REUSEPORT is available in the OS.
// It does so by attepting to open a tcp listener, setting the option, and
// checking ENOPROTOOPT on error. After checking, the decision is cached
// for the rest of the process run.
func
Available
()
bool
{
return
available
()
}
// ErrUnsuportedProtocol signals that the protocol is not currently
// supported by this package. This package currently only supports TCP.
var
ErrUnsupportedProtocol
=
errors
.
New
(
"protocol not yet supported"
)
...
...
@@ -34,6 +43,10 @@ var ErrReuseFailed = errors.New("reuse failed")
// Returns a net.Listener created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func
Listen
(
network
,
address
string
)
(
net
.
Listener
,
error
)
{
if
!
available
()
{
return
nil
,
syscall
.
Errno
(
syscall
.
ENOPROTOOPT
)
}
return
listenStream
(
network
,
address
)
}
...
...
@@ -41,6 +54,10 @@ func Listen(network, address string) (net.Listener, error) {
// Returns a net.Listener created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func
ListenPacket
(
network
,
address
string
)
(
net
.
PacketConn
,
error
)
{
if
!
available
()
{
return
nil
,
syscall
.
Errno
(
syscall
.
ENOPROTOOPT
)
}
return
listenPacket
(
network
,
address
)
}
...
...
@@ -48,6 +65,9 @@ func ListenPacket(network, address string) (net.PacketConn, error) {
// Returns a net.Conn created from a file discriptor for a socket
// with SO_REUSEPORT and SO_REUSEADDR option set.
func
Dial
(
network
,
laddr
,
raddr
string
)
(
net
.
Conn
,
error
)
{
if
!
available
()
{
return
nil
,
syscall
.
Errno
(
syscall
.
ENOPROTOOPT
)
}
var
d
Dialer
if
laddr
!=
""
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论