dist

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2020 License: MIT Imports: 26 Imported by: 0

README

goredist

go libraries redistros for quick development. its still in progress.

libraries

  • zap
  • nats
  • nats-streaming
  • zookeeper
  • redis
  • database(gorm)
  • gin
  • jwt
  • sms
  • email(smtp)
  • go error string
  • version
  • and more...

Documentation

Index

Constants

View Source
const (
	LI_FUNC_MULTI = 1000000
	LI_CATA_MULTI = 10000
	LI_SUB_MULTI  = 100
	LI_INST_MULTI = 1
	LI_MAX_ID     = 100
)

Variables

View Source
var (
	MESSAGE_SUCCESS   string = "OK"
	ACCESS_KEY_ID     string = "LTAIDquqdpFdYVmW"
	ACCESS_KEY_SECRET string = "ozeRm4LWSqEnpPEw9spdOfylSNprY1"
	SIGN_NAME         string = "海拉科技"
	TEMPLATE_CODE     string = "SMS_127790203"
	PRODUCT           string = "Dysmsapi"
	VERSION           string = "2017-05-25"
	DOMAIN            string = "dysmsapi.aliyuncs.com"
	REGION            string = "cn-hangzhou"

	ErrReturnValue = errors.New("sms return message means failed.")
)
View Source
var (
	ErrHttpStatusCode = errors.New("http return bad status code.")
)
View Source
var (
	ErrInvalidLogicId = errors.New("Invalid logic id")
)
设计规则

每个逻辑服务器都有一个ID,叫 logicId,对应一个逻辑服务器"实例" logicId = funcId(1-999) + cataId(1-99) + subId(1-99) + instId(1-99) 其中 funcId 代表一个逻辑功能 比如斗地主游戏 cataId 代表一个分类,比如中级场 subId 子分类, 比如癞子 instId 实例, 比如癞子斗地主中级场启动了 2 个实例用于负载均衡,可能 instId 分别对应1,2 gameId 对应一个游戏,忽略 instId 由其他部分组成

实例: 1 斗地主, 1 初级场 1 普通斗地主 两个实例分别为 1, 2 则这两个进程的ID 分别为 1010101, 1010102 gameId 都为 1010100

View Source
var (
	ErrInvalidParams = errors.New("invalid parameters")
)

Functions

func ConnectRedis

func ConnectRedis(addr string, password string, db int32) (*redis.Client, error)

func ConnectRedisCluster

func ConnectRedisCluster(addrs []string, password string, db int32) (*redis.ClusterClient, error)

@data: redis 连接参数 json,来自 zookeeper @conf

func GetCataIdByLogicId

func GetCataIdByLogicId(logicId uint64) (uint64, error)

func GetFuncIdByLogicId

func GetFuncIdByLogicId(logicId uint64) (uint64, error)

sugar functions

func GetGameIdByLogicId

func GetGameIdByLogicId(logicId uint64) (uint64, error)

func GetMacInfoImmutablePart

func GetMacInfoImmutablePart(mac *MachMeta)

func GetMachInfo

func GetMachInfo(mac *MachMeta)

func GetMachInfoMutablePart

func GetMachInfoMutablePart(mac *MachMeta)

func GetSubIdByLogicId

func GetSubIdByLogicId(logicId uint64) (uint64, error)

func SendSmsTo

func SendSmsTo(phone string, text string) error

Types

type AppInfo

type AppInfo struct {
	Name     string        // 程序名,和文件名无关,编译时指定
	InstName string        // 进程实例名称
	BootAt   time.Time     // 启动时间
	Ver      *Version      // 版本号
	ConfFile string        // 配置文件名
	Log      *log.SLogger  // 日志对象
	Rdb      *redis.Client // redis 客户端
	Ev       *mq.Eventbus  // 事件总线
	Mq       *mq.Mq        // 消息队列
	Zk       *ZkClient     // zookeeper
	Debug    bool
	Mode     string
	Offline  int32         // 是否下线 0 不下线
	ChQuit   chan struct{} // 进程退出
	// contains filtered or unexported fields
}

一个程序的基本信息

func NewAppInfo

func NewAppInfo(programeName string, major, minor, revision int, id uint64, confFile string, mode string) *AppInfo

func (*AppInfo) EnableLog

func (self *AppInfo) EnableLog(console bool, level int32, logPath string) error

func (*AppInfo) RegisterSelf

func (self *AppInfo) RegisterSelf(parentPath string) error

add a node to zookeeper

type AppInfoOptions

type AppInfoOptions func(info *AppInfo)

func AppEnableLogger

func AppEnableLogger(enable bool, logLevel int32, logPath string, logStdout bool) AppInfoOptions

type Event

type Event struct {
	Err  error
	Path string
	Type zk.EventType
}

watcher 发送的通知

type HttpClient

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

func NewHttpClient

func NewHttpClient() *HttpClient

func (*HttpClient) LastStatusCode

func (self *HttpClient) LastStatusCode() int

func (*HttpClient) PostJson

func (self *HttpClient) PostJson(url string, json []byte) ([]byte, error)

post json and return json

type HttpGin

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

func NewHttpGinServer

func NewHttpGinServer(addr string, options ...HttpGinOptions) *HttpGin

func (*HttpGin) Start

func (self *HttpGin) Start(fnSetupRouter func(*gin.Engine)) error

func (*HttpGin) Stop

func (self *HttpGin) Stop() error

type HttpGinOptions

type HttpGinOptions func(*HttpGin)

func EnableBlacklist

func EnableBlacklist(b []string) HttpGinOptions

func EnableCors

func EnableCors(enable bool) HttpGinOptions

func EnableDebug

func EnableDebug(enable bool) HttpGinOptions

func EnableFlowControl

func EnableFlowControl(enable bool) HttpGinOptions

func EnableHTML

func EnableHTML(enable bool, ptn string) HttpGinOptions

func EnableSession

func EnableSession(enable bool, store sessions.Store) HttpGinOptions

func EnableWhitelist

func EnableWhitelist(b []string) HttpGinOptions

type JwtTokener

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

func NewJwtTokener

func NewJwtTokener(key string) *JwtTokener

func (*JwtTokener) Parse

func (self *JwtTokener) Parse(token string) (jwt.MapClaims, error)

func (*JwtTokener) Token

func (self *JwtTokener) Token(m map[string]interface{}) (string, error)

type LogicID

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

func NewLogicIDByID

func NewLogicIDByID(id uint64) (*LogicID, error)

func NewLogicIDByParts

func NewLogicIDByParts(funcId, cataId, subId, instId uint64) (*LogicID, error)

func (*LogicID) CataId

func (self *LogicID) CataId() uint64

func (*LogicID) FuncId

func (self *LogicID) FuncId() uint64

func (*LogicID) GameId

func (self *LogicID) GameId() uint64

func (*LogicID) HeaderCata

func (self *LogicID) HeaderCata() uint64

func (*LogicID) HeaderSub

func (self *LogicID) HeaderSub() uint64

func (*LogicID) Id

func (self *LogicID) Id() uint64

func (*LogicID) InstId

func (self *LogicID) InstId() uint64

func (*LogicID) SubId

func (self *LogicID) SubId() uint64

type MachMeta

type MachMeta struct {
	MemTotal     uint64
	MemUsed      uint64
	MemAvailable uint64
	MemPercent   float64
	CpuNum       int
	CpuCoreNum   int32
	CpuModelName string
	CpuPercent   []float64
	Hostname     string
	UpTime       uint64
	BootTime     uint64
	OS           string
	Platform     string
	KernelVer    string
	Arch         string
}

服务器硬件信息

type Mail

type Mail struct {
	Subject     string
	Body        string
	Attachments []string
}

type MailReceiver

type MailReceiver struct {
	Name string
	Addr string
}

type Mailer

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

func NewMailer

func NewMailer(smtp string, port int, username, password string) *Mailer

func (*Mailer) Send

func (self *Mailer) Send(mail Mail, receivers ...MailReceiver) error

type Version

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

func NewVersion

func NewVersion(major, minor, revision int) *Version

func (*Version) Major

func (self *Version) Major() int

func (*Version) Minor

func (self *Version) Minor() int

func (*Version) Revision

func (self *Version) Revision() int

func (*Version) String

func (self *Version) String() string

type ZkCallback

type ZkCallback func(c *ZkClient, path string, ev zk.EventType)

/ 路径规则根据 unix 路径规则

type ZkClient

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

一个到 zookeeper 的连接

func NewZkClient

func NewZkClient(hosts []string, root string, user string, password string, timeout int, cb ZkCallback) (*ZkClient, error)

创建 zookeeper 连接 timeout 秒

func (*ZkClient) Close

func (self *ZkClient) Close()

func (*ZkClient) CreateEmpNode

func (self *ZkClient) CreateEmpNode(name string, data []byte) error

更新节点数据,不存在则创建

func (*ZkClient) Exists

func (self *ZkClient) Exists(name string) bool

节点是否存在

func (*ZkClient) GetChildren

func (self *ZkClient) GetChildren(name string) ([]string, error)

获取子节点列表

func (*ZkClient) GetNodeData

func (self *ZkClient) GetNodeData(name string) ([]byte, error)

获得节点数据

func (*ZkClient) SetNodeData

func (self *ZkClient) SetNodeData(path string, data []byte) error

func (*ZkClient) WatchChildren

func (self *ZkClient) WatchChildren(name string) error

节点监控,目前通知不能详细到

func (*ZkClient) WatchNode

func (self *ZkClient) WatchNode(name string) error

Directories

Path Synopsis
e

Jump to

Keyboard shortcuts

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