mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-05 18:20:31 +08:00
feat: add async conflict detection
This commit is contained in:
@@ -10,23 +10,19 @@ import (
|
||||
func (bufferPool BufferPool[USRDAT]) NewBuffer(
|
||||
buf []byte,
|
||||
) *orbyte.Item[UserBuffer[USRDAT]] {
|
||||
return bufferPool.p.New(buf)
|
||||
return bufferPool.New(buf)
|
||||
}
|
||||
|
||||
// InvolveBuffer involve external *bytes.Buffer into Item.
|
||||
func (bufferPool BufferPool[USRDAT]) InvolveBuffer(
|
||||
buf *bytes.Buffer,
|
||||
) *orbyte.Item[UserBuffer[USRDAT]] {
|
||||
return bufferPool.p.Involve(buf.Len(), buf)
|
||||
return bufferPool.Involve(buf.Len(), buf)
|
||||
}
|
||||
|
||||
// ParseBuffer convert external *bytes.Buffer into Item.
|
||||
func (bufferPool BufferPool[USRDAT]) ParseBuffer(
|
||||
buf *bytes.Buffer,
|
||||
) *orbyte.Item[UserBuffer[USRDAT]] {
|
||||
return bufferPool.p.Parse(buf.Len(), buf)
|
||||
}
|
||||
|
||||
func (bufferPool BufferPool[USRDAT]) CountItems() (outside int32, inside int32) {
|
||||
return bufferPool.p.CountItems()
|
||||
return bufferPool.Parse(buf.Len(), buf)
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ func testBuffer(buf *OBuffer, t *testing.T) {
|
||||
runtime.Gosched()
|
||||
runtime.GC()
|
||||
|
||||
out, in := bufferPool.p.CountItems()
|
||||
out, in := bufferPool.CountItems()
|
||||
t.Log(out, in)
|
||||
if out != 0 {
|
||||
t.Fail()
|
||||
|
||||
@@ -36,7 +36,7 @@ func (b UserBytes[USRDAT]) B(f func([]byte, *USRDAT)) {
|
||||
|
||||
// NewBytes alloc sz bytes.
|
||||
func (bufferPool BufferPool[USRDAT]) NewBytes(sz int) (b UserBytes[USRDAT]) {
|
||||
buf := bufferPool.p.New(sz)
|
||||
buf := bufferPool.New(sz)
|
||||
b.buf = buf
|
||||
buf.P(func(buf *UserBuffer[USRDAT]) {
|
||||
b.b = buf.Len()
|
||||
@@ -46,7 +46,7 @@ func (bufferPool BufferPool[USRDAT]) NewBytes(sz int) (b UserBytes[USRDAT]) {
|
||||
|
||||
// InvolveBytes involve outside buf into pool.
|
||||
func (bufferPool BufferPool[USRDAT]) InvolveBytes(p ...byte) (b UserBytes[USRDAT]) {
|
||||
buf := bufferPool.p.Involve(len(p), bytes.NewBuffer(p))
|
||||
buf := bufferPool.Involve(len(p), bytes.NewBuffer(p))
|
||||
b.buf = buf
|
||||
buf.P(func(buf *UserBuffer[USRDAT]) {
|
||||
b.b = buf.Len()
|
||||
@@ -57,7 +57,7 @@ func (bufferPool BufferPool[USRDAT]) InvolveBytes(p ...byte) (b UserBytes[USRDAT
|
||||
// ParseBytes convert outside bytes to Bytes safely
|
||||
// without adding it into pool.
|
||||
func (bufferPool BufferPool[USRDAT]) ParseBytes(p ...byte) (b UserBytes[USRDAT]) {
|
||||
buf := bufferPool.p.Parse(len(p), bytes.NewBuffer(p))
|
||||
buf := bufferPool.Parse(len(p), bytes.NewBuffer(p))
|
||||
b.buf = buf
|
||||
buf.P(func(buf *UserBuffer[USRDAT]) {
|
||||
b.b = buf.Len()
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestBytesSlice(t *testing.T) {
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
out, in := bufferPool.CountItems()
|
||||
t.Log(out, in)
|
||||
if out != 0 {
|
||||
t.Fail()
|
||||
@@ -60,7 +60,7 @@ func TestBytesInvolve(t *testing.T) {
|
||||
}
|
||||
}
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
out, in := bufferPool.CountItems()
|
||||
t.Log(out, in)
|
||||
if out != 0 {
|
||||
t.Fail()
|
||||
@@ -80,7 +80,7 @@ func TestBytesParse(t *testing.T) {
|
||||
}
|
||||
}
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
out, in := bufferPool.CountItems()
|
||||
t.Log(out, in)
|
||||
if out != 0 {
|
||||
t.Fail()
|
||||
@@ -107,7 +107,7 @@ func TestBytesCopy(t *testing.T) {
|
||||
runtime.GC()
|
||||
runtime.Gosched()
|
||||
runtime.GC()
|
||||
out, in := bufferPool.p.CountItems()
|
||||
out, in := bufferPool.CountItems()
|
||||
t.Log(out, in)
|
||||
if out != 0 {
|
||||
t.Fail()
|
||||
|
||||
24
pbuf/pbuf.go
24
pbuf/pbuf.go
@@ -16,12 +16,12 @@ type (
|
||||
)
|
||||
|
||||
type BufferPool[USRDAT any] struct {
|
||||
p *orbyte.Pool[UserBuffer[USRDAT]]
|
||||
*orbyte.Pool[UserBuffer[USRDAT]]
|
||||
}
|
||||
|
||||
func NewBufferPool[USRDAT any]() BufferPool[USRDAT] {
|
||||
return BufferPool[USRDAT]{
|
||||
p: orbyte.NewPool[UserBuffer[USRDAT]](bufpooler[USRDAT]{}),
|
||||
orbyte.NewPool[UserBuffer[USRDAT]](bufpooler[USRDAT]{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,3 +60,23 @@ func ParseBytes(b ...byte) Bytes {
|
||||
func CountItems() (outside int32, inside int32) {
|
||||
return bufferPool.CountItems()
|
||||
}
|
||||
|
||||
// SetNoPutBack see Pool.SetNoPutBack
|
||||
func SetNoPutBack(on bool) {
|
||||
bufferPool.SetNoPutBack(on)
|
||||
}
|
||||
|
||||
// SetSyncItem see Pool.SetSyncItem
|
||||
func SetSyncItem(on bool) {
|
||||
bufferPool.SetSyncItem(on)
|
||||
}
|
||||
|
||||
// LimitInput see Pool.LimitInput
|
||||
func LimitInput(n int32) {
|
||||
bufferPool.LimitInput(n)
|
||||
}
|
||||
|
||||
// LimitInput see Pool.LimitOutput
|
||||
func LimitOutput(n int32) {
|
||||
bufferPool.LimitOutput(n)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user