Documentation
¶
Index ¶
- Constants
- Variables
- func All[T any](data []T, condition func(*T) bool) bool
- func Any[T any](data []T, condition func(*T) bool) bool
- func Base64Decode(data string) ([]byte, error)
- func Base64Encode(data []byte) string
- func Base64URLDecode(data string) ([]byte, error)
- func Base64URLEncode(data []byte) string
- func BeginOfDay(t time.Time) time.Time
- func BeginOfMonth(t time.Time) time.Time
- func BeginOfYear(t time.Time) time.Time
- func ContainsIgnoreCase(data []string, target string) bool
- func Count[T any](arr []T, condition func(*T) bool) int
- func Days(dur time.Duration) int64
- func DaysOfMonth(t time.Time) int64
- func DaysOfThisMonth() int64
- func Deduplication[T comparable](arr []T) []T
- func EndOfDay(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func EndOfYear(t time.Time) time.Time
- func Filter[T any](data []T, filter func(*T) bool) []T
- func First[T any](data []T, f func(*T) bool) *T
- func FirstOrDefault[T any](data []T, f func(*T) bool, defaultVal T) T
- func GetClientIp(ctx *gin.Context) string
- func GetGenTime(sid int64) string
- func GetGenTimestamp(sid int64) int64
- func GetTimestamp(sid int64) int64
- func GetTimestampStatus() float64
- func GetWorkerId(sid int64) int64
- func GetWxOpenId(code, appId, appSecret string) (string, error)
- func GroupBy[T any, TField comparable](data []T, fieldFilter func(*T) TField) map[TField][]T
- func GroupByWithMap[S any, TOutField any, TGroupField comparable](data []S, fieldFilter func(*S) TGroupField, mapper func(*S) TOutField) map[TGroupField][]TOutField
- func HashMd5(str string) string
- func HashSha256(str string) string
- func HttpGet(url string, options *HttpOptions) ([]byte, error)
- func HttpGetJson(url string, options *HttpOptions, resp any) error
- func HttpPost(url string, data []byte, options *HttpOptions) ([]byte, error)
- func HttpPostJson(url string, data any, options *HttpOptions, resp any) error
- func IsCellPhone(value string) bool
- func IsColor(value string) bool
- func IsCountryCode(value string) bool
- func IsEmail(value string) bool
- func IsEmpty(value string) bool
- func IsFixedLengthInt(value string, length int) bool
- func IsLengthInRange(value string, min, max int) bool
- func IsNumber(value string) bool
- func IsPlatformStringValid(pstr string) bool
- func IsPlatformValid(p Platform) bool
- func IsTelephone(value string) bool
- func IsUrl(value string, allowEmpty bool) bool
- func LastMonthFirstDay() time.Time
- func LastMonthLastDay() time.Time
- func LastMonthNow(t time.Time) time.Time
- func Map[TIn any, TOut any](data []TIn, f func(*TIn) (TOut, bool)) []TOut
- func MaskPhone(phone string) string
- func NewEd25519CryptorKeyPair() (pubKey, priKey []byte, err error)
- func NewEd25519SignerKeyPair() (pubKey, priKey []byte, err error)
- func NewUUID() string
- func NewUUIDWithoutDash() string
- func PadLeft(val string, fixLen int, char string) string
- func PadLeftInt(num int, fixLen int, char string) string
- func PadRight(val string, fixLen int, char string) string
- func PadRightInt(num int, fixLen int, char string) string
- func Random(min, max int) int
- func RateLimitMiddleware(limitInterval time.Duration, count int) gin.HandlerFunc
- func SecureBytes(keyLenInBytes int) ([]byte, error)
- func Shuffle[T any](arr []T)
- func ShuffleCopy[T any](arr []T) []T
- func SplitCommaString(str string) []string
- func SplitIntoBatches[T any](arr []T, itemsPerBatch int) [][]T
- func SplitJoinedItems(joinedStr string, sep ...string) []string
- func ThisMonthFirstDay() time.Time
- func ThisMonthRemainDays() int64
- func TimeToYYYYMMDD(t time.Time) string
- func TimeToYYYYMMDDInt(t time.Time) (int, error)
- func WaitSysSignal(cleanup func())
- type ByteBufferPool
- type BytePool
- type Cache
- func (c *Cache) Batch(ctx context.Context, f func(pipe redis.Pipeliner)) (map[int]interface{}, map[int]error)
- func (c *Cache) Close() error
- func (c *Cache) DecrBy(ctx context.Context, key string, decrement int64) (int64, error)
- func (c *Cache) Get(ctx context.Context, key string) (string, error)
- func (c *Cache) GetDel(ctx context.Context, key string) (string, error)
- func (c *Cache) GetExpire(ctx context.Context, key string, expiration time.Duration) (string, error)
- func (c *Cache) GetJson(ctx context.Context, key string, out interface{}) error
- func (c *Cache) GetSet(ctx context.Context, key string, value any) (string, error)
- func (c *Cache) HDel(ctx context.Context, key string, fields ...string) (bool, error)
- func (c *Cache) HExists(ctx context.Context, key, field string) (bool, error)
- func (c *Cache) HGet(ctx context.Context, key, field string) (string, error)
- func (c *Cache) HGetAll(ctx context.Context, key string) (map[string]string, error)
- func (c *Cache) HGetJson(ctx context.Context, key string, field string, out any) error
- func (c *Cache) HIncrBy(ctx context.Context, key, field string, incr int64) (int64, error)
- func (c *Cache) HIncrByFloat(ctx context.Context, key, field string, incr float64) (float64, error)
- func (c *Cache) HKeys(ctx context.Context, key string) ([]string, error)
- func (c *Cache) HLen(ctx context.Context, key string) (int64, error)
- func (c *Cache) HMGet(ctx context.Context, key string, fields ...string) ([]any, error)
- func (c *Cache) HMSet(ctx context.Context, key string, values map[string]any) (bool, error)
- func (c *Cache) HMSetAndExpiry(ctx context.Context, key string, values map[string]string, ...) (bool, error)
- func (c *Cache) HSet(ctx context.Context, key string, values map[string]any) (int64, error)
- func (c *Cache) HSetJson(ctx context.Context, key string, field string, val any) (int64, error)
- func (c *Cache) HSetNX(ctx context.Context, key, field string, value any) (bool, error)
- func (c *Cache) HVals(ctx context.Context, key string) ([]string, error)
- func (c *Cache) IncrBy(ctx context.Context, key string, decrement int64) (int64, error)
- func (c *Cache) IncrByFloat(ctx context.Context, key string, decrement float64) (float64, error)
- func (c *Cache) KeyDel(ctx context.Context, keys ...string) (int64, error)
- func (c *Cache) KeyExists(ctx context.Context, keys ...string) (int64, error)
- func (c *Cache) KeyExpire(ctx context.Context, key string, expiry time.Duration) (bool, error)
- func (c *Cache) KeyExpireAt(ctx context.Context, key string, expiry time.Time) (bool, error)
- func (c *Cache) Master() *redis.Client
- func (c *Cache) MultiGet(ctx context.Context, keys ...string) ([]any, error)
- func (c *Cache) MultiSet(ctx context.Context, maps map[string]any) (string, error)
- func (c *Cache) MultiSetNX(ctx context.Context, maps map[string]any) (bool, error)
- func (c *Cache) SAdd(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Cache) SIsMember(ctx context.Context, key string, member any) (bool, error)
- func (c *Cache) SMembers(ctx context.Context, key string) ([]string, error)
- func (c *Cache) SMultiIsMember(ctx context.Context, key string, members ...any) ([]bool, error)
- func (c *Cache) SRemove(ctx context.Context, key string, members ...any) (int64, error)
- func (c *Cache) Set(ctx context.Context, key string, value any, expiry time.Duration) (string, error)
- func (c *Cache) SetJson(ctx context.Context, key string, val any, expiry time.Duration) (string, error)
- func (c *Cache) SetNX(ctx context.Context, key string, value any, expiry time.Duration) (bool, error)
- func (c *Cache) Slave() *redis.Client
- type Collection
- type ConsumeMsgHandler
- type CoroutinePool
- type Cryptor
- type DistributeId
- type DistributeLock
- type DistributeLockOptions
- type DistributeLocker
- type Ed25519Cryptor
- func (e *Ed25519Cryptor) Decrypt(rawData []byte) ([]byte, error)
- func (e *Ed25519Cryptor) DecryptFromString(ciphertext string) ([]byte, error)
- func (e *Ed25519Cryptor) Encrypt(rawData []byte) ([]byte, error)
- func (e *Ed25519Cryptor) EncryptToString(rawData []byte) (string, error)
- func (e *Ed25519Cryptor) SharedKeyString() string
- type Ed25519SignKey
- type Ed25519Signer
- type Empty
- type FastQueue
- type FastStack
- func (stack *FastStack[T]) Clear()
- func (stack *FastStack[T]) IsEmpty() bool
- func (stack *FastStack[T]) Peek() *T
- func (stack *FastStack[T]) Pop() *T
- func (stack *FastStack[T]) Print()
- func (stack *FastStack[T]) Push(val *T)
- func (stack *FastStack[T]) PushAll(vals ...T)
- func (stack *FastStack[T]) Size() int
- type HmacSigner
- type HttpOptions
- type Hub
- func (h *Hub) BroadcastMessage(data []byte)
- func (h *Hub) Close(wait time.Duration)
- func (h *Hub) CloseUserLines(userIds ...string)
- func (h *Hub) ErrorChan() <-chan *LineError
- func (h *Hub) GetUserLines(userId string) *UserLines
- func (h *Hub) LiveCount() int
- func (h *Hub) MessageChan() <-chan *LineMessage
- func (h *Hub) PushMessage(userIds []string, data []byte)
- func (h *Hub) RegisteredChan() <-chan *Line
- func (h *Hub) UnegisteredChan() <-chan *Line
- func (h *Hub) UpgradeWebSocket(userId string, platform Platform, lineId string, w http.ResponseWriter, ...) error
- type IdGenerator
- type JsonMarshaler
- type Line
- type LineError
- type LineMessage
- type MessageQueue
- type MsgPackMarshaler
- type PacketMetaData
- type PacketProtocol
- type PayloadMarshaler
- type Platform
- type RedisMessageQueue
- type ReplyDto
- type RequestPacket
- type RetryStrategy
- type RsaCryptor
- type Set
- type Signer
- type Snowflake
- type UserLines
- func (u *UserLines) Close(lineId string)
- func (u *UserLines) CloseAll()
- func (u *UserLines) CloseLines(lineIds ...string)
- func (u *UserLines) CloseLinesExcept(exceptLineIds ...string)
- func (u *UserLines) ClosePlatforms(platforms ...Platform)
- func (u *UserLines) ClosePlatformsExcept(exceptPlatforms ...Platform)
- func (u *UserLines) Get(lineId string) *Line
- func (u *UserLines) GetPlatformLines(platforms ...Platform) []*Line
- func (u *UserLines) Len() int
- func (u *UserLines) PushMessage(data []byte)
- func (u *UserLines) PushMessageExceptLines(data []byte, exceptLineIds ...string)
- func (u *UserLines) PushMessageExceptPlatforms(data []byte, exceptPlatforms ...Platform)
- func (u *UserLines) PushMessageToLines(data []byte, lineIds ...string)
- func (u *UserLines) PushMessageToPlatforms(data []byte, platforms ...Platform)
Constants ¶
const ( HeaderAuthorization = "Authorization" HeaderContentType = "Content-Type" ContentTypeJson = "application/json;charset=utf-8" ContentTypeText = "text/plain;charset=utf-8" ContentTypeEncrypted = "application/x-encrypted;charset=utf-8" )
const (
Day = 24 * time.Hour
)
Variables ¶
var ( ErrLockFailed = errors.New("lock failed") ErrLockNotHeld = errors.New("lock not held") )
var (
ErrInvalidDistributeIdParams = errors.New("invalid distribute id params")
)
Functions ¶
func FirstOrDefault ¶
找到数组中第一个满足条件的项,如果未找到,使用默认值
func GroupBy ¶
func GroupBy[T any, TField comparable](data []T, fieldFilter func(*T) TField) map[TField][]T
根据指定的字段分组
func GroupByWithMap ¶
func GroupByWithMap[S any, TOutField any, TGroupField comparable](data []S, fieldFilter func(*S) TGroupField, mapper func(*S) TOutField) map[TGroupField][]TOutField
根据指定的字段分组
func HttpPost ¶
func HttpPost(url string, data []byte, options *HttpOptions) ([]byte, error)
发送 Http Post 请求
func HttpPostJson ¶
func HttpPostJson(url string, data any, options *HttpOptions, resp any) error
resp 必须是一个指针
func IsPlatformStringValid ¶ added in v1.1.0
func IsPlatformValid ¶ added in v1.1.0
func NewEd25519CryptorKeyPair ¶
生成 Ed25519 密钥对
func NewEd25519SignerKeyPair ¶
初始化一个Ed25519 密钥对
func RateLimitMiddleware ¶
func RateLimitMiddleware(limitInterval time.Duration, count int) gin.HandlerFunc
func SplitIntoBatches ¶
将一个长数组拆分为多个小的批次数组
func SplitJoinedItems ¶
func WaitSysSignal ¶ added in v0.0.9
func WaitSysSignal(cleanup func())
wait system signals.
when signal comes, do some cleanups
Types ¶
type ByteBufferPool ¶
type ByteBufferPool struct {
// contains filtered or unexported fields
}
func NewByteBufferPool ¶
func NewByteBufferPool(size, cap int) *ByteBufferPool
func (*ByteBufferPool) Get ¶
func (p *ByteBufferPool) Get() *bytes.Buffer
func (*ByteBufferPool) Put ¶
func (p *ByteBufferPool) Put(b *bytes.Buffer)
type BytePool ¶
type BytePool struct {
// contains filtered or unexported fields
}
func NewBytePool ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func NewCacheWithAddr ¶ added in v0.0.6
func (*Cache) HIncrByFloat ¶
func (*Cache) HMSetAndExpiry ¶
func (*Cache) IncrByFloat ¶
func (*Cache) KeyExpireAt ¶
func (*Cache) MultiSetNX ¶
func (*Cache) SMultiIsMember ¶
type Collection ¶
type ConsumeMsgHandler ¶
id 消费者需要通过此Id来判断该消息是否已被消费
type CoroutinePool ¶ added in v1.1.8
type CoroutinePool interface {
Submit(task func()) error
Release()
}
type DistributeId ¶
func NewDistributeId ¶
func (*DistributeId) Close ¶ added in v1.1.8
func (d *DistributeId) Close()
func (*DistributeId) NewGenerator ¶ added in v1.1.8
func (d *DistributeId) NewGenerator(ctx context.Context, key string, start int) (*IdGenerator, error)
type DistributeLock ¶
type DistributeLock struct {
// contains filtered or unexported fields
}
type DistributeLockOptions ¶
type DistributeLockOptions struct {
Resource string
Owner string
Ttl time.Duration
RetryStrategy RetryStrategy
}
type DistributeLocker ¶
type DistributeLocker struct {
// contains filtered or unexported fields
}
func NewDistributeLocker ¶
func NewDistributeLocker(ctx context.Context, opt *redis.Options, ttl time.Duration, retryStrategy RetryStrategy) (*DistributeLocker, error)
func (*DistributeLocker) Close ¶ added in v1.1.8
func (l *DistributeLocker) Close()
func (*DistributeLocker) Lock ¶
func (l *DistributeLocker) Lock(ctx context.Context, resource string, owner string) (*DistributeLock, error)
func (*DistributeLocker) LockWithOptions ¶
func (l *DistributeLocker) LockWithOptions(ctx context.Context, opt *DistributeLockOptions) (*DistributeLock, error)
在指定资源上加锁,默认5s
type Ed25519Cryptor ¶
type Ed25519Cryptor struct {
}
func NewEd25519CryptorByNegotiate ¶
func NewEd25519CryptorByNegotiate(remotePublicKey, selfPrivateKey []byte) (*Ed25519Cryptor, error)
通过与对方公钥协商,获得共享密钥
func (*Ed25519Cryptor) Decrypt ¶
func (e *Ed25519Cryptor) Decrypt(rawData []byte) ([]byte, error)
对指定输入解密,输入为: nonce + cipherData + tag
func (*Ed25519Cryptor) DecryptFromString ¶
func (e *Ed25519Cryptor) DecryptFromString(ciphertext string) ([]byte, error)
func (*Ed25519Cryptor) Encrypt ¶
func (e *Ed25519Cryptor) Encrypt(rawData []byte) ([]byte, error)
对指定输入加密,结果为: nonce + cipherData + tag
func (*Ed25519Cryptor) EncryptToString ¶
func (e *Ed25519Cryptor) EncryptToString(rawData []byte) (string, error)
func (*Ed25519Cryptor) SharedKeyString ¶
func (e *Ed25519Cryptor) SharedKeyString() string
将密钥转换为 base64 格式字符串
type Ed25519SignKey ¶
Pri, Pub需要公开出去以供配置文件加载程序使用
func NewEd25519SignKey ¶
func NewEd25519SignKey(pri, pub string) (*Ed25519SignKey, error)
func (*Ed25519SignKey) Sign ¶
func (sk *Ed25519SignKey) Sign(message []byte) []byte
func (*Ed25519SignKey) SignString ¶
func (sk *Ed25519SignKey) SignString(message string) string
type Ed25519Signer ¶
type Ed25519Signer struct {
RemotePublicKey ed25519.PublicKey // 远端的公钥,用于验证远程发过来的数据的签名
SelfPrivateKey ed25519.PrivateKey // 本地的私钥,用于对发往服务器的数据进行签名
}
func NewEd25519Signer ¶
func NewEd25519Signer(remotePublicKey []byte, selfPrivateKey []byte) *Ed25519Signer
初始化一个签名器
func NewEd25519SignerFromString ¶
func NewEd25519SignerFromString(remotePublicKey, selfPrivateKey string) (*Ed25519Signer, error)
初始化一个签名器
func (*Ed25519Signer) RemotePublicKeyString ¶
func (e *Ed25519Signer) RemotePublicKeyString() string
func (*Ed25519Signer) SelfPrivateKeyString ¶
func (e *Ed25519Signer) SelfPrivateKeyString() string
func (*Ed25519Signer) SignatureLen ¶ added in v1.1.0
func (e *Ed25519Signer) SignatureLen() int
type HmacSigner ¶ added in v1.1.0
type HmacSigner struct {
// contains filtered or unexported fields
}
func NewHmacSigner ¶ added in v1.1.0
func NewHmacSigner(secret []byte) *HmacSigner
func (*HmacSigner) Sign ¶ added in v1.1.0
func (h *HmacSigner) Sign(data []byte) ([]byte, error)
Golang 生成 HMAC 签名
func (*HmacSigner) SignatureLen ¶ added in v1.1.0
func (h *HmacSigner) SignatureLen() int
type HttpOptions ¶
func NewEmptyHttpOptions ¶
func NewEmptyHttpOptions() *HttpOptions
func NewHttpOptions ¶
func NewHttpOptions(headers map[string]string) *HttpOptions
func NewHttpOptionsJson ¶
func NewHttpOptionsJson(authorization string) *HttpOptions
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
func (*Hub) BroadcastMessage ¶
func (*Hub) CloseUserLines ¶ added in v1.1.14
关闭指定用户的所有连接
func (*Hub) GetUserLines ¶ added in v1.1.14
获取指定用户的所有连接
func (*Hub) PushMessage ¶
func (*Hub) RegisteredChan ¶
func (*Hub) UnegisteredChan ¶
type IdGenerator ¶ added in v1.1.8
type IdGenerator struct {
// contains filtered or unexported fields
}
type JsonMarshaler ¶
type JsonMarshaler struct{}
type Line ¶
type Line struct {
// contains filtered or unexported fields
}
客户端连接
func (*Line) LastActive ¶ added in v1.1.14
type LineMessage ¶ added in v1.1.14
客户端连接的消息
type MessageQueue ¶
type MsgPackMarshaler ¶
type MsgPackMarshaler struct{}
type PacketMetaData ¶ added in v1.1.16
type PacketProtocol ¶ added in v1.1.16
type PacketProtocol struct {
// contains filtered or unexported fields
}
func NewJsonProtocol ¶
func NewJsonProtocol(signer Signer, cryptor Cryptor) *PacketProtocol
func NewMsgPackProtocol ¶
func NewMsgPackProtocol(signer Signer, cryptor Cryptor) *PacketProtocol
func (*PacketProtocol) DecodeReq ¶ added in v1.1.16
func (m *PacketProtocol) DecodeReq(data []byte) (*RequestPacket, error)
func (*PacketProtocol) EncodeResp ¶ added in v1.1.16
func (*PacketProtocol) GetMeta ¶ added in v1.1.16
func (m *PacketProtocol) GetMeta(data []byte) (*PacketMetaData, error)
type PayloadMarshaler ¶
type RedisMessageQueue ¶
type RedisMessageQueue struct {
// contains filtered or unexported fields
}
func NewRedisMessageQueue ¶
func NewRedisMessageQueue(ctx context.Context, opt *redis.Options, pool CoroutinePool, xaddMaxLen, batchSize int) (*RedisMessageQueue, error)
func (*RedisMessageQueue) Close ¶ added in v1.1.8
func (m *RedisMessageQueue) Close()
func (*RedisMessageQueue) Subscribe ¶ added in v0.0.7
func (m *RedisMessageQueue) Subscribe(ctx context.Context, topic, group, consumer string, handler ConsumeMsgHandler) error
开启协程后台消费。返回值代表消费过程中遇到的无法处理的错误 group 消费者组,一般为当前服务的名称 consumer 消费者组里的消费者,一般为一个uuid handler 消费消息的处理器,如果返回nil,则表示消息被成功消费,如果返回非nil,则表示消息被消费失败,需要重试
type RequestPacket ¶ added in v1.1.16
type RequestPacket struct {
PacketMetaData
Payload any
}
type RetryStrategy ¶
func LinearRetryStrategy ¶
func LinearRetryStrategy(backoff time.Duration) RetryStrategy
LinearRetryStrategy allows retries regularly with customized intervals
type RsaCryptor ¶
type RsaCryptor struct {
PublicKey []byte // PEM format: 即有-----BEGIN XXX KEY-----...-----END XXX KEY-----
PrivateKey []byte // PEM format: 即有-----BEGIN XXX KEY-----...-----END XXX KEY-----
}
func NewRsaCryptor ¶
func NewRsaCryptor(publicKey, privateKey []byte) *RsaCryptor
func (*RsaCryptor) DecryptFromString ¶
func (r *RsaCryptor) DecryptFromString(ciphertext string) ([]byte, error)
func (*RsaCryptor) EncryptToString ¶
func (r *RsaCryptor) EncryptToString(rawData []byte) (string, error)
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
type Snowflake ¶
func NewSnowflake ¶
type UserLines ¶ added in v1.1.14
用户在各个平台的所有连接
func (*UserLines) CloseLines ¶ added in v1.1.14
关闭指定连接
func (*UserLines) CloseLinesExcept ¶ added in v1.1.14
关闭除指定连接外的所有连接
func (*UserLines) ClosePlatforms ¶ added in v1.1.14
关闭指定平台的所有连接
func (*UserLines) ClosePlatformsExcept ¶ added in v1.1.14
关闭除指定平台外的所有连接
func (*UserLines) GetPlatformLines ¶ added in v1.1.14
获取指定平台的所有连接
func (*UserLines) PushMessage ¶ added in v1.1.14
向该用户的所有连接发送消息
func (*UserLines) PushMessageExceptLines ¶ added in v1.1.14
向该用户的所有连接发送消息,除了指定连接
func (*UserLines) PushMessageExceptPlatforms ¶ added in v1.1.14
向该用户的所有连接发送消息,除了指定平台
func (*UserLines) PushMessageToLines ¶ added in v1.1.14
向该用户的指定连接发送消息
func (*UserLines) PushMessageToPlatforms ¶ added in v1.1.14
向该用户的指定平台发送消息
Source Files
¶
- bytes_packet.go
- cache.go
- collection.go
- collection_queue.go
- collection_set.go
- collection_stack.go
- crypto.go
- distribute_id.go
- distribute_locker.go
- empty.go
- encoding.go
- gin.go
- http.go
- hub.go
- ip.go
- marshaler.go
- message_queue.go
- metrics.go
- platform.go
- pool.go
- rate-limter.go
- retry-strategy.go
- signals.go
- signer.go
- slice.go
- snowflake.go
- strs.go
- strs_validation.go
- third_wx.go
- time.go
- uuid.go