utils

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 14 Imported by: 8

Documentation

Index

Constants

View Source
const (
	CONN_PREPARE                        = "Conn.Prepare"
	CONN_PREPARE_CONTEXT                = "Conn.PrepareContext"
	CONN_CLOSE                          = "Conn.Close"
	CONN_BEGIN                          = "Conn.Begin"
	CONN_BEGIN_TX                       = "Conn.BeginTx"
	CONN_QUERY_CONTEXT                  = "Conn.QueryContext"
	CONN_EXEC_CONTEXT                   = "Conn.ExecContext"
	CONN_PING                           = "Conn.Ping"
	CONN_IS_VALID                       = "Conn.IsValid"
	CONN_RESET_SESSION                  = "Conn.ResetSession"
	CONN_CHECK_NAMED_VALUE              = "Conn.CheckNamedValue"
	STMT_CLOSE                          = "Stmt.Close"
	STMT_EXEC                           = "Stmt.Exec"
	STMT_EXEC_CONTEXT                   = "Stmt.ExecContext"
	STMT_NUM_INPUT                      = "Stmt.NumInput"
	STMT_QUERY                          = "Stmt.Query"
	STMT_QUERY_CONTEXT                  = "Stmt.QueryContext"
	STMT_CHECK_NAMED_VALUE              = "Stmt.CheckNamedValue"
	RESULT_LAST_INSERT_ID               = "Result.LastInsertId"
	RESULT_ROWS_AFFECTED                = "Result.RowsAffected"
	TX_COMMIT                           = "Tx.Commit"
	TX_ROLLBACK                         = "Tx.Rollback"
	ROWS_CLOSE                          = "Rows.Close"
	ROWS_COLUMNS                        = "Rows.Columns"
	ROWS_NEXT                           = "Rows.Next"
	ROWS_COLUMN_TYPE_PRECISION_SCALE    = "Rows.ColumnTypePrecisionScale"
	ROWS_COLUMN_TYPE_DATABASE_TYPE_NAME = "Rows.ColumnTypeDatabaseTypeName"
	ROWS_COLUMN_TYPE_LENGTH             = "Rows.ColumnTypeLength"
	ROWS_HAS_NEXT_RESULT_SET            = "Rows.HasNextResultSet"
	ROWS_NEXT_RESULT_SET                = "Rows.NextResultSet"
	ROWS_COLUMN_TYPE_SCAN_TYPE          = "Rows.ColumnTypeScanType"
	ROWS_COLUMN_TYPE_NULLABLE           = "Rows.ColumnTypeNullable"
)
View Source
const (
	INSTANCE_GROUP = "instance"
	DNS_GROUP      = "dns"
	DOMAIN_GROUP   = "domain"
	REGION_GROUP   = "region"
)
View Source
const SET_AUTOCOMMIT_0 = "set autocommit = 0"

Variables

View Source
var (
	AURORA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.rds\\.amazonaws\\.com\\.?)$")

	AURORA_CHINA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.rds\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.amazonaws\\.com\\.cn\\.?)$")

	AURORA_OLD_CHINA_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?P<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.rds\\.amazonaws\\.com\\.cn\\.?)$")

	AURORA_GOV_DNS_PATTERN = regexp.MustCompile(
		"(?i)^(?<instance>.+)\\." +
			"(?P<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?" +
			"(?P<domain>[a-zA-Z0-9]+\\.rds\\.(?P<region>[a-zA-Z0-9\\-]+)" +
			"\\.(amazonaws\\.com\\.?|c2s\\.ic\\.gov\\.?|sc2s\\.sgov\\.gov\\.?))$")

	IP_V4_REGEXP = regexp.MustCompile(
		"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){1}" +
			"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){2}" +
			"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")

	IP_V6_REGEXP = regexp.MustCompile("^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$")

	IP_V6_COMPRESSED_REGEXP = regexp.MustCompile("^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" +
		"::(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$")

	BG_OLD_HOST_PATTERN     = regexp.MustCompile("(?i).*(?P<prefix>-old1\\.)..*") //nolint:all
	BG_GREEN_HOSTID_PATTERN = regexp.MustCompile("(?i)(.*)-green-[0-9a-z]{6}")
	BG_GREEN_HOST_PATTERN   = regexp.MustCompile("(?i).*(?P<prefix>-green-[0-9a-z]{6})..*")
)
View Source
var CleanupIntervalNanos time.Duration = 10 * time.Minute

Functions

func AllKeys

func AllKeys[T comparable, V any](mapOfKeysAndValues map[T]V) []T

func CombineMaps

func CombineMaps[K comparable, V any](mapToCopy map[K]V, mapToAdd map[K]V) map[K]V

func ConvertDriverValueToString

func ConvertDriverValueToString(value driver.Value) (string, bool)

func CreateMapCopy

func CreateMapCopy[K comparable, V any](mapToCopy map[K]V) map[K]V

func DoesCloseTransaction

func DoesCloseTransaction(methodName string, methodArgs ...any) bool

func DoesOpenTransaction

func DoesOpenTransaction(methodName string, methodArgs ...any) bool

func DoesSetReadOnly added in v1.1.0

func DoesSetReadOnly(query string, dialectFunc func(string) (bool, bool)) (bool, bool)

func ExecQueryDirectly

func ExecQueryDirectly(conn driver.Conn, query string) error

ExecQueryDirectly Directly executes query on conn.

func FilterMapFindFirstValue added in v1.1.0

func FilterMapFindFirstValue[T comparable, U any](set map[T]U, filter func(U) bool) U

func FilterSetFindFirst added in v1.1.0

func FilterSetFindFirst[T comparable, U any](set map[T]U, filter func(T) bool) T

func FilterSlice

func FilterSlice[T any](slice []T, filter func(T) bool) []T

func FilterSliceFindFirst added in v1.1.0

func FilterSliceFindFirst[T any](slice []T, filter func(T) bool) T

func FindHostInTopology

func FindHostInTopology(hosts []*host_info_util.HostInfo, hostNames ...string) *host_info_util.HostInfo

func FindRegisteredDriver

func FindRegisteredDriver(dialectCode string) bool

func GetFirstRowFromQuery

func GetFirstRowFromQuery(conn driver.Conn, query string) []driver.Value

func GetFirstRowFromQueryAsString

func GetFirstRowFromQueryAsString(conn driver.Conn, query string) []string

GetFirstRowFromQueryAsString Directly executes query on conn and converts all possible values in the first row to strings. Any values that cannot be converted are returned as "". Returns nil if unable to obtain a row.

func GetHostNameFromEndpoint

func GetHostNameFromEndpoint(endpoint string) string

func GetPreparedHost

func GetPreparedHost(host string) string

func GetQueryFromSqlOrMethodArgs

func GetQueryFromSqlOrMethodArgs(sql string, methodArgs ...any) string

func GetRdsClusterHostUrl

func GetRdsClusterHostUrl(host string) string

func GetRdsClusterId added in v1.1.0

func GetRdsClusterId(host string) string

func GetRdsInstanceHostPattern

func GetRdsInstanceHostPattern(host string) string

func GetRdsRegion

func GetRdsRegion(host string) string

func GetRowsFromQuery added in v1.1.0

func GetRowsFromQuery(conn driver.Conn, query string, n int) [][]driver.Value

GetRowsFromQuery Directly executes query on conn, and returns the first n rows.

func GetSeparateSqlStatements

func GetSeparateSqlStatements(query string) []string

func GetSetReadOnlyFromCtx added in v1.1.0

func GetSetReadOnlyFromCtx(ctx context.Context) (bool, bool)

func GetStructName

func GetStructName(v any) string

func IndexOf

func IndexOf[T any](slice []T, item T, compareFunc func(T, T) bool) int

func IsGreenInstance added in v1.1.0

func IsGreenInstance(host string) bool

func IsIP added in v1.1.0

func IsIP(host string) bool

func IsIPV6

func IsIPV6(host string) bool

func IsIPv4

func IsIPv4(host string) bool

func IsLimitlessDbShardGroupDns

func IsLimitlessDbShardGroupDns(host string) bool

func IsNotGreenAndNotOldInstance added in v1.1.0

func IsNotGreenAndNotOldInstance(host string) bool

IsNotGreenAndNotOldInstance Verify host contains neither green prefix nor old prefix.

func IsNotOldInstance added in v1.1.0

func IsNotOldInstance(host string) bool

func IsRdsClusterDns

func IsRdsClusterDns(host string) bool

func IsRdsCustomClusterDns

func IsRdsCustomClusterDns(host string) bool

func IsRdsDns

func IsRdsDns(host string) bool

func IsRdsInstance added in v1.1.0

func IsRdsInstance(host string) bool

func IsRdsProxyDns

func IsRdsProxyDns(host string) bool

func IsReachable

func IsReachable(conn driver.Conn, ctx context.Context) bool

func IsReaderClusterDns

func IsReaderClusterDns(host string) bool

func IsWriterClusterDns

func IsWriterClusterDns(host string) bool

func LengthOfSyncMap

func LengthOfSyncMap(syncMap *sync.Map) int

func LogTopology

func LogTopology(hosts []*host_info_util.HostInfo, msgPrefix string) string

func MySqlConvertValToString added in v1.1.0

func MySqlConvertValToString(value driver.Value) (string, bool)

func PgConvertValToString added in v1.1.0

func PgConvertValToString(value driver.Value) (string, bool)

func RemoveFromSlice

func RemoveFromSlice[T any](slice []T, item T, compareFunc func(T, T) bool) []T

func RemoveGreenInstancePrefix added in v1.1.0

func RemoveGreenInstancePrefix(host string) string

func RemovePort added in v1.2.0

func RemovePort(hostAndPort string) string

func ResetPreparedHostFunc

func ResetPreparedHostFunc()

func Rollback

func Rollback(conn driver.Conn, currentTx driver.Tx)

func SetPreparedHostFunc

func SetPreparedHostFunc(newPrepareHostFunc func(string) string)

func SliceAndMapHaveCommonElement

func SliceAndMapHaveCommonElement[T comparable, V any](slice []T, mapOfKeysAndValues map[T]V) bool

Types

type CacheMap

type CacheMap[T any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[T any]() *CacheMap[T]

func (*CacheMap[T]) CleanUp

func (c *CacheMap[T]) CleanUp()

func (*CacheMap[T]) Clear

func (c *CacheMap[T]) Clear()

func (*CacheMap[T]) ComputeIfAbsent

func (c *CacheMap[T]) ComputeIfAbsent(key string, computeFunc func() T, itemExpiration time.Duration) T

func (*CacheMap[T]) Get

func (c *CacheMap[T]) Get(key string) (T, bool)

func (*CacheMap[T]) GetAllEntries

func (c *CacheMap[T]) GetAllEntries() map[string]T

Get a map copy of all entries in the cache, including expired entries.

func (*CacheMap[T]) Put

func (c *CacheMap[T]) Put(key string, value T, expiration time.Duration)

func (*CacheMap[T]) PutIfAbsent

func (c *CacheMap[T]) PutIfAbsent(key string, value T, expiration time.Duration)

func (*CacheMap[T]) Remove

func (c *CacheMap[T]) Remove(key string)

func (*CacheMap[T]) Size

func (c *CacheMap[T]) Size() int

type DisposalFunc

type DisposalFunc[T any] func(T) bool

type Pair added in v1.1.0

type Pair[T any, U any] struct {
	// contains filtered or unexported fields
}

func NewPair added in v1.1.0

func NewPair[T any, U any](t T, u U) Pair[T, U]

func (*Pair[T, U]) GetLeft added in v1.1.0

func (p *Pair[T, U]) GetLeft() (t T)

func (*Pair[T, U]) GetRight added in v1.1.0

func (p *Pair[T, U]) GetRight() (u U)

type RWMap added in v1.1.0

type RWMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func CombineRWMaps added in v1.1.0

func CombineRWMaps[K comparable, V any](mapToCopy *RWMap[K, V], mapToAdd *RWMap[K, V]) *RWMap[K, V]

func NewRWMap added in v1.1.0

func NewRWMap[K comparable, V any]() *RWMap[K, V]

func NewRWMapFromCopy added in v1.1.0

func NewRWMapFromCopy[K comparable, V any](rwMap *RWMap[K, V]) *RWMap[K, V]

func NewRWMapFromMap added in v1.1.0

func NewRWMapFromMap[K comparable, V any](mapForCache map[K]V) *RWMap[K, V]

func NewRWMapWithDisposalFunc added in v1.1.0

func NewRWMapWithDisposalFunc[K comparable, V any](disposalFunc DisposalFunc[V]) *RWMap[K, V]

func (*RWMap[K, V]) Clear added in v1.1.0

func (c *RWMap[K, V]) Clear()

func (*RWMap[K, V]) ComputeIfAbsent added in v1.1.0

func (c *RWMap[K, V]) ComputeIfAbsent(key K, computeFunc func() V) V

func (*RWMap[K, V]) Filter added in v1.2.0

func (c *RWMap[K, V]) Filter(condition func(K, V) bool)

Filter removes entries from the map that don't match the filter condition.

func (*RWMap[K, V]) ForEach added in v1.2.0

func (c *RWMap[K, V]) ForEach(fn func(K, V))

func (*RWMap[K, V]) Get added in v1.1.0

func (c *RWMap[K, V]) Get(key K) (V, bool)

func (*RWMap[K, V]) GetAllEntries added in v1.1.0

func (c *RWMap[K, V]) GetAllEntries() map[K]V

func (*RWMap[K, V]) ProcessAndRemoveIf added in v1.1.0

func (c *RWMap[K, V]) ProcessAndRemoveIf(condition func(K) bool, processor func(K, V))

ProcessAndRemoveIf executes the provided function on each key-value pair where the key satisfies the condition function. Matching entries are removed after processing.

func (*RWMap[K, V]) Put added in v1.1.0

func (c *RWMap[K, V]) Put(key K, value V)

func (*RWMap[K, V]) PutIfAbsent added in v1.1.0

func (c *RWMap[K, V]) PutIfAbsent(key K, value V)

func (*RWMap[K, V]) Remove added in v1.1.0

func (c *RWMap[K, V]) Remove(key K)

func (*RWMap[K, V]) ReplaceCacheWithCopy added in v1.1.0

func (c *RWMap[K, V]) ReplaceCacheWithCopy(mapToCopy *RWMap[K, V])

func (*RWMap[K, V]) ReplaceCacheWithMap added in v1.1.0

func (c *RWMap[K, V]) ReplaceCacheWithMap(entryMap map[K]V)

func (*RWMap[K, V]) Size added in v1.1.0

func (c *RWMap[K, V]) Size() int

type RWQueue added in v1.1.0

type RWQueue[T any] struct {
	// contains filtered or unexported fields
}

func NewRWQueue added in v1.1.0

func NewRWQueue[T any]() *RWQueue[T]

func (*RWQueue[T]) Dequeue added in v1.1.0

func (q *RWQueue[T]) Dequeue() (T, bool)

func (*RWQueue[T]) Enqueue added in v1.1.0

func (q *RWQueue[T]) Enqueue(item T)

func (*RWQueue[T]) ForEach added in v1.2.0

func (q *RWQueue[T]) ForEach(fn func(T))

func (*RWQueue[T]) IsEmpty added in v1.1.0

func (q *RWQueue[T]) IsEmpty() bool

func (*RWQueue[T]) RemoveIf added in v1.2.0

func (q *RWQueue[T]) RemoveIf(condition func(T) bool)

func (*RWQueue[T]) Size added in v1.1.0

func (q *RWQueue[T]) Size() int

type RdsUrlType

type RdsUrlType struct {
	Id           int
	IsRds        bool
	IsRdsCluster bool
}
var (
	OTHER                               RdsUrlType = RdsUrlType{0, false, false}
	RDS_WRITER_CLUSTER                  RdsUrlType = RdsUrlType{1, true, true}
	RDS_READER_CLUSTER                  RdsUrlType = RdsUrlType{2, true, true}
	RDS_CUSTOM_CLUSTER                  RdsUrlType = RdsUrlType{3, true, true}
	RDS_PROXY                           RdsUrlType = RdsUrlType{4, true, false}
	RDS_INSTANCE                        RdsUrlType = RdsUrlType{5, true, false}
	RDS_AURORA_LIMITLESS_DB_SHARD_GROUP RdsUrlType = RdsUrlType{6, true, false}
	IP_ADDRESS                          RdsUrlType = RdsUrlType{7, false, false}
)

func IdentifyRdsUrlType

func IdentifyRdsUrlType(host string) RdsUrlType

type SlidingExpirationCache

type SlidingExpirationCache[T any] struct {
	// contains filtered or unexported fields
}

func NewSlidingExpirationCache

func NewSlidingExpirationCache[T any](id string, funcs ...DisposalFunc[T]) *SlidingExpirationCache[T]

func (*SlidingExpirationCache[T]) CleanUp

func (c *SlidingExpirationCache[T]) CleanUp()

func (*SlidingExpirationCache[T]) Clear

func (c *SlidingExpirationCache[T]) Clear()

func (*SlidingExpirationCache[T]) ComputeIfAbsent

func (c *SlidingExpirationCache[T]) ComputeIfAbsent(key string, computeFunc func() T, itemExpiration time.Duration) T

func (*SlidingExpirationCache[T]) ComputeIfAbsentWithError added in v1.2.0

func (c *SlidingExpirationCache[T]) ComputeIfAbsentWithError(key string, computeFunc func() (T, error), itemExpiration time.Duration) (T, error)

func (*SlidingExpirationCache[T]) Get

func (c *SlidingExpirationCache[T]) Get(key string, itemExpiration time.Duration) (T, bool)

func (*SlidingExpirationCache[T]) GetAllEntries

func (c *SlidingExpirationCache[T]) GetAllEntries() map[string]T

Get a map copy of all entries in the cache, including expired entries.

func (*SlidingExpirationCache[T]) Put

func (c *SlidingExpirationCache[T]) Put(key string, value T, itemExpiration time.Duration)

func (*SlidingExpirationCache[T]) PutIfAbsent

func (c *SlidingExpirationCache[T]) PutIfAbsent(key string, value T, expiration time.Duration)

func (*SlidingExpirationCache[T]) Remove

func (c *SlidingExpirationCache[T]) Remove(key string)

func (*SlidingExpirationCache[T]) SetCleanupIntervalNanos

func (c *SlidingExpirationCache[T]) SetCleanupIntervalNanos(newIntervalNanos time.Duration)

func (*SlidingExpirationCache[T]) Size

func (c *SlidingExpirationCache[T]) Size() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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