mirror of
https://github.com/golang/go
synced 2024-11-22 15:54:52 -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
|
||||
// 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) {
|
||||
return dial(context.Background(), dialer, network, addr, config)
|
||||
}
|
||||
@ -224,6 +227,9 @@ type Dialer struct {
|
||||
// handshake, returning the resulting TLS connection.
|
||||
//
|
||||
// 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) {
|
||||
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,
|
||||
// establishing a connection if necessary.
|
||||
//
|
||||
// Ping uses context.Background internally; to specify the context, use
|
||||
// PingContext.
|
||||
func (db *DB) Ping() error {
|
||||
return db.PingContext(context.Background())
|
||||
}
|
||||
@ -1481,6 +1484,9 @@ func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
|
||||
// returned statement.
|
||||
// The caller must call the statement's Close method
|
||||
// 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) {
|
||||
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.
|
||||
// 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) {
|
||||
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.
|
||||
// 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) {
|
||||
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.
|
||||
// Otherwise, the *Row's Scan scans the first selected row and discards
|
||||
// the rest.
|
||||
//
|
||||
// QueryRow uses context.Background internally; to specify the context, use
|
||||
// QueryRowContext.
|
||||
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
||||
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
|
||||
// the driver.
|
||||
//
|
||||
// Begin uses context.Background internally; to specify the context, use
|
||||
// BeginTx.
|
||||
func (db *DB) Begin() (*Tx, error) {
|
||||
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.
|
||||
//
|
||||
// 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) {
|
||||
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
|
||||
// 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 {
|
||||
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.
|
||||
// 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) {
|
||||
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 uses context.Background internally; to specify the context, use
|
||||
// QueryContext.
|
||||
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
||||
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.
|
||||
// Otherwise, the *Row's Scan scans the first selected row and discards
|
||||
// the rest.
|
||||
//
|
||||
// QueryRow uses context.Background internally; to specify the context, use
|
||||
// QueryRowContext.
|
||||
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
||||
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
|
||||
// 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) {
|
||||
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
|
||||
// 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) {
|
||||
return s.QueryContext(context.Background(), args...)
|
||||
}
|
||||
@ -2726,6 +2765,9 @@ func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row {
|
||||
//
|
||||
// var name string
|
||||
// err := nameByUseridStmt.QueryRow(id).Scan(&name)
|
||||
//
|
||||
// QueryRow uses context.Background internally; to specify the context, use
|
||||
// QueryRowContext.
|
||||
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
||||
return s.QueryRowContext(context.Background(), args...)
|
||||
}
|
||||
|
@ -344,6 +344,9 @@ type sysDialer struct {
|
||||
//
|
||||
// See func Dial for a description of the network and address
|
||||
// parameters.
|
||||
//
|
||||
// Dial uses context.Background internally; to specify the context, use
|
||||
// DialContext.
|
||||
func (d *Dialer) Dial(network, address string) (Conn, error) {
|
||||
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
|
||||
// parameters.
|
||||
//
|
||||
// Listen uses context.Background internally; to specify the context, use
|
||||
// ListenConfig.Listen.
|
||||
func Listen(network, address string) (Listener, error) {
|
||||
var lc ListenConfig
|
||||
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
|
||||
// parameters.
|
||||
//
|
||||
// ListenPacket uses context.Background internally; to specify the context, use
|
||||
// ListenConfig.ListenPacket.
|
||||
func ListenPacket(network, address string) (PacketConn, error) {
|
||||
var lc ListenConfig
|
||||
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
|
||||
}
|
||||
|
||||
// NewRequest wraps NewRequestWithContext using the background context.
|
||||
// NewRequest wraps NewRequestWithContext using context.Background.
|
||||
func NewRequest(method, url string, body io.Reader) (*Request, error) {
|
||||
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
|
||||
// of a forward proxy connection.
|
||||
//
|
||||
// Dial uses context.Background internally.
|
||||
//
|
||||
// Deprecated: Use DialContext or DialWithConn instead.
|
||||
func (d *socksDialer) Dial(network, address string) (net.Conn, error) {
|
||||
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.
|
||||
// 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) {
|
||||
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 uses context.Background internally; to specify the context, use
|
||||
// Resolver.LookupPort.
|
||||
func LookupPort(network, service string) (port int, err error) {
|
||||
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
|
||||
// contain DNS "CNAME" records, as long as host resolves to
|
||||
// address records.
|
||||
//
|
||||
// LookupCNAME uses context.Background internally; to specify the context, use
|
||||
// Resolver.LookupCNAME.
|
||||
func LookupCNAME(host string) (cname string, err error) {
|
||||
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 uses context.Background internally; to specify the context, use
|
||||
// Resolver.LookupMX.
|
||||
func LookupMX(name string) ([]*MX, error) {
|
||||
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 uses context.Background internally; to specify the context, use
|
||||
// Resolver.LookupNS.
|
||||
func LookupNS(name string) ([]*NS, error) {
|
||||
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 uses context.Background internally; to specify the context, use
|
||||
// Resolver.LookupTXT.
|
||||
func LookupTXT(name string) ([]string, error) {
|
||||
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
|
||||
// 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) {
|
||||
return DefaultResolver.lookupAddr(context.Background(), addr)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user