ref
This commit is contained in:
1 parent
3c801a4c09
commit
7a22e3558e
3 files changed
+33
-24
No files matched your search
@@ -30,7 +30,7 @@ func ReceiveConfiguration(conf conf.Accounting) {
|
||||
newRequestsStoring := make(map[int32]bool)
|
||||
|
||||
if conf.Enable {
|
||||
if err := storage.Init(conf.Storing); err != nil {
|
||||
if err := InitStoringTask(conf.Storing); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
@@ -83,7 +83,9 @@ func Accept(appId int32, path string) bool {
|
||||
|
||||
func Close() {
|
||||
snapshot.Stop()
|
||||
storage.Stop()
|
||||
if storage != nil {
|
||||
storage.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func takeSnapshot() []entity.Snapshot {
|
||||
|
||||
+24
-16
@@ -10,14 +10,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var storage = &storingTask{
|
||||
mx: sync.Mutex{},
|
||||
counter: 0,
|
||||
process: false,
|
||||
chanTimeout: make(chan time.Time),
|
||||
chanCounter: make(chan []entity.Request),
|
||||
chanClose: make(chan bool),
|
||||
}
|
||||
var storage *storingTask
|
||||
|
||||
type storingTask struct {
|
||||
buffer []entity.Request
|
||||
@@ -29,20 +22,17 @@ type storingTask struct {
|
||||
chanClose chan bool
|
||||
|
||||
mx sync.Mutex
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func (u *storingTask) Init(setting conf.StoringSetting) error {
|
||||
u.mx.Lock()
|
||||
u.process = true
|
||||
u.buffer = make([]entity.Request, setting.Size)
|
||||
u.counter = 0
|
||||
u.mx.Unlock()
|
||||
|
||||
func InitStoringTask(setting conf.StoringSetting) error {
|
||||
timeout, err := time.ParseDuration(setting.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go u.run(timeout)
|
||||
|
||||
storage = newStoringTask(timeout, setting.Size)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -80,10 +70,12 @@ func (u *storingTask) Stop() {
|
||||
u.chanClose <- true
|
||||
u.process = false
|
||||
}
|
||||
u.wg.Wait()
|
||||
u.mx.Unlock()
|
||||
}
|
||||
|
||||
func (u *storingTask) run(timeout time.Duration) {
|
||||
defer u.wg.Done()
|
||||
u.chanTimeout = time.After(timeout)
|
||||
for {
|
||||
select {
|
||||
@@ -115,3 +107,19 @@ func (u *storingTask) unload(cache []entity.Request) {
|
||||
log.Error(log_code.ErrorUnloadAccounting, err)
|
||||
}
|
||||
}
|
||||
|
||||
func newStoringTask(timeout time.Duration, bufSize int) *storingTask {
|
||||
task := &storingTask{
|
||||
mx: sync.Mutex{},
|
||||
counter: 0,
|
||||
buffer: make([]entity.Request, bufSize),
|
||||
process: true,
|
||||
chanTimeout: make(chan time.Time),
|
||||
chanCounter: make(chan []entity.Request),
|
||||
chanClose: make(chan bool),
|
||||
}
|
||||
task.wg.Add(1)
|
||||
go task.run(timeout)
|
||||
|
||||
return task
|
||||
}
|
||||
@@ -30,8 +30,7 @@ var (
|
||||
func initStoring(setting conf.StoringSetting) (*requestsRepository, error) {
|
||||
repository := &requestsRepository{cache: make([]entity.Request, 0), wg: &sync.WaitGroup{}}
|
||||
model.RequestsRep = repository
|
||||
|
||||
err := storage.Init(setting)
|
||||
err := InitStoringTask(setting)
|
||||
return repository, err
|
||||
}
|
||||
|
||||
@@ -55,7 +54,7 @@ func TestStoringTask_Stop(t *testing.T) {
|
||||
a.NoError(err)
|
||||
|
||||
rep.wg.Add(2)
|
||||
for i, j := range []string{"1", "2", "3", "4", "5"} {
|
||||
for i, j := range []string{"Stop_1", "Stop_2", "Stop_3", "Stop_4", "Stop_5"} {
|
||||
storage.TakeRequest(int32(i), j, time.Now())
|
||||
}
|
||||
storage.Stop()
|
||||
@@ -68,7 +67,7 @@ func TestStoringTask_Buffer(t *testing.T) {
|
||||
a.NoError(err)
|
||||
|
||||
rep.wg.Add(1)
|
||||
for i, j := range []string{"1", "2", "3", "4", "5"} {
|
||||
for i, j := range []string{"Buffer_1", "Buffer_2", "Buffer_3", "Buffer_4", "Buffer_5"} {
|
||||
storage.TakeRequest(int32(i), j, time.Now())
|
||||
}
|
||||
a.True(wait(rep.wg))
|
||||
@@ -81,7 +80,7 @@ func TestStoringTask_Timeout(t *testing.T) {
|
||||
a.NoError(err)
|
||||
|
||||
rep.wg.Add(1)
|
||||
for i, j := range []string{"1", "2"} {
|
||||
for i, j := range []string{"Timeout_1", "Timeout_2"} {
|
||||
storage.TakeRequest(int32(i), j, time.Now())
|
||||
}
|
||||
a.True(wait(rep.wg))
|
||||
@@ -94,7 +93,7 @@ func TestStoringTask_Unload(t *testing.T) {
|
||||
a.NoError(err)
|
||||
|
||||
rep.wg.Add(2)
|
||||
for i, j := range []string{"1", "2", "3", "4", "5"} {
|
||||
for i, j := range []string{"Unload_1", "Unload_2", "Unload_3", "Unload_4", "Unload_5"} {
|
||||
storage.TakeRequest(int32(i), j, time.Now())
|
||||
}
|
||||
a.True(wait(rep.wg))
|
||||
|
||||
Reference in new issue
Block a user