This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add go-daemon, add safe time.Ticker to prevent panic, which make atx-…
…agent live longer
- Loading branch information
1 parent
562392f
commit 058be8b
Showing
4 changed files
with
123 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// SafeTime add thread-safe for time.Timer | ||
type SafeTimer struct { | ||
*time.Timer | ||
mu sync.Mutex | ||
} | ||
|
||
func NewSafeTimer(d time.Duration) *SafeTimer { | ||
return &SafeTimer{ | ||
Timer: time.NewTimer(d), | ||
} | ||
} | ||
|
||
// Reset is thread-safe now | ||
func (t *SafeTimer) Reset(d time.Duration) bool { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
return t.Timer.Reset(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSafeTimer(tt *testing.T) { | ||
deadtime := time.Now().Add(2 * time.Second) | ||
t := NewSafeTimer(100 * time.Hour) | ||
wg := sync.WaitGroup{} | ||
wg.Add(8) | ||
for i := 0; i < 8; i++ { | ||
go func() { | ||
for { | ||
t.Reset(10 * time.Hour) | ||
if time.Now().After(deadtime) { | ||
break | ||
} | ||
} | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} | ||
|
||
// func TestMustPanic(tt *testing.T) { | ||
// defer func() { | ||
// if r := recover(); r == nil { | ||
// tt.Errorf("The code did not panic") | ||
// } | ||
// }() | ||
// deadtime := time.Now().Add(2 * time.Second) | ||
// t := time.NewTimer(100 * time.Hour) | ||
// wg := sync.WaitGroup{} | ||
// wg.Add(8) | ||
// for i := 0; i < 8; i++ { | ||
// go func() { | ||
// for { | ||
// t.Reset(10 * time.Hour) | ||
// if time.Now().After(deadtime) { | ||
// break | ||
// } | ||
// } | ||
// wg.Done() | ||
// }() | ||
// } | ||
// wg.Wait() | ||
// } |