add unloading all requests

This commit is contained in:
DeniskaRediska committed 2019-11-19 19:02:17 +03:00
1 parent 8af97e56d7
commit 37c08f4500
13 files changed
+191 -6

No files matched your search

+7 -1
View File
@@ -25,9 +25,14 @@ type Accounting interface {
func ReceiveConfiguration(conf conf.Accounting) {
snapshot.Stop()
Unload.Stop()
newAccountingByApplicationId := make(map[int32]Accounting)
if conf.Enable {
if err := Unload.Init(conf.Unload); err != nil {
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
}
for _, s := range conf.Setting {
limitStates, patternArray, err := state.InitLimitState(s.Limits)
if err != nil {
@@ -59,13 +64,14 @@ func ReceiveConfiguration(conf conf.Accounting) {
func Close() {
snapshot.Stop()
Unload.Stop()
}
func GetAccounting(appId int32) Accounting {
return accountingStorage[appId]
}
func TakeSnapshot() []entity.Snapshot {
func takeSnapshot() []entity.Snapshot {
response := make([]entity.Snapshot, 0)
for appId, account := range accountingStorage {
response = append(response, entity.Snapshot{
+4
View File
@@ -26,6 +26,10 @@ var (
{Pattern: "mdm-master/group/method", MaxCount: 0, Timeout: "10s"},
}},
},
Unload: conf.UnloadSetting{
Count: 100,
Timeout: "1h",
},
}
reqExample = []struct {
+1 -1
View File
@@ -52,6 +52,6 @@ func (s *snapshotTask) Stop() {
}
func (s *snapshotTask) complete() error {
snapshotList := TakeSnapshot()
snapshotList := takeSnapshot()
return model.SnapshotRep.Update(snapshotList)
}
+110
View File
@@ -0,0 +1,110 @@
package accounting
import (
log "github.com/integration-system/isp-log"
"isp-gate-service/conf"
"isp-gate-service/entity"
"isp-gate-service/log_code"
"isp-gate-service/model"
"sync"
"time"
)
var Unload = &unloadingTask{
mx: sync.Mutex{},
counter: 0,
process: false,
chanTimeout: make(chan time.Time),
chanCounter: make(chan bool),
chanClose: make(chan bool),
}
type unloadingTask struct {
cache []entity.Unload
counter int
process bool
chanTimeout <-chan time.Time
chanCounter chan bool
chanClose chan bool
mx sync.Mutex
}
func (u *unloadingTask) Init(setting conf.UnloadSetting) error {
u.mx.Lock()
u.process = true
u.cache = make([]entity.Unload, setting.Count)
u.counter = 0
u.mx.Unlock()
timeout, err := time.ParseDuration(setting.Timeout)
if err != nil {
return err
}
go u.run(timeout)
return nil
}
func (u *unloadingTask) TakeRequest(appId int32, method string, date time.Time) {
if !u.process {
return
}
u.mx.Lock()
u.cache[u.counter] = entity.Unload{
AppId: appId,
Method: method,
CreatedAt: date,
}
u.counter++
if u.counter == len(u.cache) {
u.chanCounter <- true
}
u.mx.Unlock()
}
func (u *unloadingTask) Stop() {
u.mx.Lock()
if u.process {
defer u.unload()
u.chanClose <- true
u.process = false
}
u.mx.Unlock()
}
func (u *unloadingTask) run(timeout time.Duration) {
u.chanTimeout = time.After(timeout)
for {
select {
case <-u.chanClose:
return
case <-u.chanCounter:
u.unload()
u.chanTimeout = time.After(timeout)
case <-u.chanTimeout:
u.unload()
u.chanTimeout = time.After(timeout)
}
}
}
func (u *unloadingTask) unload() {
u.mx.Lock()
if u.counter == 0 {
u.mx.Unlock()
return
}
oldLen := len(u.cache)
if err := model.UnloadRep.Insert(u.cache[:u.counter]); err != nil {
log.Error(log_code.ErrorUnloadAccounting, err)
}
u.counter = 0
u.cache = make([]entity.Unload, oldLen)
u.mx.Unlock()
}