Skip to content

Commit 9bb39b3

Browse files
author
eamon
committed
coding conent for deadline pattern
1 parent 6c107e7 commit 9bb39b3

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

resiliency/03_deadline/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# deadline pattern
2+
3+
do a thing ,until the deadline time point
4+
5+
which act like time.after(), but time.after() only do once.

resiliency/03_deadline/deadline.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* @Description: https://github.com/crazybber
3+
* @Author: Edward
4+
* @Date: 2020-06-05 12:43:39
5+
* @Last Modified by: Edward
6+
* @Last Modified time: 2020-06-05 12:56:40
7+
*/
8+
19
// Package deadline implements the deadline (also known as "timeout") resiliency pattern for Go.
210
package deadline
311

@@ -12,10 +20,11 @@ var ErrTimedOut = errors.New("timed out waiting for function to finish")
1220
// Deadline implements the deadline/timeout resiliency pattern.
1321
type Deadline struct {
1422
timeout time.Duration
23+
action string
1524
}
1625

1726
// New constructs a new Deadline with the given timeout.
18-
func New(timeout time.Duration) *Deadline {
27+
func New(timeout time.Duration, sometile string) *Deadline {
1928
return &Deadline{
2029
timeout: timeout,
2130
}
@@ -29,6 +38,7 @@ func New(timeout time.Duration) *Deadline {
2938
// the return value of the function is returned from Run.
3039
func (d *Deadline) Run(work func(<-chan struct{}) error) error {
3140
result := make(chan error)
41+
3242
stopper := make(chan struct{})
3343

3444
go func() {
@@ -39,6 +49,8 @@ func (d *Deadline) Run(work func(<-chan struct{}) error) error {
3949
}
4050
}()
4151

52+
//handle result
53+
4254
select {
4355
case ret := <-result:
4456
return ret

resiliency/03_deadline/deadline_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"time"
77
)
88

9-
func takesFiveMillis(stopper <-chan struct{}) error {
9+
func takes5ms(stopper <-chan struct{}) error {
1010
time.Sleep(5 * time.Millisecond)
1111
return nil
1212
}
1313

14-
func takesTwentyMillis(stopper <-chan struct{}) error {
14+
func takes20ms(stopper <-chan struct{}) error {
1515
time.Sleep(20 * time.Millisecond)
1616
return nil
1717
}
@@ -20,14 +20,14 @@ func returnsError(stopper <-chan struct{}) error {
2020
return errors.New("foo")
2121
}
2222

23-
func TestDeadline(t *testing.T) {
24-
dl := New(10 * time.Millisecond)
23+
func TestMultiDeadline(t *testing.T) {
24+
dl := New(10*time.Millisecond, "test multi deadline case")
2525

26-
if err := dl.Run(takesFiveMillis); err != nil {
26+
if err := dl.Run(takes5ms); err != nil {
2727
t.Error(err)
2828
}
2929

30-
if err := dl.Run(takesTwentyMillis); err != ErrTimedOut {
30+
if err := dl.Run(takes20ms); err != ErrTimedOut {
3131
t.Error(err)
3232
}
3333

@@ -47,19 +47,19 @@ func TestDeadline(t *testing.T) {
4747
<-done
4848
}
4949

50-
func ExampleDeadline() {
51-
dl := New(1 * time.Second)
50+
func TestDeadline(t *testing.T) {
51+
dl := New(1*time.Second, "one dead line case")
5252

5353
err := dl.Run(func(stopper <-chan struct{}) error {
54-
// do something possibly slow
55-
// check stopper function and give up if timed out
54+
time.Sleep(time.Second * 10)
5655
return nil
5756
})
5857

5958
switch err {
6059
case ErrTimedOut:
61-
// execution took too long, oops
60+
t.Error("execution took too long, oops")
6261
default:
6362
// some other error
63+
t.Log("done")
6464
}
6565
}

0 commit comments

Comments
 (0)