]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240309114649
authorqydysky <qydysky@foxmail.com>
Sat, 9 Mar 2024 11:41:53 +0000 (19:41 +0800)
committerqydysky <qydysky@foxmail.com>
Sat, 9 Mar 2024 11:41:53 +0000 (19:41 +0800)
web/Web.go
web/Web_test.go

index 5edf57194c148a55520c098e440eb3bfc1a97bdf..2f12d4fc2a309a854766e78022587b4222c818e4 100644 (file)
@@ -102,11 +102,12 @@ func (t *WebSync) Shutdown(ctx ...context.Context) {
 type WebPath struct {
        Path string `json:"path"`
        // current net.Conn: conn, ok := r.Context().Value(&WebPath).(net.Conn)
-       f    func(w http.ResponseWriter, r *http.Request)
-       Per  *WebPath `json:"-"`
-       Same *WebPath `json:"same"`
-       Next *WebPath `json:"next"`
-       l    sync.RWMutex
+       f       func(w http.ResponseWriter, r *http.Request)
+       PerSame *WebPath `json:"-"`
+       Per     *WebPath `json:"-"`
+       Same    *WebPath `json:"same"`
+       Next    *WebPath `json:"next"`
+       l       sync.RWMutex
 }
 
 // WebSync
@@ -134,7 +135,7 @@ func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request
 
        if key, left, fin := parsePath(path); t.Path == key {
                if fin {
-                       return t.f, true
+                       return t.f, t.f != nil
                } else {
                        if t.Same != nil {
                                return t.Same.Load(left)
@@ -199,6 +200,11 @@ func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Reque
                return
        }
 
+       if f == nil {
+               t.Delete(path)
+               return
+       }
+
        t.l.Lock()
        defer t.l.Unlock()
 
@@ -209,7 +215,7 @@ func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Reque
                        t.f = f
                        return
                } else {
-                       t.Same = &WebPath{}
+                       t.Same = &WebPath{PerSame: t}
                        t.Same.Store(left, f)
                        return
                }
@@ -225,7 +231,7 @@ func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Reque
                                t.Same.Store(left, f)
                                return
                        } else {
-                               t.Same = &WebPath{}
+                               t.Same = &WebPath{PerSame: t}
                                t.Same.Store(left, f)
                                return
                        }
@@ -244,6 +250,40 @@ func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Reque
        }
 }
 
+func (t *WebPath) Delete(path string) (deleteMe bool) {
+       if len(path) == 0 || path[0] != '/' {
+               return
+       }
+
+       t.l.Lock()
+       defer t.l.Unlock()
+
+       if key, left, fin := parsePath(path); t.Path == key {
+               if fin {
+                       t.f = nil
+                       return t.f == nil && t.Next == nil && t.Same == nil
+               } else {
+                       if t.Same != nil {
+                               if t.Same.Delete(left) {
+                                       t.Same = nil
+                               }
+                               return t.f == nil && t.Next == nil && t.Same == nil
+                       } else {
+                               return false
+                       }
+               }
+       } else {
+               if t.Next != nil {
+                       if t.Next.Delete(path) {
+                               t.Next = nil
+                       }
+                       return t.f == nil && t.Next == nil && t.Same == nil
+               } else {
+                       return false
+               }
+       }
+}
+
 type Limits struct {
        g []*limitItem
        l sync.RWMutex
index 84b1fd5787bc05bbb4203ff69b1f7222f4cad0a8..1f974e7f0291437c2c126298234117b46a5be57f 100644 (file)
@@ -183,7 +183,11 @@ func Test_Store(t *testing.T) {
 
        webPath.Store("/", f("a"))
        webPath.Store("/1", f("b"))
+       webPath.Store("/2", f("b"))
        webPath.Store("/1/1", f("c"))
+       webPath.Store("/1/2", f("d"))
+       webPath.Delete("/2")
+       webPath.Delete("/1/2")
        if m, e := json.Marshal(webPath); e != nil {
                t.Fatal(e)
        } else if string(m) != `{"path":"/","same":null,"next":{"path":"/1","same":{"path":"/1","same":null,"next":null},"next":null}}` {