Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-delay
Commits
f91cdb03
Commit
f91cdb03
authored
Nov 09, 2018
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reorder params and defaults for delay
parent
f1bb990c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
6 deletions
+9
-6
delay.go
delay.go
+8
-5
delay_test.go
delay_test.go
+1
-1
No files found.
delay.go
View file @
f91cdb03
...
@@ -20,8 +20,8 @@ type D interface {
...
@@ -20,8 +20,8 @@ type D interface {
type
delay
struct
{
type
delay
struct
{
l
sync
.
RWMutex
l
sync
.
RWMutex
t
time
.
Duration
t
time
.
Duration
sleeper
Sleeper
generator
Generator
generator
Generator
sleeper
Sleeper
}
}
func
(
d
*
delay
)
Set
(
t
time
.
Duration
)
time
.
Duration
{
func
(
d
*
delay
)
Set
(
t
time
.
Duration
)
time
.
Duration
{
...
@@ -51,7 +51,10 @@ func (d *delay) Get() time.Duration {
...
@@ -51,7 +51,10 @@ func (d *delay) Get() time.Duration {
}
}
// Delay generates a generic delay form a t, a sleeper, and a generator
// Delay generates a generic delay form a t, a sleeper, and a generator
func
Delay
(
t
time
.
Duration
,
sleeper
Sleeper
,
generator
Generator
)
D
{
func
Delay
(
t
time
.
Duration
,
generator
Generator
,
sleeper
Sleeper
)
D
{
if
sleeper
==
nil
{
sleeper
=
sharedRealSleeper
}
return
&
delay
{
return
&
delay
{
t
:
t
,
t
:
t
,
sleeper
:
sleeper
,
sleeper
:
sleeper
,
...
@@ -61,19 +64,19 @@ func Delay(t time.Duration, sleeper Sleeper, generator Generator) D {
...
@@ -61,19 +64,19 @@ func Delay(t time.Duration, sleeper Sleeper, generator Generator) D {
// Fixed returns a delay with fixed latency
// Fixed returns a delay with fixed latency
func
Fixed
(
t
time
.
Duration
)
D
{
func
Fixed
(
t
time
.
Duration
)
D
{
return
Delay
(
t
,
sharedRealSleeper
,
FixedGenerator
())
return
Delay
(
t
,
FixedGenerator
()
,
nil
)
}
}
// VariableUniform is a delay following a uniform distribution
// VariableUniform is a delay following a uniform distribution
// Notice that to implement the D interface Set can only change the minimum delay
// Notice that to implement the D interface Set can only change the minimum delay
// the delta is set only at initialization
// the delta is set only at initialization
func
VariableUniform
(
t
,
d
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
func
VariableUniform
(
t
,
d
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
return
Delay
(
t
,
sharedRealSleeper
,
VariableUniformGenerator
(
d
,
rng
))
return
Delay
(
t
,
VariableUniformGenerator
(
d
,
rng
)
,
nil
)
}
}
// VariableNormal is a delay following a normal distribution
// VariableNormal is a delay following a normal distribution
// Notice that to implement the D interface Set can only change the mean delay
// Notice that to implement the D interface Set can only change the mean delay
// the standard deviation is set only at initialization
// the standard deviation is set only at initialization
func
VariableNormal
(
t
,
std
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
func
VariableNormal
(
t
,
std
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
return
Delay
(
t
,
sharedRealSleeper
,
VariableNormalGenerator
(
std
,
rng
))
return
Delay
(
t
,
VariableNormalGenerator
(
std
,
rng
)
,
nil
)
}
}
delay_test.go
View file @
f91cdb03
...
@@ -68,7 +68,7 @@ func TestDelaySleep(t *testing.T) {
...
@@ -68,7 +68,7 @@ func TestDelaySleep(t *testing.T) {
generator
:=
&
fixedAdd
{
toAdd
:
toAdd
}
generator
:=
&
fixedAdd
{
toAdd
:
toAdd
}
sleeper
:=
&
recordSleeper
{
lastSleep
:
-
1
}
sleeper
:=
&
recordSleeper
{
lastSleep
:
-
1
}
delay
:=
Delay
(
initialValue
,
sleeper
,
generato
r
)
delay
:=
Delay
(
initialValue
,
generator
,
sleepe
r
)
if
delay
.
NextWaitTime
()
!=
initialValue
+
toAdd
{
if
delay
.
NextWaitTime
()
!=
initialValue
+
toAdd
{
t
.
Fatal
(
"NextWaitTime should call the generator"
)
t
.
Fatal
(
"NextWaitTime should call the generator"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment