refactor to fit the modified API request
diff --git a/api.go b/api.go
index 8da5978..b71bc82 100644
--- a/api.go
+++ b/api.go
@@ -2,6 +2,7 @@
import (
"encoding/json"
+ "fmt"
"github.com/30x/apid-core"
"github.com/30x/apidQuota/constants"
"github.com/30x/apidQuota/globalVariables"
@@ -43,12 +44,15 @@
return
}
+ fmt.Println("test1")
+
results, err := qBucket.IncrementQuotaLimit()
if err != nil {
util.WriteErrorResponse(http.StatusBadRequest, constants.ErrorCheckingQuotaLimit, "error retrieving count for the give identifier "+err.Error(), res, req)
return
}
+ fmt.Println("test2 : ", results)
respMap := results.ToAPIResponse()
respbytes, err := json.Marshal(respMap)
@@ -91,4 +95,4 @@
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(http.StatusOK)
res.Write(respbytes)
-}
\ No newline at end of file
+}
diff --git a/api_test.go b/api_test.go
index 6dfde56..226b3d3 100644
--- a/api_test.go
+++ b/api_test.go
@@ -3,12 +3,13 @@
import (
. "github.com/onsi/ginkgo"
//"net/http"
- "encoding/json"
- "net/http"
- "io/ioutil"
"bytes"
- "time"
+ "encoding/json"
+ "fmt"
"github.com/google/uuid"
+ "io/ioutil"
+ "net/http"
+ "time"
)
func init() {
@@ -29,11 +30,13 @@
requestData["id"] = "testID"
requestData["interval"] = 1
requestData["timeUnit"] = "HOUR"
- requestData["quotaType"] = "CALENDAR"
+ requestData["type"] = "CALENDAR"
requestData["preciseAtSecondsLevel"] = false
- requestData["startTime"] = time.Now().UTC().AddDate(0,0,1).Unix()
+ requestData["startTime"] = time.Now().UTC().AddDate(0, 0, 1).Unix()
requestData["maxCount"] = 5
- requestData["bucketType"] = "Synchronous"
+ requestData["weight"] = 2
+ requestData["distributed"] = true
+ requestData["synchronous"] = true
requestData["weight"] = 2
reqBytes, err := json.Marshal(requestData)
@@ -54,6 +57,14 @@
// Check the status code is what we expect.
if status := res.StatusCode; status != http.StatusOK {
+ respBodyBytes, err := ioutil.ReadAll(res.Body)
+ respBody := make(map[string]interface{})
+ err = json.Unmarshal(respBodyBytes, &respBody)
+ if err != nil {
+ fmt.Println("error: ", err)
+ }
+
+ fmt.Println(respBody)
Fail("wrong status code: " + res.Status)
}
@@ -92,12 +103,19 @@
// Check the status code is what we expect.
if status := res.StatusCode; status != http.StatusOK {
+ respBodyBytes, err := ioutil.ReadAll(res.Body)
+ respBody := make(map[string]interface{})
+ err = json.Unmarshal(respBodyBytes, &respBody)
+ if err != nil {
+ fmt.Println("error: ", err)
+ }
+
Fail("wrong status code: " + res.Status)
}
//TestCase3: quotaType = "RollingWidow"
requestData["quotaType"] = "RollingWindow"
- requestData["startTime"] = time.Now().UTC().AddDate(0,0,1).Unix()
+ requestData["startTime"] = time.Now().UTC().AddDate(0, 0, 1).Unix()
req, err = http.NewRequest("POST", testQuotaAPIURL, ioutil.NopCloser(bytes.NewReader(reqBytes)))
if err != nil {
Fail("error getting newRequest: " + err.Error())
@@ -113,12 +131,11 @@
Fail("wrong status code: " + res.Status)
}
-
})
It("test Synchronous quota - invalidation test cases", func() {
requestData := make(map[string]interface{})
- uuid,err := uuid.NewUUID()
+ uuid, err := uuid.NewUUID()
if err != nil {
Fail("error getting uuid")
}
@@ -129,7 +146,7 @@
requestData["timeUnit"] = "HOUR"
requestData["quotaType"] = "CALENDAR"
requestData["preciseAtSecondsLevel"] = false
- requestData["startTime"] = time.Now().UTC().AddDate(0,0,1).Unix()
+ requestData["startTime"] = time.Now().UTC().AddDate(0, 0, 1).Unix()
requestData["maxCount"] = 5
requestData["bucketType"] = "Synchronous"
requestData["weight"] = 2
@@ -154,6 +171,13 @@
// Check the status code is what we expect.
if status := res.StatusCode; status != http.StatusBadRequest {
+ respBodyBytes, err := ioutil.ReadAll(res.Body)
+ respBody := make(map[string]interface{})
+ err = json.Unmarshal(respBodyBytes, &respBody)
+ if err != nil {
+ fmt.Println("error: ", err)
+ }
+
Fail("wrong status code: " + res.Status)
}
diff --git a/constants/constants.go b/constants/constants.go
index 261e9cb..459efb2 100644
--- a/constants/constants.go
+++ b/constants/constants.go
@@ -1,6 +1,25 @@
package constants
const (
+
+ //add to acceptedTimeUnitList in init() if case any other new timeUnit is added
+ TimeUnitSECOND = "second"
+ TimeUnitMINUTE = "minute"
+ TimeUnitHOUR = "hour"
+ TimeUnitDAY = "day"
+ TimeUnitWEEK = "week"
+ TimeUnitMONTH = "month"
+
+ //errors
+ InvalidQuotaTimeUnitType = "invalidQuotaTimeUnitType"
+ InvalidQuotaBucketType = "invalidQuotaType"
+ InvalidQuotaType = "invalidQUotaType"
+ InvalidQuotaPeriod = "invalidQuotaPeriod"
+
+ QuotaTypeCalendar = "calendar" // after start time
+ QuotaTypeRollingWindow = "rollingwindow" // in the past "window" time
+
+ cacheKeyDelimiter = "|"
UnableToParseBody = "unable_to_parse_body"
UnMarshalJSONError = "unmarshal_json_error"
ErrorConvertReqBodyToEntity = "error_convert_reqBody_to_entity"
diff --git a/quotaBucket/apiUtil.go b/quotaBucket/apiUtil.go
index 30cffed..27d964d 100644
--- a/quotaBucket/apiUtil.go
+++ b/quotaBucket/apiUtil.go
@@ -23,10 +23,12 @@
}
func (qBucket *QuotaBucket) FromAPIRequest(quotaBucketMap map[string]interface{}) error {
- var edgeOrgID, id, timeUnit, quotaType, bucketType string
+ var edgeOrgID, id, timeUnit, quotaType string
var interval int
var startTime, maxCount, weight int64
- var preciseAtSecondsLevel bool
+ var preciseAtSecondsLevel, distributed bool
+ newQBucket := &QuotaBucket{}
+ var err error
value, ok := quotaBucketMap[reqEdgeOrgID]
if !ok {
@@ -67,13 +69,13 @@
}
timeUnit = value.(string)
- //Type {CALENDAR, FLEXI, ROLLING_WINDOW}
- value, ok = quotaBucketMap["quotaType"]
+ //QuotaType {CALENDAR, FLEXI, ROLLING_WINDOW}
+ value, ok = quotaBucketMap["type"]
if !ok {
- return errors.New(`missing field: 'quotaType' is required`)
+ return errors.New(`missing field: 'type' is required`)
}
if quotaTypeType := reflect.TypeOf(value); quotaTypeType.Kind() != reflect.String {
- return errors.New(`invalid type : 'quotaType' should be a string`)
+ return errors.New(`invalid type : 'type' should be a string`)
}
quotaType = value.(string)
@@ -86,16 +88,17 @@
}
preciseAtSecondsLevel = value.(bool)
- value, ok = quotaBucketMap["startTime"]
- if !ok { //todo: in the current cps code startTime is optional for QuotaBucket. should we make start time optional to NewQuotaBucket?
+ value, ok = quotaBucketMap["startTimestamp"]
+ if !ok { //todo: in the current cps code startTime is optional for QuotaBucket. should we make startTime optional to NewQuotaBucket?
startTime = time.Now().UTC().Unix()
+ } else {
+ // //from input when its read its float, need to then convert to int.
+ if startTimeType := reflect.TypeOf(value); startTimeType.Kind() != reflect.Float64 {
+ return errors.New(`invalid type : 'startTime' should be UNIX timestamp`)
+ }
+ startTimeFloat := value.(float64)
+ startTime = int64(startTimeFloat)
}
- //from input when its read its float, need to then convert to int.
- if startTimeType := reflect.TypeOf(value); startTimeType.Kind() != reflect.Float64 {
- return errors.New(`invalid type : 'startTime' should be UNIX timestamp`)
- }
- startTimeFloat := value.(float64)
- startTime = int64(startTimeFloat)
value, ok = quotaBucketMap[reqMaxCount]
if !ok {
@@ -108,15 +111,6 @@
maxCountFloat := value.(float64)
maxCount = int64(maxCountFloat)
- value, ok = quotaBucketMap["bucketType"]
- if !ok {
- return errors.New(`missing field: 'bucketType' is required`)
- }
- if bucketTypeType := reflect.TypeOf(value); bucketTypeType.Kind() != reflect.String {
- return errors.New(`invalid type : 'bucketType' should be a string`)
- }
- bucketType = value.(string)
-
value, ok = quotaBucketMap["weight"]
if !ok {
return errors.New(`missing field: 'weight' is required`)
@@ -128,7 +122,97 @@
weightFloat := value.(float64)
weight = int64(weightFloat)
- newQBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ value, ok = quotaBucketMap["distributed"]
+ if !ok {
+ return errors.New(`missing field: 'distributed' is required`)
+ }
+ if preciseAtSecondsLevelType := reflect.TypeOf(value); preciseAtSecondsLevelType.Kind() != reflect.Bool {
+ return errors.New(`invalid type : 'distributed' should be boolean`)
+ }
+ distributed = value.(bool)
+
+ //if distributed check for sync or async Quota
+ if distributed {
+ value, ok = quotaBucketMap["synchronous"]
+ if !ok {
+ return errors.New(`missing field: 'synchronous' is required`)
+ }
+ if synchronousType := reflect.TypeOf(value); synchronousType.Kind() != reflect.Bool {
+ return errors.New(`invalid type : 'synchronous' should be boolean`)
+ }
+ synchronous := value.(bool)
+
+ // for async retrieve syncTimeSec or syncMessageCount
+ if !synchronous {
+ syncTimeValue, syncTimeOK := quotaBucketMap["syncTimeInSec"]
+ syncMsgCountValue, syncMsgCountOK := quotaBucketMap["syncMessageCount"]
+
+ if syncTimeOK && syncMsgCountOK {
+ return errors.New(`either syncTimeInSec or syncMessageCount should be present but not both.`)
+ }
+
+ if !syncTimeOK && !syncMsgCountOK {
+ return errors.New(`either syncTimeInSec or syncMessageCount should be present. both cant be empty.`)
+ }
+
+ if syncTimeOK {
+ if syncTimeType := reflect.TypeOf(syncTimeValue); syncTimeType.Kind() != reflect.Float64 {
+ return errors.New(`invalid type : 'syncTimeInSec' should be a number`)
+ }
+ syncTimeFloat := value.(float64)
+ syncTimeInt := int64(syncTimeFloat)
+ newQBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel,
+ startTime, maxCount, weight, distributed, synchronous, syncTimeInt, -1)
+ if err != nil {
+ return errors.New("error creating quotaBucket: " + err.Error())
+ }
+ qBucket.quotaBucketData = newQBucket.quotaBucketData
+
+ if err := qBucket.Validate(); err != nil {
+ return errors.New("failed in Validating the quotaBucket: " + err.Error())
+ }
+
+ return nil
+
+ } else if syncMsgCountOK {
+ if syncMsgCountType := reflect.TypeOf(syncMsgCountValue); syncMsgCountType.Kind() != reflect.Float64 {
+ return errors.New(`invalid type : 'syncTimeInSec' should be a number`)
+ }
+ syncMsgCountFloat := value.(float64)
+ syncMsgCountInt := int64(syncMsgCountFloat)
+ newQBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel,
+ startTime, maxCount, weight, distributed, synchronous, -1, syncMsgCountInt)
+ if err != nil {
+ return errors.New("error creating quotaBucket: " + err.Error())
+ }
+ qBucket.quotaBucketData = newQBucket.quotaBucketData
+
+ if err := qBucket.Validate(); err != nil {
+ return errors.New("failed in Validating the quotaBucket: " + err.Error())
+ }
+
+ return nil
+ }
+ }
+
+ //for synchronous quotaBucket
+ newQBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel,
+ startTime, maxCount, weight, distributed, synchronous, -1, -1)
+ if err != nil {
+ return errors.New("error creating quotaBucket: " + err.Error())
+ }
+ qBucket.quotaBucketData = newQBucket.quotaBucketData
+
+ if err := qBucket.Validate(); err != nil {
+ return errors.New("failed in Validating the quotaBucket: " + err.Error())
+ }
+
+ return nil
+ }
+
+ //for non distributed quotaBucket
+ newQBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel,
+ startTime, maxCount, weight, distributed, false, -1, -1)
if err != nil {
return errors.New("error creating quotaBucket: " + err.Error())
diff --git a/quotaBucket/quotaBucket.go b/quotaBucket/quotaBucket.go
index a2dc84b..ca22202 100644
--- a/quotaBucket/quotaBucket.go
+++ b/quotaBucket/quotaBucket.go
@@ -2,44 +2,23 @@
import (
"errors"
+ "github.com/30x/apidQuota/constants"
"strings"
"time"
)
-const (
- //add to acceptedTimeUnitList in init() if case any other new timeUnit is added
- TimeUnitSECOND = "second"
- TimeUnitMINUTE = "minute"
- TimeUnitHOUR = "hour"
- TimeUnitDAY = "day"
- TimeUnitWEEK = "week"
- TimeUnitMONTH = "month"
-
- //add to acceptedBucketTypeList in init() if case any other new bucketType is added
- QuotaBucketTypeSynchronous = "synchronous"
- QuotaBucketTypeAsynchronous = "asynchronous"
- QuotaBucketTypeNonDistributed = "nonDistributed"
- //todo: Add other accepted bucketTypes
-
- //errors
- InvalidQuotaTimeUnitType = "invalidQuotaTimeUnitType"
- InvalidQuotaDescriptorType = "invalidQuotaTimeUnitType"
- InvalidQuotaBucketType = "invalidQuotaBucketType"
- InvalidQuotaPeriod = "invalidQuotaPeriod"
-)
-
var (
- acceptedTimeUnitList map[string]bool
- acceptedBucketTypeList map[string]bool
+ acceptedTimeUnitList map[string]bool
+ acceptedTypeList map[string]bool
)
func init() {
- acceptedTimeUnitList = map[string]bool{TimeUnitSECOND: true,
- TimeUnitMINUTE: true, TimeUnitHOUR: true,
- TimeUnitDAY: true, TimeUnitWEEK: true, TimeUnitMONTH: true}
- acceptedBucketTypeList = map[string]bool{QuotaBucketTypeSynchronous: true,
- QuotaBucketTypeAsynchronous: true, QuotaBucketTypeNonDistributed: true} //todo: add other accpeted bucketTypes
+ acceptedTimeUnitList = map[string]bool{constants.TimeUnitSECOND: true,
+ constants.TimeUnitMINUTE: true, constants.TimeUnitHOUR: true,
+ constants.TimeUnitDAY: true, constants.TimeUnitWEEK: true, constants.TimeUnitMONTH: true}
+ acceptedTypeList = map[string]bool{constants.QuotaTypeCalendar: true,
+ constants.QuotaTypeRollingWindow: true}
}
@@ -88,7 +67,7 @@
if qp.startTime.Before(qp.endTime) {
return true, nil
}
- return false, errors.New(InvalidQuotaPeriod + " : startTime in the period must be before endTime")
+ return false, errors.New(constants.InvalidQuotaPeriod + " : startTime in the period must be before endTime")
}
@@ -97,13 +76,16 @@
ID string
Interval int
TimeUnit string //TimeUnit {SECOND, MINUTE, HOUR, DAY, WEEK, MONTH}
- QuotaDescriptorType string //Type {CALENDAR, FLEXI, ROLLING_WINDOW}
+ QuotaType string //QuotaType {CALENDAR, FLEXI, ROLLING_WINDOW}
PreciseAtSecondsLevel bool
Period QuotaPeriod
StartTime time.Time
MaxCount int64
- BucketType string // SyncDistributed, AsyncDistributed, NonDistributed
Weight int64
+ Distributed bool
+ Synchronous bool
+ SyncTimeInSec int64
+ SyncMessageCount int64
}
type QuotaBucket struct {
@@ -112,7 +94,8 @@
func NewQuotaBucket(edgeOrgID string, id string, interval int,
timeUnit string, quotaType string, preciseAtSecondsLevel bool,
- startTime int64, maxCount int64, bucketType string, weight int64) (*QuotaBucket, error) {
+ startTime int64, maxCount int64, weight int64, distributed bool,
+ synchronous bool, syncTimeInSec int64, syncMessageCount int64) (*QuotaBucket, error) {
fromUNIXTime := time.Unix(startTime, 0)
@@ -121,12 +104,15 @@
ID: id,
Interval: interval,
TimeUnit: timeUnit,
- QuotaDescriptorType: quotaType,
+ QuotaType: quotaType,
PreciseAtSecondsLevel: preciseAtSecondsLevel,
StartTime: fromUNIXTime,
MaxCount: maxCount,
- BucketType: bucketType,
Weight: weight,
+ Distributed: distributed,
+ Synchronous: synchronous,
+ SyncTimeInSec: syncTimeInSec,
+ SyncMessageCount: syncMessageCount,
}
quotaBucket := &QuotaBucket{
@@ -145,19 +131,18 @@
//check valid quotaTimeUnit
if ok := IsValidTimeUnit(strings.ToLower(q.GetTimeUnit())); !ok {
- return errors.New(InvalidQuotaTimeUnitType)
+ return errors.New(constants.InvalidQuotaTimeUnitType)
}
- //check valid quotaBucketType
- if ok := IsValidQuotaBucketType(strings.ToLower(q.GetBucketType())); !ok {
- return errors.New(InvalidQuotaBucketType)
+ if ok := IsValidType(strings.ToLower(q.GetType())); !ok {
+ return errors.New(constants.InvalidQuotaBucketType)
}
-
//check if the period is valid
period, err := q.GetQuotaBucketPeriod()
if err != nil {
return err
}
+
if ok, err := period.Validate(); !ok {
return errors.New("invalid Period: " + err.Error())
}
@@ -185,8 +170,8 @@
return q.quotaBucketData.StartTime
}
-func (q *QuotaBucket) GetQuotaDescriptorType() string {
- return q.quotaBucketData.QuotaDescriptorType
+func (q *QuotaBucket) GetType() string {
+ return q.quotaBucketData.QuotaType
}
func (q *QuotaBucket) GetIsPreciseAtSecondsLevel() bool {
@@ -197,19 +182,22 @@
return q.quotaBucketData.MaxCount
}
-func (q *QuotaBucket) GetBucketType() string {
- return q.quotaBucketData.BucketType
-}
-
func (q *QuotaBucket) GetWeight() int64 {
return q.quotaBucketData.Weight
}
+func (q *QuotaBucket) IsDistrubuted() bool {
+ return q.quotaBucketData.Distributed
+}
+
+func (q *QuotaBucket) IsSynchronous() bool {
+ return q.quotaBucketData.Synchronous
+}
//Calls setCurrentPeriod if DescriptorType is 'rollingWindow' or period.endTime is before now().
// It is required to setPeriod while incrementing the count.
func (q *QuotaBucket) GetPeriod() (*QuotaPeriod, error) {
- if q.quotaBucketData.QuotaDescriptorType == QuotaTypeRollingWindow {
+ if q.quotaBucketData.QuotaType == constants.QuotaTypeRollingWindow {
qRWType := RollingWindowQuotaDescriptorType{}
err := qRWType.SetCurrentPeriod(q)
if err != nil {
@@ -234,7 +222,7 @@
//setCurrentPeriod only for rolling window else just return the value of QuotaPeriod.
func (q *QuotaBucket) GetQuotaBucketPeriod() (*QuotaPeriod, error) {
- if q.quotaBucketData.QuotaDescriptorType == QuotaTypeRollingWindow {
+ if q.quotaBucketData.QuotaType == constants.QuotaTypeRollingWindow {
qRWType := RollingWindowQuotaDescriptorType{}
err := qRWType.SetCurrentPeriod(q)
if err != nil {
@@ -260,7 +248,7 @@
func (q *QuotaBucket) setCurrentPeriod() error {
- qDescriptorType, err := GetQuotaDescriptorTypeHandler(q.GetQuotaDescriptorType())
+ qDescriptorType, err := GetQuotaTypeHandler(q.GetType())
if err != nil {
return err
}
@@ -269,8 +257,8 @@
}
func (period *QuotaPeriod) IsCurrentPeriod(qBucket *QuotaBucket) bool {
- if qBucket != nil && qBucket.GetBucketType() != "" {
- if qBucket.GetQuotaDescriptorType() == QuotaTypeRollingWindow {
+ if qBucket != nil && qBucket.GetType() != "" {
+ if qBucket.GetType() == constants.QuotaTypeRollingWindow {
return (period.inputStartTime.Equal(time.Now().UTC()) || period.inputStartTime.Before(time.Now().UTC()))
}
@@ -285,20 +273,22 @@
}
func (q *QuotaBucket) ResetQuotaLimit() (*QuotaBucketResults, error) {
- qBucketHandler, err := GetQuotaBucketHandler(q.BucketType)
+ bucketType, err := GetQuotaBucketHandler(q)
if err != nil {
- return nil, errors.New("error getting QuotaBucketType: " + err.Error())
+ return nil, errors.New("error getting quotaBucketHandler: " + err.Error())
}
- return qBucketHandler.resetQuotaForCurrentPeriod(q)
+
+ return bucketType.resetQuotaForCurrentPeriod(q)
}
func (q *QuotaBucket) IncrementQuotaLimit() (*QuotaBucketResults, error) {
- qBucketHandler, err := GetQuotaBucketHandler(q.BucketType)
+ qBucketHandler, err := GetQuotaBucketHandler(q)
if err != nil {
- return nil, errors.New("error getting QuotaBucketType: " + err.Error())
+ return nil, errors.New("error getting quotaBucketHandler: " + err.Error())
}
+
return qBucketHandler.incrementQuotaCount(q)
}
@@ -310,8 +300,8 @@
return false
}
-func IsValidQuotaBucketType(bucketType string) bool {
- if _, ok := acceptedBucketTypeList[bucketType]; ok {
+func IsValidType(qtype string) bool {
+ if _, ok := acceptedTypeList[qtype]; ok {
return true
}
return false
diff --git a/quotaBucket/quotaBucketType.go b/quotaBucket/quotaBucketType.go
index dd02914..b905bd7 100644
--- a/quotaBucket/quotaBucketType.go
+++ b/quotaBucket/quotaBucketType.go
@@ -2,9 +2,8 @@
import (
"errors"
- "strings"
- "github.com/30x/apidQuota/services"
"fmt"
+ "github.com/30x/apidQuota/services"
)
type QuotaBucketType interface {
@@ -42,7 +41,7 @@
}
-func (sQuotaBucket SynchronousQuotaBucketType) incrementQuotaCount(q *QuotaBucket) (*QuotaBucketResults, error){
+func (sQuotaBucket SynchronousQuotaBucketType) incrementQuotaCount(q *QuotaBucket) (*QuotaBucketResults, error) {
fmt.Println("increment count for sync")
maxCount := q.GetMaxCount()
@@ -98,10 +97,11 @@
return results, nil
}
-type AsynchronousQuotaBucketType struct{
- initialized bool
-
-
+type AsynchronousQuotaBucketType struct {
+ initialized bool
+ globalCount int64
+ syncMessageCount int64
+ syncTimeInSec int64
}
func (quotaBucketType AsynchronousQuotaBucketType) resetCount(qBucket *QuotaBucket) error {
@@ -110,11 +110,14 @@
}
func (quotaBucketType AsynchronousQuotaBucketType) incrementQuotaCount(qBucket *QuotaBucket) (*QuotaBucketResults, error) {
+ //getCount()
+ fmt.Println("increment count for async")
+
return nil, nil
}
func (quotaBucketType AsynchronousQuotaBucketType) resetQuotaForCurrentPeriod(q *QuotaBucket) (*QuotaBucketResults, error) {
- return nil,nil
+ return nil, nil
}
type NonDistributedQuotaBucketType struct{}
@@ -124,27 +127,29 @@
return nil
}
func (sQuotaBucket NonDistributedQuotaBucketType) incrementQuotaCount(qBucket *QuotaBucket) (*QuotaBucketResults, error) {
+ fmt.Println("increment count for nondistributed.")
+
return nil, nil
}
func (sQuotaBucket NonDistributedQuotaBucketType) resetQuotaForCurrentPeriod(q *QuotaBucket) (*QuotaBucketResults, error) {
- return nil,nil
+ return nil, nil
}
-func GetQuotaBucketHandler(qBucket string) (QuotaBucketType, error) {
- var quotaBucketType QuotaBucketType
- qBucketType := strings.ToLower(strings.TrimSpace(qBucket))
- switch qBucketType {
- case QuotaBucketTypeSynchronous:
- quotaBucketType = &SynchronousQuotaBucketType{}
+func GetQuotaBucketHandler(qBucket *QuotaBucket) (QuotaBucketType, error) {
+
+ if !qBucket.IsDistrubuted() {
+ quotaBucketType := &NonDistributedQuotaBucketType{}
return quotaBucketType, nil
- case QuotaBucketTypeAsynchronous:
- quotaBucketType = &AsynchronousQuotaBucketType{}
+ } else {
+ if qBucket.IsSynchronous() {
+ quotaBucketType := &SynchronousQuotaBucketType{}
+ return quotaBucketType, nil
+ }
+ quotaBucketType := &AsynchronousQuotaBucketType{}
return quotaBucketType, nil
- case QuotaBucketTypeNonDistributed:
- quotaBucketType = &NonDistributedQuotaBucketType{}
- return quotaBucketType, nil
- default:
- return nil, errors.New("Ignoring unrecognized quota type in request: " + qBucket)
}
+
+ return nil, errors.New("ignoring: unrecognized quota type")
+
}
diff --git a/quotaBucket/quotaBucket_test.go b/quotaBucket/quotaBucket_test.go
index aa1cebc..3a8fcfb 100644
--- a/quotaBucket/quotaBucket_test.go
+++ b/quotaBucket/quotaBucket_test.go
@@ -1,6 +1,7 @@
package quotaBucket_test
import (
+ "github.com/30x/apidQuota/constants"
. "github.com/30x/apidQuota/quotaBucket"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -82,44 +83,31 @@
})
})
-var _ = Describe("Test AcceptedQuotaBucketTypes", func() {
- It("testBucketTypes", func() {
- if !IsValidQuotaBucketType("synchronous") {
- Fail("Expected true: synchronous is a valid quotaBucket")
- }
- if !IsValidQuotaBucketType("asynchronous") {
- Fail("Expected true: asynchronous is a valid quotaBucket")
- }
- if !IsValidQuotaBucketType("nonDistributed") {
- Fail("Expected true: nonDistributed is a valid quotaBucket")
- }
-
- //invalid type
- if IsValidQuotaBucketType("invalidType") {
- Fail("Expected false: invalidType is a invalid quotaBucket")
- }
- })
-})
-
//Tests for QuotaBucket
var _ = Describe("QuotaBucket", func() {
It("Create with NewQuotaBucket", func() {
edgeOrgID := "sampleOrg"
id := "sampleID"
+ interval := 1
timeUnit := "hour"
quotaType := "calendar"
- bucketType := "synchronous"
- interval := 1
+ preciseAtSecondsLevel := true
maxCount := int64(10)
weight := int64(1)
- preciseAtSecondsLevel := true
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
+
//start time before now()
startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
now := time.Now().UTC()
- currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(),0,0,0, time.UTC)
+ currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC)
err = quotaBucket.Validate()
Expect(err).NotTo(HaveOccurred())
@@ -129,8 +117,7 @@
Expect(edgeOrgID).To(Equal(quotaBucket.GetEdgeOrgID()))
Expect(id).To(Equal(quotaBucket.GetID()))
Expect(timeUnit).To(Equal(quotaBucket.GetTimeUnit()))
- Expect(quotaType).To(Equal(quotaBucket.GetQuotaDescriptorType()))
- Expect(bucketType).To(Equal(quotaBucket.GetBucketType()))
+ Expect(quotaType).To(Equal(quotaBucket.GetType()))
Expect(interval).To(Equal(quotaBucket.GetInterval()))
Expect(maxCount).To(Equal(quotaBucket.GetMaxCount()))
Expect(preciseAtSecondsLevel).To(Equal(quotaBucket.GetIsPreciseAtSecondsLevel()))
@@ -142,14 +129,15 @@
Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String()))
Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String()))
-
//start time is after now() -> should still set period.
startTime = time.Now().UTC().AddDate(0, 1, 0).Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
now = time.Now().UTC()
- currentHour = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(),0,0,0, time.UTC)
+ currentHour = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC)
err = quotaBucket.Validate()
Expect(err).NotTo(HaveOccurred())
@@ -162,7 +150,6 @@
Expect(getPeriod.GetPeriodStartTime().String()).Should(Equal(currentHour.String()))
Expect(getPeriod.GetPeriodEndTime().String()).Should(Equal(currentHour.Add(time.Hour).String()))
-
})
//end before start
@@ -171,14 +158,19 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 1, 0), time.Now().UTC().AddDate(0, 0, -1))
@@ -186,8 +178,8 @@
if err == nil {
Fail("error expected but got <nil>")
}
- if !strings.Contains(err.Error(), InvalidQuotaPeriod) {
- Fail("expected: " + InvalidQuotaPeriod + " in the error but got: " + err.Error())
+ if !strings.Contains(err.Error(), constants.InvalidQuotaPeriod) {
+ Fail("expected: " + constants.InvalidQuotaPeriod + " in the error but got: " + err.Error())
}
})
@@ -197,17 +189,23 @@
id := "sampleID"
timeUnit := "invalidTimeUnit"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
+
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).To(HaveOccurred())
- if !strings.Contains(err.Error(), InvalidQuotaTimeUnitType) {
- Fail("expected: " + InvalidQuotaTimeUnitType + "but got: " + err.Error())
+ if !strings.Contains(err.Error(), constants.InvalidQuotaTimeUnitType) {
+ Fail("expected: " + constants.InvalidQuotaTimeUnitType + "but got: " + err.Error())
}
if quotaBucket != nil {
Fail("quotaBucket returned should be nil.")
@@ -215,30 +213,6 @@
})
- It("Test invalid quotaBucketType", func() {
- edgeOrgID := "sampleOrg"
- id := "sampleID"
- timeUnit := "hour"
- quotaType := "calendar"
- bucketType := "invalidQuotaBucket"
- interval := 1
- maxCount := int64(10)
- weight := int64(1)
- preciseAtSecondsLevel := true
- startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
-
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
- Expect(err).NotTo(HaveOccurred())
-
- err = quotaBucket.Validate()
- if err == nil {
- Fail("error expected but got <nil>")
- }
- if !strings.Contains(err.Error(), InvalidQuotaBucketType) {
- Fail("expected: " + InvalidQuotaBucketType + " in the error message but got: " + err.Error())
- }
- })
-
})
var _ = Describe("IsCurrentPeriod", func() {
@@ -248,15 +222,21 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "rollingwindow"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
+
//InputStart time is before now
startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
Expect(err).NotTo(HaveOccurred())
@@ -271,7 +251,9 @@
//InputStart time is now
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
Expect(err).NotTo(HaveOccurred())
@@ -292,15 +274,20 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "rollingwindow"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
//InputStart time is after now.
startTime := time.Now().UTC().AddDate(0, 1, 0).Unix()
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -315,7 +302,9 @@
//endTime before startTime in period
startTime = time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
quotaBucket.SetPeriod(time.Now().UTC(), time.Now().UTC().AddDate(0, -1, 0))
@@ -330,16 +319,21 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
//InputStart time is before now
startTime := time.Now().UTC().UTC().AddDate(-1, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -355,7 +349,9 @@
//InputStart time is now
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -372,7 +368,9 @@
//start Time in period is before now
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -391,7 +389,9 @@
//start Time in period is now
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -426,7 +426,9 @@
//end Time in period is after now
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -446,7 +448,9 @@
//start time in period is before end time
startTime = time.Now().UTC().Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -470,18 +474,23 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
//InputStart time is after now.
period := NewQuotaPeriod(time.Now().UTC().AddDate(0, 1, 0).Unix(),
time.Now().UTC().AddDate(0, 1, 0).Unix(), time.Now().AddDate(1, 0, 1).Unix())
startTime := time.Now().UTC().AddDate(0, 1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -493,7 +502,9 @@
//endTime is before start time
startTime = time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -528,14 +539,20 @@
id := "sampleID"
timeUnit := "hour"
quotaType := "rollingwindow"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
+
//InputStart time is before now
startTime := time.Now().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
quotaBucket.SetPeriod(time.Now().UTC().AddDate(0, 0, -1), time.Now().UTC().AddDate(0, 1, 0))
@@ -557,7 +574,9 @@
quotaType = "calendar"
pstartTime := time.Now().UTC().AddDate(0, -1, 0)
pendTime := time.Now().UTC().AddDate(0, 1, 0)
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
quotaBucket.SetPeriod(pstartTime, pendTime)
@@ -573,7 +592,9 @@
quotaType = "calendar"
pstartTime = time.Now().UTC().AddDate(0, -1, 0)
pendTime = time.Now().UTC().AddDate(0, -1, 0)
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
quotaBucket.SetPeriod(pstartTime, pendTime)
diff --git a/quotaBucket/quotaDescriptorType.go b/quotaBucket/quotaDescriptorType.go
index 8dec92c..af25b56 100644
--- a/quotaBucket/quotaDescriptorType.go
+++ b/quotaBucket/quotaDescriptorType.go
@@ -2,32 +2,27 @@
import (
"errors"
+ "github.com/30x/apidQuota/constants"
"strings"
"time"
)
-const (
- QuotaTypeCalendar = "calendar" // after start time
- QuotaTypeFlexi = "flexi" //after first request
- QuotaTypeRollingWindow = "rollingwindow" // in the past "window" time
-)
-
type QuotaDescriptorType interface {
SetCurrentPeriod(bucket *QuotaBucket) error
}
-func GetQuotaDescriptorTypeHandler(qType string) (QuotaDescriptorType, error) {
+func GetQuotaTypeHandler(qType string) (QuotaDescriptorType, error) {
var qDescriptor QuotaDescriptorType
quotaType := strings.ToLower(strings.TrimSpace(qType))
switch quotaType {
- case QuotaTypeCalendar:
+ case constants.QuotaTypeCalendar:
qDescriptor = &CalendarQuotaDescriptorType{}
return qDescriptor, nil
- case QuotaTypeRollingWindow:
+ case constants.QuotaTypeRollingWindow:
qDescriptor = &RollingWindowQuotaDescriptorType{}
return qDescriptor, nil
default:
- return nil, errors.New(InvalidQuotaDescriptorType + " Quota type " + qType + " in the request is not supported")
+ return nil, errors.New(constants.InvalidQuotaType + " Quota type: " + qType + " in the request is not supported")
}
}
@@ -49,9 +44,9 @@
if currentPeriod.IsCurrentPeriod(qbucket) {
return nil
} else {
- qBucketHandler, err := GetQuotaBucketHandler(qbucket.BucketType)
+ qBucketHandler, err := GetQuotaBucketHandler(qbucket)
if err != nil {
- return errors.New("error getting QuotaBucketType: " + err.Error())
+ return errors.New("error retrieving qBucketHandler: " + err.Error())
}
qBucketHandler.resetCount(qbucket)
}
@@ -62,27 +57,27 @@
now := time.Now().UTC()
timeUnit := strings.ToLower(strings.TrimSpace(qbucket.TimeUnit))
switch timeUnit {
- case TimeUnitSECOND:
+ case constants.TimeUnitSECOND:
currentStart = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 0, time.UTC)
secInDuration := time.Duration(int64(qbucket.Interval) * time.Second.Nanoseconds())
currentEnd = currentStart.Add(secInDuration)
break
- case TimeUnitMINUTE:
+ case constants.TimeUnitMINUTE:
currentStart = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.UTC)
minInDuration := time.Duration(int64(qbucket.Interval) * time.Minute.Nanoseconds())
currentEnd = currentStart.Add(minInDuration)
break
- case TimeUnitHOUR:
+ case constants.TimeUnitHOUR:
currentStart = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.UTC)
hoursInDuration := time.Duration(int64(qbucket.Interval) * time.Hour.Nanoseconds())
currentEnd = currentStart.Add(hoursInDuration)
break
- case TimeUnitDAY:
+ case constants.TimeUnitDAY:
currentStart = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
currentEnd = currentStart.AddDate(0, 0, 1*qbucket.Interval)
break
- case TimeUnitWEEK:
+ case constants.TimeUnitWEEK:
//todo
currentStart = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
for currentStart.Weekday() != time.Monday {
@@ -90,12 +85,12 @@
}
currentEnd = currentStart.AddDate(0, 0, 7*qbucket.Interval)
break
- case TimeUnitMONTH:
+ case constants.TimeUnitMONTH:
currentStart = time.Date(now.Year(), now.Month(), 0, 0, 0, 0, 0, time.UTC)
currentEnd = currentStart.AddDate(0, qbucket.Interval, 0)
break
default:
- return errors.New(InvalidQuotaTimeUnitType + " : ignoring unrecognized timeUnit : " + timeUnit)
+ return errors.New(constants.InvalidQuotaTimeUnitType + " : ignoring unrecognized timeUnit : " + timeUnit)
}
@@ -123,35 +118,35 @@
timeUnit := strings.ToLower(strings.TrimSpace(qb.TimeUnit))
switch timeUnit {
- case TimeUnitSECOND:
+ case constants.TimeUnitSECOND:
return time.Duration(int64(qb.Interval) * time.Second.Nanoseconds()), nil
- case TimeUnitMINUTE:
+ case constants.TimeUnitMINUTE:
return time.Duration(int64(qb.Interval) * time.Minute.Nanoseconds()), nil
- case TimeUnitHOUR:
+ case constants.TimeUnitHOUR:
return time.Duration(int64(qb.Interval) * time.Hour.Nanoseconds()), nil
- case TimeUnitDAY:
+ case constants.TimeUnitDAY:
return time.Duration(int64(qb.Interval*24) * time.Hour.Nanoseconds()), nil
- case TimeUnitWEEK:
+ case constants.TimeUnitWEEK:
return time.Duration(int64(qb.Interval*24*7) * time.Hour.Nanoseconds()), nil
- case TimeUnitMONTH:
+ case constants.TimeUnitMONTH:
now := time.Now().UTC()
var currentStart, currentEnd time.Time
- quotaType := strings.ToLower(strings.TrimSpace(qb.QuotaDescriptorType))
+ quotaType := strings.ToLower(strings.TrimSpace(qb.QuotaType))
switch quotaType {
- case QuotaTypeCalendar:
+ case constants.QuotaTypeCalendar:
currentStart = time.Date(now.Year(), now.Month(), 0, 0, 0, 0, 0, time.UTC)
currentEnd = currentStart.AddDate(0, qb.Interval, 0)
return currentEnd.Sub(currentStart), nil
- case QuotaTypeRollingWindow:
+ case constants.QuotaTypeRollingWindow:
currentEnd = now
currentStart = currentEnd.AddDate(0, (-1)*qb.Interval, 0)
return currentEnd.Sub(currentStart), nil
default:
- return time.Duration(0), errors.New(InvalidQuotaDescriptorType + " : ignoring unrecognized quotaType : " + quotaType)
+ return time.Duration(0), errors.New(constants.InvalidQuotaBucketType + " : ignoring unrecognized quotaType : " + quotaType)
}
default:
- return time.Duration(0), errors.New(InvalidQuotaTimeUnitType + " : ignoring unrecognized timeUnit : " + timeUnit)
+ return time.Duration(0), errors.New(constants.InvalidQuotaTimeUnitType + " : ignoring unrecognized timeUnit : " + timeUnit)
}
diff --git a/quotaBucket/quotaDescriptorType_test.go b/quotaBucket/quotaDescriptorType_test.go
index bceac12..ee85724 100644
--- a/quotaBucket/quotaDescriptorType_test.go
+++ b/quotaBucket/quotaDescriptorType_test.go
@@ -1,6 +1,7 @@
package quotaBucket_test
import (
+ "github.com/30x/apidQuota/constants"
. "github.com/30x/apidQuota/quotaBucket"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -11,7 +12,7 @@
var _ = Describe("Check Descriptor Type ", func() {
It("test Calendar Type descriptor", func() {
- descriptorType, err := GetQuotaDescriptorTypeHandler("calendar")
+ descriptorType, err := GetQuotaTypeHandler("calendar")
Expect(err).NotTo(HaveOccurred())
if reflect.TypeOf(descriptorType) != reflect.TypeOf(&CalendarQuotaDescriptorType{}) {
Fail("Excepted CalendarQuotaDescriptorType, but got: " + reflect.TypeOf(descriptorType).String())
@@ -19,7 +20,7 @@
})
It("test RollingWindow Type descriptor", func() {
- descriptorType, err := GetQuotaDescriptorTypeHandler("rollingwindow")
+ descriptorType, err := GetQuotaTypeHandler("rollingwindow")
Expect(err).NotTo(HaveOccurred())
if reflect.TypeOf(descriptorType) != reflect.TypeOf(&RollingWindowQuotaDescriptorType{}) {
Fail("Excepted RollingWindowQuotaDescriptorType, but got: " + reflect.TypeOf(descriptorType).String())
@@ -27,10 +28,10 @@
})
It("test invalid Type descriptor", func() {
- _, err := GetQuotaDescriptorTypeHandler("invalxidDescriptorType")
+ _, err := GetQuotaTypeHandler("invalidDescriptorType")
Expect(err).To(HaveOccurred())
- if !strings.Contains(err.Error(), InvalidQuotaDescriptorType) {
- Fail("Excepted error to contain: " + InvalidQuotaDescriptorType + " but got: " + err.Error())
+ if !strings.Contains(err.Error(), constants.InvalidQuotaType) {
+ Fail("Excepted error to contain: " + constants.InvalidQuotaType + " but got: " + err.Error())
}
})
})
@@ -43,15 +44,19 @@
id := "sampleID"
timeUnit := "second"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
-
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().UTC().AddDate(0, -1, 0).Unix()
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -72,7 +77,9 @@
// test set period for timeUnit=minute
timeUnit = "minute"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -93,7 +100,9 @@
// test set period for timeUnit=hour
timeUnit = "hour"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -114,7 +123,9 @@
// test set period for timeUnit=day
timeUnit = "day"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -135,7 +146,9 @@
// test set period for timeUnit=week
timeUnit = "week"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -156,7 +169,9 @@
// test set period for timeUnit=month
timeUnit = "month"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -185,14 +200,19 @@
id := "sampleID"
timeUnit := "second"
quotaType := "calendar"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().UTC().AddDate(0, -1, 0).Unix()
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -213,10 +233,12 @@
// test set period for timeUnit=month
timeUnit = "invalidTimeUnit"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).To(HaveOccurred())
- if ok := strings.Contains(err.Error(), InvalidQuotaTimeUnitType); !ok {
- Fail("expected error to contain " + InvalidQuotaTimeUnitType + " but got different error message: " + err.Error())
+ if ok := strings.Contains(err.Error(), constants.InvalidQuotaTimeUnitType); !ok {
+ Fail("expected error to contain " + constants.InvalidQuotaTimeUnitType + " but got different error message: " + err.Error())
}
})
@@ -228,14 +250,19 @@
id := "sampleID"
timeUnit := "second"
quotaType := "rollingWindow"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().UTC().AddDate(0, -1, 0).Unix()
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -256,7 +283,9 @@
// test set period for timeUnit=minute
timeUnit = "minute"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -277,7 +306,9 @@
// test set period for timeUnit=hour
timeUnit = "hour"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -298,7 +329,9 @@
// test set period for timeUnit=day
timeUnit = "day"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -319,7 +352,9 @@
// test set period for timeUnit=week
timeUnit = "week"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -340,7 +375,9 @@
// test set period for timeUnit=month
timeUnit = "month"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -369,14 +406,19 @@
id := "sampleID"
timeUnit := "second"
quotaType := "rollingwindow"
- bucketType := "synchronous"
interval := 1
maxCount := int64(10)
weight := int64(1)
preciseAtSecondsLevel := true
startTime := time.Now().UTC().UTC().AddDate(0, -1, 0).Unix()
+ distributed := true
+ synchronous := true
+ syncTimeInSec := int64(-1)
+ syncMessageCount := int64(-1)
- quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err := NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).NotTo(HaveOccurred())
err = quotaBucket.Validate()
@@ -397,10 +439,12 @@
// test set period for timeUnit=month
timeUnit = "invalidTimeUnit"
- quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit, quotaType, preciseAtSecondsLevel, startTime, maxCount, bucketType, weight)
+ quotaBucket, err = NewQuotaBucket(edgeOrgID, id, interval, timeUnit,
+ quotaType, preciseAtSecondsLevel, startTime, maxCount,
+ weight, distributed, synchronous, syncTimeInSec, syncMessageCount)
Expect(err).To(HaveOccurred())
- if ok := strings.Contains(err.Error(), InvalidQuotaTimeUnitType); !ok {
- Fail("expected error to contain " + InvalidQuotaTimeUnitType + " but got different error message: " + err.Error())
+ if ok := strings.Contains(err.Error(), constants.InvalidQuotaTimeUnitType); !ok {
+ Fail("expected error to contain " + constants.InvalidQuotaTimeUnitType + " but got different error message: " + err.Error())
}
})
diff --git a/services/counterServiceHelper.go b/services/counterServiceHelper.go
index d689fc8..a2c95aa 100644
--- a/services/counterServiceHelper.go
+++ b/services/counterServiceHelper.go
@@ -28,10 +28,9 @@
func GetCount(orgID string, quotaKey string, startTimeInt int64, endTimeInt int64) (int64, error) {
- return IncrementAndGetCount(orgID, quotaKey,0,startTimeInt,endTimeInt)
+ return IncrementAndGetCount(orgID, quotaKey, 0, startTimeInt, endTimeInt)
}
-
func IncrementAndGetCount(orgID string, quotaKey string, count int64, startTimeInt int64, endTimeInt int64) (int64, error) {
fmt.Println("calling counter service")
headers := http.Header{}
@@ -74,6 +73,7 @@
ContentLength: int64(contentLength),
}
+ fmt.Println("req: ", request)
resp, err := client.Do(request)
if err != nil {
@@ -82,10 +82,10 @@
globalVariables.Log.Debug("response: ", resp)
if resp.StatusCode != http.StatusOK {
- if resp.StatusCode == http.StatusNotFound {
- return 0, errors.New(err.Error())
- }
respBodyBytes, err := ioutil.ReadAll(resp.Body)
+ if resp.StatusCode == http.StatusNotFound {
+ return 0, errors.New("response from counter service: " + resp.Status + " and response body is: " + string(respBodyBytes))
+ }
if err != nil {
}