ghclient

package
v0.199.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 11 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 ghclient is the fleet's GitHub client for a command that WRITES and must not use `gh`: it authenticates from the token file only, guards the per-resource hourly budget from the rate-limit headers, spaces its writes, and backs off when GitHub answers with a secondary rate limit.

References (opened 2026-11-28):

https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api
  primary limit: x-ratelimit-remaining / -reset / -resource headers;
  secondary: at most 80 content-generating requests per minute and 500 per
  hour, 100 concurrent, 900 points per minute with POST/PATCH/PUT/DELETE at 5
  points; on a 403 or 429 honour retry-after, else wait at least a minute.

The guard is NOT internal/fleet/budget.go: that one reads `rate_limit` through the retired `gh`. This one reads the same counter once through its own token, then learns every later value from the headers of the answers it already gets, so the guard costs no request of its own. The floor is fleet.DefaultReserve, the number the rest of the fleet stops at.

The token goes only in the Authorization header. Redirects are refused, so it never follows a Location elsewhere; https only (http for 127.0.0.1 test servers).

Index

Constants

View Source
const APIBase = "https://api.github.com"

APIBase is GitHub's REST and GraphQL host.

Variables

View Source
var ErrReserve = errors.New("stopped at the budget reserve")

ErrReserve is what an error is when a budget has run down to its reserve.

Functions

This section is empty.

Types

type AccessError

type AccessError struct {
	Status  int
	Message string
}

AccessError is a 401 or 403 that is not a rate limit: the answer is UNREAD, not empty.

func (*AccessError) Error

func (e *AccessError) Error() string

type Client

type Client struct {
	Base     string
	Token    string
	HTTP     *http.Client
	Reserve  int           // stop when a resource has this many calls left or fewer
	WriteGap time.Duration // minimum time between two writes
	Backoff  time.Duration // minimum wait after a secondary limit
	Retries  int           // retries after a secondary limit
	Sleep    func(time.Duration)
	Now      func() time.Time
	// contains filtered or unexported fields
}

Client is not safe for two goroutines asking at once, by design: every write here is serial.

func New

func New(token string, reserve int) *Client

New returns a client with the fleet's defaults: a reserve of reserve calls, writes at least a second apart, a minute's back-off, two retries.

func (*Client) Do

func (c *Client) Do(method, path string, payload any) (*Response, error)

Do sends one request and returns its answer whatever its status, except for a rate limit that outlasts the retries, a 401/403 that is not one, and a resource at its reserve: those are errors, never an empty answer. payload, if not nil, is sent as JSON.

func (*Client) GraphQL

func (c *Client) GraphQL(query string, vars map[string]any) ([]byte, error)

GraphQL runs one query and returns the raw answer of an HTTP 200, errors member included: the caller judges which GraphQL errors it tolerates. Any other status is an error that names it ("HTTP 502"), which is what the page-halving of the scan looks for.

type RateLimitError

type RateLimitError struct {
	Secondary bool
	Status    int
	Message   string
	Reset     string // when known
}

RateLimitError is a rate limit GitHub answered with, after the retries.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

type ReserveError

type ReserveError struct {
	Resource      string
	Left, Reserve int
}

ReserveError names the resource that ran down.

func (*ReserveError) Error

func (e *ReserveError) Error() string

func (*ReserveError) Is

func (e *ReserveError) Is(target error) bool

Is makes errors.Is(err, ErrReserve) true.

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
}

Response is an answer that is not an error of the client's own.

Jump to

Keyboard shortcuts

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