-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add background sync worker
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package bsync | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type LocalReader[T any] interface { | ||
Get() (T, time.Time) | ||
} | ||
|
||
// Worker support for background sync local state. | ||
type Worker[T any] struct { | ||
interval time.Duration | ||
stop chan struct{} | ||
fetchFunc func() (T, error) | ||
localValue T | ||
localTime time.Time | ||
lock sync.Mutex | ||
} | ||
|
||
func New[T any](updateInternal time.Duration, fetchFunc func() (T, error)) *Worker[T] { | ||
return &Worker[T]{ | ||
interval: updateInternal, | ||
fetchFunc: fetchFunc, | ||
stop: make(chan struct{}, 1), | ||
} | ||
} | ||
|
||
func (w *Worker[T]) setValue(v T) { | ||
w.lock.Lock() | ||
defer w.lock.Unlock() | ||
w.localValue = v | ||
w.localTime = time.Now() | ||
} | ||
|
||
func (w *Worker[T]) Get() (T, time.Time) { | ||
w.lock.Lock() | ||
defer w.lock.Unlock() | ||
return w.localValue, w.localTime | ||
} | ||
|
||
func (w *Worker[T]) Start() { | ||
ticker := time.NewTicker(w.interval) | ||
defer ticker.Stop() | ||
if v, err := w.fetchFunc(); err == nil { | ||
w.setValue(v) | ||
} | ||
for { | ||
select { | ||
case <-w.stop: | ||
break | ||
case <-ticker.C: | ||
v, err := w.fetchFunc() | ||
if err != nil { // user should handle error themself. | ||
continue | ||
} | ||
w.setValue(v) | ||
} | ||
} | ||
} | ||
|
||
func (w *Worker[T]) Stop() { | ||
w.stop <- struct{}{} | ||
} |
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,23 @@ | ||
package bsync_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/KyberNetwork/tradinglib/pkg/bsync" | ||
) | ||
|
||
func TestWorker(t *testing.T) { | ||
v := 0 | ||
w := bsync.New(time.Millisecond, func() (int, error) { | ||
v++ | ||
return v, nil | ||
}) | ||
go w.Start() | ||
for i := 0; i < 10; i++ { | ||
c, at := w.Get() | ||
t.Log(c, at) | ||
time.Sleep(time.Millisecond) | ||
} | ||
w.Stop() | ||
} |