优化代码规范

This commit is contained in:
duanhf2012
2024-09-20 17:25:08 +08:00
parent 1cf071a444
commit 39116c4402
63 changed files with 1971 additions and 2043 deletions

View File

@@ -9,7 +9,7 @@ import (
func NewAesEncrypt(key string) (aes *AesEncrypt, err error) {
keyLen := len(key)
if keyLen < 16 {
err = fmt.Errorf("The length of res key shall not be less than 16")
err = fmt.Errorf("the length of res key shall not be less than 16")
return
}
aes = &AesEncrypt{
@@ -22,12 +22,12 @@ type AesEncrypt struct {
StrKey string
}
func (this *AesEncrypt) getKey() []byte {
keyLen := len(this.StrKey)
func (ae *AesEncrypt) getKey() []byte {
keyLen := len(ae.StrKey)
if keyLen < 16 {
panic("The length of res key shall not be less than 16")
}
arrKey := []byte(this.StrKey)
arrKey := []byte(ae.StrKey)
if keyLen >= 32 {
//取前32个字节
return arrKey[:32]
@@ -40,37 +40,37 @@ func (this *AesEncrypt) getKey() []byte {
return arrKey[:16]
}
//加密字符串
func (this *AesEncrypt) Encrypt(strMesg string) ([]byte, error) {
key := this.getKey()
var iv = []byte(key)[:aes.BlockSize]
encrypted := make([]byte, len(strMesg))
// Encrypt 加密字符串
func (ae *AesEncrypt) Encrypt(str string) ([]byte, error) {
key := ae.getKey()
var iv = key[:aes.BlockSize]
encrypted := make([]byte, len(str))
aesBlockEncrypter, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
aesEncrypter.XORKeyStream(encrypted, []byte(strMesg))
aesEncrypter.XORKeyStream(encrypted, []byte(str))
return encrypted, nil
}
//解密字符串
func (this *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) {
// Decrypt 解密字符串
func (ae *AesEncrypt) Decrypt(src []byte) (strDesc string, err error) {
defer func() {
//错误处理
if e := recover(); e != nil {
err = e.(error)
}
}()
key := this.getKey()
var iv = []byte(key)[:aes.BlockSize]
key := ae.getKey()
var iv = key[:aes.BlockSize]
decrypted := make([]byte, len(src))
var aesBlockDecrypter cipher.Block
aesBlockDecrypter, err = aes.NewCipher([]byte(key))
aesBlockDecrypter, err = aes.NewCipher(key)
if err != nil {
return "", err
}
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
aesDecrypter.XORKeyStream(decrypted, src)
return string(decrypted), nil
}
}

View File

@@ -42,7 +42,7 @@ func setBitTagByIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number
func GetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (bool, error) {
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
if ret == false {
return false, errors.New("Invalid parameter")
return false, errors.New("invalid parameter")
}
return (bitBuff[sliceIndex] & (1 << sliceBitIndex)) > 0, nil

View File

@@ -4,7 +4,7 @@ import (
"sync"
)
type IBytesMempool interface {
type IBytesMemPool interface {
MakeBytes(size int) []byte
ReleaseBytes(byteBuff []byte) bool
}

View File

@@ -7,18 +7,18 @@ import (
"runtime/debug"
)
func F(callback interface{},recoverNum int, args ...interface{}) {
func F(callback interface{}, recoverNum int, args ...interface{}) {
defer func() {
if r := recover(); r != nil {
var coreInfo string
coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
log.SError(coreInfo)
if recoverNum > 0{
if recoverNum > 0 {
recoverNum -= 1
}
if recoverNum == -1 || recoverNum > 0 {
go F(callback,recoverNum, args...)
go F(callback, recoverNum, args...)
}
}
}()
@@ -36,10 +36,10 @@ func F(callback interface{},recoverNum int, args ...interface{}) {
}
func Go(callback interface{}, args ...interface{}) {
go F(callback,0, args...)
go F(callback, 0, args...)
}
//-1表示一直恢复
func GoRecover(callback interface{},recoverNum int, args ...interface{}) {
go F(callback,recoverNum, args...)
}
// GoRecover -1表示一直恢复
func GoRecover(callback interface{}, recoverNum int, args ...interface{}) {
go F(callback, recoverNum, args...)
}

View File

@@ -39,12 +39,13 @@ func RandN[E ~[]T, T any](arr E, num int) []T {
}
Shuffle(index)
if len(index) > num {
index = index
index = index[:num]
}
ret := make([]T, 0, len(index))
for i := range index {
ret = append(ret, arr[index[i]])
}
return ret
}
@@ -104,4 +105,4 @@ func GetPointerFunc[E ~[]T, T any](arr E, f func(T) bool) *T {
}
return &arr[index]
}
}

View File

@@ -5,34 +5,32 @@ import (
)
type Pool struct {
C chan interface{} //最大缓存的数量
C chan interface{} //最大缓存的数量
syncPool sysSync.Pool
}
type IPoolData interface {
Reset()
IsRef()bool
IsRef() bool
Ref()
UnRef()
}
type PoolEx struct{
C chan IPoolData //最大缓存的数量
type PoolEx struct {
C chan IPoolData //最大缓存的数量
syncPool sysSync.Pool
}
func (pool *Pool) Get() interface{}{
func (pool *Pool) Get() interface{} {
select {
case d := <-pool.C:
return d
default:
return pool.syncPool.Get()
}
return nil
}
func (pool *Pool) Put(data interface{}){
func (pool *Pool) Put(data interface{}) {
select {
case pool.C <- data:
default:
@@ -41,14 +39,14 @@ func (pool *Pool) Put(data interface{}){
}
func NewPool(C chan interface{},New func()interface{}) *Pool{
func NewPool(C chan interface{}, New func() interface{}) *Pool {
var p Pool
p.C = C
p.syncPool.New = New
return &p
}
func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{
func NewPoolEx(C chan IPoolData, New func() IPoolData) *PoolEx {
var pool PoolEx
pool.C = C
pool.syncPool.New = func() interface{} {
@@ -57,7 +55,7 @@ func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{
return &pool
}
func (pool *PoolEx) Get() IPoolData{
func (pool *PoolEx) Get() IPoolData {
select {
case d := <-pool.C:
if d.IsRef() {
@@ -79,7 +77,7 @@ func (pool *PoolEx) Get() IPoolData{
return nil
}
func (pool *PoolEx) Put(data IPoolData){
func (pool *PoolEx) Put(data IPoolData) {
if data.IsRef() == false {
panic("Repeatedly freeing memory")
}
@@ -94,5 +92,3 @@ func (pool *PoolEx) Put(data IPoolData){
pool.syncPool.Put(data)
}
}

View File

@@ -6,11 +6,10 @@ import (
"github.com/duanhf2012/origin/v2/util/sync"
"reflect"
"runtime"
"time"
"sync/atomic"
"time"
)
// ITimer
type ITimer interface {
GetId() uint64
Cancel()
@@ -27,10 +26,9 @@ type ITimer interface {
type OnCloseTimer func(timer ITimer)
type OnAddTimer func(timer ITimer)
// Timer
type Timer struct {
Id uint64
cancelled int32 //是否关闭
cancelled int32 //是否关闭
C chan ITimer //定时器管道
interval time.Duration // 时间间隔(用于循环定时器)
fireTime time.Time // 触发时间
@@ -46,12 +44,10 @@ type Timer struct {
ref bool
}
// Ticker
type Ticker struct {
Timer
}
// Cron
type Cron struct {
Timer
}
@@ -101,7 +97,6 @@ func releaseCron(cron *Cron) {
cronPool.Put(cron)
}
// one dispatcher per goroutine (goroutine not safe)
type Dispatcher struct {
ChanTimer chan ITimer
}
@@ -132,7 +127,7 @@ func (t *Timer) Do() {
buf := make([]byte, 4096)
l := runtime.Stack(buf, false)
errString := fmt.Sprint(r)
log.Dump(string(buf[:l]),log.String("error",errString))
log.Dump(string(buf[:l]), log.String("error", errString))
}
}()
@@ -172,10 +167,10 @@ func (t *Timer) GetInterval() time.Duration {
}
func (t *Timer) Cancel() {
atomic.StoreInt32(&t.cancelled,1)
atomic.StoreInt32(&t.cancelled, 1)
}
// 判断定时器是否已经取消
// IsActive 判断定时器是否已经取消
func (t *Timer) IsActive() bool {
return atomic.LoadInt32(&t.cancelled) == 0
}
@@ -218,7 +213,7 @@ func (c *Cron) Do() {
buf := make([]byte, 4096)
l := runtime.Stack(buf, false)
errString := fmt.Sprint(r)
log.Dump(string(buf[:l]),log.String("error",errString))
log.Dump(string(buf[:l]), log.String("error", errString))
}
}()
@@ -274,7 +269,7 @@ func (c *Ticker) Do() {
buf := make([]byte, 4096)
l := runtime.Stack(buf, false)
errString := fmt.Sprint(r)
log.Dump(string(buf[:l]),log.String("error",errString))
log.Dump(string(buf[:l]), log.String("error", errString))
}
}()