From: qydysky Date: Sat, 9 Mar 2024 11:41:53 +0000 (+0800) Subject: 1 X-Git-Tag: v0.28.20240309114649 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=9f5b66fe055a9c1efcff4dcecd83a2c240f64e13;p=part%2F.git 1 --- diff --git a/web/Web.go b/web/Web.go index 5edf571..2f12d4f 100644 --- a/web/Web.go +++ b/web/Web.go @@ -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 diff --git a/web/Web_test.go b/web/Web_test.go index 84b1fd5..1f974e7 100644 --- a/web/Web_test.go +++ b/web/Web_test.go @@ -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}}` {