fix: fixed the MRPS.

This commit is contained in:
Andrey Parhomenko 2024-05-31 22:19:41 +05:00
parent d7e751677f
commit 546600a027
4 changed files with 37 additions and 14 deletions

View file

@ -40,7 +40,7 @@ type Client struct {
mrps int mrps int
ticker *time.Ticker ticker *time.Ticker
req chan struct{} req, reqNeed chan struct{}
requestsMade int64 requestsMade int64
} }
@ -125,16 +125,16 @@ func NewAPI(secretPath string) (*Client, error) {
func (client *Client) SetMRPS(rps int) *Client { func (client *Client) SetMRPS(rps int) *Client {
client.mrps = rps client.mrps = rps
client.req = make(chan struct{}) client.req = make(chan struct{})
client.ticker = time.NewTicker(time.Second) client.reqNeed = make(chan struct{})
client.ticker = time.NewTicker(
time.Second/time.Duration(rps),
)
go func() { go func() {
for { for {
for _ = range make( select {
[]struct{}, case <- client.reqNeed :
client.mrps - client.availableRequests,
) {
client.req <- struct{}{} client.req <- struct{}{}
} }
client.availableRequests = client.mrps
<-client.ticker.C <-client.ticker.C
} }
}() }()
@ -147,8 +147,8 @@ func (client *Client) waitInQueue() bool {
return false return false
} }
client.reqNeed <- struct{}{}
<- client.req <- client.req
client.availableRequests--
return true return true
} }

View file

@ -43,6 +43,10 @@ var getLead = mtool.T("get-leads").Func(func(flags *mtool.Flags){
"id", "id",
ids, ids,
}, },
urlenc.Value[string]{
"with",
"contacts",
},
) )
if err != nil { if err != nil {
log.Fatalf("GetLeadsByIDs(...): %s\n", err) log.Fatalf("GetLeadsByIDs(...): %s\n", err)

View file

@ -1,6 +1,10 @@
package common package common
type Values struct { import (
"regexp"
)
type Value struct {
Value any `json:"value,omitempty"` Value any `json:"value,omitempty"`
EnumID int `json:"enum_id,omitempty"` EnumID int `json:"enum_id,omitempty"`
Enum string `json:"enum,omitempty"` Enum string `json:"enum,omitempty"`
@ -10,19 +14,32 @@ type CustomFieldsValue struct {
FieldName string `json:"field_name,omitempty"` FieldName string `json:"field_name,omitempty"`
FieldCode string `json:"field_code,omitempty"` FieldCode string `json:"field_code,omitempty"`
FieldType string `json:"field_type,omitempty"` FieldType string `json:"field_type,omitempty"`
Values []Values `json:"values"` Values []Value `json:"values"`
} }
type CustomFieldsValues []CustomFieldsValue type CustomFieldsValues []CustomFieldsValue
func (vs CustomFieldsValues) GetValuesByID( func (vs CustomFieldsValues) GetByID(
id int, id int,
) ([]Values, bool) { ) (CustomFieldsValue, bool) {
for _, v := range vs { for _, v := range vs {
if v.FieldID == id { if v.FieldID == id {
return v.Values, true return v, true
} }
} }
return nil, false return CustomFieldsValue{}, false
} }
func (fields CustomFieldsValues) FilterByNameRegex(
re *regexp.Regexp,
) CustomFieldsValues {
ret := CustomFieldsValues{}
for _, field := range fields {
if re.MatchString(field.FieldName) {
ret = append(ret, field)
}
}
return ret
}

View file

@ -5,6 +5,7 @@ import "surdeus.su/core/ss/urlenc"
import "errors" import "errors"
import "surdeus.su/core/amo/api" import "surdeus.su/core/amo/api"
import "fmt" import "fmt"
//import "log"
func (client *Client) GetContact( func (client *Client) GetContact(
contactID int, contactID int,
@ -61,4 +62,5 @@ func (client *Client) UpdateContact(contact *contacts.Contact) error {
contact.ID, contact.ID,
contact, contact,
) )
} }