Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion util/resolver/limited/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package limited
import (
"context"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"

Expand All @@ -21,10 +23,20 @@ type contextKeyT string
var contextKey = contextKeyT("buildkit/util/resolver/limited")

// DefaultMaxConcurrency is the default number of concurrent connections per registry.
var DefaultMaxConcurrency int64 = 4
// It can be overridden by setting the BUILDKIT_MAX_REGISTRY_CONCURRENCY environment variable.
var DefaultMaxConcurrency = getMaxConcurrency()

var Default = New(int(DefaultMaxConcurrency))

func getMaxConcurrency() int64 {
if v := os.Getenv("BUILDKIT_MAX_REGISTRY_CONCURRENCY"); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 {
return n
}
}
return 4
}

type Group struct {
mu sync.Mutex
size int
Expand Down
59 changes: 59 additions & 0 deletions util/resolver/limited/group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package limited

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetMaxConcurrency(t *testing.T) {
tests := []struct {
name string
envValue string
expected int64
}{
{
name: "default when unset",
envValue: "",
expected: 4,
},
{
name: "custom valid value",
envValue: "20",
expected: 20,
},
{
name: "minimum valid value",
envValue: "1",
expected: 1,
},
{
name: "zero falls back to default",
envValue: "0",
expected: 4,
},
{
name: "negative falls back to default",
envValue: "-1",
expected: 4,
},
{
name: "non-numeric falls back to default",
envValue: "abc",
expected: 4,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue == "" {
os.Unsetenv("BUILDKIT_MAX_REGISTRY_CONCURRENCY")
} else {
t.Setenv("BUILDKIT_MAX_REGISTRY_CONCURRENCY", tt.envValue)
}
result := getMaxConcurrency()
require.Equal(t, tt.expected, result)
})
}
}