github.com/nutsdb/nutsdb@v1.0.4/value.go (about) 1 package nutsdb 2 3 import ( 4 "sync" 5 "sync/atomic" 6 ) 7 8 type request struct { 9 tx *Tx 10 Wg sync.WaitGroup 11 Err error 12 ref int32 13 } 14 15 var requestPool = sync.Pool{ 16 New: func() interface{} { 17 return new(request) 18 }, 19 } 20 21 func (req *request) reset() { 22 req.tx = nil 23 req.Wg = sync.WaitGroup{} 24 req.Err = nil 25 26 atomic.StoreInt32(&req.ref, 0) 27 } 28 29 func (req *request) IncrRef() { 30 atomic.AddInt32(&req.ref, 1) 31 } 32 33 func (req *request) DecrRef() { 34 nRef := atomic.AddInt32(&req.ref, -1) 35 if nRef > 0 { 36 return 37 } 38 req.tx = nil 39 requestPool.Put(req) 40 } 41 42 func (req *request) Wait() error { 43 req.Wg.Wait() 44 err := req.Err 45 req.DecrRef() // DecrRef after writing to DB. 46 return err 47 }