-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathrouter_concurrent.go
More file actions
47 lines (36 loc) · 886 Bytes
/
router_concurrent.go
File metadata and controls
47 lines (36 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package echo
import (
"sync"
)
// NewConcurrentRouter creates concurrency safe Router which routes can be added/removed safely
// even after http.Server has been started.
func NewConcurrentRouter(r Router) Router {
return &concurrentRouter{
mu: sync.RWMutex{},
router: r,
}
}
type concurrentRouter struct {
mu sync.RWMutex
router Router
}
func (r *concurrentRouter) Route(c *Context) HandlerFunc {
r.mu.RLock()
defer r.mu.RUnlock()
return r.router.Route(c)
}
func (r *concurrentRouter) Routes() Routes {
r.mu.RLock()
defer r.mu.RUnlock()
return r.router.Routes().Clone()
}
func (r *concurrentRouter) Add(routable Route) (RouteInfo, error) {
r.mu.Lock()
defer r.mu.Unlock()
return r.router.Add(routable)
}
func (r *concurrentRouter) Remove(method string, path string) error {
r.mu.Lock()
defer r.mu.Unlock()
return r.router.Remove(method, path)
}