perf(sub): read the configured public host once per subscription request

An inbound with a wildcard listen and no node takes its link host from
subDomain, then webDomain. That lookup ran for every link, so a
subscription with N such links issued 2N extra settings queries on top of
the batched render settings (7 instead of 1 for three links).

Load both keys in the per-request settings batch and keep the resolved
host on the request's SubService. The order subDomain, webDomain, request
host is unchanged.
This commit is contained in:
Sanaei
2026-11-25 17:20:10 +03:30
parent ff7d31984f
commit 99e7c5a827
4 changed files with 74 additions and 27 deletions
+12 -12
View File
@@ -138,9 +138,9 @@ func TestMergeFinalMask_EmptyExtraTcpKeepsBase(t *testing.T) {
}
}
// --- service.go:69-77 — configuredPublicHost priority: subDomain > webDomain > "" ---
// --- link host of a wildcard-listen inbound: subDomain > webDomain > request host ---
func TestConfiguredPublicHost_Priority(t *testing.T) {
func TestWildcardInboundLinkHost_Priority(t *testing.T) {
initMutDB(t)
db := database.GetDB()
set := func(key, val string) {
@@ -148,23 +148,23 @@ func TestConfiguredPublicHost_Priority(t *testing.T) {
t.Fatalf("save %s: %v", key, err)
}
}
s := &SubService{}
// Both empty → "".
if got := s.configuredPublicHost(); got != "" {
t.Fatalf("no domains configured: got %q, want empty", got)
linkHost := func() string {
s := &SubService{}
s.PrepareForRequest("192.168.1.50")
return s.resolveInboundAddress(&model.Inbound{Listen: ""})
}
if got := linkHost(); got != "192.168.1.50" {
t.Fatalf("no domains configured: got %q, want the request host", got)
}
// Only webDomain → webDomain wins (exercises the second branch, service.go:73).
set("webDomain", "web.example.com")
if got := s.configuredPublicHost(); got != "web.example.com" {
if got := linkHost(); got != "web.example.com" {
t.Fatalf("webDomain fallback: got %q, want web.example.com", got)
}
// subDomain set → subDomain takes precedence over webDomain (service.go:70).
set("subDomain", "sub.example.com")
if got := s.configuredPublicHost(); got != "sub.example.com" {
if got := linkHost(); got != "sub.example.com" {
t.Fatalf("subDomain priority: got %q, want sub.example.com", got)
}
}
+48
View File
@@ -197,3 +197,51 @@ func TestSubscriptionRenderSettingsPartialReadFailure(t *testing.T) {
req.datepicker, req.subExpiredTemplate, req.subTrafficDepletedTemplate, req.subCalendarExpireInclusive, req.calendarExpireLocation)
}
}
// A wildcard-listen inbound resolves its link host from subDomain/webDomain; reading
// those per link cost two more settings queries for every link in the subscription.
func TestSubscriptionRequestReadsSettingsOncePerRequest(t *testing.T) {
seedSubDB(t)
db := database.GetDB()
for i := range 3 {
ib := seedSubInbound(t, "one-read", fmt.Sprintf("wild-%d", i), 4940+i, i+1, wsTLSStream)
if err := db.Model(ib).Update("listen", "").Error; err != nil {
t.Fatal(err)
}
}
seedRenderSettings(t, model.Setting{Key: "webDomain", Value: "panel.example.com"})
gin.SetMode(gin.TestMode)
router := newSubscriptionTestRouter(subscriptionTestRouterConfig{})
var reads atomic.Int64
const name = "test:count_settings"
if err := db.Callback().Query().After("gorm:query").Register(name, func(tx *gorm.DB) {
if tx.Statement.Table == "settings" {
reads.Add(1)
}
}); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Callback().Query().Remove(name) })
for _, format := range []string{"sub", "json", "clash"} {
t.Run(format, func(t *testing.T) {
reads.Store(0)
res := httptest.NewRecorder()
router.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "http://203.0.113.9/"+format+"/one-read", nil))
body := res.Body.String()
if format == "sub" {
raw, err := base64.StdEncoding.DecodeString(body)
if err != nil {
t.Fatalf("decode subscription: %v", err)
}
body = string(raw)
}
if got := strings.Count(body, "panel.example.com"); res.Code != http.StatusOK || got != 3 {
t.Fatalf("status=%d, links on the configured host = %d, want 3; body=%q", res.Code, got, body)
}
if got := reads.Load(); got != 1 {
t.Fatalf("settings queries = %d, want 1", got)
}
})
}
}
+7 -15
View File
@@ -38,6 +38,7 @@ var salamanderWarningSeen sync.Map
// SubService provides business logic for generating subscription links and managing subscription data.
type SubService struct {
address string
configuredPublicHost string
remarkTemplate string
datepicker string
wireguardDevice string
@@ -156,9 +157,11 @@ func (s *SubService) ForWireguardDevice(id string) *SubService {
// SubService instance. HTTP handlers should call ForRequest instead so the
// controller's shared base service is never mutated by concurrent requests.
func (s *SubService) PrepareForRequest(host string) {
settings := s.settingService.GetSubscriptionRenderSettings()
s.configuredPublicHost = settings.PublicHost
if !isRoutableHost(host) {
if d := s.configuredPublicHost(); d != "" {
host = d
if s.configuredPublicHost != "" {
host = s.configuredPublicHost
} else if isLoopbackHost(host) {
host = "localhost"
}
@@ -181,7 +184,6 @@ func (s *SubService) PrepareForRequest(host string) {
s.clientGroupIndexLoaded = false
s.rendered = 0
s.wireguardDeviceSelected = false
settings := s.settingService.GetSubscriptionRenderSettings()
s.hostsFirst = settings.HostSortMode == "hosts-first"
s.loadNodes()
s.loadRemarkSettings(settings)
@@ -374,16 +376,6 @@ func (s *SubService) loadRemarkSettings(settings service.SubscriptionRenderSetti
}
}
func (s *SubService) configuredPublicHost() string {
if d, err := s.settingService.GetSubDomain(); err == nil && d != "" {
return d
}
if d, err := s.settingService.GetWebDomain(); err == nil && d != "" {
return d
}
return ""
}
func isRoutableHost(host string) bool {
if host == "" {
return false
@@ -1931,8 +1923,8 @@ func (s *SubService) resolveInboundAddress(inbound *model.Inbound) string {
return c
}
}
if d := s.configuredPublicHost(); d != "" {
return d
if s.configuredPublicHost != "" {
return s.configuredPublicHost
}
return s.address
}
@@ -17,6 +17,8 @@ type SubscriptionRenderSettings struct {
TrafficDepletedTemplate string
CalendarExpireInclusive bool
CalendarExpireLocation *time.Location
// PublicHost is subDomain, else webDomain: the address links fall back to.
PublicHost string
}
// GetSubscriptionRenderSettings loads the presentation settings for one preparation.
@@ -25,6 +27,7 @@ func (s *SettingService) GetSubscriptionRenderSettings() SubscriptionRenderSetti
keys := []string{
"subHostSortMode", "datepicker", "subShowIdentityOnAllLinks", "subInfoNodeEnable",
"subExpiredTemplate", "subTrafficDepletedTemplate", "subCalendarExpireInclusive", "timeLocation",
"subDomain", "webDomain",
}
get := s.getString
var rows []model.Setting
@@ -65,5 +68,9 @@ func (s *SettingService) GetSubscriptionRenderSettings() SubscriptionRenderSetti
settings.CalendarExpireLocation = loadSettingTimeLocation(value)
}
}
settings.PublicHost, _ = get("subDomain")
if settings.PublicHost == "" {
settings.PublicHost, _ = get("webDomain")
}
return settings
}