Files
routewatch/internal/ristypes/ris.go
T
clawbot 825afa2ebe
check / check (push) Waiting to run
Remove a peer's live routes when its session goes down (closes #56)
RIS sends no withdrawals for the routes of a peer whose BGP session goes
down, so routewatch kept them.

The streamer now hands STATE messages to the prefix handler. They are never
dropped for backpressure: one that finds the queue full is held and queued
first once there is room, and the handler's other messages are dropped
meanwhile, so none overtakes it. Nothing waits. On a down, the prefix
handler drops the peer's pending updates and its next flush deletes all the
peer's live routes before the batch's other writes, keeping the in-memory
counts exact. The live route tables are unique on peer and prefix, which
also finds a peer's routes.

Model: opus-5-5
2026-10-19 11:40:31 +02:00

115 lines
4.0 KiB
Go

// Package ristypes defines the data structures for RIS Live BGP messages and announcements.
package ristypes
import (
"encoding/json"
"time"
)
// Message types and the session state the handlers act on, as the feed writes
// them in a message's type and state fields.
const (
// MessageTypeUpdate is a BGP UPDATE: announcements and withdrawals.
MessageTypeUpdate = "UPDATE"
// MessageTypeState is a change in the state of the BGP session between
// the collector and the peer.
MessageTypeState = "STATE"
// StateDown is the state of a STATE message whose session has ended: the
// collector no longer has any of the peer's routes, and sends no
// withdrawals for them.
StateDown = "down"
)
// ASPath represents a BGP AS path as a slice of AS numbers.
// It handles JSON unmarshaling of both simple arrays and nested AS sets,
// flattening any nested structures into a single sequence of AS numbers.
type ASPath []int
// UnmarshalJSON implements the json.Unmarshaler interface for ASPath.
// It handles both simple integer arrays [1, 2, 3] and nested AS sets
// like [1, [2, 3], 4], flattening them into a single slice of integers.
func (p *ASPath) UnmarshalJSON(data []byte) error {
// First try to unmarshal as a simple array of integers
var simple []int
if err := json.Unmarshal(data, &simple); err == nil {
*p = ASPath(simple)
return nil
}
// If that fails, unmarshal as array of interfaces and flatten
var raw []interface{}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
// Flatten the array
result := make([]int, 0)
for _, item := range raw {
switch v := item.(type) {
case float64:
result = append(result, int(v))
case []interface{}:
// Nested array - flatten it
for _, nested := range v {
if num, ok := nested.(float64); ok {
result = append(result, int(num))
}
}
}
}
*p = ASPath(result)
return nil
}
// RISLiveMessage represents the outer wrapper message from the RIPE RIS Live stream.
// Each message contains a Type field indicating the message type and a Data field
// containing the actual BGP message payload.
type RISLiveMessage struct {
Type string `json:"type"`
Data RISMessage `json:"data"`
}
// RISMessage represents a BGP update message from the RIPE RIS Live stream.
// It contains metadata about the BGP session (peer, ASN, host) along with
// the actual BGP update data including AS path, communities, announcements,
// and withdrawals.
type RISMessage struct {
Type string `json:"type"`
Timestamp float64 `json:"timestamp"`
ParsedTimestamp time.Time `json:"-"` // Parsed from Timestamp field
Peer string `json:"peer"`
PeerASN string `json:"peer_asn"`
ID string `json:"id"`
Host string `json:"host"`
RRC string `json:"rrc,omitempty"`
MrtTime float64 `json:"mrt_time,omitempty"`
SocketTime float64 `json:"socket_time,omitempty"`
Path ASPath `json:"path,omitempty"`
// Community and Raw are present in the feed but read by no handler.
// They are the largest fields on a message that lives in up to four
// handler queues, so json:"-" keeps them out of the decoded message
// to save queue memory. Do not decode them without a consumer.
Community [][]int `json:"-"`
Origin string `json:"origin,omitempty"`
MED *int `json:"med,omitempty"`
LocalPref *int `json:"local_pref,omitempty"`
Announcements []RISAnnouncement `json:"announcements,omitempty"`
Withdrawals []string `json:"withdrawals,omitempty"`
Raw string `json:"-"`
// State is set on STATE messages: the session's new state, such as StateDown.
State string `json:"state,omitempty"`
}
// RISAnnouncement represents a BGP route announcement within a RIS message.
// It contains the next hop IP address and the list of prefixes being announced
// via that next hop.
type RISAnnouncement struct {
NextHop string `json:"next_hop"`
Prefixes []string `json:"prefixes"`
}