web

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 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 web is the one way celestial-data reaches an archive: a GET, a HEAD and a POST. A client keeps the cookies its answers set, so a session signed in goes with what it asks. Everything goes through Web, so a test can hold an adapter to the exact addresses it asks for.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ceiling added in v0.0.4

func Ceiling(policy Policy, attempt int) time.Duration

Ceiling is the longest the wait before a retry may be: the policy's backoff base for the first retry, doubled for each one after, and never more than the policy's backoff cap. It does not overflow however many retries are asked for.

func Jitter added in v0.0.4

func Jitter(random Random, ceiling time.Duration) time.Duration

Jitter is a wait picked at random from none up to the ceiling, every wait as likely as another ("full jitter"), so many callers that failed together do not all come back together. It is never negative and never more than the ceiling, whatever the random source gives.

func RecordPath added in v0.0.4

func RecordPath(folder, host string) string

RecordPath is where a host's record is kept in a folder.

func Seconds

func Seconds(count float64) time.Duration

Seconds is a count of seconds as a wait New takes. The waits celestial-data picks are kept in numbers files as seconds.

Types

type Chooser added in v0.0.4

type Chooser interface {
	For(address string) (*Gentle, error)
}

Chooser says which Gentle asks the host of an address. A *Gentle is the Chooser of the one host it asks; *Hosts has one for each.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is Web over HTTP. Its zero value is not ready; New makes one.

func Direct added in v0.0.6

func Direct(wait, whole time.Duration, agent string) Client

Direct makes a client that refuses redirects instead of asking another address without passing it through the caller's host gate.

func New

func New(wait, whole time.Duration, agent string) Client

New makes a Client. The server gets wait to connect and to begin its answer. The whole request gets whole, up to its last byte. Zero is no limit. That suits a large file on a slow link.

func Patient added in v0.0.3

func Patient(wait time.Duration, agent string) Client

Patient makes a Client whose server gets wait to connect and to begin its answer, and whose whole request has no limit, so a large file on a slow link is never cut off.

func (Client) Cookies

func (client Client) Cookies(address string) ([]*http.Cookie, error)

Cookies are the cookies the client holds for an address: those its answers set, and those Keep gave it.

func (Client) Get

func (client Client) Get(url string) (io.ReadCloser, int64, error)

Get is Web's Get over HTTP. Any answer but a success is an error that names the URL and the status. Its body is closed.

func (Client) Head

func (client Client) Head(url string) (int64, error)

Head is Web's Head over HTTP.

func (Client) Keep

func (client Client) Keep(address string, cookies []*http.Cookie) error

Keep gives the client cookies to send to an address, as a session signed in before would be.

func (Client) Post

func (client Client) Post(address string, form url.Values) (string,
	error)

Post is Web's Post over HTTP. An answer that is neither a success nor a redirect is an error that names the address and the status, and never the form, which may hold a password.

type Clock added in v0.0.4

type Clock interface {
	Now() time.Time
	Sleep(ctx context.Context, wait time.Duration) error
}

Clock is the time a gentle keeps. Its Sleep ends early when the context ends, and gives the context's error.

func Wall added in v0.0.4

func Wall() Clock

Wall is the clock on the wall.

type Doer added in v0.0.4

type Doer interface {
	Do(request *http.Request) (*http.Response, error)
}

Doer is what a request is sent through. An *http.Client is one, and so is what Through gives.

func RewritingDoer added in v0.0.4

func RewritingDoer(inner Doer, rewrite func(error) error) Doer

RewritingDoer is a Doer that gives what the inner Doer gives, with each error put through rewrite. An answer that came is given as it came.

func Through added in v0.0.4

func Through(doer Doer, choose Chooser) (Doer, error)

Through is a Doer whose every request goes through the Gentle the Chooser gives for its host. The request's own context stops the waits.

An answer of 403, 429, or 503 is read as Within reads it, and is given as an error, a *Stopped when the host asked us to stop; any other answer is given as it came, for the adapter to read, and shows the host is talking.

A request that carries a body is asked again only when the policy says a POST to the host is safe to repeat and the request can give its body again.

For a host whose policy says HoldsUntilClosed, the answer's body MUST be closed before the host is asked again. For any other the gentle is let go as the answer arrives.

It refuses a missing doer or chooser.

type Gentle added in v0.0.4

type Gentle struct {
	// contains filtered or unexported fields
}

Gentle is the one core of asking a host without being a nuisance: one request at a time, each at least a pace after the one before began, nothing sent while the host has asked us to wait, and no more asking once the host has failed too often with no word.

A request enters, is answered, is judged and leaves; the next waits at the gate. For a policy that holds until a body is closed, a request leaves when its body is closed, not when the answer arrives.

Held, a gentle keeps its last start, any time the host asked it to wait until, what the host's Retry-After said, and its overrides in a record, so the pace and the wait carry over between runs.

Within and Through are its faces for a Web and a Doer; Enter, Leave, Judge and GiveUp are for a library whose requests we do not make.

Insist makes it fail soft: it says what the host asked for and asks anyway.

A Gentle may be used from many goroutines: requests go one at a time, and Insist, Hold and Release may be called while one is in flight.

func NewGentle added in v0.0.4

func NewGentle(policy Policy, clock Clock, random Random) (*Gentle,
	error)

NewGentle is a gentle at a policy, on a clock, with a source of randomness for jitter. It refuses a policy that cannot be asked by, such as a pace quicker than the host's least, and a missing clock or source.

func (*Gentle) Asked added in v0.0.4

func (gentle *Gentle) Asked() int

Asked is how many requests the gentle has let through.

func (*Gentle) Enter added in v0.0.4

func (gentle *Gentle) Enter(ctx context.Context) error

Enter waits until the request before has left and the pace has passed since it started, then notes the start in the record. A request that is let through must be Left.

When the context ends first, or the record will not write, it gives why and does not hold the gentle. Before the time the host asked to be left until, or when the circuit is open, it sends nothing and says so, unless the gentle insists.

func (*Gentle) For added in v0.0.4

func (gentle *Gentle) For(string) (*Gentle, error)

For is the gentle itself, for any address: a gentle is the Chooser of a host that is asked by none other.

func (*Gentle) GiveUp added in v0.0.4

func (gentle *Gentle) GiveUp()

GiveUp is a request that has ended with no word from the host, after all the tries it was allowed. The gentle counts them in a row, and when the policy's opens have passed it leaves the host alone for the rest of the run.

func (*Gentle) Hold added in v0.0.4

func (gentle *Gentle) Hold(path string) error

Hold has the gentle keep its pace and its waits across runs, for this user on this machine. Its record at path holds when the last request began, any time the host asked to wait until, what its Retry-After said, and each override.

The policy's Keeping says how. A shared record, the default, is locked only while a request begins and while a stop or an override is noted, so any number of runs share one pace and one stop, and none is refused. A record held for the run is locked from here until Release.

Either refuses a record it cannot read as a record. A shared record that cannot be made, locked or written does not refuse: the gentle says so once, to the writer it was given for saying things, and keeps its pace in memory for the run.

A record held for the run gives those as errors, as they came.

func (*Gentle) Insist added in v0.0.4

func (gentle *Gentle) Insist(say io.Writer) error

Insist has the gentle ask anyway, for this run. A wait the host asked for, here or in an earlier run, and an open circuit, are asked through after it writes what the host asked for, and the override is kept in the record.

A request the host refuses with a fresh stop is asked once more, after the pace, and only once. A stop that comes back after that is given, and says the override was tried.

It is an error to insist with no writer to say what is overridden: the override would go unsaid, so it is not set.

func (*Gentle) Judge added in v0.0.4

func (gentle *Gentle) Judge(code int, retryAfter string,
	failure error) (silent bool, err error)

Judge says what a request's answer means to the host, once the request is done, with the gentle held. The code is the status's, or zero when no answer came; retryAfter is the host's Retry-After as written, or empty; failure is nil for a success.

A 403 or 429, or a 503 with a Retry-After, is the host asking us to stop: the wait is kept, and err is a *Stopped. So is a 503 with none when the policy allows no retries, since it may not be asked again.

Silent is a failure with no word from the host that is worth asking again: a timeout, a reset connection, a connection that ended early, or a 503 with no Retry-After. The caller may ask again after a wait of its own, and calls GiveUp when it will not.

Within and Through do both.

Any other error, a malformed address or a refused connection among them, comes back as it was, and is not silent and not counted.

Any answer from the host but a silent one, whatever its status, shows it is talking, and starts the count of failures again; a success or any other answer closes an open circuit.

func (*Gentle) Leave added in v0.0.4

func (gentle *Gentle) Leave()

Leave lets the next request through to wait out the pace. A Leave with nothing held does nothing, so a second one cannot wait forever.

func (*Gentle) Reask added in v0.0.4

func (gentle *Gentle) Reask(ctx context.Context, err error,
	reasked *bool) error

Reask is what follows a fresh stop, for a request made with Enter, Leave and Judge, as Within does it for a Web. Give it the error Judge gave and a flag that starts false and is the caller's to keep for the request.

When the gentle insists, and has not asked again for this request, it says what the host asked for, keeps the override, waits the pace and gives nil: ask once more, with the gentle still held. Otherwise it gives the stop, which says the override was tried when it was.

An error that is no stop is given as it came.

func (*Gentle) Release added in v0.0.4

func (gentle *Gentle) Release() error

Release writes the record, so the next run keeps the pace from this one's last request, waits as long as the host asked, and knows what was overridden, and lets the lock go. A gentle that holds no record has nothing to release.

A shared record is written as it goes, so there is nothing to let go; only a note that could not be written then, for want of the lock, is tried once more.

func (*Gentle) Say added in v0.0.4

func (gentle *Gentle) Say(say io.Writer)

Say gives the gentle a writer for saying what it cannot keep, such as a record that will not write. With none, and none to insist by, it stays quiet and goes on.

type Hosts added in v0.0.4

type Hosts struct {
	// contains filtered or unexported fields
}

Hosts has a Gentle for each host an address names, made when the host is first asked, so one Web can ask many hosts and each is asked in its own way. A host with no policy of its own is asked by the default policy, and its stops say so: it fails gentle, not open.

A host is named in lower case with no dot at its end, in the policies, in its record and wherever an address spells it otherwise.

func NewHosts added in v0.0.4

func NewHosts(folder string, clock Clock, random Random,
	policies map[string]Policy) *Hosts

NewHosts is the Gentles of many hosts. Each host's record is kept in the folder, as RecordPath gives; with no folder none is kept. The policies are by the host's name in lower case with no dot at its end.

func (*Hosts) For added in v0.0.4

func (hosts *Hosts) For(address string) (*Gentle, error)

For is the Gentle of the host an address names, made if it is the first time the host is asked. Example.ORG and example.org. are the same host.

func (*Hosts) Insist added in v0.0.4

func (hosts *Hosts) Insist(say io.Writer) error

Insist has every host's gentle insist, those made already and those made later. It is an error to insist with no writer. See Gentle.Insist.

func (*Hosts) Release added in v0.0.4

func (hosts *Hosts) Release() error

Release releases every host's record, so the next run keeps each pace, wait and override.

func (*Hosts) Say added in v0.0.4

func (hosts *Hosts) Say(say io.Writer)

Say has every host's gentle say what it cannot keep, such as a record that will not write, to a writer, those made already and those made later. See Gentle.Say.

func (*Hosts) Unkept added in v0.0.4

func (hosts *Hosts) Unkept(why error)

Unkept says why there is no folder to keep records in, when Hosts was made with none. When the first host is asked, it is said once, to the writer given to Say, that no wait will be kept between runs.

type Keeping added in v0.0.4

type Keeping int

Keeping is how a gentle keeps its record, when it has one.

const (
	// KeepsShared is the default: the record is shared between runs. A
	// request takes the record's lock only for the moment it begins, to
	// read what any run wrote and write its own start, and again for the
	// moment a stop or an override is noted.
	//
	// Two runs asking one host at once keep one pace and one stop between
	// them, and neither is ever refused.
	KeepsShared Keeping = iota
	// KeepsForTheRun holds the record's lock from the first Hold until
	// Release, or until the run ends. A second run is refused while one
	// is asking. It is for a host whose adapter has always asked that way.
	KeepsForTheRun
)

type Numbers added in v0.0.4

type Numbers interface {
	Get(name string) (float64, error)
}

Numbers is where a host's numbers are kept, by name. A celestialdata.Measured is one.

type Policy added in v0.0.4

type Policy struct {
	// Host is the host as a person says it, such as arXiv. It is in
	// every message about the host.
	Host string
	// Pace is the least time from the start of one request to the start
	// of the next.
	Pace time.Duration
	// LeastPace, when not zero, is the quickest pace the host allows. A
	// pace under it is refused, so a mistake in a numbers file cannot
	// ask faster than the host's terms.
	LeastPace time.Duration
	// Rest is how long the host is left alone after it asked us to
	// stop and did not say how long.
	Rest time.Duration
	// Tries is how many more times a request is asked after a failure
	// with no word from the host. None means it is asked once. It is
	// never more than MostTries.
	Tries int
	// MostTries is the most Tries a policy may have. It is this
	// package's number, and a policy is refused with more.
	MostTries int
	// Opens is how many requests in a row may use up their tries before
	// the host is left alone for the rest of the run.
	//
	// Only a request that was allowed its tries and used every one
	// counts. A request that may not be asked again, a POST the host
	// does not say is safe to repeat, does not count.
	//
	// Nor does any request the host answered with a status, whatever it
	// was: an answer shows the host is talking, and starts the count
	// again.
	//
	// Zero means the host's circuit never opens: it is always asked.
	Opens int
	// BackoffBase and BackoffCap bound the wait before a retry: the
	// base for the first, doubled for each after, never over the cap.
	BackoffBase, BackoffCap time.Duration
	// OverridesKept is how many overrides the host's record keeps.
	OverridesKept int
	// Keeping is how the record is kept: shared between runs, which is
	// the default, or held for the run.
	Keeping Keeping
	// LockWait is the longest a request waits for another run to let go
	// of a shared record's lock, which it holds only for a moment. When
	// it passes, the request goes ahead on what this run knows. It is
	// not used for a record held for the run.
	LockWait time.Duration
	// RepeatsPosts is whether a request with a body may be sent again
	// after a failure. It is false unless the host's own numbers say a
	// POST to it is safe to repeat, and why.
	RepeatsPosts bool
	// HoldsUntilClosed is whether the gate is held until the body of an
	// answer is closed, so a slow download is still the only request to
	// the host. It is false unless a host's own code says so.
	//
	// When false, the gate is let go as the answer arrives, and the pace
	// is still measured from the start of each request. A host that
	// holds MUST have each body closed before it is asked again, or the
	// next request waits for it.
	HoldsUntilClosed bool
	// Defaulted is true when no numbers were picked for this host, so
	// the pace is the default. A stop says so.
	Defaulted bool
}

Policy is how one host is asked: how often, how long to wait when it says stop, how often to try again when it says nothing. Each of its numbers is kept in a numbers file, where it says whether it was picked or given by a source.

func DefaultPolicy added in v0.0.4

func DefaultPolicy(host string) (Policy, error)

DefaultPolicy is the policy for a host that has none of its own, from this package's numbers file.

func PolicyFrom added in v0.0.4

func PolicyFrom(host, key string, kept Numbers) (Policy, error)

PolicyFrom is the policy for a host, read from the numbers it names with its key and a dash before, as arxiv-pace. Its pace must be there. A number it does not name is this package's default.

It may also name its least pace, as arxiv-least-pace. A number it names that is not a number the policy can use is refused, saying which.

type Random added in v0.0.4

type Random interface {
	Float64() float64
}

Random is where jitter comes from. Float64 gives a number from zero up to but not including one. A test gives it numbers it chose, so no test waits on chance.

func Unix added in v0.0.4

func Unix() Random

Unix is the random numbers the standard library makes, for a run that is not a test.

type RecordRefused added in v0.0.4

type RecordRefused struct {
	Why string
}

RecordRefused is why a record may not be held: another run holds it, or what is there is not a record. Any other failure of Hold, a folder or a file that will not open, is no refusal, and is given as it came. Find it with errors.As.

func (*RecordRefused) Error added in v0.0.4

func (refused *RecordRefused) Error() string

Error is why.

type Status added in v0.0.2

type Status struct {
	// contains filtered or unexported fields
}

Status is an answer that was not a success, as Get and Head refuse it: the request's method and address, the status as the server wrote it and its code, and its Retry-After header as written, or none. A caller that must stop when a server asks it to wait reads Code.

func (*Status) Code added in v0.0.2

func (status *Status) Code() int

Code is the status's code, as 429 for Too Many Requests.

func (*Status) Error added in v0.0.2

func (status *Status) Error() string

Error is the method, the address and the status.

func (*Status) RetryAfter added in v0.0.2

func (status *Status) RetryAfter() string

RetryAfter is how long the server asked us to wait, as its Retry-After header wrote it: seconds or a date. It is empty when it asked none.

type Stopped added in v0.0.4

type Stopped struct {
	// Host is the host as a person says it.
	Host string
	// Why is what happened, as the request that was refused says it.
	Why string
	// Until is when the host may be asked again. It is the zero time for
	// an open circuit, which lasts as long as the run.
	Until time.Time
	// RetryAfter is what the host's Retry-After said, as written, or
	// empty when it said none.
	RetryAfter string
	// Ours is true when the stop is our own circuit, not the host's word.
	Ours bool
	// Defaulted is true when the host has no pace of its own picked, and
	// the default was used.
	Defaulted bool
	// Insisting is true when the override was set for the run.
	Insisting bool
	// Tried is true when the override was tried: the request was asked
	// once more, and the host refused it again.
	Tried bool
}

Stopped is a host that is not being asked: because it asked us to stop, because the wait it asked for has not passed, or because it failed too often with no word and its circuit is open for this run.

It says which host, why, and until when. An override for one run lets a caller ask anyway.

func (*Stopped) Error added in v0.0.4

func (stopped *Stopped) Error() string

Error is which host, why, until when, and when the pace is the default, says so. It does not name the override: a caller that has one names it in its own words. When one is set, it says what the override did, or why it did not ask again.

type Web

type Web interface {
	// Get opens what a URL holds. length is the number of bytes the
	// server said it would send, or -1 when it did not say.
	Get(url string) (body io.ReadCloser, length int64, err error)
	// Head is the length of what a URL holds, as its server tells it.
	// It is -1 when the server does not say.
	Head(url string) (length int64, err error)
	// Post sends a form to an address and gives the address its answer
	// sends on to, or none. It does not go on to it.
	Post(address string, form url.Values) (where string, err error)
}

Web is what an adapter asks of the network.

func Rewriting added in v0.0.4

func Rewriting(inner Web, rewrite func(error) error) Web

Rewriting is a Web that gives what the inner Web gives, with each error put through rewrite, so an adapter can say a stop in its own words. A request that went well is given as it came, and rewrite is not called.

func Within added in v0.0.4

func Within(ctx context.Context, inner Web, choose Chooser) (Web, error)

Within is a Web whose every request goes through the Gentle the Chooser gives for its host, waiting there while ctx lasts. It is a Web as it stands, so no adapter changes to use it.

A Get holds the gentle until its body is closed only for a host whose policy says HoldsUntilClosed, so a slow download is still the only request. For such a host the body MUST be closed before the host is asked again, or the next request waits for it for as long as ctx lasts.

For any other the gentle is let go as the answer arrives, and the pace is still measured from the start of each request.

A Get or a Head that fails with no word from the host is asked again after a wait, as the policy says. A Post is paced and judged and never asked again unless the policy says a POST to the host is safe to repeat.

The context stops the waits and any attempt that has not begun. The Web interface carries none into a request, so a request in the air is bounded only by the timeouts of the Web inside, as New sets them.

Those are the wait for the server to connect and begin its answer, and the whole of the request when one is given.

It refuses a missing context, web or chooser.

Jump to

Keyboard shortcuts

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