Skip to content

Commit

Permalink
ADD common.RetrieveToMap limiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
karminski committed Feb 19, 2024
1 parent 7da1566 commit 9c2e3b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/actionruntime/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"log"

"github.com/DmitriyVTitov/size"
parser_template "github.com/illacloud/builder-backend/src/utils/parser/template"
)

Expand Down Expand Up @@ -59,8 +62,10 @@ func RetrieveToMap(rows *sql.Rows) ([]map[string]interface{}, error) {
values := make([]interface{}, count)
// pointer of every row values
valPointers := make([]interface{}, count)
iteratorNums := 0
tableDataCapacity := 10000
for rows.Next() {

iteratorNums++
// get pointer for every row
for i := 0; i < count; i++ {
valPointers[i] = &values[i]
Expand All @@ -87,6 +92,15 @@ func RetrieveToMap(rows *sql.Rows) ([]map[string]interface{}, error) {
entry[col] = v
}
mapData = append(mapData, entry)
// check tableData size by sample
if iteratorNums == SQL_RESULT_MEMORY_CHECK_SAMPLE {
tableDataSizeBySample := size.Of(mapData)
tableDataCapacity = (SQL_RESULT_MEMORY_LIMIT / tableDataSizeBySample) * 1000
}
if iteratorNums > tableDataCapacity {
log.Printf("[ERROR] RetrieveToMap result exceeds 200MiB by iteratorNums: %d, size: %d", iteratorNums, size.Of(mapData))
return nil, errors.New("returned result exceeds 200MiB, please adjust the query limit to reduce the number of results")
}
}

return mapData, nil
Expand Down Expand Up @@ -118,8 +132,11 @@ func RetrieveToMapByDriverRows(rows driver.Rows) ([]map[string]interface{}, erro

// value of every row
values := make([]driver.Value, len(renamedColumns))
iteratorNums := 0
tableDataCapacity := 10000
// get all values
for {
iteratorNums++
errInFetchNextRows := rows.Next(values)
if errInFetchNextRows != nil {
break
Expand All @@ -142,6 +159,15 @@ func RetrieveToMapByDriverRows(rows driver.Rows) ([]map[string]interface{}, erro
entry[col] = v
}
mapData = append(mapData, entry)
// check tableData size by sample
if iteratorNums == SQL_RESULT_MEMORY_CHECK_SAMPLE {
tableDataSizeBySample := size.Of(mapData)
tableDataCapacity = (SQL_RESULT_MEMORY_LIMIT / tableDataSizeBySample) * 1000
}
if iteratorNums > tableDataCapacity {
log.Printf("[ERROR] RetrieveToMap result exceeds 200MiB by iteratorNums: %d, size: %d", iteratorNums, size.Of(mapData))
return nil, errors.New("returned result exceeds 200MiB, please adjust the query limit to reduce the number of results")
}
}

return mapData, nil
Expand Down
2 changes: 1 addition & 1 deletion src/actionruntime/postgresql/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func RetrieveToMap(rows pgx.Rows) ([]map[string]interface{}, error) {
tableDataCapacity = (common.SQL_RESULT_MEMORY_LIMIT / tableDataSizeBySample) * 1000
}
if iteratorNums > tableDataCapacity {
log.Printf("[ERROR] RetrieveToMap result exceeds 200MiB by iteratorNums: %d", iteratorNums)
log.Printf("[ERROR] RetrieveToMap result exceeds 200MiB by iteratorNums: %d, size: %d", iteratorNums, size.Of(tableData))
return nil, errors.New("returned result exceeds 200MiB, please adjust the query limit to reduce the number of results")
}
}
Expand Down

0 comments on commit 9c2e3b7

Please sign in to comment.