Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
// internal/adminroute/snapshot.go 把路由行转换为热路径使用的不可变查找映射。
|
||
|
||
package adminroute
|
||
|
||
import "sync/atomic"
|
||
|
||
type Snapshot struct {
|
||
// atomic.Value 保存整张路由表,连接热路径读取时无需加锁。
|
||
value atomic.Value
|
||
}
|
||
|
||
func NewSnapshot() *Snapshot {
|
||
return &Snapshot{}
|
||
}
|
||
|
||
func (s *Snapshot) Store(routes map[string]string) {
|
||
if routes == nil {
|
||
routes = map[string]string{}
|
||
}
|
||
// Store 前复制 map,避免调用方发布后继续修改导致并发读写 map。
|
||
copied := make(map[string]string, len(routes))
|
||
for host, upstream := range routes {
|
||
copied[host] = upstream
|
||
}
|
||
s.value.Store(copied)
|
||
}
|
||
|
||
func (s *Snapshot) Clone() map[string]string {
|
||
value := s.value.Load()
|
||
if value == nil {
|
||
return nil
|
||
}
|
||
routes, ok := value.(map[string]string)
|
||
if !ok {
|
||
return nil
|
||
}
|
||
// Clone 返回副本,管理端或测试修改结果不会影响热路径快照。
|
||
copied := make(map[string]string, len(routes))
|
||
for host, upstream := range routes {
|
||
copied[host] = upstream
|
||
}
|
||
return copied
|
||
}
|
||
|
||
func (s *Snapshot) Lookup(host string) (string, bool) {
|
||
value := s.value.Load()
|
||
if value == nil {
|
||
return "", false
|
||
}
|
||
routes, ok := value.(map[string]string)
|
||
if !ok {
|
||
return "", false
|
||
}
|
||
upstream, ok := routes[host]
|
||
if ok {
|
||
return upstream, true
|
||
}
|
||
// default 是显式兜底路由,只有精确 host 未命中时才使用。
|
||
upstream, ok = routes["default"]
|
||
return upstream, ok
|
||
}
|