Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: add BRIDGE_MAX_TABS config (default 20)

Mario813789c11616
diff --git a/config.go b/config.go
index dbe4e010..8370e77a 100644
--- a/config.go
+++ b/config.go
@@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"os"
+ "strconv"
"path/filepath"
"strings"
"time"
@@ -58,6 +59,7 @@ type RuntimeConfig struct {
Timezone string
BlockImages bool
BlockMedia bool
+ MaxTabs int
ChromeBinary string
ChromeExtraFlags string
NoAnimations bool
@@ -78,6 +80,18 @@ func envOr(key, fallback string) string {
return fallback
}
+func envIntOr(key string, fallback int) int {
+ v := os.Getenv(key)
+ if v == "" {
+ return fallback
+ }
+ n, err := strconv.Atoi(v)
+ if err != nil || n < 0 {
+ return fallback
+ }
+ return n
+}
+
func envBoolOr(key string, fallback bool) bool {
v, ok := os.LookupEnv(key)
if !ok {
@@ -107,6 +121,7 @@ type FileConfig struct {
ProfileDir string `json:"profileDir"`
Headless *bool `json:"headless,omitempty"`
NoRestore bool `json:"noRestore"`
+ MaxTabs *int `json:"maxTabs,omitempty"`
TimeoutSec int `json:"timeoutSec,omitempty"`
NavigateSec int `json:"navigateSec,omitempty"`
}
@@ -125,6 +140,7 @@ func loadConfig() {
Timezone: os.Getenv("BRIDGE_TIMEZONE"),
BlockImages: os.Getenv("BRIDGE_BLOCK_IMAGES") == "true",
BlockMedia: os.Getenv("BRIDGE_BLOCK_MEDIA") == "true",
+ MaxTabs: envIntOr("BRIDGE_MAX_TABS", 20),
ChromeBinary: os.Getenv("CHROME_BINARY"),
ChromeExtraFlags: os.Getenv("CHROME_FLAGS"),
NoAnimations: os.Getenv("BRIDGE_NO_ANIMATIONS") == "true",
@@ -170,6 +186,9 @@ func loadConfig() {
if fc.NoRestore && os.Getenv("BRIDGE_NO_RESTORE") == "" {
cfg.NoRestore = true
}
+ if fc.MaxTabs != nil && os.Getenv("BRIDGE_MAX_TABS") == "" {
+ cfg.MaxTabs = *fc.MaxTabs
+ }
if fc.TimeoutSec > 0 && os.Getenv("BRIDGE_TIMEOUT") == "" {
cfg.ActionTimeout = time.Duration(fc.TimeoutSec) * time.Second
}
@@ -244,6 +263,7 @@ func handleConfigCommand() {
fmt.Printf(" State Dir: %s\n", cfg.StateDir)
fmt.Printf(" Profile: %s\n", cfg.ProfileDir)
fmt.Printf(" Headless: %v\n", cfg.Headless)
+ fmt.Printf(" Max Tabs: %d\n", cfg.MaxTabs)
fmt.Printf(" No Restore: %v\n", cfg.NoRestore)
fmt.Printf(" Timeouts: action=%v navigate=%v\n", cfg.ActionTimeout, cfg.NavigateTimeout)
diff --git a/config_test.go b/config_test.go
index e1aaf42e..822aaedf 100644
--- a/config_test.go
+++ b/config_test.go
@@ -70,6 +70,43 @@ func TestMaskToken(t *testing.T) {
}
}
+func TestEnvIntOr(t *testing.T) {
+ t.Setenv("TEST_INT", "42")
+ if got := envIntOr("TEST_INT", 10); got != 42 {
+ t.Errorf("envIntOr = %d, want 42", got)
+ }
+ t.Setenv("TEST_INT", "")
+ if got := envIntOr("TEST_INT", 10); got != 10 {
+ t.Errorf("envIntOr empty = %d, want 10", got)
+ }
+ t.Setenv("TEST_INT", "bad")
+ if got := envIntOr("TEST_INT", 10); got != 10 {
+ t.Errorf("envIntOr bad = %d, want 10", got)
+ }
+ t.Setenv("TEST_INT", "-5")
+ if got := envIntOr("TEST_INT", 10); got != 10 {
+ t.Errorf("envIntOr negative = %d, want 10", got)
+ }
+}
+
+func TestMaxTabsDefault(t *testing.T) {
+ t.Setenv("BRIDGE_MAX_TABS", "")
+ t.Setenv("BRIDGE_CONFIG", "/nonexistent")
+ loadConfig()
+ if cfg.MaxTabs != 20 {
+ t.Errorf("default MaxTabs = %d, want 20", cfg.MaxTabs)
+ }
+}
+
+func TestMaxTabsEnv(t *testing.T) {
+ t.Setenv("BRIDGE_MAX_TABS", "5")
+ t.Setenv("BRIDGE_CONFIG", "/nonexistent")
+ loadConfig()
+ if cfg.MaxTabs != 5 {
+ t.Errorf("MaxTabs = %d, want 5", cfg.MaxTabs)
+ }
+}
+
func TestDefaultConfig(t *testing.T) {
fc := defaultFileConfig()
if fc.Port != "9867" {
diff --git a/tab_manager.go b/tab_manager.go
index 2a386c7a..b9fd8fd1 100644
--- a/tab_manager.go
+++ b/tab_manager.go
@@ -87,6 +87,16 @@ func (tm *TabManager) CreateTab(url string) (string, context.Context, context.Ca
if tm.browserCtx == nil {
return "", nil, nil, fmt.Errorf("no browser context available")
}
+
+ if cfg.MaxTabs > 0 {
+ targets, err := tm.ListTargets()
+ if err != nil {
+ return "", nil, nil, fmt.Errorf("check tab count: %w", err)
+ }
+ if len(targets) >= cfg.MaxTabs {
+ return "", nil, nil, fmt.Errorf("tab limit reached (%d/%d) — close a tab first", len(targets), cfg.MaxTabs)
+ }
+ }
ctx, cancel := chromedp.NewContext(tm.browserCtx)
if tm.onTabSetup != nil {