fix(approve mutex)

This commit is contained in:
DeniskaRediska committed 2019-11-06 19:14:40 +03:00
1 parent 1a8ed4e8ea
commit 2a81e83b2e
5 files changed
+26 -28

No files matched your search

+21 -13
View File
@@ -6,39 +6,47 @@ import (
"isp-gate-service/approve/state"
"isp-gate-service/conf"
"isp-gate-service/log_code"
"sync"
)
var approvingByAppId = make(map[int64]approve)
var approvingByAppId map[int64]*approve
type approve struct {
mx sync.Mutex
matcher matcher.Matcher
limitStates map[string]state.LimitState
}
func ReceiveConfiguration(setting []conf.ApproveSetting) {
approvingByAppId = make(map[int64]*approve)
for _, s := range setting {
limitState, patternArray, err := state.InitLimitState(s.Limits)
if err != nil {
log.Fatal(log_code.FatalConfigApproveSetting, err)
}
approvingByAppId[s.ApplicationId] = approve{
approvingByAppId[s.ApplicationId] = &approve{
matcher: matcher.NewCacheableMatcher(patternArray),
limitStates: limitState,
mx: sync.Mutex{},
}
}
}
func Complete(appId int64, method string) bool {
approve, ok := approvingByAppId[appId]
if !ok {
return true
} else {
stateStorage := make([]state.LimitState, 0)
patternArray := approve.matcher.Match(method)
for _, pattern := range patternArray {
stateStorage = append(stateStorage, approve.limitStates[pattern])
}
return state.Update(stateStorage)
func GetApprove(appId int64) *approve {
return approvingByAppId[appId]
}
func (app *approve) Complete(method string) bool {
app.mx.Lock()
stateStorage := make([]state.LimitState, 0)
patternArray := app.matcher.Match(method)
for _, pattern := range patternArray {
stateStorage = append(stateStorage, app.limitStates[pattern])
}
resp := state.Update(stateStorage)
app.mx.Unlock()
return resp
}
+4 -4
View File
@@ -52,7 +52,7 @@ func TestApprove(t *testing.T) {
expected := true
req := reqExample[0]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, path))
a.Equal(expected, GetApprove(req.appId).Complete(path))
expected = !expected
time.Sleep(150 * time.Millisecond)
}
@@ -60,19 +60,19 @@ func TestApprove(t *testing.T) {
//expected == true
req = reqExample[1]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, path))
a.Equal(expected, GetApprove(req.appId).Complete(path))
expected = !expected
}
req = reqExample[2]
expectedArray := []bool{true, true, true, true, true, false}
for key, path := range req.path {
a.Equal(expectedArray[key], Complete(req.appId, path))
a.Equal(expectedArray[key], GetApprove(req.appId).Complete(path))
}
//expected == true
req = reqExample[3]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, path))
a.Equal(expected, GetApprove(req.appId).Complete(path))
}
}
-2
View File
@@ -2,7 +2,6 @@ package state
import (
"isp-gate-service/conf"
"sync"
"time"
)
@@ -37,7 +36,6 @@ func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string
datetime: make([]time.Time, limit.MaxCount),
pattern: limit.Pattern,
point: -1,
mx: sync.RWMutex{},
}
}
return limitStates, patternArray, nil
-8
View File
@@ -1,7 +1,6 @@
package state
import (
"sync"
"time"
)
@@ -10,7 +9,6 @@ type limiter struct {
datetime []time.Time
pattern string
point int
mx sync.RWMutex
}
func (lim *limiter) check() (bool, int, time.Time) {
@@ -21,7 +19,6 @@ func (lim *limiter) check() (bool, int, time.Time) {
return true, 0, time.Now()
}
lim.mx.RLock()
point := lim.point + 1
if point >= len(lim.datetime) {
point = 0
@@ -30,15 +27,10 @@ func (lim *limiter) check() (bool, int, time.Time) {
now := time.Now()
approve := now.Sub(date) > lim.lifetime
lim.mx.RUnlock()
return approve, point, now
}
func (lim *limiter) update(point int, time time.Time) {
lim.mx.Lock()
lim.point = point
lim.datetime[point] = time
lim.mx.Unlock()
}
+1 -1
View File
@@ -23,7 +23,7 @@ func Complete(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if !approve.Complete(applicationId, path) {
if approver := approve.GetApprove(applicationId); approver != nil && !approver.Complete(path) {
utils.SendError("approve error", codes.PermissionDenied, nil, ctx)
return
}