common

package
v5.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: Apache-2.0 Imports: 34 Imported by: 135

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSON

func GetJSON(ctx context.Context, client *http.Client, url string, v interface{}) error

GetJSON fetches a page and parses it as JSON. The parsed result will be stored into the `v`. The variable `v` must be a pointer to a type that can be unmarshalled from JSON.

Example:

client := &http.Client{}
var resp struct { IP string }
if err := util.GetJSON(client, "http://api.ipify.org/?format=json", &resp); err != nil {
	return errors.Trace(err)
}
fmt.Println(resp.IP)

func InterpolateMySQLString

func InterpolateMySQLString(s string) string

func IsContextCanceledError

func IsContextCanceledError(err error) bool

IsContextCanceledError returns whether the error is caused by context cancellation. This function should only be used when the code logic is affected by whether the error is canceling or not.

This function returns `false` (not a context-canceled error) if `err == nil`.

func IsDirExists

func IsDirExists(name string) bool

IsDirExists checks if dir exists.

func IsEmptyDir

func IsEmptyDir(name string) bool

IsEmptyDir checks if dir is empty.

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError returns whether the error is transient (e.g. network connection dropped) or irrecoverable (e.g. user pressing Ctrl+C). This function returns `false` (irrecoverable) if `err == nil`.

If the error is a multierr, returns true only if all suberrors are retryable.

func KillMySelf

func KillMySelf() error

KillMySelf sends sigint to current process, used in integration test only

Only works on Unix. Signaling on Windows is not supported.

func Retry

func Retry(purpose string, parentLogger log.Logger, action func() error) error

Retry is shared by SQLWithRetry.perform, implementation of GlueCheckpointsDB and TiDB's glue implementation

func StringSliceEqual

func StringSliceEqual(a, b []string) bool

StringSliceEqual checks if two string slices are equal.

func TableHasAutoRowID

func TableHasAutoRowID(info *model.TableInfo) bool

TableHasAutoRowID return whether table has auto generated row id

func ToTLSConfig

func ToTLSConfig(caPath, certPath, keyPath string) (*tls.Config, error)

ToTLSConfig constructs a `*tls.Config` from the CA, certification and key paths.

If the CA path is empty, returns nil.

func UniqueTable

func UniqueTable(schema string, table string) string

UniqueTable returns an unique table name.

func WriteMySQLIdentifier

func WriteMySQLIdentifier(builder *strings.Builder, identifier string)

Writes a MySQL identifier into the string builder. The identifier is always escaped into the form "`foo`".

Types

type KvPair

type KvPair struct {
	// Key is the key of the KV pair
	Key []byte
	// Val is the value of the KV pair
	Val []byte
}

KvPair is a pair of key and value.

type MySQLConnectParam

type MySQLConnectParam struct {
	Host             string
	Port             int
	User             string
	Password         string
	SQLMode          string
	MaxAllowedPacket uint64
	TLS              string
	Vars             map[string]string
}

MySQLConnectParam records the parameters needed to connect to a MySQL database.

func (*MySQLConnectParam) Connect

func (param *MySQLConnectParam) Connect() (*sql.DB, error)

func (*MySQLConnectParam) ToDSN

func (param *MySQLConnectParam) ToDSN() string

type OnceError

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

OnceError is an error value which will can be assigned once.

The zero value is ready for use.

func (*OnceError) Get

func (oe *OnceError) Get() error

Get returns the first error value stored in this instance.

func (*OnceError) Set

func (oe *OnceError) Set(e error)

Set assigns an error to this instance, if `e != nil`.

If this method is called multiple times, only the first call is effective.

type Pauser

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

Pauser is a type which could allow multiple goroutines to wait on demand, similar to a gate or traffic light.

func NewPauser

func NewPauser() *Pauser

NewPauser returns an initialized pauser.

func (*Pauser) IsPaused

func (p *Pauser) IsPaused() bool

IsPaused gets whether the current state is paused or not.

func (*Pauser) Pause

func (p *Pauser) Pause()

Pause causes all calls to Wait() to block.

func (*Pauser) Resume

func (p *Pauser) Resume()

Resume causes all calls to Wait() to continue.

func (*Pauser) Wait

func (p *Pauser) Wait(ctx context.Context) error

Wait blocks the current goroutine if the current state is paused, until the pauser itself is resumed at least once.

If `ctx` is done, this method will also unblock immediately, and return the context error.

type SQLWithRetry

type SQLWithRetry struct {
	DB           *sql.DB
	Logger       log.Logger
	HideQueryLog bool
}

SQLWithRetry constructs a retryable transaction.

func (SQLWithRetry) Exec

func (t SQLWithRetry) Exec(ctx context.Context, purpose string, query string, args ...interface{}) error

Exec executes a single SQL with optional retry.

func (SQLWithRetry) QueryRow

func (t SQLWithRetry) QueryRow(ctx context.Context, purpose string, query string, dest ...interface{}) error

func (SQLWithRetry) Transact

func (t SQLWithRetry) Transact(ctx context.Context, purpose string, action func(context.Context, *sql.Tx) error) error

Transact executes an action in a transaction, and retry if the action failed with a retryable error.

type StorageSize

type StorageSize struct {
	Capacity  uint64
	Available uint64
}

StorageSize represents the storage's capacity and available size Learn from tidb-binlog source code.

func GetStorageSize

func GetStorageSize(dir string) (size StorageSize, err error)

GetStorageSize gets storage's capacity and available size

type TLS

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

TLS

func NewTLS

func NewTLS(caPath, certPath, keyPath, host string) (*TLS, error)

NewTLS constructs a new HTTP client with TLS configured with the CA, certificate and key paths.

If the CA path is empty, returns an instance where TLS is disabled.

func NewTLSFromMockServer

func NewTLSFromMockServer(server *httptest.Server) *TLS

NewTLSFromMockServer constructs a new TLS instance from the certificates of an *httptest.Server.

func (*TLS) GetJSON

func (tc *TLS) GetJSON(ctx context.Context, path string, v interface{}) error

func (*TLS) TLSConfig

func (tc *TLS) TLSConfig() *tls.Config

func (*TLS) ToGRPCDialOption

func (tc *TLS) ToGRPCDialOption() grpc.DialOption

ToGRPCDialOption constructs a gRPC dial option.

func (*TLS) ToPDSecurityOption

func (tc *TLS) ToPDSecurityOption() pd.SecurityOption

func (*TLS) WithHost

func (tc *TLS) WithHost(host string) *TLS

WithHost creates a new TLS instance with the host replaced.

func (*TLS) WrapListener

func (tc *TLS) WrapListener(l net.Listener) net.Listener

WrapListener places a TLS layer on top of the existing listener.

Jump to

Keyboard shortcuts

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