endpoint

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const LastGoodTTL = 10 * time.Minute

LastGoodTTL is how long a successful address keeps its priority boost.

View Source
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.

View Source
const StateFileName = "state.json"

StateFileName is the file that holds cross-process endpoint memory inside the configuration directory.

Variables

View Source
var ErrAllUnreachable = errors.New("no reachable endpoint")

ErrAllUnreachable reports that every candidate failed.

Functions

func IsNetworkError

func IsNetworkError(err error) bool

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.

func TCPProbe

func TCPProbe(ctx context.Context, rawURL string, timeout time.Duration) bool

TCPProbe is the default ProbeFunc: it dials the candidate's host:port and closes the connection immediately.

It dials DIRECTLY. When a proxy is configured, use ProbeWith instead, or the probe and the login that follows it would measure two different networks (DESIGN.md「代理策略」).

Types

type Candidate

type Candidate struct {
	Kind    Kind
	URL     string
	SSHPort int
}

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

func (c Candidate) String() 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.

func (*FileStateStore) Set

func (s *FileStateStore) Set(server string, st State) error

Set records the state for a server alias, writing the file atomically.

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

type LoginFunc func(ctx context.Context, cand Candidate) error

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.

func (*MemoryStateStore) Get

func (m *MemoryStateStore) Get(server string) (State, bool)

Get returns the recorded state for a server alias.

func (*MemoryStateStore) Set

func (m *MemoryStateStore) Set(server string, st State) error

Set records the state for a server alias.

type ProbeFunc

type ProbeFunc func(ctx context.Context, url string, timeout time.Duration) bool

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

func ProbeWith(px netproxy.Proxy) ProbeFunc

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

func (s State) BackendFor(kind Kind) string

BackendFor returns the remembered backend for a kind, or "" when none is recorded (DESIGN.md「端点故障转移」第 4 点).

func (State) LastGoodFresh

func (s State) LastGoodFresh(now time.Time) bool

LastGoodFresh reports whether the recorded address is still within TTL.

func (State) Touch

func (s State) Touch(kind Kind, at time.Time) State

Touch records a successful selection of kind at at, preserving the backend memory.

func (State) WithBackend added in v0.1.5

func (s State) WithBackend(kind Kind, backend string) State

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL