Documentation
¶
Overview ¶
Package proposal is the logic of the panel's proposals that has no I/O and no API: the modes of automation (off, suggest, auto), which mode is in force for an object (Resolve), and what the panel does about one problem at one moment (Decide). The rows live in package store, the API in proto/mistgate/proposal/v1; this package imports neither the generated code nor anything a Worker would pay for.
Index ¶
- Constants
- Variables
- func Execute(ctx context.Context, st *store.Store, appliers map[string]Applier, ...) error
- func ExecuteRows(ctx context.Context, st *store.Store, rows []store.Proposal, ...) error
- func ReadStatus(ctx context.Context, st *store.Store, kind string) (map[string]Status, error)
- func Run(ctx context.Context, st *store.Store, kind string, subjects []Subject, ...) error
- func StatusKey(kind string) string
- type Applier
- type Do
- type Input
- type KindInfo
- type Limits
- type Mode
- type Notify
- type Result
- type Status
- type Subject
- type Verdict
Constants ¶
const ( HoldAwaitingConfirm = "awaiting_confirm" // mode auto, the second source has not confirmed yet HoldCooldown = "cooldown" // the last change was too recent (Until) HoldExhausted = "exhausted" // the limit of changes in the window is reached HoldNotEligible = "not_eligible" // mode auto, but auto is closed for the server (Input.AutoAllowed) HoldNoTarget = "no_target" // nothing suitable to change to HoldSnoozed = "snoozed" // the owner said "not now" (Until) HoldOff = "off" // mode off )
Hold says why the panel is not acting although there is a problem; Verdict.Hold is one of these or "".
const ( EventCreated = "proposal_created" // severity 1: a proposal was made (it waits, or the panel is making it) EventApplied = "proposal_applied" // severity 1: it is done; by = panel | owner EventFailed = "proposal_failed" // severity 2: the change did not happen; code says why )
The events of the panel's proposals (source panel; the codes are in store.panelEventCodes). The params carry ids and codes, and the values of the change only for a kind whose values are public (KindInfo.PublicValues: a port yes, the name of a REALITY target no); no address of a server is in a proposal at all (store.ErrProposalAddress).
const ( NotifyCreated = "created" // a proposal waits for the owner NotifyApplied = "applied" // the panel made a change itself NotifyFailed = "failed" // a change did not happen )
What a Notify is told, the second argument.
const ( KindPortMove = "port_move" KindRealityTarget = "reality_target" KindRelayLeg = "relay_leg" )
The kinds of proposal, the values of proposal.kind.
const ( SourceDefault = "default" SourcePanel = store.AutomationScopePanel SourceNode = store.AutomationScopeNode SourceProfile = store.AutomationScopeProfile SourceServer = store.AutomationScopeServer )
Sources of a mode, the second result of Resolve.
const ApplyDeadline = time.Hour
ApplyDeadline is how long a proposal may stay applying; past it the executor ends it as failed (interrupted).
const DefaultMode = ModeSuggest
DefaultMode is the mode of a panel that has none set. After an update it is what every installation is in: the migration writes no row, and nothing here may ever make it auto.
const MaxStatusEntries = 2000
MaxStatusEntries bounds the setting: more objects than this with a problem or a hold are not listed.
Variables ¶
var Kinds = map[string]KindInfo{ KindPortMove: { AutoClosed: map[string]string{ "awg": "amnezia_keys", "mtproto": "telegram_proxy", "trusttunnel": "trusttunnel", }, PublicValues: true, Revertible: true, Limits: Limits{Pause: 6 * time.Hour, Window: 48 * time.Hour, Max: 3}, }, KindRealityTarget: {}, KindRelayLeg: {}, }
Kinds holds the three kinds. reality_target and relay_leg have no limits and are not revertible until the step that adds their automaton says otherwise; each such step fills its own line.
Functions ¶
func Execute ¶
func Execute(ctx context.Context, st *store.Store, appliers map[string]Applier, notify Notify, now time.Time) error
Execute is one pass of the executor: every applying proposal goes to the applier of its kind. Of any number of executors that end the same proposal one wins the write and sends the event and the message; the others find *store.ProposalStateError and send nothing. A kind with no applier is left alone until ApplyDeadline. Errors of the store come back joined; the proposal stays applying and the next pass takes it up again.
func ExecuteRows ¶
func ExecuteRows(ctx context.Context, st *store.Store, rows []store.Proposal, appliers map[string]Applier, notify Notify, now time.Time) error
ExecuteRows is Execute for rows the caller has read (ApplyingProposals): the edge reads them first and takes its lease only when there are any, so an empty system costs one read.
func ReadStatus ¶
ReadStatus is the stored statuses of kind by subject; none stored is an empty map.
func Run ¶
func Run(ctx context.Context, st *store.Store, kind string, subjects []Subject, notify Notify, now time.Time) error
Run is one pass of the automaton of kind over subjects: every object of the kind that exists, Problem false for the ones that are fine. A pass that could not see the objects (a failed read) must not call it: an object missing from the list is taken as gone. It reads the modes, the open proposals, the changes attempted in the limits' window (made, or failed: a change the node refuses is not tried again at every pass), the owner's "not now"s and the last status (five reads), decides each subject with Decide and writes what the verdict says:
- Propose: store.InsertProposal as awaiting (an equal open one is left as it was, another change replaces it);
- Apply: the same, inserted as applying, decided by the panel;
- Accept: the awaiting one becomes applying, decided by the panel;
- Stale: the awaiting one ends (resolved, mode_off, exhausted, no_target).
An awaiting proposal whose subject is not in the list ends as subject_gone. An applying one is the executor's (Execute). Whoever wins a write sends the event and the message; a loser (the owner decided in the same moment) does nothing. The holds and the open proposals' ids are stored in the setting auto.status.<kind> for the admin. A store failure on one subject does not stop the others; the errors come back joined. The limits are Kinds[kind].Limits.
Types ¶
type Applier ¶
Applier carries a proposal of one kind out. The executor calls it on every pass while the proposal is applying, so it must be idempotent (proposaltest.ApplierContract holds every applier to it):
- write the change guarded by From (for a port: the write that compares the old value);
- when the value already is To, write nothing;
- when it is neither From nor To, the owner changed it meanwhile: Result{Stale, "changed_meanwhile"};
- then check the node works with the new value; until it does, Result{} (still going);
- if the node refuses the new value, put From back (guarded) and answer Failed ("node_rejected").
It finishes by itself well before ApplyDeadline (for example node_offline after 15 minutes).
type Do ¶
type Do int
Do is what Decide tells the caller to do.
const ( // None: write nothing (the Hold, if any, says why). None Do = iota // Propose: make the proposal (store.InsertProposal; it writes nothing when the same one is open, and replaces an awaiting // one that has another change). Propose // Apply: mode auto and confirmed, nothing open: make the change at once (a proposal inserted as applying). Apply // Accept: mode auto and confirmed, a proposal awaits: accept it as the panel (store.AcceptProposal with no admin). Accept // Stale: the awaiting proposal no longer applies; StaleCode says why. Stale )
What the panel does about one problem of one object at one moment.
type Input ¶
type Input struct {
Mode Mode
AutoAllowed bool // auto is not closed for the protocol of this server (Kinds[kind].AutoClosed)
Problem bool // the automaton would propose now
Confirmed bool // a second source (a probe, a check between nodes) agrees; the only thing that lets auto act
// Change is what to change to; nil when nothing suitable exists (no free port, no candidate).
Change *store.ProposalChange
// Open is the awaiting or applying proposal of this kind and subject, if any.
Open *store.Proposal
// SnoozedUntil is the retry_after of the owner's last "not now"; the zero time or a past one holds nothing.
SnoozedUntil time.Time
// Applied are the times the kind was applied to this subject within Limits.Window (store.AppliedProposalsSince from
// Now - Window); Decide counts them all.
Applied []time.Time
Limits Limits
Now time.Time
}
Input is what Decide looks at. Every field is a fact the caller has read; Decide reads nothing and keeps nothing.
type KindInfo ¶
type KindInfo struct {
// AutoClosed maps a protocol to the reason mode auto is not offered for it ("amnezia_keys": the change would reach people
// only by new keys). On such a server the mode is suggest at most.
AutoClosed map[string]string
// PublicValues: from and to may go to events and Telegram (ports: yes; the names of REALITY targets: no).
PublicValues bool
// Revertible: an applied proposal of the kind can be undone ("Вернуть как было").
Revertible bool
// Limits for the automaton of the kind (the window is the one the caller reads the applied changes over).
Limits Limits
}
KindInfo is what the panel knows about a kind of proposal before it has seen one.
type Limits ¶
Limits bound how often the panel changes one object by itself: no more than Max applied changes within Window, and Pause between two of them. A zero Max or Pause is no limit of that kind.
type Mode ¶
type Mode string
Mode is how far the panel goes by itself for one kind of change.
func Resolve ¶
func Resolve(rows []store.AutomationMode, kind, nodeID, profileID, inboundID string) (Mode, string)
Resolve is the mode of kind for one server: the most careful of the modes set at the server, its node and its profile (off, then suggest, then auto; on a tie the server, then the node, then the profile is named as the source). When none of them is set, the panel's own line; when there is none either, DefaultMode. The second result says where the mode comes from: "server", "node", "profile", "panel" or "default". A level is "set" when it has a line, and "inherited" when it has none. An id that is empty names no level.
type Notify ¶
Notify tells the owner. It is called only by the caller that won the write of the transition, and only for what the owner should hear: a proposal that waits for a decision (created), a change the panel made on its own (applied: what the owner accepted or reverted himself he knows), and any change that failed. The events are written either way.
type Result ¶
Result is what an Applier says about an applying proposal this pass. Status "" means it is still going (the node has not answered yet); store.ProposalApplied, ProposalFailed and ProposalStale end it, Code and Params being the outcome the screen words (no address of a server in a param: store.ErrProposalAddress).
type Status ¶
type Status struct {
Node string `json:"node,omitempty"`
Hold string `json:"hold,omitempty"` // one of the Hold constants, or "": nothing holds the panel back
Since int64 `json:"since,omitempty"`
Until int64 `json:"until,omitempty"` // for cooldown and snoozed
ProposalID string `json:"proposal_id,omitempty"`
Params map[string]string `json:"params,omitempty"`
}
Status is what the panel did about one object at the last pass; Run writes a map of them by subject for the objects that have a problem or a hold.
type Subject ¶
type Subject struct {
NodeID, ProfileID, InboundID string // the levels the mode is looked up at (Resolve); ProfileID and InboundID may be ""
Protocol string // for Kinds[kind].AutoClosed
Subject string // the id the proposal is about (the inbound, or the relay); the key of the open proposal
Reason string // silent_port | target_failed | leg_failed
Problem bool // the automaton would propose now
Confirmed bool // a second source agrees; only this lets mode auto act
Change *store.ProposalChange
Facts []store.Fact
Why []store.Evidence
}
Subject is one object an automaton looks at in a pass: the facts Run needs to decide, and what to propose if it comes to that.
type Verdict ¶
type Verdict struct {
Do Do
StaleCode string // for Stale: "resolved", "mode_off", "exhausted" or "no_target"
Hold string // why the panel holds back, or ""
Until time.Time // for HoldCooldown and HoldSnoozed: when it ends
}
Verdict is Decide's answer.
func Decide ¶
Decide is pure. The rules apply in this order and the first that fits answers:
- a proposal is applying: nothing, the executor has it;
- no problem: an awaiting proposal goes stale (resolved);
- mode off: an awaiting proposal goes stale (mode_off), hold off;
- as many applied changes in the window as Limits.Max: stale (exhausted), hold exhausted;
- the last change plus Limits.Pause is still ahead: hold cooldown until then;
- no change to propose: an awaiting proposal goes stale (no_target), hold no_target;
- mode suggest, or mode auto where auto is closed (hold not_eligible): hold snoozed until SnoozedUntil, else Propose;
- mode auto, not confirmed: hold awaiting_confirm, nothing;
- mode auto, confirmed: hold snoozed while the owner's "not now" lasts, else Accept the awaiting proposal or Apply.
It never answers Apply or Accept unless Mode is auto, AutoAllowed, Problem and Confirmed all hold and there is a Change: a mode that is empty or unknown is suggest.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package proposalrpc is the ProposalService (proto/mistgate/proposal/v1): what the owner sees and decides about the panel's proposals, and the modes of automation.
|
Package proposalrpc is the ProposalService (proto/mistgate/proposal/v1): what the owner sees and decides about the panel's proposals, and the modes of automation. |
|
Package proposaltest holds what the tests of the panel's proposals share: a made-up kind with an applier that keeps the contract of proposal.Applier, and ApplierContract, which every real applier of a kind must pass.
|
Package proposaltest holds what the tests of the panel's proposals share: a made-up kind with an applier that keeps the contract of proposal.Applier, and ApplierContract, which every real applier of a kind must pass. |