Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/peer"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
moOpt protojson.MarshalOptions
|
||||
uoOpt protojson.UnmarshalOptions
|
||||
)
|
||||
|
||||
func init() {
|
||||
moOpt = protojson.MarshalOptions{
|
||||
// Multiline: true,
|
||||
AllowPartial: true,
|
||||
UseProtoNames: true,
|
||||
UseEnumNumbers: true,
|
||||
EmitUnpopulated: true,
|
||||
}
|
||||
uoOpt = protojson.UnmarshalOptions{
|
||||
DiscardUnknown: true,
|
||||
}
|
||||
}
|
||||
|
||||
// PbCopy
|
||||
// @description: 将from对象的内容copy给to对象
|
||||
// parameter:
|
||||
// @from:proto.Message
|
||||
// @to:proto.Message
|
||||
// return:
|
||||
// @error:如果失败,则返回错误,否则返回nil
|
||||
func PbCopy(from, to proto.Message) error {
|
||||
data, err := proto.Marshal(from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return proto.Unmarshal(data, to)
|
||||
}
|
||||
|
||||
// Pb2Json
|
||||
// @description: 将pb对象转换为json字符串
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @string:pb对象的json标识形式
|
||||
// @error:
|
||||
func Pb2Json(m proto.Message) (string, error) {
|
||||
str, err := moOpt.Marshal(m)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(str), nil
|
||||
}
|
||||
|
||||
// Json2Pb
|
||||
// @description: 将json字符串转换成对应pb对象
|
||||
// parameter:
|
||||
// @js:
|
||||
// @m:
|
||||
// return:
|
||||
// @error:
|
||||
func Json2Pb(js string, m proto.Message) error {
|
||||
return uoOpt.Unmarshal([]byte(js), m)
|
||||
}
|
||||
|
||||
// Marshal
|
||||
// @description: 序列化pb对象
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @[]byte:
|
||||
// @error:
|
||||
func Marshal(m proto.Message) ([]byte, error) {
|
||||
data, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// Marshal_Panic
|
||||
// @description: 序列化pb对象
|
||||
// parameter:
|
||||
// @m:
|
||||
// return:
|
||||
// @[]byte:
|
||||
func Marshal_Panic(m proto.Message) []byte {
|
||||
data, err := Marshal(m)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("pbUtil Marshal pb错误,err:%s", err))
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// Unmarshal
|
||||
// @description: 将数据转换为pb对象
|
||||
// parameter:
|
||||
// @data:
|
||||
// @m:
|
||||
// return:
|
||||
func Unmarshal(data []byte, m proto.Message) error {
|
||||
err := proto.Unmarshal(data, m)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetClientIP
|
||||
// @description: 获取客户端Ip
|
||||
// parameter:
|
||||
// @ctx:grpc底层传递过来的上下文对象
|
||||
// return:
|
||||
// @string:客户端ip
|
||||
// @error:错误对象
|
||||
func GetClientIP(ctx context.Context) (string, error) {
|
||||
pr, ok := peer.FromContext(ctx)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("GetClietIP未获取到客户端ip")
|
||||
}
|
||||
if pr.Addr == net.Addr(nil) {
|
||||
return "", fmt.Errorf("GetClientIP 获取到的peer.Addr=nil")
|
||||
}
|
||||
addSlice := strings.Split(pr.Addr.String(), ":")
|
||||
|
||||
return addSlice[0], nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package gameServerMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestActive(t *testing.T) {
|
||||
err := ActiveServer("https://managecenterapitest-xxx.79yougame.com/API/ServerActivate.ashx", 20002)
|
||||
if err != nil {
|
||||
fmt.Println("xxx")
|
||||
}
|
||||
|
||||
CheckNewResourceVersion(1001, 20002, 100, "1584085505_769926880ac0ae89a31dcdfef5b94b1e")
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package impl_console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
}
|
||||
|
||||
// NewLogger
|
||||
// @description: 构造控制台日志
|
||||
// parameter:
|
||||
// return:
|
||||
// @*Logger:
|
||||
func NewLogger() *Logger {
|
||||
return &Logger{}
|
||||
}
|
||||
|
||||
// InfoLog
|
||||
// @description: 信息日志记录
|
||||
// parameter:
|
||||
// @format:日志格式
|
||||
// @args:参数列表
|
||||
// return:
|
||||
func (cl *Logger) InfoLog(format string, args ...interface{}) {
|
||||
c := color.New(color.FgGreen)
|
||||
s := c.Sprint("Info:")
|
||||
fmt.Println(s, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// DebugLog
|
||||
// @description: 调试日志记录
|
||||
// parameter:
|
||||
// @format:日志格式
|
||||
// @args:参数列表
|
||||
// return:
|
||||
func (cl *Logger) DebugLog(format string, args ...interface{}) {
|
||||
c := color.New(color.FgGreen)
|
||||
s := c.Sprint("Debug:")
|
||||
fmt.Println(s, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// WarnLog
|
||||
// @description: 警告日志记录
|
||||
// parameter:
|
||||
// @format:日志格式
|
||||
// @args:参数列表
|
||||
// return:
|
||||
func (cl *Logger) WarnLog(format string, args ...interface{}) {
|
||||
c := color.New(color.FgYellow)
|
||||
_, _ = c.Println("Warn:", fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// ErrorLog
|
||||
// @description: 错误日志记录
|
||||
// parameter:
|
||||
// @format:日志格式
|
||||
// @args:参数列表
|
||||
// return:
|
||||
func (cl *Logger) ErrorLog(format string, args ...interface{}) {
|
||||
c := color.New(color.FgRed)
|
||||
_, _ = c.Println("Error:", fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// FatalLog
|
||||
// @description: 致命错误日志记录
|
||||
// parameter:
|
||||
// @format:日志格式
|
||||
// @args:参数列表
|
||||
// return:
|
||||
func (cl *Logger) FatalLog(format string, args ...interface{}) {
|
||||
c := color.New(color.FgRed)
|
||||
_, _ = c.Println("Fatal:", fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// CloseLog
|
||||
// @description: 关闭日志
|
||||
// parameter:
|
||||
// @waitFinish:是否等待日志
|
||||
// return:
|
||||
func (cl *Logger) CloseLog(waitFinish bool) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package redisUtil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gomodule/redigo/redis"
|
||||
)
|
||||
|
||||
// 订阅回调函数
|
||||
type SubscribeCallback func() error
|
||||
|
||||
// Subscriber
|
||||
// @description: 订阅者
|
||||
type Subscriber struct {
|
||||
// pool 订阅者连接池
|
||||
pool *RedisPool
|
||||
// callBack 订阅者回调函数
|
||||
callBack SubscribeCallback
|
||||
}
|
||||
|
||||
// NewSubscriber
|
||||
// @description: 构建一个订阅者
|
||||
// parameter:
|
||||
// @pool:
|
||||
// @callBack:
|
||||
// return:
|
||||
// @*Subscriber:
|
||||
func NewSubscriber(pool *RedisPool, callBack SubscribeCallback) *Subscriber {
|
||||
return &Subscriber{pool: pool, callBack: callBack}
|
||||
}
|
||||
|
||||
// Promulgator
|
||||
// @description: 发布者
|
||||
type Promulgator struct {
|
||||
// pool 发布者连接池
|
||||
pool *RedisPool
|
||||
}
|
||||
|
||||
// NewPromulgator
|
||||
// @description: 构建一个发布者
|
||||
// parameter:
|
||||
// @pool:
|
||||
// return:
|
||||
// @*Promulgator:
|
||||
func NewPromulgator(pool *RedisPool) *Promulgator {
|
||||
return &Promulgator{pool: pool}
|
||||
}
|
||||
|
||||
// Publish
|
||||
// @description: 发布消息
|
||||
// parameter:
|
||||
// @receiver s:
|
||||
// @channel:
|
||||
// @message:
|
||||
// return:
|
||||
// @error:
|
||||
func (s *Promulgator) Publish(channel, message string) error {
|
||||
c := s.pool.GetConnection()
|
||||
defer c.Close()
|
||||
|
||||
_, err := c.Do("PUBLISH", channel, message)
|
||||
if err != nil {
|
||||
return fmt.Errorf("redis publish %s %s, err: %v", channel, message, err)
|
||||
}
|
||||
//n, err := s.pool.Int(result)
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("redis publish %s %s, err: %v", channel, message, err)
|
||||
//}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Subscribe
|
||||
// @description: 订阅者订阅消息
|
||||
// parameter:
|
||||
// @receiver s:
|
||||
// @ctx:
|
||||
// @channel: 频道
|
||||
// return:
|
||||
// @error:
|
||||
func (s *Subscriber) Subscribe(ctx context.Context, channel ...string) error {
|
||||
sub := redis.PubSubConn{Conn: s.pool.GetConnection()}
|
||||
if err := sub.Subscribe(redis.Args{}.AddFlat(channel)...); err != nil {
|
||||
return err
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
|
||||
// 启动一个新协程去持续订阅消息
|
||||
go func() {
|
||||
defer sub.Close()
|
||||
for {
|
||||
switch msg := sub.Receive().(type) {
|
||||
case error:
|
||||
done <- fmt.Errorf("redis pubsub receive err: %v", msg)
|
||||
return
|
||||
case redis.Message:
|
||||
if err := s.callBack(); err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
case redis.Subscription:
|
||||
if msg.Count == 0 {
|
||||
// 所有的订阅者都退出
|
||||
done <- nil
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// health check
|
||||
tick := time.NewTicker(time.Minute)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err := sub.Unsubscribe(); err != nil {
|
||||
return fmt.Errorf("redis pubsub unsubscribe err: %v", err)
|
||||
}
|
||||
return nil
|
||||
case err := <-done:
|
||||
return err
|
||||
case <-tick.C:
|
||||
if err := sub.Ping(""); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user