utils

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2021 License: Apache-2.0 Imports: 36 Imported by: 1

README

utils

utils

Documentation

Index

Constants

View Source
const (
	DateFmt = "_%d_%02d_%02d"
)
View Source
const (
	DefaultWorkerNum = 2
)
View Source
const METRIC_SERVICE_DEFAULT = "161.202.208.20:18694"

Variables

View Source
var (
	TdyIndex  string
	IndexLock sync.RWMutex
	EsClient  elasticsearch.EsClientV3

	// config
	Hosts           []string
	Index           string
	Type            string
	ShardNum        int
	ReplicaNum      int
	RefreshInterval int
)
View Source
var Default = &UIDFactory{}
View Source
var IDFactory *snowflake.Node
View Source
var SERVICE string

Functions

func FormatLogID

func FormatLogID(logid string) string

func GenerateLogID

func GenerateLogID() string

func GetIndex

func GetIndex() string

func GetLocalIP

func GetLocalIP() string

func GetLocalIPWithPrefix

func GetLocalIPWithPrefix(prefix string) string

func GetMiddleNight

func GetMiddleNight() time.Time

func GetNewID

func GetNewID() int64

func GetNextMiddleNight

func GetNextMiddleNight() time.Time

func GetPosBefore

func GetPosBefore(mid int64, groupMsgIds []int64) int

在有序列表中二分查找位置

func Hashing

func Hashing() int64

func InitDirtyManager

func InitDirtyManager(wds []string) (err error)

func InitEsClient

func InitEsClient(hosts []string, index, ttype string, shardNum, replicaNum, refreshInterval int)

func InitHeartBeat

func InitHeartBeat(isMaster bool, port int, cb func(val string), addr string)

func InitIDFactory

func InitIDFactory(node int64) (err error)

func InitKafkaLogProducer

func InitKafkaLogProducer(addrs []string, topic string, bufSize int, timeout time.Duration, workerNum int)

func InitKafkaProducer

func InitKafkaProducer(addrs []string, topic string, bufSize int, timeout time.Duration, workerNum int)

func InitKafkaProducerA

func InitKafkaProducerA(clientName string, addrs []string, topic string, bufSize int, timeout time.Duration, workerNum int)

func InitMetrics

func InitMetrics(service, metricSrv string) (err error)

func InitTicker

func InitTicker()

func InitUdpClient

func InitUdpClient(token string)

func Insert2Es

func Insert2Es(dat string) error

func Insert2EsBulk

func Insert2EsBulk(dats []interface{}) error

func IsForbidden

func IsForbidden(content string) bool

func KafkaClose

func KafkaClose()

func LookupIP

func LookupIP() string

func MetricCounter

func MetricCounter(tags map[string]string, cnt int64)

func MetricTimer

func MetricTimer(tags map[string]string, latency int64)

单位毫秒

func SendDingDing

func SendDingDing(title, content string) error

func SendHeartBeat

func SendHeartBeat()

func SendLog

func SendLog(key, data []byte) error

func SendMsgA

func SendMsgA(clientName string, key, data []byte) error

func SendMsgAWithTopic

func SendMsgAWithTopic(topic, clientName string, key, data []byte) error

func SendTask

func SendTask(key, data []byte) error

func UDPServer

func UDPServer(port int, cb func(string))

func Ungzip

func Ungzip(r io.Reader) (io.ReadCloser, error)

func Ungzip2Bytes

func Ungzip2Bytes(r io.Reader) ([]byte, error)

Types

type ApplicationMessage

type ApplicationMessage struct {
	Title   string   `json:"title"`
	MType   string   `json:"mtype"`   //dingidng, kafka, influx
	Content string   `json:"content"` // 具体数据信息,json格式
	At      []string `json:"at"`
	Token   string   `json:"token"`
}

type AsyncJob

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

AsyncJob 异步写入, 考虑更通用的方式

func NewAsyncJob

func NewAsyncJob(workerNum, batchNum int, bf func([]interface{}), wait time.Duration) *AsyncJob

func NewAsyncJobA

func NewAsyncJobA(funChanSize, workerNum, batchNum int, bf func([]interface{}), wait time.Duration) *AsyncJob

func (*AsyncJob) AddData

func (af *AsyncJob) AddData(dt interface{})

func (*AsyncJob) AddJob

func (af *AsyncJob) AddJob(ff func() error) error

func (*AsyncJob) AddJobNoLoss

func (af *AsyncJob) AddJobNoLoss(ff func() error)

func (*AsyncJob) Close

func (af *AsyncJob) Close()

Close safe clean

func (*AsyncJob) Flush

func (af *AsyncJob) Flush()

func (*AsyncJob) Run

func (af *AsyncJob) Run()

type CacheManager

type CacheManager struct {
	sync.Pool
	// contains filtered or unexported fields
}

func NewCacheManager

func NewCacheManager() *CacheManager

func (*CacheManager) AddChains

func (cm *CacheManager) AddChains(chains ...GetData)

注意按添加的顺序依次执行

func (*CacheManager) GetData

func (cm *CacheManager) GetData(ctx context.Context, key interface{}) interface{}

context.WithValue(ctx, userKey, u) u, ok := ctx.Value(userKey).(*User)

type CacheRequest

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

func (*CacheRequest) Abort

func (c *CacheRequest) Abort()

func (*CacheRequest) GetData

func (c *CacheRequest) GetData() (interface{}, bool)

func (*CacheRequest) GetKey

func (c *CacheRequest) GetKey() interface{}

func (*CacheRequest) Next

func (c *CacheRequest) Next()

func (*CacheRequest) SetData

func (c *CacheRequest) SetData(data interface{})

type Chain

type Chain struct {
	Errors errorMsgs

	Val interface{}
	// contains filtered or unexported fields
}

func (*Chain) Abort

func (c *Chain) Abort(err error)

func (*Chain) CheckErr

func (c *Chain) CheckErr() error

func (*Chain) Do

func (c *Chain) Do(val interface{}) error

func (*Chain) Handler

func (c *Chain) Handler() HandlerFunc

func (*Chain) Next

func (c *Chain) Next()

func (*Chain) Use

func (c *Chain) Use(h HandlerFunc) *Chain

func (*Chain) Uses

func (c *Chain) Uses(h ...HandlerFunc) *Chain

type CommonCache

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

key=value 通用存储

func NewCommonCache

func NewCommonCache(size int, expireTime time.Duration) *CommonCache

func (*CommonCache) Get

func (localCache *CommonCache) Get(key interface{}) (interface{}, bool)

func (*CommonCache) IsHit

func (localCache *CommonCache) IsHit(key interface{}) bool

这个函数可能有性能瓶颈

func (*CommonCache) Len

func (localCache *CommonCache) Len() int

func (*CommonCache) Remove

func (localCache *CommonCache) Remove(key interface{})

func (*CommonCache) Set

func (localCache *CommonCache) Set(key, val interface{})

func (*CommonCache) SetWithTtl

func (localCache *CommonCache) SetWithTtl(key, val interface{}, ttl int)

type Comparator

type Comparator func(a, b interface{}) int

type ConsumerCallback

type ConsumerCallback interface {
	Consume(message *sarama.ConsumerMessage) error
}

type DBClient

type DBClient struct {
	Name     string
	Server   string
	User     string
	Password string

	RealDb *gorm.DB
}

func NewDBClient

func NewDBClient(name, server, user, passwd string) *DBClient

func NewDBClientPhoenix

func NewDBClientPhoenix(dataSourceName string, maxConn int, sessionDuration time.Duration) *DBClient

func NewDBClientV2

func NewDBClientV2(name, server, user, passwd string, maxConn int, timeout int) *DBClient

timeout ms

func NewDBClientV3

func NewDBClientV3(name, server, user, passwd string, maxConn int, timeout int, sessionDuration time.Duration) *DBClient

func NewDBClientV4

func NewDBClientV4(name, server, user, passwd string, maxConn int, connectTime int, timeout int, sessionDuration time.Duration) *DBClient

type GenFixedTimePoint

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

func NewGenFixTimePoint

func NewGenFixTimePoint(fixedHour int) *GenFixedTimePoint

func (*GenFixedTimePoint) GetFixedTime

func (gf *GenFixedTimePoint) GetFixedTime() time.Time

func (*GenFixedTimePoint) GetFixedTimeStr

func (gf *GenFixedTimePoint) GetFixedTimeStr() string

func (*GenFixedTimePoint) GetPrevFixedTimeStr

func (gf *GenFixedTimePoint) GetPrevFixedTimeStr() string

type GetData

type GetData func(*CacheRequest)

type GetDataChain

type GetDataChain []GetData

type Hadoop

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

func (*Hadoop) Close

func (hp *Hadoop) Close() error

协程安全问题

func (*Hadoop) New

func (hp *Hadoop) New(address string) error

func (*Hadoop) ReadTextFile

func (hp *Hadoop) ReadTextFile(path string) ([]string, error)

type HandlerFunc

type HandlerFunc func(*Chain)

type HandlersChain

type HandlersChain []HandlerFunc

func (HandlersChain) Last

func (c HandlersChain) Last() HandlerFunc

Last returns the last handler in the chain. ie. the last handler is the main own.

type Imgo

type Imgo interface {
	SaveRecord(collection, rd string) error
	UpdateRecord(collection string, id int64) error
	DelRecord(collection string, id int64) error
	GetRecord(collection string, id int64) (bson.M, error)
	LoadReord(collection string, from, to int64) ([]bson.M, error)
}

type KafkaConsumer

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

func NewKafkaConsumer

func NewKafkaConsumer(brokers []string, topic, group string, workerNum int, cb ConsumerCallback) *KafkaConsumer

func NewKafkaConsumerV2

func NewKafkaConsumerV2(brokers []string, topics []string, group string, workerNum int, cb ConsumerCallback) *KafkaConsumer

func (*KafkaConsumer) Close

func (kc *KafkaConsumer) Close()

func (*KafkaConsumer) Run

func (kc *KafkaConsumer) Run()

type KafkaProducer

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

func NewKafkaProducer

func NewKafkaProducer(addrs []string, topic string, bufSize int, timeout time.Duration, workerNum int) *KafkaProducer

func (*KafkaProducer) AddMsg

func (kw *KafkaProducer) AddMsg(key, data []byte) error

func (*KafkaProducer) AddMsgWithTopic

func (kw *KafkaProducer) AddMsgWithTopic(topic string, key, data []byte) error

func (*KafkaProducer) Close

func (kw *KafkaProducer) Close()

func (*KafkaProducer) Start

func (kw *KafkaProducer) Start()

type LocalCache

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

key=value 通用存储

func NewLocalCache

func NewLocalCache(size int, expireTime time.Duration) *LocalCache

func (*LocalCache) Get

func (localCache *LocalCache) Get(key interface{}) (interface{}, bool)

func (*LocalCache) IsHit

func (localCache *LocalCache) IsHit(key interface{}) bool

这个函数可能有性能瓶颈

func (*LocalCache) Len

func (localCache *LocalCache) Len() int

func (*LocalCache) Remove

func (localCache *LocalCache) Remove(key interface{})

func (*LocalCache) Set

func (localCache *LocalCache) Set(key, val interface{})

func (*LocalCache) SetWithTtl

func (localCache *LocalCache) SetWithTtl(key, val interface{}, ttl int)

type MongoDb

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

func InitMongo

func InitMongo(user, pwd, dbname string, servers []string) (*MongoDb, error)

func (*MongoDb) Find

func (mdb *MongoDb) Find(col string, query interface{}, result interface{}, sortFileds ...string) error

func (*MongoDb) FindAll

func (mdb *MongoDb) FindAll(col string, query interface{}, result interface{}, sortFileds ...string) error

func (*MongoDb) FindOne

func (mdb *MongoDb) FindOne(col string, query interface{}, result interface{}) error

func (*MongoDb) FindX

func (mdb *MongoDb) FindX(col string, query interface{}, result interface{}, count int, sortFileds ...string) error

func (*MongoDb) Insert

func (mdb *MongoDb) Insert(col string, record interface{}) error

func (*MongoDb) Remove

func (mdb *MongoDb) Remove(col string, selector interface{}) error

func (*MongoDb) Update

func (mdb *MongoDb) Update(col string, selector interface{}, update interface{}) error

func (*MongoDb) Upsert

func (mdb *MongoDb) Upsert(col string, selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)

type OrderSet

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

func NewWith

func NewWith(comparator utils.Comparator, size int) *OrderSet

func (*OrderSet) Add

func (ors *OrderSet) Add(items ...interface{})

func (*OrderSet) AddTopN

func (ors *OrderSet) AddTopN(items ...interface{})

func (*OrderSet) Clear

func (ors *OrderSet) Clear()

func (*OrderSet) Contains

func (ors *OrderSet) Contains(items ...interface{}) bool

func (*OrderSet) Empty

func (ors *OrderSet) Empty() bool

func (*OrderSet) Remove

func (ors *OrderSet) Remove(items ...interface{})

func (*OrderSet) Size

func (ors *OrderSet) Size() int

func (*OrderSet) String

func (ors *OrderSet) String() string

func (*OrderSet) Values

func (ors *OrderSet) Values() []interface{}

type RedisClient

type RedisClient struct {
	redis.Conn
	// contains filtered or unexported fields
}

func NewRedisClient

func NewRedisClient(address string, db int) *RedisClient

func (*RedisClient) Del

func (rc *RedisClient) Del(key string) error

func (*RedisClient) GetAllKeys

func (rc *RedisClient) GetAllKeys(pat string) (keys []string)

func (*RedisClient) GetInt

func (rc *RedisClient) GetInt(key string) (int, error)

func (*RedisClient) GetInt64

func (rc *RedisClient) GetInt64(key string) (int64, error)

func (*RedisClient) GetString

func (rc *RedisClient) GetString(key string) (string, error)

func (*RedisClient) HGet

func (rc *RedisClient) HGet(key, member string) (interface{}, error)

func (*RedisClient) HGetInt64

func (rc *RedisClient) HGetInt64(key, member string) (int64, error)

func (*RedisClient) HSet

func (rc *RedisClient) HSet(key, member string, val interface{}) error

func (*RedisClient) MultiGetString

func (rc *RedisClient) MultiGetString(key []string) ([]string, error)

func (*RedisClient) MultiSetString

func (rc *RedisClient) MultiSetString(members map[string]string, ex int) (err error)

func (*RedisClient) Remove

func (rc *RedisClient) Remove(key string)

func (*RedisClient) Scan

func (rc *RedisClient) Scan(pat string, offset int) (keys []string, iter int)

func (*RedisClient) SetInt

func (rc *RedisClient) SetInt(key string, val int, ex time.Duration) error

func (*RedisClient) SetInt64

func (rc *RedisClient) SetInt64(key string, val int64, ex time.Duration) error

func (*RedisClient) SetString

func (rc *RedisClient) SetString(key, val string, ex int) error

func (*RedisClient) ZAdd

func (rc *RedisClient) ZAdd(key string, members map[string]int64, ttl int)

func (*RedisClient) ZAddAndTrim

func (rc *RedisClient) ZAddAndTrim(key string, members map[string]int64, start int, stop int, ex time.Duration) error

func (*RedisClient) ZAddOne

func (rc *RedisClient) ZAddOne(key string, score int64, val string, limit int, ex time.Duration) error

func (*RedisClient) ZRange

func (rc *RedisClient) ZRange(key string, start int, stop int) ([]string, error)

func (*RedisClient) ZRangeWithScore

func (rc *RedisClient) ZRangeWithScore(key string, start int, stop int) (map[string]int64, error)

func (*RedisClient) ZRem

func (rc *RedisClient) ZRem(key, member string) (int, error)

func (*RedisClient) ZRemRangeByRank

func (rc *RedisClient) ZRemRangeByRank(key string, start int, stop int) (int, error)

func (*RedisClient) ZRevRange

func (rc *RedisClient) ZRevRange(key string, start int, stop int) ([]string, error)

func (*RedisClient) ZScore

func (rc *RedisClient) ZScore(key string, member string) (string, error)

type Ticker

type Ticker struct {
	// contains filtered or unexported fields
}
var TickerInstance *Ticker

func (*Ticker) AddFunc

func (tk *Ticker) AddFunc(spec string, cmd func()) error

type UIDFactory added in v1.0.5

type UIDFactory struct {
	IDFactory *snowflake.Node
}

func NewIDFactory added in v1.0.5

func NewIDFactory(node int64) (*UIDFactory, error)

type UdpClient

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

Jump to

Keyboard shortcuts

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