Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
30f2c1f3
提交
30f2c1f3
authored
6月 20, 2016
作者:
Jeromy Johnson
提交者:
GitHub
6月 20, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2871 from rikonor/master
Added variable latency delay, normal and uniform based
上级
720fa80d
cd7010f7
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
61 行增加
和
2 行删除
+61
-2
delay.go
thirdparty/delay/delay.go
+61
-2
没有找到文件。
thirdparty/delay/delay.go
浏览文件 @
30f2c1f3
package
delay
import
(
"math/rand"
"sync"
"time"
)
var
sharedRNG
=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
// Delay makes it easy to add (threadsafe) configurable delays to other
// objects.
type
D
interface
{
...
...
@@ -23,8 +26,6 @@ type delay struct {
t
time
.
Duration
}
// TODO func Variable(time.Duration) D returns a delay with probablistic latency
func
(
d
*
delay
)
Set
(
t
time
.
Duration
)
time
.
Duration
{
d
.
l
.
Lock
()
defer
d
.
l
.
Unlock
()
...
...
@@ -44,3 +45,61 @@ func (d *delay) Get() time.Duration {
defer
d
.
l
.
Unlock
()
return
d
.
t
}
// VariableNormal is a delay following a normal distribution
// Notice that to implement the D interface Set can only change the mean delay
// the standard deviation is set only at initialization
func
VariableNormal
(
t
,
std
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
if
rng
==
nil
{
rng
=
sharedRNG
}
v
:=
&
variableNormal
{
std
:
std
,
rng
:
rng
,
}
v
.
t
=
t
return
v
}
type
variableNormal
struct
{
delay
std
time
.
Duration
rng
*
rand
.
Rand
}
func
(
d
*
variableNormal
)
Wait
()
{
d
.
l
.
RLock
()
defer
d
.
l
.
RUnlock
()
randomDelay
:=
time
.
Duration
(
d
.
rng
.
NormFloat64
()
*
float64
(
d
.
std
))
time
.
Sleep
(
randomDelay
+
d
.
t
)
}
// VariableUniform is a delay following a uniform distribution
// Notice that to implement the D interface Set can only change the minimum delay
// the delta is set only at initialization
func
VariableUniform
(
t
,
d
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
if
rng
==
nil
{
rng
=
sharedRNG
}
v
:=
&
variableUniform
{
d
:
d
,
rng
:
rng
,
}
v
.
t
=
t
return
v
}
type
variableUniform
struct
{
delay
d
time
.
Duration
// max delta
rng
*
rand
.
Rand
}
func
(
d
*
variableUniform
)
Wait
()
{
d
.
l
.
RLock
()
defer
d
.
l
.
RUnlock
()
randomDelay
:=
time
.
Duration
(
d
.
rng
.
Float64
()
*
float64
(
d
.
d
))
time
.
Sleep
(
randomDelay
+
d
.
t
)
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论