github.com/Rookout/GoSDK@v0.1.48/pkg/services/collection/variable/variables_pool.go (about) 1 package variable 2 3 import ( 4 "reflect" 5 "unsafe" 6 7 "github.com/Rookout/GoSDK/pkg/logger" 8 ) 9 10 type VariablesPool chan *internalVariable 11 12 var variablesPool = make(VariablesPool, 102400) 13 14 var internalVariableSize = func() int { 15 return int(unsafe.Sizeof(internalVariable{})) 16 }() 17 18 func (v *VariablesPool) get() *internalVariable { 19 var i *internalVariable 20 select { 21 case i = <-variablesPool: 22 default: 23 i = &internalVariable{} 24 } 25 i.inPool = false 26 return i 27 } 28 29 func clear(i *internalVariable) { 30 defer func() { 31 if r := recover(); r != nil { 32 logger.Logger().Errorf("Failed to clear variable, error: %v", r) 33 } 34 }() 35 36 a := unsafe.Pointer(reflect.ValueOf(i).Pointer()) 37 for i := 0; i < internalVariableSize; i++ { 38 b := (*byte)(a) 39 *b = 0 40 a = unsafe.Pointer(uintptr(a) + unsafe.Sizeof(*b)) 41 } 42 } 43 44 func (v *VariablesPool) set(i *internalVariable) { 45 clear(i) 46 i.inPool = true 47 48 select { 49 case variablesPool <- i: 50 default: 51 } 52 }