large refactoring, bug fixes, performance improvement

This commit is contained in:
yusing
2024-03-18 04:51:59 +00:00
parent eee6ff4f15
commit a52b1bcadd
27 changed files with 1530 additions and 419 deletions

View File

@@ -2,15 +2,8 @@ package main
import (
"fmt"
"sync"
)
type Routes struct {
HTTPRoutes SafeMap[string, pathPoolMap] // alias -> (path -> routes)
StreamRoutes SafeMap[string, StreamRoute] // id -> target
Mutex sync.Mutex
}
type Route interface {
SetupListen()
Listen()
@@ -21,22 +14,22 @@ type Route interface {
func NewRoute(cfg *ProxyConfig) (Route, error) {
if isStreamScheme(cfg.Scheme) {
id := cfg.GetID()
if routes.StreamRoutes.Contains(id) {
if streamRoutes.Contains(id) {
return nil, fmt.Errorf("duplicated %s stream %s, ignoring", cfg.Scheme, id)
}
route, err := NewStreamRoute(cfg)
if err != nil {
return nil, err
}
routes.StreamRoutes.Set(id, route)
streamRoutes.Set(id, route)
return route, nil
} else {
routes.HTTPRoutes.Ensure(cfg.Alias)
httpRoutes.Ensure(cfg.Alias)
route, err := NewHTTPRoute(cfg)
if err != nil {
return nil, err
}
routes.HTTPRoutes.Get(cfg.Alias).Add(cfg.Path, route)
httpRoutes.Get(cfg.Alias).Add(cfg.Path, route)
return route, nil
}
}
@@ -59,11 +52,7 @@ func isStreamScheme(s string) bool {
return false
}
func initRoutes() *Routes {
r := Routes{}
r.HTTPRoutes = NewSafeMap[string](newPathPoolMap)
r.StreamRoutes = NewSafeMap[string, StreamRoute]()
return &r
}
// id -> target
type StreamRoutes = SafeMap[string, StreamRoute]
var routes = initRoutes()
var streamRoutes = NewSafeMap[string, StreamRoute]()