feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
23a7a5963a
commit
8546d14cef
130 files changed
+9396
-3311
No files matched your search
+178
-343
@@ -1,287 +1,139 @@
|
||||
// Package fed lets one instance aggregate the summaries of other instances.
|
||||
// Package fed aggregates the nodes of the hub: one node per probe, each with
|
||||
// its own store, served in process through the Backend interface.
|
||||
package fed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
||||
)
|
||||
|
||||
// maxBody caps what a hub accepts from one peer response.
|
||||
const maxBody = 8 << 20
|
||||
|
||||
// Peer is a remote Urutau instance.
|
||||
type Peer struct {
|
||||
Name, URL string
|
||||
// Token overrides the hub-wide token for this peer (URUTAU_PEER_TOKEN_<NAME>).
|
||||
Token string
|
||||
// Backend is one probe node seen from the aggregation layer.
|
||||
type Backend interface {
|
||||
Name() string
|
||||
Export(rg store.Range) store.PeerExport
|
||||
Study(q store.PeriodQuery) store.PeerStudy
|
||||
Egress(from, to int64) json.RawMessage
|
||||
Subscribe() (<-chan store.LiveEvent, func())
|
||||
SubscribeVisits() (<-chan store.VisitMsg, func())
|
||||
// Online reports whether the probe is connected. The data of an offline
|
||||
// node is still served (it lives in the hub).
|
||||
Online() bool
|
||||
Location() *store.Location
|
||||
}
|
||||
|
||||
// ParsePeer parses "name=https://url".
|
||||
func ParsePeer(s string) (Peer, error) {
|
||||
name, u, ok := strings.Cut(s, "=")
|
||||
name, u = strings.TrimSpace(name), strings.TrimRight(strings.TrimSpace(u), "/")
|
||||
if !ok || name == "" || u == "" {
|
||||
return Peer{}, fmt.Errorf("peer %q: expected name=https://url", s)
|
||||
}
|
||||
pu, err := url.Parse(u)
|
||||
if err != nil || pu.Host == "" || pu.User != nil {
|
||||
return Peer{}, fmt.Errorf("peer %q: invalid URL", s)
|
||||
}
|
||||
switch {
|
||||
case pu.Scheme == "https":
|
||||
case pu.Scheme == "http" && isLoopbackHost(pu.Hostname()):
|
||||
default:
|
||||
return Peer{}, fmt.Errorf("peer %q: the URL must be https (plain http only for loopback)", s)
|
||||
}
|
||||
return Peer{Name: name, URL: u}, nil
|
||||
}
|
||||
|
||||
func isLoopbackHost(h string) bool {
|
||||
if h == "localhost" {
|
||||
return true
|
||||
}
|
||||
a, err := netip.ParseAddr(h)
|
||||
return err == nil && a.IsLoopback()
|
||||
}
|
||||
|
||||
type peerState struct {
|
||||
online bool
|
||||
lastOK int64
|
||||
err string
|
||||
latency int64
|
||||
version string
|
||||
exports map[string]store.PeerExport
|
||||
extra map[string]*extraExport // on-demand windows
|
||||
studies map[string]*studyCached
|
||||
live bool
|
||||
}
|
||||
|
||||
// extraExport is an export of a window outside the background set.
|
||||
type extraExport struct {
|
||||
at time.Time
|
||||
e store.PeerExport
|
||||
// HostStatus is implemented by backends that know more than Online (version,
|
||||
// last contact, lag); Hosts uses it when present.
|
||||
type HostStatus interface {
|
||||
HostInfo() store.HostInfo
|
||||
}
|
||||
|
||||
const (
|
||||
extraTTL = 15 * time.Second
|
||||
extraMax = 12 // cached on-demand exports per peer
|
||||
exportTTL = 5 * time.Second
|
||||
studyTTL = 30 * time.Second
|
||||
cacheMax = 24
|
||||
liveBuf = 500
|
||||
)
|
||||
|
||||
// Hub polls peers and serves their latest summaries.
|
||||
type cached[T any] struct {
|
||||
at time.Time
|
||||
v T
|
||||
}
|
||||
|
||||
// Hub merges the backends supplied by Provider.
|
||||
type Hub struct {
|
||||
Peers []Peer
|
||||
Token string
|
||||
Interval time.Duration
|
||||
client *http.Client
|
||||
stream *http.Client // long-lived SSE: no overall timeout, no redirects
|
||||
Provider func() []Backend
|
||||
|
||||
swg sync.WaitGroup // live stream goroutines
|
||||
lmu sync.Mutex
|
||||
live liveState
|
||||
mu sync.Mutex
|
||||
exports map[string]cached[store.PeerExport]
|
||||
studies map[string]cached[store.PeerStudy]
|
||||
|
||||
mu sync.RWMutex
|
||||
state map[string]*peerState
|
||||
|
||||
// OnLive, when set, sees every sanitized peer event before it is published
|
||||
// (it assigns the local id and registers it in the event log).
|
||||
OnLive func(*store.LiveEvent)
|
||||
lmu sync.Mutex
|
||||
recent []store.LiveEvent
|
||||
head int
|
||||
subs map[chan store.LiveEvent]struct{}
|
||||
vsubs map[chan store.VisitMsg]struct{}
|
||||
attach map[string]func() // backend name -> stop
|
||||
}
|
||||
|
||||
// NewHub creates a hub; Interval defaults to 15 seconds.
|
||||
func NewHub(peers []Peer, token string) *Hub {
|
||||
h := &Hub{Peers: peers, Token: token, Interval: 15 * time.Second,
|
||||
client: &http.Client{Timeout: 4 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, state: map[string]*peerState{}}
|
||||
tr := http.DefaultTransport.(*http.Transport).Clone()
|
||||
tr.ResponseHeaderTimeout = 10 * time.Second
|
||||
h.stream = &http.Client{Transport: tr, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
||||
for _, p := range peers {
|
||||
h.state[p.Name] = &peerState{exports: map[string]store.PeerExport{}, extra: map[string]*extraExport{}}
|
||||
}
|
||||
return h
|
||||
// NewHub creates a hub over the backends returned by provider (called on every use).
|
||||
func NewHub(provider func() []Backend) *Hub {
|
||||
return &Hub{Provider: provider, exports: map[string]cached[store.PeerExport]{}, studies: map[string]cached[store.PeerStudy]{},
|
||||
subs: map[chan store.LiveEvent]struct{}{}, vsubs: map[chan store.VisitMsg]struct{}{}, attach: map[string]func(){}}
|
||||
}
|
||||
|
||||
func (h *Hub) get(ctx context.Context, p Peer, path string, out any) (time.Duration, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.URL+path, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
func (h *Hub) backends(only string) []Backend {
|
||||
if h == nil || h.Provider == nil {
|
||||
return nil
|
||||
}
|
||||
tok := h.Token
|
||||
if p.Token != "" {
|
||||
tok = p.Token
|
||||
}
|
||||
if tok != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+tok)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
t := time.Now()
|
||||
resp, err := h.client.Do(req)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
io.Copy(io.Discard, io.LimitReader(resp.Body, 4096))
|
||||
return 0, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, maxBody+1))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(body) > maxBody {
|
||||
return 0, fmt.Errorf("response larger than %d bytes", maxBody)
|
||||
}
|
||||
if err := json.Unmarshal(body, out); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time.Since(t), nil
|
||||
}
|
||||
|
||||
// Refresh fetches every peer once, concurrently. Failures only mark the peer offline.
|
||||
func (h *Hub) Refresh(ctx context.Context) {
|
||||
var wg sync.WaitGroup
|
||||
for _, p := range h.Peers {
|
||||
wg.Add(1)
|
||||
go func(p Peer) {
|
||||
defer wg.Done()
|
||||
exports := map[string]store.PeerExport{}
|
||||
var lat time.Duration
|
||||
var ferr error
|
||||
for _, name := range store.BackgroundRanges {
|
||||
var e store.PeerExport
|
||||
d, err := h.get(ctx, p, "/api/v1/peer/export?range="+name, &e)
|
||||
if err == nil {
|
||||
err = e.Sanitize(name)
|
||||
}
|
||||
if err != nil {
|
||||
ferr = err
|
||||
break
|
||||
}
|
||||
lat = d
|
||||
e.Summary = rename(e.Summary, p.Name)
|
||||
exports[name] = e
|
||||
}
|
||||
h.mu.Lock()
|
||||
st := h.state[p.Name]
|
||||
if ferr != nil {
|
||||
st.online, st.err = false, ferr.Error()
|
||||
if len(st.err) > 160 {
|
||||
st.err = st.err[:160]
|
||||
}
|
||||
} else {
|
||||
st.online, st.err, st.lastOK, st.latency, st.exports = true, "", time.Now().Unix(), lat.Milliseconds(), exports
|
||||
for _, e := range exports {
|
||||
st.version = e.Summary.Version
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
}(p)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// fetchRange asks a peer for the export of a window and sanitizes it.
|
||||
func (h *Hub) fetchRange(ctx context.Context, p Peer, rg store.Range) (store.PeerExport, error) {
|
||||
var e store.PeerExport
|
||||
path := "/api/v1/peer/export?range=" + url.QueryEscape(rg.Name)
|
||||
if rg.Custom() {
|
||||
path = fmt.Sprintf("/api/v1/peer/export?from=%d&to=%d", rg.From, rg.To)
|
||||
}
|
||||
if _, err := h.get(ctx, p, path, &e); err != nil {
|
||||
return e, err
|
||||
}
|
||||
if err := e.SanitizeRange(rg); err != nil {
|
||||
return e, err
|
||||
}
|
||||
e.Summary = rename(e.Summary, p.Name)
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// exportsFor returns the exports of the online peers (or one) for a window:
|
||||
// the cached background ones, or an on-demand fetch cached for 15 s.
|
||||
func (h *Hub) exportsFor(ctx context.Context, rg store.Range, only string) map[string]store.PeerExport {
|
||||
out := map[string]store.PeerExport{}
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
for _, p := range h.Peers {
|
||||
if only != "" && only != p.Name {
|
||||
continue
|
||||
all := h.Provider()
|
||||
out := make([]Backend, 0, len(all))
|
||||
for _, b := range all {
|
||||
if only == "" || only == b.Name() {
|
||||
out = append(out, b)
|
||||
}
|
||||
h.mu.RLock()
|
||||
st := h.state[p.Name]
|
||||
online := st.online
|
||||
var cached *store.PeerExport
|
||||
if !rg.Custom() {
|
||||
if e, ok := st.exports[rg.Name]; ok {
|
||||
cached = &e
|
||||
}
|
||||
}
|
||||
if cached == nil {
|
||||
if x := st.extra[rg.Key()]; x != nil && time.Since(x.at) < extraTTL {
|
||||
e := x.e
|
||||
cached = &e
|
||||
}
|
||||
}
|
||||
h.mu.RUnlock()
|
||||
if !online {
|
||||
continue
|
||||
}
|
||||
if cached != nil {
|
||||
out[p.Name] = *cached
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(p Peer) {
|
||||
defer wg.Done()
|
||||
e, err := h.fetchRange(ctx, p, rg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
st := h.state[p.Name]
|
||||
if len(st.extra) >= extraMax {
|
||||
st.extra = map[string]*extraExport{}
|
||||
}
|
||||
st.extra[rg.Key()] = &extraExport{at: time.Now(), e: e}
|
||||
h.mu.Unlock()
|
||||
mu.Lock()
|
||||
out[p.Name] = e
|
||||
mu.Unlock()
|
||||
}(p)
|
||||
}
|
||||
wg.Wait()
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name() < out[j].Name() })
|
||||
return out
|
||||
}
|
||||
|
||||
// SummariesFor returns the summaries of online peers for any window.
|
||||
// PeerNames lists the names of the nodes (nil-safe).
|
||||
func (h *Hub) PeerNames() []string {
|
||||
var out []string
|
||||
for _, b := range h.backends("") {
|
||||
out = append(out, b.Name())
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Has reports whether name is a node served by this hub.
|
||||
func (h *Hub) Has(name string) bool { return len(h.backends(name)) > 0 }
|
||||
|
||||
func (h *Hub) export(b Backend, rg store.Range) (store.PeerExport, bool) {
|
||||
key := b.Name() + "|" + rg.Key()
|
||||
h.mu.Lock()
|
||||
if c, ok := h.exports[key]; ok && time.Since(c.at) < exportTTL {
|
||||
h.mu.Unlock()
|
||||
return c.v, true
|
||||
}
|
||||
h.mu.Unlock()
|
||||
e := b.Export(rg)
|
||||
if err := e.SanitizeRange(rg); err != nil {
|
||||
return e, false
|
||||
}
|
||||
e.Summary = rename(e.Summary, b.Name())
|
||||
h.mu.Lock()
|
||||
if len(h.exports) >= cacheMax {
|
||||
h.exports = map[string]cached[store.PeerExport]{}
|
||||
}
|
||||
h.exports[key] = cached[store.PeerExport]{time.Now(), e}
|
||||
h.mu.Unlock()
|
||||
return e, true
|
||||
}
|
||||
|
||||
// SummariesFor returns the summaries of the nodes (or one) for any window.
|
||||
func (h *Hub) SummariesFor(ctx context.Context, rg store.Range, only string) []store.Summary {
|
||||
m := h.exportsFor(ctx, rg, only)
|
||||
var out []store.Summary
|
||||
for _, p := range h.Peers {
|
||||
if e, ok := m[p.Name]; ok {
|
||||
for _, b := range h.backends(only) {
|
||||
if e, ok := h.export(b, rg); ok {
|
||||
out = append(out, e.Summary)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TrafficFor returns the analytics of online peers for any window and domain.
|
||||
// TrafficFor returns the analytics of the nodes (or one) for a window and domain.
|
||||
func (h *Hub) TrafficFor(ctx context.Context, rg store.Range, domain, only string) []store.TrafficData {
|
||||
m := h.exportsFor(ctx, rg, only)
|
||||
var out []store.TrafficData
|
||||
for _, p := range h.Peers {
|
||||
if e, ok := m[p.Name]; ok {
|
||||
for _, b := range h.backends(only) {
|
||||
if e, ok := h.export(b, rg); ok {
|
||||
if t, ok := e.Traffic[domain]; ok {
|
||||
t.Host = p.Name
|
||||
t.Host = b.Name()
|
||||
out = append(out, t)
|
||||
}
|
||||
}
|
||||
@@ -289,7 +141,17 @@ func (h *Hub) TrafficFor(ctx context.Context, rg store.Range, domain, only strin
|
||||
return out
|
||||
}
|
||||
|
||||
// rename forces the configured peer name on every row of a remote summary.
|
||||
// Summaries is SummariesFor for a named range.
|
||||
func (h *Hub) Summaries(rangeName, only string) []store.Summary {
|
||||
return h.SummariesFor(context.Background(), store.ParseRange(rangeName), only)
|
||||
}
|
||||
|
||||
// Traffic is TrafficFor for a named range.
|
||||
func (h *Hub) Traffic(rangeName, domain, only string) []store.TrafficData {
|
||||
return h.TrafficFor(context.Background(), store.ParseRange(rangeName), domain, only)
|
||||
}
|
||||
|
||||
// rename forces the node name on every row of a summary.
|
||||
func rename(s store.Summary, name string) store.Summary {
|
||||
s.Host = name
|
||||
for i := range s.Hosts {
|
||||
@@ -307,96 +169,13 @@ func rename(s store.Summary, name string) store.Summary {
|
||||
return s
|
||||
}
|
||||
|
||||
// Run refreshes until ctx is done.
|
||||
func (h *Hub) Run(ctx context.Context) {
|
||||
if len(h.Peers) == 0 {
|
||||
return
|
||||
}
|
||||
h.Stream(ctx)
|
||||
h.Refresh(ctx)
|
||||
t := time.NewTicker(h.Interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
h.Refresh(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Hosts lists the status of every peer (online or not).
|
||||
func (h *Hub) Hosts() []store.HostInfo {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
out := make([]store.HostInfo, 0, len(h.Peers))
|
||||
for _, p := range h.Peers {
|
||||
st := h.state[p.Name]
|
||||
hi := store.HostInfo{Name: p.Name, Online: st.online, Error: st.err, LastSeen: st.lastOK, LatencyMS: st.latency, Version: st.version, Live: st.live && st.online}
|
||||
if e, ok := st.exports["24h"]; ok && st.online {
|
||||
s := e.Summary
|
||||
hi.Requests, hi.Malicious = s.Totals.Requests, s.Totals.Malicious
|
||||
hi.HostileIPs, hi.ActiveBans = s.Totals.HostileIPs, s.Totals.ActiveBans
|
||||
if len(s.Hosts) > 0 {
|
||||
hi.Location = s.Hosts[0].Location
|
||||
}
|
||||
}
|
||||
out = append(out, hi)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Summaries returns the summaries of online peers for a range. When only is
|
||||
// not empty just that peer is returned.
|
||||
func (h *Hub) Summaries(rangeName, only string) []store.Summary {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
var out []store.Summary
|
||||
for _, p := range h.Peers {
|
||||
if only != "" && only != p.Name {
|
||||
continue
|
||||
}
|
||||
st := h.state[p.Name]
|
||||
if e, ok := st.exports[rangeName]; ok && st.online {
|
||||
out = append(out, e.Summary)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Peers lists the configured peer names (nil-safe).
|
||||
func (h *Hub) PeerNames() []string {
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(h.Peers))
|
||||
for _, p := range h.Peers {
|
||||
out = append(out, p.Name)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Has reports whether name is a configured peer.
|
||||
func (h *Hub) Has(name string) bool {
|
||||
for _, p := range h.Peers {
|
||||
if p.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IPDetail looks an address up in the cached exports of the online peers (the
|
||||
// 30-day summary, which carries the full row). No request is made to the peer.
|
||||
// IPDetail looks an address up in the 30-day export of every node.
|
||||
func (h *Hub) IPDetail(ip string) []store.IPSummary {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
var out []store.IPSummary
|
||||
for _, p := range h.Peers {
|
||||
st := h.state[p.Name]
|
||||
e, ok := st.exports["30d"]
|
||||
if !ok || !st.online {
|
||||
rg := store.ParseRange("30d")
|
||||
for _, b := range h.backends("") {
|
||||
e, ok := h.export(b, rg)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, x := range e.Summary.IPs {
|
||||
@@ -409,22 +188,78 @@ func (h *Hub) IPDetail(ip string) []store.IPSummary {
|
||||
return out
|
||||
}
|
||||
|
||||
// Traffic returns the cached analytics of online peers (or only one) for a
|
||||
// domain ("all" or a name), from the same export.
|
||||
func (h *Hub) Traffic(rangeName, domain, only string) []store.TrafficData {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
var out []store.TrafficData
|
||||
for _, p := range h.Peers {
|
||||
if only != "" && only != p.Name {
|
||||
// Hosts lists the status of every node (online or not).
|
||||
func (h *Hub) Hosts() []store.HostInfo {
|
||||
rg := store.ParseRange("24h")
|
||||
var out []store.HostInfo
|
||||
for _, b := range h.backends("") {
|
||||
hi := store.HostInfo{Name: b.Name(), Online: b.Online(), Live: b.Online(), Location: b.Location()}
|
||||
if hs, ok := b.(HostStatus); ok {
|
||||
hi = hs.HostInfo()
|
||||
hi.Name = b.Name()
|
||||
}
|
||||
if e, ok := h.export(b, rg); ok {
|
||||
s := e.Summary
|
||||
hi.Requests, hi.Malicious = s.Totals.Requests, s.Totals.Malicious
|
||||
hi.HostileIPs, hi.ActiveBans = s.Totals.HostileIPs, s.Totals.ActiveBans
|
||||
if hi.Location == nil && len(s.Hosts) > 0 {
|
||||
hi.Location = s.Hosts[0].Location
|
||||
}
|
||||
}
|
||||
out = append(out, hi)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func studyKey(q store.PeriodQuery) string {
|
||||
return fmt.Sprintf("%d-%d|%s|%s|%s|%d", q.From, q.To, q.IP, q.Cat, q.Fam, q.Gap)
|
||||
}
|
||||
|
||||
// StudiesFor returns the period and metrics study of every node (or one).
|
||||
func (h *Hub) StudiesFor(ctx context.Context, q store.PeriodQuery, only string) map[string]store.PeerStudy {
|
||||
out := map[string]store.PeerStudy{}
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
for _, b := range h.backends(only) {
|
||||
key := b.Name() + "|" + studyKey(q)
|
||||
h.mu.Lock()
|
||||
c, hit := h.studies[key]
|
||||
h.mu.Unlock()
|
||||
if hit && time.Since(c.at) < studyTTL {
|
||||
out[b.Name()] = c.v
|
||||
continue
|
||||
}
|
||||
st := h.state[p.Name]
|
||||
if e, ok := st.exports[rangeName]; ok && st.online {
|
||||
if t, ok := e.Traffic[domain]; ok {
|
||||
t.Host = p.Name
|
||||
out = append(out, t)
|
||||
wg.Add(1)
|
||||
go func(b Backend) {
|
||||
defer wg.Done()
|
||||
pq := q
|
||||
pq.Host = b.Name()
|
||||
s := b.Study(pq)
|
||||
if !s.Sanitize(b.Name()) {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
if len(h.studies) >= cacheMax {
|
||||
h.studies = map[string]cached[store.PeerStudy]{}
|
||||
}
|
||||
h.studies[key] = cached[store.PeerStudy]{time.Now(), s}
|
||||
h.mu.Unlock()
|
||||
mu.Lock()
|
||||
out[b.Name()] = s
|
||||
mu.Unlock()
|
||||
}(b)
|
||||
}
|
||||
wg.Wait()
|
||||
return out
|
||||
}
|
||||
|
||||
// EgressFor returns the egress report of every node (or one) for [from, to];
|
||||
// nodes without egress analysis are left out.
|
||||
func (h *Hub) EgressFor(ctx context.Context, from, to int64, only string) map[string]json.RawMessage {
|
||||
out := map[string]json.RawMessage{}
|
||||
for _, b := range h.backends(only) {
|
||||
if raw := b.Egress(from, to); len(raw) > 0 {
|
||||
out[b.Name()] = raw
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
Reference in new issue
Block a user