util

package
v0.0.0-...-67cb68b Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 41 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdjustDuration

func AdjustDuration(v *time.Duration, defValue time.Duration)

AdjustDuration adjusts v to default value if v is nil

func AdjustInt

func AdjustInt(v *int, defValue int)

AdjustInt adjusts v to default value if v is nil

func AdjustString

func AdjustString(v *string, defValue string)

AdjustString adjusts v to default value if v is nil

func DefaultIP

func DefaultIP() (ip string, err error)

DefaultIP get a default non local ip, err is not nil, ip return 127.0.0.1

func DefaultListenAddr

func DefaultListenAddr(port int32) string

DefaultListenAddr returns default listen address with appointed port.

func GenFakeBinlog

func GenFakeBinlog(ts int64) *binlog.Binlog

GenFakeBinlog generates a fake binlog from given tso

func GetApproachTS

func GetApproachTS(ts int64, tm time.Time) int64

GetApproachTS get a approach ts by ts and time

func GetParentMetricsRegistry

func GetParentMetricsRegistry() metrics.Registry

GetParentMetricsRegistry get the metrics registry and expose the metrics while /debug/metrics

func GetPdClient

func GetPdClient(etcdURLs string, securityConfig security.Config) (pd.Client, error)

GetPdClient create a PD client

func GetTSO

func GetTSO(pdCli pd.Client) (int64, error)

GetTSO get tso from pd

func InitLogger

func InitLogger(level string, file string) error

InitLogger initializes logger

func IsCreateDatabaseDDL

func IsCreateDatabaseDDL(sql string, sqlMode mysql.SQLMode) bool

IsCreateDatabaseDDL checks whether ddl is a create database statement

func IsValidateListenHost

func IsValidateListenHost(host string) bool

IsValidateListenHost judge the host is validate listen host or not.

func Listen

func Listen(network, addr string, tlsConfig *tls.Config) (listener net.Listener, err error)

Listen return the listener from tls.Listen if tlsConfig is NOT Nil.

func NewSaramaConfig

func NewSaramaConfig(kafkaVersion string, metricsPrefix string) (*sarama.Config, error)

NewSaramaConfig return the default config and set the according version and metrics

func QueryLatestTsFromPD

func QueryLatestTsFromPD(tiStore kv.Storage) (int64, error)

QueryLatestTsFromPD returns the latest ts

func RetryContext

func RetryContext(ctx context.Context, retryCount int, sleepTime time.Duration, backoffFactor int, fn func(context.Context) error) error

RetryContext retries the specified `fn` until it returns no error or the context is canceled, for at most `retryCount` times. The wait time before the `i`th retry is calculated with `sleepTime` * (`backoffFactor` ** i).

func RetryOnError

func RetryOnError(retryCount int, sleepTime time.Duration, errStr string, fn func() error) error

RetryOnError defines a action with retry when fn returns error

func SetupSignalHandler

func SetupSignalHandler(shudownFunc func(sig os.Signal))

SetupSignalHandler setup signal handler

func StrictDecodeFile

func StrictDecodeFile(path, component string, cfg interface{}) error

StrictDecodeFile decodes the toml file strictly. If any item in confFile file is not mapped into the Config struct, issue an error and stop the server from starting.

func TSOToRoughTime

func TSOToRoughTime(ts int64) time.Time

TSOToRoughTime translates tso to rough time that used to display

func ToColumnMap

func ToColumnMap(columns []*model.ColumnInfo) map[int64]*model.ColumnInfo

ToColumnMap return a map index by column id

func ToColumnTypeMap

func ToColumnTypeMap(columns []*model.ColumnInfo) map[int64]*types.FieldType

ToColumnTypeMap return a map index by column id

func TryUntilSuccess

func TryUntilSuccess(ctx context.Context, waitInterval time.Duration, errMsg string, fn func() error) error

TryUntilSuccess retries the given function until error is nil or the context is done, waiting for `waitInterval` time between retries.

func WaitUntilTimeout

func WaitUntilTimeout(name string, fn func(), timeout time.Duration)

WaitUntilTimeout creates a goroutine to run fn, and then gives up waiting for the goroutine to exit When it timeouts

func WriteFileAtomic

func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error

WriteFileAtomic writes file to temp and atomically move when everything else succeeds.

Types

type Duration

type Duration string

Duration is a wrapper of time.Duration for TOML and JSON. it can be parsed to both integer and string integer 7 will be parsed to 7*24h string 10m will be parsed to 10m

func NewDuration

func NewDuration(duration time.Duration) Duration

NewDuration creates a Duration from time.Duration.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON returns the duration as a JSON string.

func (Duration) MarshalText

func (d Duration) MarshalText() ([]byte, error)

MarshalText returns the duration as a JSON string.

func (Duration) ParseDuration

func (d Duration) ParseDuration() (time.Duration, error)

ParseDuration parses gc durations. The default unit is day.

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(text []byte) error

UnmarshalJSON parses a JSON string into the duration.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

UnmarshalText parses a TOML string into the duration.

type Log

type Log struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Log prints log only after a certain amount of time

func NewLog

func NewLog() *Log

NewLog returns a new Log

func (*Log) Add

func (l *Log) Add(label string, interval time.Duration)

Add adds new label

func (*Log) Print

func (l *Log) Print(label string, fn func())

Print executes the fn to print log

type LogHook

type LogHook struct {
	// save the log entrys
	Entrys []zapcore.Entry
	// contains filtered or unexported fields
}

LogHook to get the save entrys for test

func (*LogHook) SetUp

func (h *LogHook) SetUp()

SetUp LogHook

func (*LogHook) TearDown

func (h *LogHook) TearDown()

TearDown set back the origin logger

type MetricClient

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

MetricClient manage the periodic push to the Prometheus Pushgateway.

func NewMetricClient

func NewMetricClient(addr string, interval time.Duration, registry *prometheus.Registry) *MetricClient

NewMetricClient returns a pointer to a MetricClient

func (MetricClient) Start

func (mc MetricClient) Start(ctx context.Context, grouping map[string]string)

Start run a loop of pushing metrics to Prometheus Pushgateway.

type Response

type Response struct {
	Message string      `json:"message"`
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
}

Response represents message that returns to client

func ErrResponsef

func ErrResponsef(format string, args ...interface{}) *Response

ErrResponsef returns a error response.

func NotFoundResponsef

func NotFoundResponsef(format string, args ...interface{}) *Response

NotFoundResponsef returns a not found response.

func SuccessResponse

func SuccessResponse(message string, data interface{}) *Response

SuccessResponse returns a success response.

type StdLogger

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

StdLogger implements samara.StdLogger

func NewStdLogger

func NewStdLogger(prefix string) StdLogger

NewStdLogger return an instance of StdLogger

func (StdLogger) Print

func (l StdLogger) Print(v ...interface{})

Print implements samara.StdLogger

func (StdLogger) Printf

func (l StdLogger) Printf(format string, v ...interface{})

Printf implements samara.StdLogger

func (StdLogger) Println

func (l StdLogger) Println(v ...interface{})

Println implements samara.StdLogger

Jump to

Keyboard shortcuts

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