mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-24 12:46:02 +03:00
coding conent for deadline pattern
This commit is contained in:
parent
6c107e778a
commit
9bb39b3fdf
5
resiliency/03_deadline/README.md
Normal file
5
resiliency/03_deadline/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# deadline pattern
|
||||||
|
|
||||||
|
do a thing ,until the deadline time point
|
||||||
|
|
||||||
|
which act like time.after(), but time.after() only do once.
|
@ -1,3 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* @Description: https://github.com/crazybber
|
||||||
|
* @Author: Edward
|
||||||
|
* @Date: 2020-06-05 12:43:39
|
||||||
|
* @Last Modified by: Edward
|
||||||
|
* @Last Modified time: 2020-06-05 12:56:40
|
||||||
|
*/
|
||||||
|
|
||||||
// Package deadline implements the deadline (also known as "timeout") resiliency pattern for Go.
|
// Package deadline implements the deadline (also known as "timeout") resiliency pattern for Go.
|
||||||
package deadline
|
package deadline
|
||||||
|
|
||||||
@ -12,10 +20,11 @@ var ErrTimedOut = errors.New("timed out waiting for function to finish")
|
|||||||
// Deadline implements the deadline/timeout resiliency pattern.
|
// Deadline implements the deadline/timeout resiliency pattern.
|
||||||
type Deadline struct {
|
type Deadline struct {
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
|
action string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New constructs a new Deadline with the given timeout.
|
// New constructs a new Deadline with the given timeout.
|
||||||
func New(timeout time.Duration) *Deadline {
|
func New(timeout time.Duration, sometile string) *Deadline {
|
||||||
return &Deadline{
|
return &Deadline{
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
}
|
}
|
||||||
@ -29,6 +38,7 @@ func New(timeout time.Duration) *Deadline {
|
|||||||
// the return value of the function is returned from Run.
|
// the return value of the function is returned from Run.
|
||||||
func (d *Deadline) Run(work func(<-chan struct{}) error) error {
|
func (d *Deadline) Run(work func(<-chan struct{}) error) error {
|
||||||
result := make(chan error)
|
result := make(chan error)
|
||||||
|
|
||||||
stopper := make(chan struct{})
|
stopper := make(chan struct{})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -39,6 +49,8 @@ func (d *Deadline) Run(work func(<-chan struct{}) error) error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
//handle result
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case ret := <-result:
|
case ret := <-result:
|
||||||
return ret
|
return ret
|
||||||
|
@ -6,12 +6,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func takesFiveMillis(stopper <-chan struct{}) error {
|
func takes5ms(stopper <-chan struct{}) error {
|
||||||
time.Sleep(5 * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func takesTwentyMillis(stopper <-chan struct{}) error {
|
func takes20ms(stopper <-chan struct{}) error {
|
||||||
time.Sleep(20 * time.Millisecond)
|
time.Sleep(20 * time.Millisecond)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -20,14 +20,14 @@ func returnsError(stopper <-chan struct{}) error {
|
|||||||
return errors.New("foo")
|
return errors.New("foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeadline(t *testing.T) {
|
func TestMultiDeadline(t *testing.T) {
|
||||||
dl := New(10 * time.Millisecond)
|
dl := New(10*time.Millisecond, "test multi deadline case")
|
||||||
|
|
||||||
if err := dl.Run(takesFiveMillis); err != nil {
|
if err := dl.Run(takes5ms); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dl.Run(takesTwentyMillis); err != ErrTimedOut {
|
if err := dl.Run(takes20ms); err != ErrTimedOut {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,19 +47,19 @@ func TestDeadline(t *testing.T) {
|
|||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleDeadline() {
|
func TestDeadline(t *testing.T) {
|
||||||
dl := New(1 * time.Second)
|
dl := New(1*time.Second, "one dead line case")
|
||||||
|
|
||||||
err := dl.Run(func(stopper <-chan struct{}) error {
|
err := dl.Run(func(stopper <-chan struct{}) error {
|
||||||
// do something possibly slow
|
time.Sleep(time.Second * 10)
|
||||||
// check stopper function and give up if timed out
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case ErrTimedOut:
|
case ErrTimedOut:
|
||||||
// execution took too long, oops
|
t.Error("execution took too long, oops")
|
||||||
default:
|
default:
|
||||||
// some other error
|
// some other error
|
||||||
|
t.Log("done")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user