add(approve request)

This commit is contained in:
DeniskaRediska committed 2019-11-06 16:38:50 +03:00
1 parent e3c3b86b04
commit 1a8ed4e8ea
15 files changed
+446 -44

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
package approve
import (
log "github.com/integration-system/isp-log"
"isp-gate-service/approve/matcher"
"isp-gate-service/approve/state"
"isp-gate-service/conf"
"isp-gate-service/log_code"
)
var approvingByAppId = make(map[int64]approve)
type approve struct {
matcher matcher.Matcher
limitStates map[string]state.LimitState
}
func ReceiveConfiguration(setting []conf.ApproveSetting) {
for _, s := range setting {
limitState, patternArray, err := state.InitLimitState(s.Limits)
if err != nil {
log.Fatal(log_code.FatalConfigApproveSetting, err)
}
approvingByAppId[s.ApplicationId] = approve{
matcher: matcher.NewCacheableMatcher(patternArray),
limitStates: limitState,
}
}
}
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)
}
}
+78
View File
@@ -0,0 +1,78 @@
package approve
import (
"github.com/stretchr/testify/assert"
"isp-gate-service/conf"
"testing"
"time"
)
var (
approveSetting = []conf.ApproveSetting{
{ApplicationId: 1, Limits: []conf.LimitSetting{
{Pattern: "mdm-master/group/method", MaxCount: 1, Lifetime: "290ms"},
{Pattern: "mdm-master/group3/method", MaxCount: 10, Lifetime: "10s"},
{Pattern: "mdm-master/group3/*", MaxCount: 5, Lifetime: "10s"},
{Pattern: "mdm-master/group4/method", MaxCount: 2, Lifetime: "0s"},
}},
{ApplicationId: 2, Limits: []conf.LimitSetting{
{Pattern: "mdm-master/group/method", MaxCount: 0, Lifetime: "10s"},
}},
}
reqExample = []struct {
appId int64
path []string
}{
0: {appId: 1, path: []string{
"mdm-master/group/method", "mdm-master/group/method", "mdm-master/group/method",
"mdm-master/group/method", "mdm-master/group/method", "mdm-master/group/method",
}},
1: {appId: 2, path: []string{
"mdm-master/group3/method", "mdm-master/group/method", "mdm-master/group3/method",
"mdm-master/group/method", "mdm-master/group3/method", "mdm-master/group/method",
}},
2: {appId: 1, path: []string{
"mdm-master/group3/method", "mdm-master/group3/method", "mdm-master/group3/method",
"mdm-master/group3/method", "mdm-master/group3/method", "mdm-master/group3/method",
}},
3: {appId: 1, path: []string{
"mdm-master/group4/method", "mdm-master/group4/method", "mdm-master/group4/method",
"mdm-master/group4/method", "mdm-master/group4/method", "mdm-master/group4/method",
}},
}
)
func TestApprove(t *testing.T) {
a := assert.New(t)
ReceiveConfiguration(approveSetting)
expected := true
req := reqExample[0]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, path))
expected = !expected
time.Sleep(150 * time.Millisecond)
}
//expected == true
req = reqExample[1]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, 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))
}
//expected == true
req = reqExample[3]
for _, path := range req.path {
a.Equal(expected, Complete(req.appId, path))
}
}
+32
View File
@@ -0,0 +1,32 @@
package matcher
import "sync"
type cacheableMatcher struct {
cache map[string][]string
lock sync.RWMutex
rm runtimeMatcher
}
func (mm *cacheableMatcher) Match(method string) []string {
if len(mm.rm.patterns) == 0 {
return []string{}
}
mm.lock.RLock()
arrayPattern, ok := mm.cache[method]
mm.lock.RUnlock()
if ok {
return arrayPattern
}
mm.lock.Lock()
defer mm.lock.Unlock()
if arrayPattern, ok = mm.cache[method]; ok {
return arrayPattern
} else if arrayPattern = mm.rm.Match(method); len(arrayPattern) > 0 {
mm.cache[method] = arrayPattern
}
return arrayPattern
}
+18
View File
@@ -0,0 +1,18 @@
package matcher
type Matcher interface {
Match(method string) []string
}
func NewRuntimeMatcher(patterns []string) Matcher {
return &runtimeMatcher{
patterns: patterns,
}
}
func NewCacheableMatcher(patterns []string) Matcher {
return &cacheableMatcher{
cache: make(map[string][]string, 0),
rm: runtimeMatcher{patterns: patterns},
}
}
+51
View File
@@ -0,0 +1,51 @@
package matcher
import "testing"
var (
patterns = []string{
"mdm-master/group/method",
"mdm-master/group/*",
"mdm-master/group2/method",
"mdm-master/group3/*",
}
cases = []struct {
method string
length int
}{
{method: "mdm-master/group/method", length: 2},
{method: "mdm-master/group/method2", length: 1},
{method: "mdm-master/group2/method3", length: 0},
{method: "mdm-master/group2/method", length: 1},
{method: "mdm-master/group3/some", length: 1},
{method: "mdm-master/group3/", length: 1},
}
)
func TestCacheableMatcher_Match(t *testing.T) {
matcher := NewCacheableMatcher(patterns)
for _, c := range cases {
res := matcher.Match(c.method)
if c.length != len(res) {
t.Error(c)
}
}
}
func BenchmarkCacheableMatcher_Match(b *testing.B) {
matcher := NewCacheableMatcher(patterns)
for i := 0; i < b.N; i++ {
for _, c := range cases {
_ = matcher.Match(c.method)
}
}
}
func BenchmarkRuntimeMatcher_Match(b *testing.B) {
matcher := NewRuntimeMatcher(patterns)
for i := 0; i < b.N; i++ {
for _, c := range cases {
_ = matcher.Match(c.method)
}
}
}
+17
View File
@@ -0,0 +1,17 @@
package matcher
import "path"
type runtimeMatcher struct {
patterns []string
}
func (mm runtimeMatcher) Match(method string) []string {
resp := make([]string, 0)
for _, pattern := range mm.patterns {
if matched, _ := path.Match(pattern, method); matched {
resp = append(resp, pattern)
}
}
return resp
}
+60
View File
@@ -0,0 +1,60 @@
package state
import (
"isp-gate-service/conf"
"sync"
"time"
)
type (
LimitState interface {
check() (bool, int, time.Time)
update(int, time.Time)
}
updateRequest struct {
pointer int
datetime time.Time
}
)
func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string, error) {
limitStates := make(map[string]LimitState)
patternArray := make([]string, len(limits))
for i, limit := range limits {
lifetime, err := time.ParseDuration(limit.Lifetime)
if err != nil {
return nil, nil, err
}
if lifetime == 0 && limit.MaxCount != 0 {
limit.MaxCount = 1
}
patternArray[i] = limit.Pattern
limitStates[limit.Pattern] = &limiter{
lifetime: lifetime,
datetime: make([]time.Time, limit.MaxCount),
pattern: limit.Pattern,
point: -1,
mx: sync.RWMutex{},
}
}
return limitStates, patternArray, nil
}
func Update(states []LimitState) bool {
update := make([]updateRequest, len(states))
for i, st := range states {
if ok, point, datetime := st.check(); ok {
update[i] = updateRequest{pointer: point, datetime: datetime}
} else {
return false
}
}
for i, st := range states {
u := update[i]
st.update(u.pointer, u.datetime)
}
return true
}
+44
View File
@@ -0,0 +1,44 @@
package state
import (
"sync"
"time"
)
type limiter struct {
lifetime time.Duration
datetime []time.Time
pattern string
point int
mx sync.RWMutex
}
func (lim *limiter) check() (bool, int, time.Time) {
if len(lim.datetime) == 0 {
return false, 0, time.Time{}
}
if lim.lifetime == 0 {
return true, 0, time.Now()
}
lim.mx.RLock()
point := lim.point + 1
if point >= len(lim.datetime) {
point = 0
}
date := lim.datetime[point]
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()
}