wph

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHttpDialTimeout         = 20 * time.Second
	DefaultHttpKeepAlive           = 120 * time.Second
	DefaultHttpMaxIdleConns        = 1000
	DefaultHttpMaxIdleConnsPerHost = 1000
	DefaultHttpIdleConnTimeout     = 90 * time.Second
	DefaultHttpTimeout             = 30 * time.Second
)

http请求相关默认配置

View Source
const (
	SnowflakeTimeUnit  = 1e7     // 时间单位,一纳秒的多少倍,1e6 = 一毫秒,1e7 = 百分之一秒,1e8 = 十分之一秒
	BitLenSequence     = 8       // 序列号的个数最多256个(0-255),即每单位时间并发数,如时间单位是1e7,则单实例qps = 25600
	BitLenDataCenterId = 3       // 数据中心个数最多8个(0-7),即同一个环境(生产、预发布、测试等)的数据中心(假设一个机房相同数据域的应用服务器集群只有一个,则数据中心数等于机房数)最多有8个
	BitLenMachineId    = 16      // 同一个数据中心下最多65536个应用实例(0-65535),默认是根据实例ip后两段算实例id(k8s环境动态创建Pod,也建议用此方式),所以需要预留255 * 255这么多
	BitLenTime         = 1 << 36 // 时间戳之差最大 = 2的36次方 * 时间单位 / 1e9 秒,目前的设计最多可以用21.79年就需要更新开始时间(随之还需要归档旧数据和更新次新数据id)

)

Snowflake程序id生成器 源于Twitter的Snowflake算法 但由于原版算法对应的分布式层级结构太简单,所以目前的算法实际是Sony对Snowflake算法的改进版本的Sonyflake算法 Sonyflake算法原版可参考github中的开源项目,当前算法有进一步细微调整

View Source
const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
	GBK     = Charset("GBK")
)

Variables

View Source
var (
	GB18030Decoder = simplifiedchinese.GB18030.NewDecoder()
	GBKDecoder     = simplifiedchinese.GBK.NewDecoder()
)
View Source
var SurNames = []string{
	"赵", "钱", "孙", "李", "周", "吴", "郑", "王",
	"冯", "陈", "卫", "蒋", "沈", "韩", "杨",
	"朱", "秦", "尤", "许", "何", "吕", "施", "张",
	"孔", "曹", "严", "金", "魏", "陶", "姜",
	"戚", "谢", "邹", "喻", "章",
	"云", "苏", "潘", "葛", "范", "彭", "郎",
	"鲁", "马", "花", "方",
	"俞", "任", "袁", "柳", "史", "唐",
}

Functions

func CheckMobile added in v1.2.6

func CheckMobile(mobile string) bool

验证手机号码 createdBy 周志彪

func Contains

func Contains(set interface{}, obj interface{}) bool

检查对象是否包含在指定集合内

func ConvertToEncodingBytes

func ConvertToEncodingBytes(bytes []byte, charset Charset) []byte

func Decompose

func Decompose(id uint64) map[string]uint64

func DefaultGoroutineRecover

func DefaultGoroutineRecover(l *logger.Logger, action string)

func GetCleanLocationName

func GetCleanLocationName(name string) string

获取干净的中国省份、城市、区域名称

func GetCurrentDir

func GetCurrentDir() (string, error)

获取当前执行目录

func GetFileMD5

func GetFileMD5(fileName string) (string, error)

获取文件MD5码

func GetPrivateIPv4Id

func GetPrivateIPv4Id() (uint16, error)

func GetRemoteIP

func GetRemoteIP(req *http.Request) string

获取客户端IP地址

func GetStack

func GetStack(skip int) []byte

获取调用堆栈信息

func GetUTCTicks

func GetUTCTicks(datetime time.Time) int64

获取Unix时间戳

func HttpGet

func HttpGet(reqUrl string, params map[string]interface{}) (string, error)

func HttpGetJsonHeader

func HttpGetJsonHeader(url string, params, header []byte) (string, error)

func HttpPostForm

func HttpPostForm(reqUrl string, params interface{}) (string, error)

func HttpPostJson

func HttpPostJson(reqUrl string, params interface{}) (string, error)

func HttpPostJsonHeader

func HttpPostJsonHeader(url string, params, header []byte) (string, error)

func HttpPostJsonWithToken

func HttpPostJsonWithToken(reqUrl string, params interface{}, token string) (string, error)

func IndexOfStr

func IndexOfStr(str, substr string) int

支持中文的字符串Index

func Insert

func Insert(db *xorm.Engine, beans ...interface{}) error

分页插入数据库

func InsertWithSize

func InsertWithSize(db *xorm.Engine, size int, beans ...interface{}) error

func InsertWithTableName

func InsertWithTableName(db *xorm.Engine, tableName string, beans ...interface{}) error

func InsertWithTableNameAndSize

func InsertWithTableNameAndSize(db *xorm.Engine, tableName string, size int, beans ...interface{}) error

func NewPoolingHttpClient

func NewPoolingHttpClient() *http.Client

返回一个采用了连接池和Keepalive配置的http client,可以配合RequestPromise的SetClient函数使用 默认不使用它,而是每次请求新建连接

func PrivateIPv4

func PrivateIPv4() (net.IP, error)

func RandFloat added in v1.2.6

func RandFloat(min, max float64) float64

func RandInt added in v1.2.6

func RandInt(min, max int) int

func RandIntWithZero added in v1.2.6

func RandIntWithZero(min, max int) int

func SplitChineseName

func SplitChineseName(name string) (string, string)

func SubStr

func SubStr(input string, begin, end int) string

支持中文的字符串截取, end传-1为无限大

func TransInsert

func TransInsert(trans *xorm.Session, beans ...interface{}) error

func TransInsertWithSize

func TransInsertWithSize(trans *xorm.Session, size int, beans ...interface{}) error

func TransInsertWithTableName

func TransInsertWithTableName(trans *xorm.Session, tableName string, beans ...interface{}) error

func TransInsertWithTableNameAndSize

func TransInsertWithTableNameAndSize(trans *xorm.Session, tableName string, size int, beans ...interface{}) error

func TransformObject added in v1.2.3

func TransformObject(req interface{}, targetObject interface{}) error

用于请求参数,与实体stautc转换 createdBy 周志彪

Types

type Charset

type Charset string

type IErrorCode

type IErrorCode interface {
	// 转换为int
	ToInt() int
	// 方便与int比较
	Equals(errCode int) bool
	// 获取错误信息
	ErrMsg(args ...interface{}) string
}

Error Code接口

type Long

type Long int64

func NextId

func NextId() Long

获取新的程序生成的编号

func ParseLong

func ParseLong(str string) Long

func (Long) FromString

func (t Long) FromString(str string) Long

func (Long) IsZero

func (t Long) IsZero() bool

func (Long) MarshalJSON

func (t Long) MarshalJSON() ([]byte, error)

func (Long) MarshalText

func (t Long) MarshalText() ([]byte, error)

func (*Long) Scan

func (t *Long) Scan(value interface{}) error

func (Long) String

func (t Long) String() string

func (*Long) UnmarshalJSON

func (t *Long) UnmarshalJSON(value []byte) error

func (*Long) UnmarshalText

func (t *Long) UnmarshalText(data []byte) error

func (Long) Value

func (t Long) Value() (driver.Value, error)

type PagingResult

type PagingResult struct {
	TotalCount int64       `json:"totalCount" xml:"TotalCount"` // 总大小
	PageSize   int64       `json:"pageSize" xml:"PageSize"`     // 分页大小
	PageIndex  int64       `json:"pageIndex" xml:"PageIndex"`   // 页面索引
	Data       interface{} `json:"data" xml:"Result"`           // 分页结果
}

分页数据对象

type RequestPromise

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

http请求配置

func JSONRequest

func JSONRequest() *RequestPromise

返回一个http请求配置对象,默认带上压缩头和Content-Type = application/json; charset=utf-8

func NewRequest

func NewRequest() *RequestPromise

返回一个http请求配置对象,默认带上压缩头

func XMLRequest

func XMLRequest() *RequestPromise

返回一个http请求配置对象,默认带上压缩头和Content-Type = application/xml; charset=utf-8

func (*RequestPromise) Call

func (r *RequestPromise) Call(httpMethod string, targetUri string, data io.Reader) ([]byte, error)

发起请求并返回响应内容 FORM方式提交数据请设置Content-Type=application/x-www-form-urlencoded请求头,且io.Reader传url.Values.Encode得到的字符串的reader

func (RequestPromise) MarshalEasyJSON added in v1.2.4

func (v RequestPromise) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (RequestPromise) MarshalJSON added in v1.2.4

func (v RequestPromise) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*RequestPromise) SetClient

func (r *RequestPromise) SetClient(client *http.Client) *RequestPromise

设置事先实例化好的http client,默认每次请求会新建一个http client

func (*RequestPromise) SetEncoding

func (r *RequestPromise) SetEncoding(encoding Charset) *RequestPromise

设置http响应的编码,默认utf8

func (*RequestPromise) SetHeader

func (r *RequestPromise) SetHeader(key string, value string) *RequestPromise

设置http header

func (*RequestPromise) SetHttpProxy

func (r *RequestPromise) SetHttpProxy(proxyUri string) *RequestPromise

设置http或https代理,默认无代理

func (*RequestPromise) SetSkipTls

func (r *RequestPromise) SetSkipTls() *RequestPromise

设置https忽略本地证书校验

func (*RequestPromise) SetSocket5Proxy

func (r *RequestPromise) SetSocket5Proxy(proxyUri string) *RequestPromise

设置socket5代理,默认无代理

func (*RequestPromise) SetTimeout

func (r *RequestPromise) SetTimeout(timeout time.Duration) *RequestPromise

设置超时时间,从连接到接收到响应的总时间 如果此处不设置则采用http client中设置的超时时间,默认http client超时时间30秒 如果此处设置不等于零的值,则覆盖http client中设置的超时时间 如果此处设置小于零的值,意为不超时

func (*RequestPromise) UnmarshalEasyJSON added in v1.2.4

func (v *RequestPromise) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*RequestPromise) UnmarshalJSON added in v1.2.4

func (v *RequestPromise) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Res

type Res struct {
	ResHead
	Body interface{} `json:"body,omitempty"`
}

func E

func E(status int, errMsg string, errDetail interface{}) *Res

func E2

func E2(errDetail interface{}, errCode IErrorCode, args ...interface{}) *Res

func R

func R(body interface{}) *Res

func (Res) MarshalEasyJSON added in v1.2.4

func (v Res) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Res) MarshalJSON added in v1.2.4

func (v Res) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Res) UnmarshalEasyJSON added in v1.2.4

func (v *Res) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Res) UnmarshalJSON added in v1.2.4

func (v *Res) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type ResHead

type ResHead struct {
	Status     int    `json:"status"`
	Msg        string `json:"msg,omitempty"`
	Detail     string `json:"-"`
	HttpStatus int    `json:"-"`
}

func (ResHead) MarshalEasyJSON added in v1.2.4

func (v ResHead) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (ResHead) MarshalJSON added in v1.2.4

func (v ResHead) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*ResHead) UnmarshalEasyJSON added in v1.2.4

func (v *ResHead) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*ResHead) UnmarshalJSON added in v1.2.4

func (v *ResHead) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Snowflake

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

func NewSnowflake

func NewSnowflake(st SnowflakeSettings) *Snowflake

func SingletonSnowflakeKeyGen

func SingletonSnowflakeKeyGen() *Snowflake

func (*Snowflake) NextId

func (sf *Snowflake) NextId() (uint64, error)

type SnowflakeSettings

type SnowflakeSettings struct {
	StartTime      time.Time
	DataCenterId   func() (uint16, error)
	MachineId      func() (uint16, error)
	CheckMachineId func(uint16) bool
}

Directories

Path Synopsis
github.com
axgle/mahonia
This package is a character-set conversion library for Go.
This package is a character-set conversion library for Go.
* Copyright (c) 2015, zheng-ji.info *
* Copyright (c) 2015, zheng-ji.info *
montnets
mq
* The MIT License (MIT) * * Copyright (c) 2016 tony<wuhaiyang1213@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.
* The MIT License (MIT) * * Copyright (c) 2016 tony<wuhaiyang1213@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.

Jump to

Keyboard shortcuts

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