golang sync.Pool 指針數據覆蓋問題解決
場景
1. sync.Pool設置
var stringPool = sync.Pool{
New: func() any {
return new([]string)
},
}
func NewString() *[]string {
v := stringPool.Get().(*[]string)
return v
}
func PutString(s *[]string) {
if s == nil {
return
}
if cap(*s) > 2048 {
s = nil
} else {
*s = (*s)[:0]
stringPool.Put(s)
}
}2.使用sync.Pool
func Test_Pool(t *testing.T) {
dataSlice1 := demoData()
dataSlice2 := demoData()
dataSlice2[1] = "test4"
fmt.Printf("dataSlice1:%v %p,dataSlice2:%v %p\n", dataSlice1, dataSlice1, dataSlice2, dataSlice2)
}
func demoData() []string {
strsPtr := NewString()
strs := *strsPtr
defer func() {
*strsPtr = strs
PutString(strsPtr)
}()
strs = append(strs, "test1", "test2")
return strs
}打印結果:dataSlice1:[test1 test4] 0xc0000a6400,dataSlice2:[test1 test4] 0xc0000a6400
可以看到兩個slice地址相同,內部使用同一個地址的數組,導致兩次獲取的數據互相影響
3.解決方法1
func Test_Pool(t *testing.T) {
dataSlice1 := demoData()
dataSlice2 := demoData()
dataSlice2[1] = "test4"
fmt.Printf("dataSlice1:%v %p,dataSlice2:%v %p\n", dataSlice1, dataSlice1, dataSlice2, dataSlice2)
}
func demoData() []string {
strsPtr := NewString()
strs := *strsPtr
defer func() {
*strsPtr = strs
PutString(strsPtr)
}()
strs = append(strs, "test1", "test2")
// 深復制
var items = make([]string, len(strs))
copy(items, strs)
return items
}使用深復制,在put回sync.Pool中之前把數據復制返回,但這樣資源池失去了意義,獲取到資源后有進行了一次內存的申請
4.解決方法2
我們看下golang語言源碼怎么解決的
參考 go/src/fmt/print.go 302行 Fprintln方法
func Fprintln(w io.Writer, a ...any) (n int, err error) {
p := newPrinter()
p.doPrintln(a)
n, err = w.Write(p.buf)
p.free()
return
}可以看到306行有p.free()代碼,newPrinter()和free()之間進行數據處理,數據處理完成之后再把資源返回給sync.Pool
總結
不是任何場景都適合用sync.Pool,需要關注并發(fā)情況下資源池中數據同步修改影響的問題。
到此這篇關于golang sync.Pool 指針數據覆蓋問題解決的文章就介紹到這了,更多相關golang sync.Pool 指針覆蓋內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Golang Printf,Sprintf,Fprintf 格式化詳解
這篇文章主要介紹了Golang Printf,Sprintf,Fprintf 格式化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
詳解Go語言中用 os/exec 執(zhí)行命令的五種方法
這篇文章主要介紹了Go語言中用 os/exec 執(zhí)行命令的五種方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
golang通過context控制并發(fā)的應用場景實現(xiàn)
這篇文章主要介紹了golang通過context控制并發(fā)的應用場景實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
Golang實現(xiàn)HTTP代理突破IP訪問限制的步驟詳解
在當今互聯(lián)網時代,網站和服務商為了維護安全性和保護用戶隱私,常常會對特定的IP地址進行封鎖或限制,本文將介紹如何使用Golang實現(xiàn)HTTP代理來突破IP訪問限制,需要的朋友可以參考下2023-10-10

