perf(clients): narrow inbound reads for updates without flow

This commit is contained in:
Hamed Sanaei
2026-12-03 00:38:34 +03:30
parent a3a9c53225
commit 9062e5cf94
2 changed files with 22 additions and 10 deletions
@@ -193,7 +193,7 @@ func TestSlimCreateProjectionPreservesNodeIDAndProtocolSettings(t *testing.T) {
{inbound: mtproto, generate: true, settings: mtproto.Settings},
{inbound: mtproto},
} {
got, err := getInboundForClientCreate(tc.inbound.Id, tc.generate)
got, err := getInboundForClientPreparation(tc.inbound.Id, tc.generate)
if err != nil {
t.Fatal(err)
}
@@ -268,14 +268,17 @@ func TestSlimUpdateRebasesConcurrentSettings(t *testing.T) {
record := lookupClientRecord(t, "x@stale")
current := record.ToClient()
current.Comment = "updated alongside renewal"
edit := &model.Inbound{Id: ib.Id, Settings: clientsSettings(t, []model.Client{*current})}
reads := trackInboundSettingsReads(t)
var editErr error
commitTickBetweenReadAndWrite(t, renewYTick(ib.Id), func() {
_, editErr = (&ClientService{}).UpdateInboundClient(&InboundService{}, edit, current.Email)
_, editErr = (&ClientService{}).Update(&InboundService{}, record.Id, *current, 0)
})
if editErr != nil {
t.Fatal(editErr)
}
if len(reads.sizes) != 4 || reads.sizes[0] != 0 || reads.sizes[1] == 0 || reads.sizes[2] == 0 || reads.sizes[3] == 0 {
t.Fatalf("update settings reads = %v, want empty preparation plus full locked, renewal and transactional reads", reads.sizes)
}
requireNeighbourRenewed(t, ib.Id)
if saved, ok := settingsClient(t, ib.Id, current.Email); !ok || saved.Comment != current.Comment {
t.Fatalf("update lost the edited comment: %+v", saved)
+16 -7
View File
@@ -295,7 +295,7 @@ func (s *ClientService) CreateWithContext(ctx context.Context, inboundSvc *Inbou
// shared credentials on the first inbound and every later one reuses them.
adds := make([]*model.Inbound, 0, len(payload.InboundIds))
for _, ibId := range payload.InboundIds {
inbound, getErr := getInboundForClientCreate(ibId, client.Secret == "")
inbound, getErr := getInboundForClientPreparation(ibId, client.Secret == "")
if getErr != nil {
return false, fmt.Errorf("inbound %d: %w", ibId, getErr)
}
@@ -338,9 +338,9 @@ func (s *ClientService) CreateWithContext(ctx context.Context, inboundSvc *Inbou
return needRestart || remoteRestart, errors.Join(saveErr, pushErr)
}
// getInboundForClientCreate returns the settings blob only for protocols
// getInboundForClientPreparation returns the settings blob only for protocols
// that need it to prepare the client before any writes.
func getInboundForClientCreate(id int, generateSecret bool) (*model.Inbound, error) {
func getInboundForClientPreparation(id int, generateSecret bool) (*model.Inbound, error) {
protocolsWithSettings := []model.Protocol{model.Shadowsocks, model.AmneziaWG}
if generateSecret {
protocolsWithSettings = append(protocolsWithSettings, model.MTProto)
@@ -910,7 +910,18 @@ func (s *ClientService) update(inboundSvc *InboundService, id int, updated model
// mints the shared credentials on the first inbound, later ones reuse them.
applies := make([]inboundApply, 0, len(inboundIds))
for _, ibId := range inboundIds {
inbound, getErr := inboundSvc.GetInbound(ibId)
requestedFlow := updated.Flow
if keepInboundFlows {
requestedFlow = inboundFlows[ibId]
}
var inbound *model.Inbound
var getErr error
if requestedFlow == "" {
inbound, getErr = getInboundForClientPreparation(ibId, updated.Secret == "")
} else {
inbound, getErr = inboundSvc.GetInbound(ibId)
}
if getErr != nil {
if errors.Is(getErr, gorm.ErrRecordNotFound) {
if err := database.GetDB().
@@ -932,9 +943,7 @@ func (s *ClientService) update(inboundSvc *InboundService, id int, updated model
if inbound.Protocol == model.WireGuard && tunnelCount > 1 {
clientForInbound.WireguardDevices = nil
}
if keepInboundFlows {
clientForInbound.Flow = inboundFlows[ibId]
}
clientForInbound.Flow = requestedFlow
if ips, ok := updated.AllowedIPsByInbound[ibId]; ok {
clientForInbound.AllowedIPs = ips
} else if tunnelCount > 1 && (inbound.Protocol == model.WireGuard || inbound.Protocol == model.AmneziaWG) {