Add disable-federation flag, default false

This commit is contained in:
Jeffrey Picard 2021-11-24 18:36:19 -05:00
parent 72ea236d86
commit ca0e8562f3
2 changed files with 42 additions and 33 deletions

View file

@ -37,20 +37,21 @@ type Args struct {
} }
const ( const (
DefaultHost = "0.0.0.0" DefaultHost = "0.0.0.0"
DefaultPort = "50051" DefaultPort = "50051"
DefaultEsHost = "http://localhost" DefaultEsHost = "http://localhost"
DefaultEsIndex = "claims" DefaultEsIndex = "claims"
DefaultEsPort = "9200" DefaultEsPort = "9200"
DefaultPrometheusPort = "2112" DefaultPrometheusPort = "2112"
DefaultRefreshDelta = 5 DefaultRefreshDelta = 5
DefaultCacheTTL = 5 DefaultCacheTTL = 5
DefaultPeerFile = "peers.txt" DefaultPeerFile = "peers.txt"
DefaultCountry = "US" DefaultCountry = "US"
DefaultLoadPeers = true DefaultLoadPeers = true
DefaultStartPrometheus = true DefaultStartPrometheus = true
DefaultStartUDP = true DefaultStartUDP = true
DefaultWritePeers = true DefaultWritePeers = true
DefaultDisableFederation = false
) )
// GetEnvironment takes the environment variables as an array of strings // GetEnvironment takes the environment variables as an array of strings
@ -100,6 +101,7 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
startPrometheus := parser.Flag("", "start-prometheus", &argparse.Options{Required: false, Help: "Start prometheus server", Default: DefaultStartPrometheus}) startPrometheus := parser.Flag("", "start-prometheus", &argparse.Options{Required: false, Help: "Start prometheus server", Default: DefaultStartPrometheus})
startUdp := parser.Flag("", "start-udp", &argparse.Options{Required: false, Help: "Start UDP ping server", Default: DefaultStartUDP}) startUdp := parser.Flag("", "start-udp", &argparse.Options{Required: false, Help: "Start UDP ping server", Default: DefaultStartUDP})
writePeers := parser.Flag("", "write-peers", &argparse.Options{Required: false, Help: "Write peer to disk as we learn about them", Default: DefaultWritePeers}) writePeers := parser.Flag("", "write-peers", &argparse.Options{Required: false, Help: "Write peer to disk as we learn about them", Default: DefaultWritePeers})
disableFederation := parser.Flag("", "disable-federation", &argparse.Options{Required: false, Help: "Disable server federation", Default: DefaultDisableFederation})
text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"}) text := parser.String("", "text", &argparse.Options{Required: false, Help: "text query"})
name := parser.String("", "name", &argparse.Options{Required: false, Help: "name"}) name := parser.String("", "name", &argparse.Options{Required: false, Help: "name"})
@ -118,23 +120,24 @@ func ParseArgs(searchRequest *pb.SearchRequest) *Args {
} }
args := &Args{ args := &Args{
CmdType: SearchCmd, CmdType: SearchCmd,
Host: *host, Host: *host,
Port: *port, Port: *port,
EsHost: *esHost, EsHost: *esHost,
EsPort: *esPort, EsPort: *esPort,
PrometheusPort: *prometheusPort, PrometheusPort: *prometheusPort,
EsIndex: *esIndex, EsIndex: *esIndex,
RefreshDelta: *refreshDelta, RefreshDelta: *refreshDelta,
CacheTTL: *cacheTTL, CacheTTL: *cacheTTL,
PeerFile: *peerFile, PeerFile: *peerFile,
Country: *country, Country: *country,
DisableEs: *disableEs, DisableEs: *disableEs,
Debug: *debug, Debug: *debug,
LoadPeers: *loadPeers, LoadPeers: *loadPeers,
StartPrometheus: *startPrometheus, StartPrometheus: *startPrometheus,
StartUDP: *startUdp, StartUDP: *startUdp,
WritePeers: *writePeers, WritePeers: *writePeers,
DisableFederation: *disableFederation,
} }
if esHost, ok := environment["ELASTIC_HOST"]; ok { if esHost, ok := environment["ELASTIC_HOST"]; ok {

View file

@ -267,7 +267,10 @@ func (s *Server) writePeers() {
// notifyPeer takes a peer to notify and a new peer we just learned about // notifyPeer takes a peer to notify and a new peer we just learned about
// and calls AddPeer on the first. // and calls AddPeer on the first.
func notifyPeer(peerToNotify *FederatedServer, newPeer *FederatedServer) error { func (s *Server) notifyPeer(peerToNotify *FederatedServer, newPeer *FederatedServer) error {
if s.Args.DisableFederation {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
@ -303,7 +306,7 @@ func (s *Server) notifyPeerSubs(newServer *FederatedServer) {
s.PeerSubsMut.RLock() s.PeerSubsMut.RLock()
for key, peer := range s.PeerSubs { for key, peer := range s.PeerSubs {
log.Printf("Notifying peer %s of new node %+v\n", key, newServer) log.Printf("Notifying peer %s of new node %+v\n", key, newServer)
err := notifyPeer(peer, newServer) err := s.notifyPeer(peer, newServer)
if err != nil { if err != nil {
log.Println("Failed to send data to ", key) log.Println("Failed to send data to ", key)
log.Println(err) log.Println(err)
@ -327,6 +330,9 @@ func (s *Server) notifyPeerSubs(newServer *FederatedServer) {
// if they're online, and adds them to our list of peer. If we're not currently // if they're online, and adds them to our list of peer. If we're not currently
// subscribed to a peer, it will also subscribe to it. // subscribed to a peer, it will also subscribe to it.
func (s *Server) addPeer(msg *pb.ServerMessage, ping bool, subscribe bool) error { func (s *Server) addPeer(msg *pb.ServerMessage, ping bool, subscribe bool) error {
if s.Args.DisableFederation {
return nil
}
// First thing we get our external ip if we don't have it, otherwise we // First thing we get our external ip if we don't have it, otherwise we
// could end up subscribed to our self, which is silly. // could end up subscribed to our self, which is silly.
nilIP := net.IP{} nilIP := net.IP{}