add recovery limit state from storage
This commit is contained in:
1 parent
4d81fe7a26
commit
8af97e56d7
7 files changed
+114
-106
No files matched your search
@@ -12,7 +12,7 @@ type accountant struct {
|
||||
limitStates map[string]state.LimitState
|
||||
}
|
||||
|
||||
func (app *accountant) Check(method string) bool {
|
||||
func (app *accountant) Accept(method string) bool {
|
||||
patternArray := app.matcher.Match(method)
|
||||
stateStorage := make([]state.LimitState, len(patternArray))
|
||||
for i, pattern := range patternArray {
|
||||
@@ -25,7 +25,12 @@ func (app *accountant) Check(method string) bool {
|
||||
return resp
|
||||
}
|
||||
|
||||
func (app *accountant) getLimitState() map[string]state.LimitState {
|
||||
limitStates := app.limitStates
|
||||
return limitStates
|
||||
func (app *accountant) Snapshot() map[string]state.Snapshot {
|
||||
app.mx.Lock()
|
||||
snapshotLimitState := make(map[string]state.Snapshot)
|
||||
for method, limitState := range app.limitStates {
|
||||
snapshotLimitState[method] = limitState.Export()
|
||||
}
|
||||
app.mx.Unlock()
|
||||
return snapshotLimitState
|
||||
}
|
||||
+51
-15
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/integration-system/isp-log/stdcodes"
|
||||
"isp-gate-service/accounting/state"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/model"
|
||||
"isp-gate-service/service/matcher"
|
||||
@@ -12,17 +13,20 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var accountingByApplicationId = make(map[int32]Accounting)
|
||||
var (
|
||||
accountingStorage = make(map[int32]Accounting)
|
||||
accountingWorking = false
|
||||
)
|
||||
|
||||
type Accounting interface {
|
||||
Check(string) bool
|
||||
getLimitState() map[string]state.LimitState
|
||||
Accept(string) bool
|
||||
Snapshot() map[string]state.Snapshot
|
||||
}
|
||||
|
||||
func ReceiveConfiguration(conf conf.Accounting) {
|
||||
snapshot.Stop()
|
||||
|
||||
newAccountingByApplicationId := make(map[int32]Accounting)
|
||||
|
||||
if conf.Enable {
|
||||
for _, s := range conf.Setting {
|
||||
limitStates, patternArray, err := state.InitLimitState(s.Limits)
|
||||
@@ -30,15 +34,7 @@ func ReceiveConfiguration(conf conf.Accounting) {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
}
|
||||
|
||||
if snapshot, err := model.SnapshotRep.GetByApplication(s.ApplicationId); err != nil {
|
||||
log.Warn(log_code.ErrorSnapshotAccounting, err)
|
||||
} else if snapshot != nil {
|
||||
for method, limitState := range limitStates {
|
||||
if value, ok := snapshot.LimitState[method]; ok {
|
||||
limitState.Import(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
recoveryLimitState(s.ApplicationId, limitStates)
|
||||
|
||||
newAccountingByApplicationId[s.ApplicationId] = &accountant{
|
||||
matcher: matcher.NewCacheableMatcher(patternArray),
|
||||
@@ -46,13 +42,19 @@ func ReceiveConfiguration(conf conf.Accounting) {
|
||||
mx: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
if snapshotTimeout, err := time.ParseDuration(conf.SnapshotTimeout); err != nil {
|
||||
log.Fatal(stdcodes.ModuleInvalidRemoteConfig, err)
|
||||
} else {
|
||||
go snapshot.Start(snapshotTimeout)
|
||||
}
|
||||
|
||||
accountingWorking = true
|
||||
} else {
|
||||
accountingWorking = false
|
||||
}
|
||||
accountingByApplicationId = newAccountingByApplicationId
|
||||
|
||||
accountingStorage = newAccountingByApplicationId
|
||||
}
|
||||
|
||||
func Close() {
|
||||
@@ -60,5 +62,39 @@ func Close() {
|
||||
}
|
||||
|
||||
func GetAccounting(appId int32) Accounting {
|
||||
return accountingByApplicationId[appId]
|
||||
return accountingStorage[appId]
|
||||
}
|
||||
|
||||
func TakeSnapshot() []entity.Snapshot {
|
||||
response := make([]entity.Snapshot, 0)
|
||||
for appId, account := range accountingStorage {
|
||||
response = append(response, entity.Snapshot{
|
||||
AppId: appId,
|
||||
LimitState: account.Snapshot(),
|
||||
})
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
func recoveryLimitState(appId int32, limitStates map[string]state.LimitState) {
|
||||
if accountingWorking {
|
||||
if acc, ok := accountingStorage[appId]; ok {
|
||||
snapshot := acc.Snapshot()
|
||||
importLimitState(limitStates, snapshot)
|
||||
}
|
||||
} else {
|
||||
if snapshot, err := model.SnapshotRep.GetByApplication(appId); err != nil {
|
||||
log.Warn(log_code.ErrorSnapshotAccounting, err)
|
||||
} else if snapshot != nil {
|
||||
importLimitState(limitStates, snapshot.LimitState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func importLimitState(limitStates map[string]state.LimitState, snapshot map[string]state.Snapshot) {
|
||||
for method, limitState := range limitStates {
|
||||
if oldState, ok := snapshot[method]; ok {
|
||||
limitState.Import(oldState)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,10 +61,15 @@ func TestAccounting(t *testing.T) {
|
||||
model.SnapshotRep = &snapshotRepository{cache: make(map[int32]map[string]state.Snapshot)}
|
||||
ReceiveConfiguration(accountingSetting)
|
||||
|
||||
expected := true
|
||||
req := reqExample[0]
|
||||
req := reqExample[4]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, GetAccounting(req.appId).Check(path))
|
||||
a.True(GetAccounting(req.appId).Accept(path))
|
||||
}
|
||||
|
||||
expected := true
|
||||
req = reqExample[0]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, GetAccounting(req.appId).Accept(path))
|
||||
expected = !expected
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
@@ -72,31 +77,27 @@ func TestAccounting(t *testing.T) {
|
||||
//expected == true
|
||||
req = reqExample[1]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, GetAccounting(req.appId).Check(path))
|
||||
a.Equal(expected, GetAccounting(req.appId).Accept(path))
|
||||
expected = !expected
|
||||
}
|
||||
|
||||
req = reqExample[2]
|
||||
expectedArray := []bool{true, true, true, true, true, false}
|
||||
for key, path := range req.path {
|
||||
a.Equal(expectedArray[key], GetAccounting(req.appId).Check(path))
|
||||
a.Equal(expectedArray[key], GetAccounting(req.appId).Accept(path))
|
||||
}
|
||||
|
||||
//expected == true
|
||||
req = reqExample[3]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, GetAccounting(req.appId).Check(path))
|
||||
}
|
||||
|
||||
req = reqExample[4]
|
||||
for _, path := range req.path {
|
||||
a.True(GetAccounting(req.appId).Check(path))
|
||||
a.Equal(expected, GetAccounting(req.appId).Accept(path))
|
||||
}
|
||||
|
||||
ReceiveConfiguration(accountingSetting)
|
||||
|
||||
req = reqExample[4]
|
||||
for _, path := range req.path {
|
||||
a.False(GetAccounting(req.appId).Check(path))
|
||||
a.False(GetAccounting(req.appId).Accept(path))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -122,14 +123,3 @@ func (r *snapshotRepository) Update(list []entity.Snapshot) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *snapshotRepository) GetAll() ([]entity.Snapshot, error) {
|
||||
resp := make([]entity.Snapshot, 0)
|
||||
for appId, limitState := range r.cache {
|
||||
resp = append(resp, entity.Snapshot{
|
||||
AppId: appId,
|
||||
LimitState: limitState,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
+14
-27
@@ -2,69 +2,56 @@ package accounting
|
||||
|
||||
import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-gate-service/accounting/state"
|
||||
"isp-gate-service/entity"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/model"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var snapshot = &snapshotStruct{
|
||||
var snapshot = &snapshotTask{
|
||||
process: false,
|
||||
timeout: make(chan time.Time),
|
||||
close: make(chan bool),
|
||||
}
|
||||
|
||||
type snapshotStruct struct {
|
||||
wg sync.WaitGroup
|
||||
type snapshotTask struct {
|
||||
mx sync.Mutex
|
||||
process bool
|
||||
timeout <-chan time.Time
|
||||
close chan bool
|
||||
}
|
||||
|
||||
func (s *snapshotStruct) Start(timeout time.Duration) {
|
||||
func (s *snapshotTask) Start(timeout time.Duration) {
|
||||
s.Stop()
|
||||
|
||||
s.mx.Lock()
|
||||
s.process = true
|
||||
s.timeout = time.After(timeout)
|
||||
s.mx.Unlock()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-s.close:
|
||||
s.wg.Wait()
|
||||
return
|
||||
case <-s.timeout:
|
||||
s.wg.Add(1)
|
||||
applicationAccounting := accountingByApplicationId
|
||||
if err := s.complete(applicationAccounting); err != nil {
|
||||
if err := s.complete(); err != nil {
|
||||
log.Error(log_code.ErrorSnapshotAccounting, err)
|
||||
}
|
||||
s.timeout = time.After(timeout)
|
||||
s.wg.Done()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *snapshotStruct) Stop() {
|
||||
func (s *snapshotTask) Stop() {
|
||||
s.mx.Lock()
|
||||
if s.process {
|
||||
s.process = false
|
||||
s.close <- true
|
||||
}
|
||||
s.wg.Wait()
|
||||
s.mx.Unlock()
|
||||
}
|
||||
|
||||
func (s *snapshotStruct) complete(applicationAccounting map[int32]Accounting) error {
|
||||
snapshot := make([]entity.Snapshot, 0)
|
||||
for application, account := range applicationAccounting {
|
||||
snapshotLimitState := make(map[string]state.Snapshot)
|
||||
limitStates := account.getLimitState()
|
||||
for method, limitState := range limitStates {
|
||||
snapshotLimitState[method] = limitState.Export()
|
||||
}
|
||||
snapshot = append(snapshot, entity.Snapshot{
|
||||
AppId: application,
|
||||
LimitState: snapshotLimitState,
|
||||
})
|
||||
}
|
||||
return model.SnapshotRep.Update(snapshot)
|
||||
func (s *snapshotTask) complete() error {
|
||||
snapshotList := TakeSnapshot()
|
||||
return model.SnapshotRep.Update(snapshotList)
|
||||
}
|
||||
+25
-24
@@ -21,44 +21,45 @@ func (lim *limiter) Export() Snapshot {
|
||||
}
|
||||
}
|
||||
|
||||
func (lim *limiter) Import(data Snapshot) {
|
||||
importLim := limiter{
|
||||
timeout: data.Timeout,
|
||||
datetime: data.Datetime,
|
||||
pattern: data.Pattern,
|
||||
pointer: data.Pointer,
|
||||
func (lim *limiter) Import(oldState Snapshot) {
|
||||
oldLim := limiter{
|
||||
timeout: oldState.Timeout,
|
||||
datetime: oldState.Datetime,
|
||||
pattern: oldState.Pattern,
|
||||
pointer: oldState.Pointer,
|
||||
}
|
||||
|
||||
lenImportDatetime := len(importLim.datetime)
|
||||
lenDatetime := len(lim.datetime)
|
||||
switch true {
|
||||
case lenImportDatetime == lenDatetime:
|
||||
lim.datetime = importLim.datetime
|
||||
lim.pointer = importLim.pointer
|
||||
lenOldDatetime := len(oldLim.datetime)
|
||||
lenNewDatetime := len(lim.datetime)
|
||||
|
||||
case lenImportDatetime < lenDatetime:
|
||||
oldPointer := importLim.pointer
|
||||
switch true {
|
||||
case lenOldDatetime == lenNewDatetime:
|
||||
lim.datetime = oldLim.datetime
|
||||
lim.pointer = oldLim.pointer
|
||||
|
||||
case lenOldDatetime < lenNewDatetime:
|
||||
oldPointer := oldLim.pointer
|
||||
for i := range lim.datetime {
|
||||
importLim.pointer++
|
||||
if importLim.pointer >= len(importLim.datetime) {
|
||||
importLim.pointer = 0
|
||||
oldLim.pointer++
|
||||
if oldLim.pointer >= len(oldLim.datetime) {
|
||||
oldLim.pointer = 0
|
||||
}
|
||||
|
||||
lim.datetime[i] = importLim.datetime[importLim.pointer]
|
||||
lim.datetime[i] = oldLim.datetime[oldLim.pointer]
|
||||
|
||||
if oldPointer == importLim.pointer {
|
||||
if oldPointer == oldLim.pointer {
|
||||
lim.pointer = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
case lenImportDatetime > lenDatetime:
|
||||
case lenOldDatetime > lenNewDatetime:
|
||||
for i := range lim.datetime {
|
||||
importLim.pointer++
|
||||
if importLim.pointer >= len(importLim.datetime) {
|
||||
importLim.pointer = 0
|
||||
oldLim.pointer++
|
||||
if oldLim.pointer >= len(oldLim.datetime) {
|
||||
oldLim.pointer = 0
|
||||
}
|
||||
lim.datetime[i] = importLim.datetime[importLim.pointer]
|
||||
lim.datetime[i] = oldLim.datetime[oldLim.pointer]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ func (handlerHelper) AuthenticateAccountingProxy(ctx *fasthttp.RequestCtx) domai
|
||||
return domain.Create().SetError(err)
|
||||
}
|
||||
|
||||
if account := accounting.GetAccounting(applicationId); account != nil && !account.Check(path) {
|
||||
if account := accounting.GetAccounting(applicationId); account != nil && !account.Accept(path) {
|
||||
err := errors.New("accounting error")
|
||||
utils.WriteError(ctx, "forbidden", codes.ResourceExhausted, nil)
|
||||
return domain.Create().SetError(err)
|
||||
|
||||
+1
-12
@@ -11,7 +11,6 @@ import (
|
||||
type (
|
||||
SnapshotRepository interface {
|
||||
GetByApplication(int32) (*entity.Snapshot, error)
|
||||
GetAll() ([]entity.Snapshot, error)
|
||||
Update([]entity.Snapshot) error
|
||||
}
|
||||
|
||||
@@ -22,20 +21,10 @@ type (
|
||||
)
|
||||
|
||||
func (r snapshotRepository) Update(list []entity.Snapshot) error {
|
||||
_, _ = r.getDb().Model(&list).WherePK().Delete()
|
||||
_, err := r.getDb().Model(&list).Insert()
|
||||
_, err := r.getDb().Model(&list).OnConflict("(app_id) DO UPDATE").Insert()
|
||||
return err
|
||||
}
|
||||
|
||||
func (r snapshotRepository) GetAll() ([]entity.Snapshot, error) {
|
||||
model := make([]entity.Snapshot, 0)
|
||||
if err := r.getDb().Model(&model).Returning("*").Select(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return model, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r snapshotRepository) GetByApplication(appId int32) (*entity.Snapshot, error) {
|
||||
model := new(entity.Snapshot)
|
||||
if err := r.getDb().Model(model).Where("app_id = ?", appId).Select(); err != nil {
|
||||
|
||||
Reference in new issue
Block a user