Skip to content

Commit 7cd125e

Browse files
committed
function: make max bblfsh blob size configurable
Signed-off-by: Miguel Molina <[email protected]>
1 parent 19800be commit 7cd125e

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

docs/using-gitbase/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| `GITBASE_CACHESIZE_MB` | size of the cache for git objects specified as MB |
1717
| `GITBASE_CONNECTION_TIMEOUT` | timeout in seconds used for client connections on write and reads. No timeout by default. |
1818
| `GITBASE_USER_FILE` | JSON file with user credentials |
19+
| `GITBASE_MAX_UAST_BLOB_SIZE` | Max size of blobs to send to be parsed by bblfsh. Default: 5242880 (5MB) |
1920

2021
### Jaeger tracing variables
2122

internal/function/uast.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ import (
2020
)
2121

2222
const (
23-
uastCacheSize = "GITBASE_UAST_CACHE_SIZE"
23+
uastCacheSizeKey = "GITBASE_UAST_CACHE_SIZE"
2424
defaultUASTCacheSize = 10000
25+
26+
uastMaxBlobSizeKey = "GITBASE_MAX_UAST_BLOB_SIZE"
27+
defaultUASTMaxBlobSize = 5 * 1024 * 1024 // 5MB
2528
)
2629

2730
var uastCache *lru.Cache
31+
var uastMaxBlobSize int
2832

2933
func init() {
30-
s := os.Getenv(uastCacheSize)
34+
s := os.Getenv(uastCacheSizeKey)
3135
size, err := strconv.Atoi(s)
3236
if err != nil || size <= 0 {
3337
size = defaultUASTCacheSize
@@ -37,6 +41,11 @@ func init() {
3741
if err != nil {
3842
panic(fmt.Errorf("cannot initialize UAST cache: %s", err))
3943
}
44+
45+
uastMaxBlobSize, err = strconv.Atoi(os.Getenv(uastMaxBlobSizeKey))
46+
if err != nil {
47+
uastMaxBlobSize = defaultUASTMaxBlobSize
48+
}
4049
}
4150

4251
// uastFunc shouldn't be used as an sql.Expression itself.
@@ -166,6 +175,18 @@ func (u *uastFunc) Eval(ctx *sql.Context, row sql.Row) (out interface{}, err err
166175
return nil, nil
167176
}
168177

178+
if uastMaxBlobSize >= 0 && len(bytes) > uastMaxBlobSize {
179+
logrus.WithFields(logrus.Fields{
180+
"max": uastMaxBlobSize,
181+
"size": len(bytes),
182+
}).Warnf(
183+
"uast will be skipped, file is too big to send to bblfsh."+
184+
"This can be configured using %s environment variable",
185+
uastMaxBlobSizeKey,
186+
)
187+
return nil, nil
188+
}
189+
169190
lang, err := exprToString(ctx, u.Lang, row)
170191
if err != nil {
171192
return nil, err

internal/function/uast_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ func TestUASTMode(t *testing.T) {
6262
}
6363
}
6464

65+
func TestUASTMaxBlobSize(t *testing.T) {
66+
ctx, cleanup := setup(t)
67+
defer cleanup()
68+
69+
fn := NewUASTMode(
70+
expression.NewGetField(0, sql.Text, "", false),
71+
expression.NewGetField(1, sql.Blob, "", false),
72+
expression.NewGetField(2, sql.Text, "", false),
73+
)
74+
75+
u, _ := bblfshFixtures(t, ctx)
76+
77+
require := require.New(t)
78+
row := sql.NewRow("annotated", []byte(testCode), "Python")
79+
result, err := fn.Eval(ctx, row)
80+
require.NoError(err)
81+
82+
assertUASTBlobs(t, ctx, u["annotated"], result)
83+
84+
uastMaxBlobSize = 2
85+
86+
result, err = fn.Eval(ctx, row)
87+
require.NoError(err)
88+
require.Nil(result)
89+
90+
uastMaxBlobSize = defaultUASTMaxBlobSize
91+
}
92+
6593
func TestUAST(t *testing.T) {
6694
ctx, cleanup := setup(t)
6795
defer cleanup()

0 commit comments

Comments
 (0)