]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240101145920
authorqydysky <qydysky@foxmail.com>
Mon, 1 Jan 2024 14:56:07 +0000 (22:56 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 1 Jan 2024 14:56:07 +0000 (22:56 +0800)
pool/Pool.go
pool/Pool_test.go

index 78d33c4f46f43aab3d7ddf832bbdc29215389106..a679b817c1db814fa28107a466d4ba038370794b 100644 (file)
@@ -42,7 +42,19 @@ func New[T any](NewF func() *T, InUse func(*T) bool, ReuseF func(*T) *T, PoolF f
 }
 
 // states[] 0:pooled, 1:nopooled, 2:inuse, 3:nouse, 4:sum, 5:getPerSec
+//
+// Deprecated: s
 func (t *Buf[T]) PoolState() (states []any) {
+       state := t.State()
+       return []any{state.pooled, state.nopooled, state.inuse, state.nouse, state.sum, state.getPerSec}
+}
+
+type BufState struct {
+       pooled, nopooled, inuse, nouse, sum int
+       getPerSec                           float64
+}
+
+func (t *Buf[T]) State() BufState {
        t.l.RLock()
        defer t.l.RUnlock()
 
@@ -68,7 +80,7 @@ func (t *Buf[T]) PoolState() (states []any) {
                getPerSec += t.getPerSec / diff
        }
 
-       return []any{pooled, nopooled, inuse, nouse, sum, getPerSec}
+       return BufState{pooled, nopooled, inuse, nouse, sum, getPerSec}
 }
 
 func (t *Buf[T]) Get() *T {
index b9b718a7926d80f134c2329b57246615b31041d4..ee6814296d1b979d143e22b567e2ebaa64588a18 100644 (file)
@@ -38,8 +38,8 @@ func Test1(t *testing.T) {
                b.Get()
        }
        time.Sleep(time.Millisecond * 1100)
-       if math.Abs(b.PoolState()[5].(float64)-7.5) > 2.5 {
-               t.Fatal(b.PoolState()[5])
+       if math.Abs(b.State().getPerSec-7.5) > 2.5 {
+               t.Fatal(b.State().getPerSec)
        }
 }
 
@@ -72,7 +72,7 @@ func TestXxx(t *testing.T) {
        var c2p = uintptr(unsafe.Pointer(c2))
        c2.d = append(c2.d, []byte("2")...)
 
-       if c1p == c2p || bytes.Equal(c1.d, c2.d) || b.PoolState()[2] != 0 || b.PoolState()[4] != 0 {
+       if c1p == c2p || bytes.Equal(c1.d, c2.d) || b.State().inuse != 0 || b.State().sum != 0 {
                t.Fatal()
        }
 
@@ -81,13 +81,13 @@ func TestXxx(t *testing.T) {
        var c3 = b.Get()
        var c3p = uintptr(unsafe.Pointer(c3))
 
-       if c1p == c3p || len(c1.d) == 0 || b.PoolState()[2] != 0 || b.PoolState()[4] != 0 {
+       if c1p == c3p || len(c1.d) == 0 || b.State().inuse != 0 || b.State().sum != 0 {
                t.Fatal()
        }
 
        b.Put(c1)
 
-       if len(c1.d) == 0 || b.PoolState()[2] != 0 || b.PoolState()[4] != 1 {
-               t.Fatal(len(c1.d) != 0, b.PoolState()[2] != 0, b.PoolState()[4] != 1)
+       if len(c1.d) == 0 || b.State().inuse != 0 || b.State().sum != 1 {
+               t.Fatal(len(c1.d) != 0, b.State().inuse != 0, b.State().sum != 1)
        }
 }