Skip to content

Commit e0b587e

Browse files
authored
Merge pull request #611 from erizocosmico/feature/config-bblfsh-size
function: make max bblfsh blob size configurable
2 parents 2dbc45e + 4201a8d commit e0b587e

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
@@ -21,14 +21,18 @@ import (
2121
)
2222

2323
const (
24-
uastCacheSize = "GITBASE_UAST_CACHE_SIZE"
24+
uastCacheSizeKey = "GITBASE_UAST_CACHE_SIZE"
2525
defaultUASTCacheSize = 10000
26+
27+
uastMaxBlobSizeKey = "GITBASE_MAX_UAST_BLOB_SIZE"
28+
defaultUASTMaxBlobSize = 5 * 1024 * 1024 // 5MB
2629
)
2730

2831
var uastCache *lru.Cache
32+
var uastMaxBlobSize int
2933

3034
func init() {
31-
s := os.Getenv(uastCacheSize)
35+
s := os.Getenv(uastCacheSizeKey)
3236
size, err := strconv.Atoi(s)
3337
if err != nil || size <= 0 {
3438
size = defaultUASTCacheSize
@@ -38,6 +42,11 @@ func init() {
3842
if err != nil {
3943
panic(fmt.Errorf("cannot initialize UAST cache: %s", err))
4044
}
45+
46+
uastMaxBlobSize, err = strconv.Atoi(os.Getenv(uastMaxBlobSizeKey))
47+
if err != nil {
48+
uastMaxBlobSize = defaultUASTMaxBlobSize
49+
}
4150
}
4251

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

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