mirror of
https://github.com/golang/go
synced 2024-11-23 00:20:12 -07:00
docs: clarify when APIs use context.Background.
The Go standard library retrofitted context support onto existing APIs using context.Background and later offered variants that directly supported user-defined context value specification. This commit makes that behavior clear in documentation and suggests context-aware alternatives if the user is looking for one. An example motivation is supporting code for use in systems that expect APIs to be cancelable for lifecycle correctness or load shedding/management reasons, as alluded to in https://blog.golang.org/context-and-structs. Updates #44143 Change-Id: I2d7f954ddf9b48264d5ebc8d0007058ff9bddf14 Reviewed-on: https://go-review.googlesource.com/c/go/+/296152 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Jean de Klerk <deklerk@google.com> Trust: Jean de Klerk <deklerk@google.com> Run-TryBot: Jean de Klerk <deklerk@google.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
b8e9ec856c
commit
0fc370c5d2
@ -111,6 +111,9 @@ func (timeoutError) Temporary() bool { return true }
|
|||||||
//
|
//
|
||||||
// DialWithDialer interprets a nil configuration as equivalent to the zero
|
// DialWithDialer interprets a nil configuration as equivalent to the zero
|
||||||
// configuration; see the documentation of Config for the defaults.
|
// configuration; see the documentation of Config for the defaults.
|
||||||
|
//
|
||||||
|
// DialWithDialer uses context.Background internally; to specify the context,
|
||||||
|
// use Dialer.DialContext with NetDialer set to the desired dialer.
|
||||||
func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
|
func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
|
||||||
return dial(context.Background(), dialer, network, addr, config)
|
return dial(context.Background(), dialer, network, addr, config)
|
||||||
}
|
}
|
||||||
@ -224,6 +227,9 @@ type Dialer struct {
|
|||||||
// handshake, returning the resulting TLS connection.
|
// handshake, returning the resulting TLS connection.
|
||||||
//
|
//
|
||||||
// The returned Conn, if any, will always be of type *Conn.
|
// The returned Conn, if any, will always be of type *Conn.
|
||||||
|
//
|
||||||
|
// Dial uses context.Background internally; to specify the context,
|
||||||
|
// use DialContext.
|
||||||
func (d *Dialer) Dial(network, addr string) (net.Conn, error) {
|
func (d *Dialer) Dial(network, addr string) (net.Conn, error) {
|
||||||
return d.DialContext(context.Background(), network, addr)
|
return d.DialContext(context.Background(), network, addr)
|
||||||
}
|
}
|
||||||
|
@ -813,6 +813,9 @@ func (db *DB) PingContext(ctx context.Context) error {
|
|||||||
|
|
||||||
// Ping verifies a connection to the database is still alive,
|
// Ping verifies a connection to the database is still alive,
|
||||||
// establishing a connection if necessary.
|
// establishing a connection if necessary.
|
||||||
|
//
|
||||||
|
// Ping uses context.Background internally; to specify the context, use
|
||||||
|
// PingContext.
|
||||||
func (db *DB) Ping() error {
|
func (db *DB) Ping() error {
|
||||||
return db.PingContext(context.Background())
|
return db.PingContext(context.Background())
|
||||||
}
|
}
|
||||||
@ -1481,6 +1484,9 @@ func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
|
|||||||
// returned statement.
|
// returned statement.
|
||||||
// The caller must call the statement's Close method
|
// The caller must call the statement's Close method
|
||||||
// when the statement is no longer needed.
|
// when the statement is no longer needed.
|
||||||
|
//
|
||||||
|
// Prepare uses context.Background internally; to specify the context, use
|
||||||
|
// PrepareContext.
|
||||||
func (db *DB) Prepare(query string) (*Stmt, error) {
|
func (db *DB) Prepare(query string) (*Stmt, error) {
|
||||||
return db.PrepareContext(context.Background(), query)
|
return db.PrepareContext(context.Background(), query)
|
||||||
}
|
}
|
||||||
@ -1551,6 +1557,9 @@ func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}
|
|||||||
|
|
||||||
// Exec executes a query without returning any rows.
|
// Exec executes a query without returning any rows.
|
||||||
// The args are for any placeholder parameters in the query.
|
// The args are for any placeholder parameters in the query.
|
||||||
|
//
|
||||||
|
// Exec uses context.Background internally; to specify the context, use
|
||||||
|
// ExecContext.
|
||||||
func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
|
func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
|
||||||
return db.ExecContext(context.Background(), query, args...)
|
return db.ExecContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -1621,6 +1630,9 @@ func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{
|
|||||||
|
|
||||||
// Query executes a query that returns rows, typically a SELECT.
|
// Query executes a query that returns rows, typically a SELECT.
|
||||||
// The args are for any placeholder parameters in the query.
|
// The args are for any placeholder parameters in the query.
|
||||||
|
//
|
||||||
|
// Query uses context.Background internally; to specify the context, use
|
||||||
|
// QueryContext.
|
||||||
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
||||||
return db.QueryContext(context.Background(), query, args...)
|
return db.QueryContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -1719,6 +1731,9 @@ func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interfa
|
|||||||
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
|
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
|
||||||
// Otherwise, the *Row's Scan scans the first selected row and discards
|
// Otherwise, the *Row's Scan scans the first selected row and discards
|
||||||
// the rest.
|
// the rest.
|
||||||
|
//
|
||||||
|
// QueryRow uses context.Background internally; to specify the context, use
|
||||||
|
// QueryRowContext.
|
||||||
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
||||||
return db.QueryRowContext(context.Background(), query, args...)
|
return db.QueryRowContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -1750,6 +1765,9 @@ func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
|
|||||||
|
|
||||||
// Begin starts a transaction. The default isolation level is dependent on
|
// Begin starts a transaction. The default isolation level is dependent on
|
||||||
// the driver.
|
// the driver.
|
||||||
|
//
|
||||||
|
// Begin uses context.Background internally; to specify the context, use
|
||||||
|
// BeginTx.
|
||||||
func (db *DB) Begin() (*Tx, error) {
|
func (db *DB) Begin() (*Tx, error) {
|
||||||
return db.BeginTx(context.Background(), nil)
|
return db.BeginTx(context.Background(), nil)
|
||||||
}
|
}
|
||||||
@ -2255,6 +2273,9 @@ func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
|
|||||||
// be used once the transaction has been committed or rolled back.
|
// be used once the transaction has been committed or rolled back.
|
||||||
//
|
//
|
||||||
// To use an existing prepared statement on this transaction, see Tx.Stmt.
|
// To use an existing prepared statement on this transaction, see Tx.Stmt.
|
||||||
|
//
|
||||||
|
// Prepare uses context.Background internally; to specify the context, use
|
||||||
|
// PrepareContext.
|
||||||
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
||||||
return tx.PrepareContext(context.Background(), query)
|
return tx.PrepareContext(context.Background(), query)
|
||||||
}
|
}
|
||||||
@ -2358,6 +2379,9 @@ func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
|
|||||||
//
|
//
|
||||||
// The returned statement operates within the transaction and will be closed
|
// The returned statement operates within the transaction and will be closed
|
||||||
// when the transaction has been committed or rolled back.
|
// when the transaction has been committed or rolled back.
|
||||||
|
//
|
||||||
|
// Stmt uses context.Background internally; to specify the context, use
|
||||||
|
// StmtContext.
|
||||||
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
||||||
return tx.StmtContext(context.Background(), stmt)
|
return tx.StmtContext(context.Background(), stmt)
|
||||||
}
|
}
|
||||||
@ -2374,6 +2398,9 @@ func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}
|
|||||||
|
|
||||||
// Exec executes a query that doesn't return rows.
|
// Exec executes a query that doesn't return rows.
|
||||||
// For example: an INSERT and UPDATE.
|
// For example: an INSERT and UPDATE.
|
||||||
|
//
|
||||||
|
// Exec uses context.Background internally; to specify the context, use
|
||||||
|
// ExecContext.
|
||||||
func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
|
func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
|
||||||
return tx.ExecContext(context.Background(), query, args...)
|
return tx.ExecContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -2389,6 +2416,9 @@ func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Query executes a query that returns rows, typically a SELECT.
|
// Query executes a query that returns rows, typically a SELECT.
|
||||||
|
//
|
||||||
|
// Query uses context.Background internally; to specify the context, use
|
||||||
|
// QueryContext.
|
||||||
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
||||||
return tx.QueryContext(context.Background(), query, args...)
|
return tx.QueryContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -2410,6 +2440,9 @@ func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interfa
|
|||||||
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
|
// If the query selects no rows, the *Row's Scan will return ErrNoRows.
|
||||||
// Otherwise, the *Row's Scan scans the first selected row and discards
|
// Otherwise, the *Row's Scan scans the first selected row and discards
|
||||||
// the rest.
|
// the rest.
|
||||||
|
//
|
||||||
|
// QueryRow uses context.Background internally; to specify the context, use
|
||||||
|
// QueryRowContext.
|
||||||
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
||||||
return tx.QueryRowContext(context.Background(), query, args...)
|
return tx.QueryRowContext(context.Background(), query, args...)
|
||||||
}
|
}
|
||||||
@ -2516,6 +2549,9 @@ func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, er
|
|||||||
|
|
||||||
// Exec executes a prepared statement with the given arguments and
|
// Exec executes a prepared statement with the given arguments and
|
||||||
// returns a Result summarizing the effect of the statement.
|
// returns a Result summarizing the effect of the statement.
|
||||||
|
//
|
||||||
|
// Exec uses context.Background internally; to specify the context, use
|
||||||
|
// ExecContext.
|
||||||
func (s *Stmt) Exec(args ...interface{}) (Result, error) {
|
func (s *Stmt) Exec(args ...interface{}) (Result, error) {
|
||||||
return s.ExecContext(context.Background(), args...)
|
return s.ExecContext(context.Background(), args...)
|
||||||
}
|
}
|
||||||
@ -2687,6 +2723,9 @@ func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, er
|
|||||||
|
|
||||||
// Query executes a prepared query statement with the given arguments
|
// Query executes a prepared query statement with the given arguments
|
||||||
// and returns the query results as a *Rows.
|
// and returns the query results as a *Rows.
|
||||||
|
//
|
||||||
|
// Query uses context.Background internally; to specify the context, use
|
||||||
|
// QueryContext.
|
||||||
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
||||||
return s.QueryContext(context.Background(), args...)
|
return s.QueryContext(context.Background(), args...)
|
||||||
}
|
}
|
||||||
@ -2726,6 +2765,9 @@ func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row {
|
|||||||
//
|
//
|
||||||
// var name string
|
// var name string
|
||||||
// err := nameByUseridStmt.QueryRow(id).Scan(&name)
|
// err := nameByUseridStmt.QueryRow(id).Scan(&name)
|
||||||
|
//
|
||||||
|
// QueryRow uses context.Background internally; to specify the context, use
|
||||||
|
// QueryRowContext.
|
||||||
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
||||||
return s.QueryRowContext(context.Background(), args...)
|
return s.QueryRowContext(context.Background(), args...)
|
||||||
}
|
}
|
||||||
|
@ -344,6 +344,9 @@ type sysDialer struct {
|
|||||||
//
|
//
|
||||||
// See func Dial for a description of the network and address
|
// See func Dial for a description of the network and address
|
||||||
// parameters.
|
// parameters.
|
||||||
|
//
|
||||||
|
// Dial uses context.Background internally; to specify the context, use
|
||||||
|
// DialContext.
|
||||||
func (d *Dialer) Dial(network, address string) (Conn, error) {
|
func (d *Dialer) Dial(network, address string) (Conn, error) {
|
||||||
return d.DialContext(context.Background(), network, address)
|
return d.DialContext(context.Background(), network, address)
|
||||||
}
|
}
|
||||||
@ -701,6 +704,9 @@ type sysListener struct {
|
|||||||
//
|
//
|
||||||
// See func Dial for a description of the network and address
|
// See func Dial for a description of the network and address
|
||||||
// parameters.
|
// parameters.
|
||||||
|
//
|
||||||
|
// Listen uses context.Background internally; to specify the context, use
|
||||||
|
// ListenConfig.Listen.
|
||||||
func Listen(network, address string) (Listener, error) {
|
func Listen(network, address string) (Listener, error) {
|
||||||
var lc ListenConfig
|
var lc ListenConfig
|
||||||
return lc.Listen(context.Background(), network, address)
|
return lc.Listen(context.Background(), network, address)
|
||||||
@ -728,6 +734,9 @@ func Listen(network, address string) (Listener, error) {
|
|||||||
//
|
//
|
||||||
// See func Dial for a description of the network and address
|
// See func Dial for a description of the network and address
|
||||||
// parameters.
|
// parameters.
|
||||||
|
//
|
||||||
|
// ListenPacket uses context.Background internally; to specify the context, use
|
||||||
|
// ListenConfig.ListenPacket.
|
||||||
func ListenPacket(network, address string) (PacketConn, error) {
|
func ListenPacket(network, address string) (PacketConn, error) {
|
||||||
var lc ListenConfig
|
var lc ListenConfig
|
||||||
return lc.ListenPacket(context.Background(), network, address)
|
return lc.ListenPacket(context.Background(), network, address)
|
||||||
|
@ -823,7 +823,7 @@ func validMethod(method string) bool {
|
|||||||
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
|
return len(method) > 0 && strings.IndexFunc(method, isNotToken) == -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRequest wraps NewRequestWithContext using the background context.
|
// NewRequest wraps NewRequestWithContext using context.Background.
|
||||||
func NewRequest(method, url string, body io.Reader) (*Request, error) {
|
func NewRequest(method, url string, body io.Reader) (*Request, error) {
|
||||||
return NewRequestWithContext(context.Background(), method, url, body)
|
return NewRequestWithContext(context.Background(), method, url, body)
|
||||||
}
|
}
|
||||||
|
@ -362,6 +362,8 @@ func (d *socksDialer) DialWithConn(ctx context.Context, c net.Conn, network, add
|
|||||||
// Unlike DialContext, it returns a raw transport connection instead
|
// Unlike DialContext, it returns a raw transport connection instead
|
||||||
// of a forward proxy connection.
|
// of a forward proxy connection.
|
||||||
//
|
//
|
||||||
|
// Dial uses context.Background internally.
|
||||||
|
//
|
||||||
// Deprecated: Use DialContext or DialWithConn instead.
|
// Deprecated: Use DialContext or DialWithConn instead.
|
||||||
func (d *socksDialer) Dial(network, address string) (net.Conn, error) {
|
func (d *socksDialer) Dial(network, address string) (net.Conn, error) {
|
||||||
if err := d.validateTarget(network, address); err != nil {
|
if err := d.validateTarget(network, address); err != nil {
|
||||||
|
@ -166,6 +166,9 @@ func (r *Resolver) getLookupGroup() *singleflight.Group {
|
|||||||
|
|
||||||
// LookupHost looks up the given host using the local resolver.
|
// LookupHost looks up the given host using the local resolver.
|
||||||
// It returns a slice of that host's addresses.
|
// It returns a slice of that host's addresses.
|
||||||
|
//
|
||||||
|
// LookupHost uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupHost.
|
||||||
func LookupHost(host string) (addrs []string, err error) {
|
func LookupHost(host string) (addrs []string, err error) {
|
||||||
return DefaultResolver.LookupHost(context.Background(), host)
|
return DefaultResolver.LookupHost(context.Background(), host)
|
||||||
}
|
}
|
||||||
@ -353,6 +356,9 @@ func ipAddrsEface(addrs []IPAddr) []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LookupPort looks up the port for the given network and service.
|
// LookupPort looks up the port for the given network and service.
|
||||||
|
//
|
||||||
|
// LookupPort uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupPort.
|
||||||
func LookupPort(network, service string) (port int, err error) {
|
func LookupPort(network, service string) (port int, err error) {
|
||||||
return DefaultResolver.LookupPort(context.Background(), network, service)
|
return DefaultResolver.LookupPort(context.Background(), network, service)
|
||||||
}
|
}
|
||||||
@ -389,6 +395,9 @@ func (r *Resolver) LookupPort(ctx context.Context, network, service string) (por
|
|||||||
// LookupCNAME does not return an error if host does not
|
// LookupCNAME does not return an error if host does not
|
||||||
// contain DNS "CNAME" records, as long as host resolves to
|
// contain DNS "CNAME" records, as long as host resolves to
|
||||||
// address records.
|
// address records.
|
||||||
|
//
|
||||||
|
// LookupCNAME uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupCNAME.
|
||||||
func LookupCNAME(host string) (cname string, err error) {
|
func LookupCNAME(host string) (cname string, err error) {
|
||||||
return DefaultResolver.lookupCNAME(context.Background(), host)
|
return DefaultResolver.lookupCNAME(context.Background(), host)
|
||||||
}
|
}
|
||||||
@ -434,6 +443,9 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
|
// LookupMX returns the DNS MX records for the given domain name sorted by preference.
|
||||||
|
//
|
||||||
|
// LookupMX uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupMX.
|
||||||
func LookupMX(name string) ([]*MX, error) {
|
func LookupMX(name string) ([]*MX, error) {
|
||||||
return DefaultResolver.lookupMX(context.Background(), name)
|
return DefaultResolver.lookupMX(context.Background(), name)
|
||||||
}
|
}
|
||||||
@ -444,6 +456,9 @@ func (r *Resolver) LookupMX(ctx context.Context, name string) ([]*MX, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LookupNS returns the DNS NS records for the given domain name.
|
// LookupNS returns the DNS NS records for the given domain name.
|
||||||
|
//
|
||||||
|
// LookupNS uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupNS.
|
||||||
func LookupNS(name string) ([]*NS, error) {
|
func LookupNS(name string) ([]*NS, error) {
|
||||||
return DefaultResolver.lookupNS(context.Background(), name)
|
return DefaultResolver.lookupNS(context.Background(), name)
|
||||||
}
|
}
|
||||||
@ -454,6 +469,9 @@ func (r *Resolver) LookupNS(ctx context.Context, name string) ([]*NS, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LookupTXT returns the DNS TXT records for the given domain name.
|
// LookupTXT returns the DNS TXT records for the given domain name.
|
||||||
|
//
|
||||||
|
// LookupTXT uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupTXT.
|
||||||
func LookupTXT(name string) ([]string, error) {
|
func LookupTXT(name string) ([]string, error) {
|
||||||
return DefaultResolver.lookupTXT(context.Background(), name)
|
return DefaultResolver.lookupTXT(context.Background(), name)
|
||||||
}
|
}
|
||||||
@ -468,6 +486,9 @@ func (r *Resolver) LookupTXT(ctx context.Context, name string) ([]string, error)
|
|||||||
//
|
//
|
||||||
// When using the host C library resolver, at most one result will be
|
// When using the host C library resolver, at most one result will be
|
||||||
// returned. To bypass the host resolver, use a custom Resolver.
|
// returned. To bypass the host resolver, use a custom Resolver.
|
||||||
|
//
|
||||||
|
// LookupAddr uses context.Background internally; to specify the context, use
|
||||||
|
// Resolver.LookupAddr.
|
||||||
func LookupAddr(addr string) (names []string, err error) {
|
func LookupAddr(addr string) (names []string, err error) {
|
||||||
return DefaultResolver.lookupAddr(context.Background(), addr)
|
return DefaultResolver.lookupAddr(context.Background(), addr)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user