]> 127.0.0.1 Git - part/.git/commitdiff
1
authorqydysky <qydysky@foxmail.com>
Tue, 9 Jan 2024 15:57:50 +0000 (23:57 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 9 Jan 2024 15:57:50 +0000 (23:57 +0800)
web/Web.go
web/Web_test.go

index 3efc9237f801a41031060dd40f7c2c1f5e9766f5..3c2a09fcff4c425fa65b83fdb081156c60fe3d46 100644 (file)
@@ -470,29 +470,31 @@ func (t *Exprier) Check(key string) error {
        return nil
 }
 
-func (t *Exprier) LoopCheck(key string, dru time.Duration, whenfail func(key string, e error)) (breakLoop func()) {
+func (t *Exprier) LoopCheck(key string, dru time.Duration, whenfail func(key string, e error)) (breakLoop func(), e error) {
        breakLoop = func() {}
        if t.Max <= 0 {
                return
        }
        if key == "" {
+               e = ErrNoFound
                whenfail(key, ErrNoFound)
                return
        }
        if t.m.Len() >= t.Max {
+               e = ErrOverflow
                whenfail(key, ErrOverflow)
                return
        }
 
-       c := make(chan struct{})
+       var close atomic.Bool
        t.m.Store(key, time.Now().Add(dru))
        breakLoop = func() {
                t.m.Delete(key)
-               close(c)
+               close.Store(true)
        }
 
        go func() {
-               for t.Max > 0 {
+               for !close.Load() && t.Max > 0 {
                        ey, ok := t.m.LoadV(key).(time.Time)
                        if !ok {
                                whenfail(key, ErrNoFound)
@@ -502,11 +504,7 @@ func (t *Exprier) LoopCheck(key string, dru time.Duration, whenfail func(key str
                                whenfail(key, ErrExprie)
                                return
                        }
-                       select {
-                       case <-c:
-                               return
-                       case <-time.After(time.Until(ey) + time.Second):
-                       }
+                       time.Sleep(time.Until(ey) + time.Second)
                }
        }()
        return
index 4a21010a22b8708ca5d2ab3f3b7ea4f9de6e708d..dd5acc13d6a640ee2a3dc979a51ce86190f4f87e 100644 (file)
@@ -7,6 +7,7 @@ import (
        "io"
        "net"
        "net/http"
+       "strconv"
        "strings"
        "testing"
        "time"
@@ -307,3 +308,27 @@ func (t ResStruct) Write(w http.ResponseWriter) {
        }
        w.Write(data)
 }
+
+func Test1(b *testing.T) {
+       exp := Exprier{Max: 10}
+
+       el := make(chan error, 100)
+       for i := 0; i < 20; i++ {
+               done, e := exp.LoopCheck(strconv.Itoa(i), time.Second*10, func(key string, e error) {
+                       if e != nil {
+                               el <- e
+                               b.Log(key, e)
+                       }
+               })
+               if e != nil {
+                       b.Fatal(e)
+               }
+               done()
+       }
+
+       time.Sleep(time.Second * 3)
+
+       if len(el) > 0 {
+               b.Fatal(<-el)
+       }
+}