improve nodes relay path and accelerate memory search with token index cache

This commit is contained in:
DBT
2026-02-24 15:30:45 +00:00
parent 532f01e4ee
commit 2486bbd80c
6 changed files with 142 additions and 20 deletions

View File

@@ -2,18 +2,22 @@ package nodes
import (
"sort"
"strings"
"sync"
"time"
)
// Manager keeps paired node metadata and basic routing helpers.
type Handler func(req Request) Response
type Manager struct {
mu sync.RWMutex
nodes map[string]NodeInfo
mu sync.RWMutex
nodes map[string]NodeInfo
handlers map[string]Handler
}
func NewManager() *Manager {
return &Manager{nodes: map[string]NodeInfo{}}
return &Manager{nodes: map[string]NodeInfo{}, handlers: map[string]Handler{}}
}
func (m *Manager) Upsert(info NodeInfo) {
@@ -51,6 +55,32 @@ func (m *Manager) List() []NodeInfo {
return out
}
func (m *Manager) RegisterHandler(nodeID string, h Handler) {
m.mu.Lock()
defer m.mu.Unlock()
if strings.TrimSpace(nodeID) == "" || h == nil {
return
}
m.handlers[nodeID] = h
}
func (m *Manager) Invoke(req Request) (Response, bool) {
m.mu.RLock()
h, ok := m.handlers[req.Node]
m.mu.RUnlock()
if !ok {
return Response{}, false
}
resp := h(req)
if strings.TrimSpace(resp.Node) == "" {
resp.Node = req.Node
}
if strings.TrimSpace(resp.Action) == "" {
resp.Action = req.Action
}
return resp, true
}
func (m *Manager) PickFor(action string) (NodeInfo, bool) {
m.mu.RLock()
defer m.mu.RUnlock()