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()

View File

@@ -57,10 +57,16 @@ func (s *StubP2PTransport) Send(ctx context.Context, req Request) (Response, err
}
// StubRelayTransport provides executable placeholder until real bridge lands.
type StubRelayTransport struct{}
type StubRelayTransport struct{ Manager *Manager }
func (s *StubRelayTransport) Name() string { return "relay" }
func (s *StubRelayTransport) Send(ctx context.Context, req Request) (Response, error) {
_ = ctx
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "relay bridge not implemented yet"}, nil
if s.Manager == nil {
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "relay manager not configured"}, nil
}
if resp, ok := s.Manager.Invoke(req); ok {
return resp, nil
}
return Response{OK: false, Node: req.Node, Action: req.Action, Error: "relay handler not found for node"}, nil
}