]> 127.0.0.1 Git - part/.git/commitdiff
fix
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 13:35:22 +0000 (21:35 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sun, 12 Mar 2023 13:35:22 +0000 (21:35 +0800)
.github/workflows/test.yml
pool/Pool.go
pool/Pool_test.go
reqf/Reqf_test.go
sync/Map.go
sync/Map_test.go

index 0f8c09364f4813ac8498ac6d05abb024b0686202..f890c1d842691c6065c04ca5121052e051ff56ec 100644 (file)
@@ -31,3 +31,4 @@ jobs:
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/pool
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/funcCtrl
         go test -count 1 -timeout 30s -v -race github.com/qydysky/part/msgq
+        go test -count 1 -timeout 5s -v -race github.com/qydysky/part/sync
index 25e3c9db47da86e8a5d2a7bc232bb4d65007c8ba..f7c4b2457a70256568f21486ba524c8f751e88a6 100644 (file)
@@ -10,10 +10,15 @@ type Buf[T any] struct {
        inUse   func(*T) bool
        reuseF  func(*T) *T
        poolF   func(*T) *T
-       buf     []*T
+       buf     []poolItem[T]
        l       sync.RWMutex
 }
 
+type poolItem[T any] struct {
+       i      *T
+       pooled bool
+}
+
 // 创建池
 //
 // NewF: func() *T 新值
@@ -40,7 +45,7 @@ func (t *Buf[T]) PoolInUse() (inUse int) {
        defer t.l.RUnlock()
 
        for i := 0; i < len(t.buf); i++ {
-               if t.inUse(t.buf[i]) {
+               if !t.buf[i].pooled && t.inUse(t.buf[i].i) {
                        inUse++
                }
        }
@@ -60,8 +65,7 @@ func (t *Buf[T]) Trim() {
        defer t.l.Unlock()
 
        for i := 0; i < len(t.buf); i++ {
-               if !t.inUse(t.buf[i]) {
-                       t.buf[i] = nil
+               if t.buf[i].pooled && !t.inUse(t.buf[i].i) {
                        t.buf = append(t.buf[:i], t.buf[i+1:]...)
                        i--
                }
@@ -73,8 +77,9 @@ func (t *Buf[T]) Get() *T {
        defer t.l.Unlock()
 
        for i := 0; i < len(t.buf); i++ {
-               if !t.inUse(t.buf[i]) {
-                       return t.reuseF(t.buf[i])
+               if t.buf[i].pooled && !t.inUse(t.buf[i].i) {
+                       t.buf[i].pooled = false
+                       return t.reuseF(t.buf[i].i)
                }
        }
 
@@ -91,8 +96,9 @@ func (t *Buf[T]) Put(item ...*T) {
 
        var cu = 0
        for i := 0; i < len(t.buf); i++ {
-               if !t.inUse(t.buf[i]) {
-                       t.buf[i] = t.poolF(item[cu])
+               if t.buf[i].pooled && !t.inUse(t.buf[i].i) {
+                       t.buf[i].i = t.poolF(item[cu])
+                       t.buf[i].pooled = true
                        cu++
                        if cu >= len(item) {
                                return
@@ -101,6 +107,6 @@ func (t *Buf[T]) Put(item ...*T) {
        }
 
        for i := cu; i < len(item) && t.maxsize > len(t.buf); i++ {
-               t.buf = append(t.buf, t.poolF(item[i]))
+               t.buf = append(t.buf, poolItem[T]{t.poolF(item[i]), true})
        }
 }
index 51152dae762ed94723f0158c39a702970b31e596..4fa847c6ffdeb8471d90a819f0786542d6d5236e 100644 (file)
@@ -44,12 +44,18 @@ func TestXxx(t *testing.T) {
                t.Fatal()
        }
 
-       b.Put(c1)
        c1.v = false
+
        var c3 = b.Get()
        var c3p = uintptr(unsafe.Pointer(c3))
 
-       if c1p != c3p || len(c1.d) != 0 || b.PoolInUse() != 1 || b.PoolSum() != 1 {
+       if c1p == c3p || len(c1.d) == 0 || b.PoolInUse() != 0 || b.PoolSum() != 0 {
                t.Fatal()
        }
+
+       b.Put(c1)
+
+       if len(c1.d) == 0 || b.PoolInUse() != 0 || b.PoolSum() != 1 {
+               t.Fatal(len(c1.d) != 0, b.PoolInUse() != 0, b.PoolSum() != 1)
+       }
 }
index da6438da17b5e90a9c3bdfafe36a85424ca1321e..50274aceb93e5b9a292ee684df466b81a5dc33c8 100644 (file)
@@ -3,6 +3,7 @@ package part
 import (
        "bytes"
        "context"
+       "encoding/json"
        "io"
        "net/http"
        "testing"
@@ -58,6 +59,22 @@ func init() {
                                time.Sleep(time.Second)
                        }
                },
+               `/json`: func(w http.ResponseWriter, _ *http.Request) {
+                       w.WriteHeader(200)
+                       flusher, flushSupport := w.(http.Flusher)
+                       if flushSupport {
+                               flusher.Flush()
+                       }
+                       w.Write([]byte("{\"a\":"))
+                       if flushSupport {
+                               flusher.Flush()
+                       }
+                       time.Sleep(time.Millisecond * 20)
+                       w.Write([]byte("123}"))
+                       if flushSupport {
+                               flusher.Flush()
+                       }
+               },
                `/exit`: func(_ http.ResponseWriter, _ *http.Request) {
                        s.Server.Shutdown(context.Background())
                },
@@ -98,6 +115,21 @@ func Test_req(t *testing.T) {
        }
 }
 
+type J struct {
+       A int `json:"a"`
+}
+
+func Test_req12(t *testing.T) {
+       r := New()
+       r.Reqf(Rval{
+               Url:     "http://" + addr + "/json",
+               Timeout: 10 * 1000,
+               Retry:   2,
+       })
+       var j J
+       json.Unmarshal(r.Respon, &j)
+}
+
 func Test_req2(t *testing.T) {
        r := New()
        {
index c1c961cf1d7a0fd633a5fe29b15e64f107e034cf..fb62b0c567ae33beff3e620047df5f96d596061f 100644 (file)
@@ -1,10 +1,8 @@
 package part
 
 import (
-       "runtime"
        "sync"
        "sync/atomic"
-       "unsafe"
 )
 
 type Map struct {
@@ -40,41 +38,10 @@ func (t *Map) Len() int {
        return int(t.size.Load())
 }
 
-type ptr struct {
-       p unsafe.Pointer
-}
-
-func (t *ptr) tryStore(v *any) {
-       t.p = unsafe.Pointer(v)
-       // atomic.StorePointer(&t.p, unsafe.Pointer(v))
-}
-
-func (t *ptr) tryLoad() (any, bool) {
-       // p := atomic.LoadPointer(&t.p)
-       if t.p == nil {
-               return nil, false
-       }
-       return *(*any)(t.p), true
-}
-
-type pLock struct {
-       i    unsafe.Pointer
-       busy unsafe.Pointer
-}
-
-func (l *pLock) Lock() {
-       if l.busy == nil {
-               l.busy = unsafe.Pointer(&struct{}{})
-       }
-       for !atomic.CompareAndSwapPointer(&l.i, nil, l.busy) {
-               runtime.Gosched()
-       }
-}
-
-func (l *pLock) Locking() bool {
-       return atomic.LoadPointer(&l.i) != nil
-}
-
-func (l *pLock) Unlock() {
-       atomic.CompareAndSwapPointer(&l.i, l.busy, nil)
+func (t *Map) Copy() (m Map) {
+       t.Range(func(k, v any) bool {
+               m.Store(k, v)
+               return true
+       })
+       return
 }
index 4f13cd8e0cb5ec6df213dc73289902eb01b11531..1457b205232afb46763bbb863358b3c093300f00 100644 (file)
@@ -26,7 +26,6 @@ func Test_customMap(t *testing.T) {
        //range
        c.Store(1, 1)
        c.Range(func(key, value interface{}) bool {
-               t.Log(key, value)
                if key.(int) != value.(int) {
                        t.Error(`3`)
                }
@@ -37,12 +36,10 @@ func Test_customMap(t *testing.T) {
        if v, ok := c.Load(0); ok && v != nil {
                t.Error(`4`)
        }
-       t.Log(c.Len())
        c.Delete(1)
        if v, ok := c.Load(1); ok && v != nil {
                t.Error(`6`)
        }
-       t.Log(c.Len())
 }
 
 func Benchmark_customMap_Set(b *testing.B) {
@@ -167,6 +164,19 @@ func Test_Range(t *testing.T) {
        }
 }
 
+func Test_1(t *testing.T) {
+       var c Map
+       c.Store("o", []string{"111", "222"})
+       m := c.Copy()
+       if v, ok := m.LoadV("o").([]string); !ok || len(v) != 2 || v[0] != "111" || v[1] != "222" {
+               t.Fatal()
+       }
+       c.Delete("o")
+       if v, ok := m.LoadV("o").([]string); !ok || len(v) != 2 || v[0] != "111" || v[1] != "222" {
+               t.Fatal()
+       }
+}
+
 func Benchmark_customMap_Range(b *testing.B) {
        var c Map
        var w = &sync.WaitGroup{}