-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jchicode <[email protected]>
- Loading branch information
Showing
11 changed files
with
192 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022-present The ZTDBP Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import "sync" | ||
|
||
var ( | ||
byteSlicePool = sync.Pool{ | ||
New: func() interface{} { | ||
return []byte{} | ||
}, | ||
} | ||
byteSliceChan = make(chan []byte, 10) | ||
) | ||
|
||
func ByteSliceGet(length int) (data []byte) { | ||
select { | ||
case data = <-byteSliceChan: | ||
default: | ||
data = byteSlicePool.Get().([]byte)[:0] | ||
} | ||
|
||
if cap(data) < length { | ||
data = make([]byte, length) | ||
} else { | ||
data = data[:length] | ||
} | ||
|
||
return data | ||
} | ||
|
||
func ByteSlicePut(data []byte) { | ||
select { | ||
case byteSliceChan <- data: | ||
default: | ||
byteSlicePool.Put(data) //nolint:staticcheck | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022-present The ZTDBP Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
var ( | ||
bytesBufferPool = sync.Pool{ | ||
New: func() interface{} { | ||
return &bytes.Buffer{} | ||
}, | ||
} | ||
bytesBufferChan = make(chan *bytes.Buffer, 10) | ||
) | ||
|
||
func BytesBufferGet() (data *bytes.Buffer) { | ||
select { | ||
case data = <-bytesBufferChan: | ||
default: | ||
data = bytesBufferPool.Get().(*bytes.Buffer) | ||
} | ||
|
||
data.Reset() | ||
|
||
return data | ||
} | ||
|
||
func BytesBufferPut(data *bytes.Buffer) { | ||
select { | ||
case bytesBufferChan <- data: | ||
default: | ||
bytesBufferPool.Put(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022-present The ZTDBP Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"runtime/debug" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func InArray(in string, array []string) bool { | ||
for k := range array { | ||
if in == array[k] { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func GoWithRecover(handler func(), recoverHandler func(r interface{})) { | ||
go func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
logrus.Errorf("%s goroutine panic: %v\n%s\n", time.Now().Format("2006-01-02 15:04:05"), r, string(debug.Stack())) | ||
if recoverHandler != nil { | ||
go func() { | ||
defer func() { | ||
if p := recover(); p != nil { | ||
logrus.Errorf("recover goroutine panic:%v\n%s\n", p, string(debug.Stack())) | ||
} | ||
}() | ||
recoverHandler(r) | ||
}() | ||
} | ||
} | ||
}() | ||
handler() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022-present The ZTDBP Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import "unsafe" | ||
|
||
func StringToByteSlice(s string) []byte { | ||
return *(*[]byte)(unsafe.Pointer(&s)) | ||
} | ||
|
||
func ByteSliceToString(b []byte) string { | ||
return *(*string)(unsafe.Pointer(&b)) | ||
} | ||
|
||
func Uint64ToInt64(val uint64) int64 { | ||
return *(*int64)(unsafe.Pointer(&val)) | ||
} | ||
|
||
func Uint64ToFloat64(val uint64) float64 { | ||
return *(*float64)(unsafe.Pointer(&val)) | ||
} | ||
|
||
func Int64ToUint64(val int64) uint64 { | ||
return *(*uint64)(unsafe.Pointer(&val)) | ||
} | ||
|
||
func Float64ToUint64(val float64) uint64 { | ||
return *(*uint64)(unsafe.Pointer(&val)) | ||
} |