]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240307073721
authorqydysky <qydysky@foxmail.com>
Thu, 7 Mar 2024 07:31:33 +0000 (15:31 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 7 Mar 2024 07:31:33 +0000 (15:31 +0800)
web/Web.go
web/Web_test.go

index 5c581eb95be5cade16e7e919041142f2a3f96fd4..bddad85769440dae04be1d034895feb5fe8712b4 100644 (file)
@@ -105,7 +105,7 @@ type WebPath struct {
        f     func(w http.ResponseWriter, r *http.Request)
        sameP *WebPath
        next  *WebPath
-       sync.RWMutex
+       l     sync.RWMutex
 }
 
 // WebSync
@@ -114,8 +114,8 @@ func (t *WebPath) GetConn(r *http.Request) net.Conn {
 }
 
 func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request), bool) {
-       t.RLock()
-       defer t.RUnlock()
+       t.l.RLock()
+       defer t.l.RUnlock()
        if t.path == path {
                // 操作本节点
                return t.f, t.f != nil
@@ -142,12 +142,12 @@ func (t *WebPath) Load(path string) (func(w http.ResponseWriter, r *http.Request
 }
 
 func (t *WebPath) LoadPerfix(path string) (func(w http.ResponseWriter, r *http.Request), bool) {
-       t.RLock()
-       defer t.RUnlock()
+       t.l.RLock()
+       defer t.l.RUnlock()
        if t.path == path {
                // 操作本节点
                return t.f, t.f != nil
-       } else if lp, ltp := len(path), len(t.path); lp > ltp && path[:ltp] == t.path && (path[ltp] == '/' || t.path[ltp-1] == '/') {
+       } else if lp, ltp := len(path), len(t.path); lp > ltp && path[:ltp] == t.path && t.path[ltp-1] == '/' {
                // 操作sameP节点
                if t.sameP != nil {
                        if f, ok := t.sameP.LoadPerfix(path); ok {
@@ -174,8 +174,8 @@ func (t *WebPath) LoadPerfix(path string) (func(w http.ResponseWriter, r *http.R
 }
 
 func (t *WebPath) Store(path string, f func(w http.ResponseWriter, r *http.Request)) {
-       t.Lock()
-       defer t.Unlock()
+       t.l.Lock()
+       defer t.l.Unlock()
        if t.path == path || (t.path == "" && t.f == nil) {
                // 操作本节点
                t.path = path
index 392f02a451512c159a01b50c3fee4f9cacfeefba..d5f3c6783d1c5c4fd5dc83ba65075fda875fe51d 100644 (file)
@@ -65,6 +65,39 @@ func Test_Server(t *testing.T) {
        }
 }
 
+func failIfNot[T comparable](t *testing.T, a, b T) {
+       t.Logf("a:'%v' b:'%v'", a, b)
+       if a != b {
+               t.Fail()
+       }
+}
+
+func Test_path(t *testing.T) {
+       var m WebPath
+       var res string
+       var f1 = func(_ http.ResponseWriter, _ *http.Request) { res += "f1" }
+       var f2 = func(_ http.ResponseWriter, _ *http.Request) { res += "f2" }
+       m.Store("/1", f2)
+       m.Store("/1/", f1)
+       failIfNot(t, res, "")
+       if sf1, ok := m.LoadPerfix("/1/"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1")
+       if sf1, ok := m.LoadPerfix("/1"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1f2")
+       if sf1, ok := m.LoadPerfix("/121"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1f2")
+       if sf1, ok := m.LoadPerfix("/1/1"); ok {
+               sf1(nil, nil)
+       }
+       failIfNot(t, res, "f1f2f1")
+}
+
 func Test_Server2(t *testing.T) {
        var m WebPath
        m.Store("/", func(w http.ResponseWriter, _ *http.Request) {