Skip to content

refactor: decompose God Files xclient.go (1444→231) and server.go (1098→299) - #939

Merged
smallnest merged 8 commits into
masterfrom
refactor/god-object-file-split
Jun 30, 2026
Merged

smallnest merged 8 commits into
masterfrom
refactor/god-object-file-split

Conversation

@smallnest

Copy link
Copy Markdown
Owner

Summary

Decomposes the two God Files flagged as Critical in the smell report (tasks/smell-report-2026-06-30-2329.md, follow-up to PR #938):

  • client/xclient.go — 1444 → 231 lines
  • server/server.go — 1098 → 299 lines

Each is split into 5 cohesive files within the same package. No logic, API, or behavior change — pure mechanical move of declarations between files in the same package.

Why same-package file decomposition (not true sub-packages)

A deep-dive found both xClient and Server are shared-mutable-receiver God Objects: nearly every method acquires the same mu mutex and touches the same field set (servers, cachedClient, auth, Plugins, isShutdown, selector). Hub methods (selectClient, getCachedClient, sendResponse, handleRequest) are called across what would be separate concerns. Go forbids defining one struct's methods across packages, so true sub-package extraction would require breaking the shared struct into multiple types plus internal interfaces/accessors — a rewrite touching the hot path and forcing internal-access tests to move. That's major-version work, deferred. This file decomposition is the safe prerequisite: each concern now sits in its own focused, scannable file, and a future major-version split has clear boundaries to extract.

Files

client/ (package client unchanged)

File Lines Concern
xclient.go 231 struct, XClient interface, ctors, config, Close
xclient_discovery.go 290 watch, filterByStateAndGroup, selectClient, client cache, generateClient
xclient_call.go 456 Go, Call, Oneshot, SendRaw, wrapCall, error classification
xclient_broadcast.go 335 Broadcast, Fork, Inform
xclient_transfer.go 181 SendFile, DownloadFile, Stream

server/ (package server unchanged; existing separate files like gateway.go/plugin.go/service.go untouched)

File Lines Concern
server.go 299 struct, types, NewServer, Serve, HTTP/WS handlers, IP validators
server_conn.go 383 serveListener, serveConn, processOneRequest, readRequest, auth
server_dispatch.go 200 handleRequest, handleRequestForFunction, handleError
server_response.go 81 sendResponse, SendMessage
server_shutdown.go 194 Close, Shutdown, Restart, lifecycle hooks

Diff: 10 files, +2120 / -2012 (net +108 = 8 file-header comments + reformatted imports).

How it was done

For each file: extracted contiguous decl blocks to the new file via sed, let goimports resolve exact imports, deleted the blocks from the source, gofmt, then build + vet + test. One file per commit (8 commits) so each is independently revertible. Code text is unchanged — only which file a declaration lives in.

Test status

  • go build ./... — clean
  • go vet ./client/... ./server/... — clean (one pre-existing warning in plugin_test.go:56, untouched, unrelated)
  • go test ./share/... ./protocol/... ./codec/... ./client/... ./server/... — all pass
  • go test -race -run 'TestShutdownHook|TestPluginHeartbeat' ./server/ — pass (the de-flaked tests from PR refactor: smell-report fixes — panics, dead sort, swallowed errors, test de-flake #938 still green)
  • Duplicate-method check: 0 duplicates for both xClient and Server — no methods lost or duplicated (27 xClient methods, 54 Server methods preserved)
  • All new/modified files gofmt-clean

Backward compatibility

Package import paths unchanged (github.com/smallnest/rpcx/client, .../server). All exported types/methods remain in place. External consumers (serverplugin, clientplugin) use only public interfaces — verified unaffected. Same-package move means the shared receiver, mutex, fields, and internal-access tests all work exactly as before.

Checklist

Not in this PR (deferred to a major version)

  • True sub-package extraction (client/discover, server/gateway, etc.) — requires breaking the shared mutable receiver into multiple types + internal interfaces; a rewrite suited to a major version. This file decomposition is the safe prerequisite that makes it tractable.

smallnest added 8 commits July 1, 2026 00:16
Move service-discovery, server-filtering, and cached-client-management
methods out of xclient.go (God File, Critical #1) into a focused
xclient_discovery.go. Same package, pure mechanical move — no logic,
API, or behavior change.

Moved: watch, filterByStateAndGroup, selectClient, safeCloseClient,
getCachedClient/setCachedClient/findCachedClient/deleteCachedClient/
removeClient/generateClient/getCachedClientWithoutLock,
splitNetworkAndAddress.
Move core invocation (Go, Call, Oneshot, SendRaw) and their helpers
(setServerTimeout, uncoverError, contextCanceled, wrapCall, wrapSendRaw)
out of xclient.go into xclient_call.go. Same package, mechanical move.
Move Broadcast, Fork, Inform out of xclient.go into xclient_broadcast.go.
Same package, mechanical move.
…sfer.go

Move SendFile, DownloadFile, Stream out of xclient.go into
xclient_transfer.go. Close stays in xclient.go (lifecycle).
Same package, mechanical move.

xclient.go is now 231 lines (down from 1444); the God File (Critical #1)
is decomposed into 5 cohesive files: xclient.go (home/types/ctors),
xclient_discovery.go, xclient_call.go, xclient_broadcast.go,
xclient_transfer.go.
Move handleRequest, handleRequestForFunction, handleError out of
server.go into server_dispatch.go. Same package, mechanical move.
Move sendResponse and SendMessage out of server.go into server_response.go.
Same package, mechanical move.
Move serveListener, serveConn, processOneRequest, parseServerTimeout,
isShutdown, closeConn, readRequest, auth out of server.go into
server_conn.go. Same package, mechanical move.
Move getDoneChan, Close, RegisterOnShutdown, RegisterOnRestart,
Shutdown, Restart, startProcess, checkProcessMsg, closeDoneChanLocked
(and shutdownPollInterval) out of server.go into server_shutdown.go.
Same package, mechanical move.

server.go is now 299 lines (down from 1098); the God File (Critical #2)
is decomposed into 5 cohesive files: server.go (home/types/ctors/Serve),
server_conn.go, server_dispatch.go, server_response.go, server_shutdown.go.
@smallnest smallnest added enhancement go Pull requests that update go code labels Jun 30, 2026
@smallnest
smallnest merged commit 0cc430c into master Jun 30, 2026
1 check passed
@smallnest
smallnest deleted the refactor/god-object-file-split branch June 30, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant