Documentation
¶
Overview ¶
Package endpoint resolves which of a server's addresses to use and performs the internal/external failover described in DESIGN.md「端点故障转移」.
The policy in one paragraph: order the configured addresses (an explicit force wins, then a recent last-good address, then the configured preference, then the rest), probe each with a short TCP timeout, and attempt a login on the first reachable one. Network-class failures move to the next candidate; an authentication failure is terminal, because a credential problem is not fixed by another address. When every probe fails, the first candidate still gets one full login attempt — the probe is an accelerator, not a verdict.
Dependency direction: endpoint depends on api and config.
Index ¶
Constants ¶
const LastGoodTTL = 10 * time.Minute
LastGoodTTL is how long a successful address keeps its priority boost.
const ProbeTimeout = 1500 * time.Millisecond
ProbeTimeout is the TCP dial timeout for a candidate (DESIGN.md「数值基线」). It is deliberately short: a DROP-style firewall would otherwise stall for 75s.
const StateFileName = "state.json"
StateFileName is the file that holds cross-process endpoint memory inside the configuration directory.
Variables ¶
var ErrAllUnreachable = errors.New("no reachable endpoint")
ErrAllUnreachable reports that every candidate failed.
Functions ¶
func IsNetworkError ¶
IsNetworkError reports whether err should trigger failover to the next candidate (DESIGN.md「端点故障转移」第 2 点).
Terminal - never a reason to try another address:
- a credential rejection: *api.AuthError carrying a 400/401/403/422, and auth.ErrMFANotProvided / auth.ErrMissingSessionCookie (both mean the endpoint answered and the problem is the account or its configuration)
- context cancellation or deadline expiry: the caller asked to stop
Network-class - fail over: a transport failure (api.APIError with StatusCode 0, i.e. TCP reachable but the service behind it broken - the 「端点故障转移」 VPN-half-connected case) and 5xx responses.
Types ¶
type Candidate ¶
Candidate is one address worth trying.
func Candidates ¶
func Candidates(srv *config.ServerConfig, force Kind, st State) []Candidate
Candidates orders the addresses to try, given a server, an optional forced kind, and the recorded state.
Order: a forced kind returns exactly that candidate (or nothing when the server has no such address — forcing must never silently fall back); otherwise the fresh last-good kind, then the configured preference, then any remaining address. Each kind appears at most once.
func (Candidate) String ¶
String renders the candidate as "internal http://host:port".
type FileStateStore ¶
type FileStateStore struct {
// contains filtered or unexported fields
}
FileStateStore persists State in a small JSON file.
It is used so that a one-shot CLI invocation still benefits from the last-good address without paying for a probe (DESIGN.md「端点故障转移」第 5 点). The file is written atomically with mode 0600 on Unix.
func (*FileStateStore) Get ¶
func (s *FileStateStore) Get(server string) (State, bool)
Get returns the recorded state for a server alias. A missing or corrupt file is not an error: it reports "not found" and the caller pays one extra probe.
func (*FileStateStore) Path ¶
func (s *FileStateStore) Path() string
Path returns the backing file path, or "" on a nil receiver.
type Kind ¶
type Kind string
Kind identifies which configured address a candidate came from.
const ( KindInternal Kind = config.KindInternal KindExternal Kind = config.KindExternal )
Address kinds. These values are also written to the audit stream and rendered by `jms tail` / `jms attach`.
func KindOf ¶ added in v0.1.5
func KindOf(srv *config.ServerConfig, rawURL string) (Kind, bool)
KindOf reports which configured address a URL came from, and whether it matched one. The comparison uses api.NormalizeBaseURL, so the address a candidate carries and the address a session is bound to are compared the way the HTTP client spells them.
It is what lets a long-lived process ask "is the session I have cached still the endpoint I would pick now?" without remembering the answer (DESIGN.md「端点故障转移」第 1 点).
type LoginFunc ¶
LoginFunc attempts a login against one candidate.
It must return an error matching api.AuthError for credential problems; any other error is treated as a network-class failure and triggers failover.
type MemoryStateStore ¶
type MemoryStateStore struct {
// contains filtered or unexported fields
}
MemoryStateStore is an in-memory StateStore.
func NewMemoryStateStore ¶
func NewMemoryStateStore() *MemoryStateStore
NewMemoryStateStore returns an in-memory StateStore for tests.
type ProbeFunc ¶
ProbeFunc reports whether an address is reachable. It must honour the context and must not return an error: unreachability is a false result.
func ProbeWith ¶
ProbeWith returns a ProbeFunc that reaches the candidate through the given proxy policy.
A direct policy returns TCPProbe itself, so the default path is unchanged. This is what keeps the 「端点故障转移」 policy honest: the probe is an accelerator for the login, and it can only accelerate the right thing if it travels the same route.
type Selection ¶
type Selection struct {
Candidate Candidate
ProbeMS int64 // measured probe time in milliseconds; 0 when not probed
Reason string // human-readable decision reason, for logs and audit
}
Selection records which candidate was chosen and why.
func SelectAndLogin ¶
func SelectAndLogin(ctx context.Context, srv *config.ServerConfig, force Kind, probe ProbeFunc, login LoginFunc, st StateStore) (Selection, error)
SelectAndLogin applies the failover policy and returns the chosen candidate.
probe may be nil, in which case every candidate is assumed reachable. login may be nil, in which case the first candidate is returned without attempting a login (used by `jms config test` to report reachability only).
On success the chosen address is recorded in st; a nil st is tolerated. Authentication failures are returned immediately and never trigger failover (DESIGN.md「端点故障转移」第 2 点).
type State ¶
type State struct {
LastGood Kind `json:"last_good,omitempty"`
At time.Time `json:"at,omitempty"`
Backend map[Kind]string `json:"backend,omitempty"`
}
State is the persisted cross-process memory of the last successful address and backend.
func (State) BackendFor ¶
BackendFor returns the remembered backend for a kind, or "" when none is recorded (DESIGN.md「端点故障转移」第 4 点).
func (State) LastGoodFresh ¶
LastGoodFresh reports whether the recorded address is still within TTL.
func (State) Touch ¶
Touch records a successful selection of kind at at, preserving the backend memory.
func (State) WithBackend ¶ added in v0.1.5
WithBackend records backend as the one that worked for kind, preserving the last-good address memory. An empty backend is ignored, so a caller that never observed one cannot erase what an earlier run learned.
It is the write half of the backend memory: the CLI records what it used, and a long-lived MCP host reads it back instead of starting from the compiled-in default (DESIGN.md「端点故障转移」第 4 点).
type StateStore ¶
type StateStore interface {
// Get returns the recorded state for a server alias. The bool is
// false when nothing is recorded.
Get(server string) (State, bool)
// Set records the state for a server alias.
Set(server string, st State) error
}
StateStore persists State between processes.
func NewFileStateStore ¶
func NewFileStateStore(path string) StateStore
NewFileStateStore returns a StateStore backed by path. When path is empty, StateFileName in the current directory is used.