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
3cd25604
Commit
3cd25604
authored
Jan 18, 2015
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move generic packages to thirdparty (see thirdparty/README.md)
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
delay.go
delay.go
+46
-0
No files found.
delay.go
0 → 100644
View file @
3cd25604
package
delay
import
(
"sync"
"time"
)
// Delay makes it easy to add (threadsafe) configurable delays to other
// objects.
type
D
interface
{
Set
(
time
.
Duration
)
time
.
Duration
Wait
()
Get
()
time
.
Duration
}
// Fixed returns a delay with fixed latency
func
Fixed
(
t
time
.
Duration
)
D
{
return
&
delay
{
t
:
t
}
}
type
delay
struct
{
l
sync
.
RWMutex
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
()
prev
:=
d
.
t
d
.
t
=
t
return
prev
}
func
(
d
*
delay
)
Wait
()
{
d
.
l
.
RLock
()
defer
d
.
l
.
RUnlock
()
time
.
Sleep
(
d
.
t
)
}
func
(
d
*
delay
)
Get
()
time
.
Duration
{
d
.
l
.
Lock
()
defer
d
.
l
.
Unlock
()
return
d
.
t
}
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