Browse Source

see if spider works

imwald-v0.58.5
silberengel 4 months ago
parent
commit
219fa7a85f
  1. 2
      go.mod
  2. 91
      pkg/spider/spider.go

2
go.mod

@ -269,4 +269,4 @@ require (
) )
retract v1.0.3 retract v1.0.3
replace git.mleku.dev/mleku/nostr => ./docker-build-context/nostr replace git.mleku.dev/mleku/nostr => ../nostr

91
pkg/spider/spider.go

@ -235,6 +235,10 @@ func (s *Spider) mainLoop() {
log.I.F("spider: main loop started, checking every %v", MainLoopInterval) log.I.F("spider: main loop started, checking every %v", MainLoopInterval)
// Do an immediate check on startup
log.I.F("spider: performing initial connection check")
s.updateConnections()
for { for {
select { select {
case <-s.ctx.Done(): case <-s.ctx.Done():
@ -243,7 +247,7 @@ func (s *Spider) mainLoop() {
log.I.F("spider: follow list updated, refreshing connections") log.I.F("spider: follow list updated, refreshing connections")
s.updateConnections() s.updateConnections()
case <-ticker.C: case <-ticker.C:
log.D.F("spider: periodic check triggered") log.I.F("spider: periodic check triggered")
s.updateConnections() s.updateConnections()
} }
} }
@ -251,44 +255,105 @@ func (s *Spider) mainLoop() {
// updateConnections updates relay connections based on current admin relays and follow lists // updateConnections updates relay connections based on current admin relays and follow lists
func (s *Spider) updateConnections() { func (s *Spider) updateConnections() {
s.mu.Lock() s.mu.RLock()
defer s.mu.Unlock() running := s.running
s.mu.RUnlock()
if !s.running { if !running {
return return
} }
// Get current admin relays and follow list // Get current admin relays and follow list
adminRelays := s.getAdminRelays() if s.getAdminRelays == nil {
followList := s.getFollowList() log.W.F("spider: getAdminRelays callback is nil")
return
}
if s.getFollowList == nil {
log.W.F("spider: getFollowList callback is nil")
return
}
// Call callbacks with panic recovery
var adminRelays []string
var followList [][]byte
func() {
defer func() {
if r := recover(); r != nil {
log.E.F("spider: panic in getAdminRelays callback: %v", r)
adminRelays = nil
}
}()
adminRelays = s.getAdminRelays()
}()
func() {
defer func() {
if r := recover(); r != nil {
log.E.F("spider: panic in getFollowList callback: %v", r)
followList = nil
}
}()
followList = s.getFollowList()
}()
log.I.F("spider: updateConnections - admin relays: %d, follow list: %d", len(adminRelays), len(followList))
if len(adminRelays) > 0 {
log.I.F("spider: admin relays: %v", adminRelays)
} else {
log.I.F("spider: admin relays callback returned empty/nil - checking bootstrap relay configuration")
}
if len(followList) > 0 {
log.D.F("spider: follow list has %d pubkeys", len(followList))
} else {
log.I.F("spider: follow list callback returned empty/nil - check if admin follow lists have been fetched")
}
if len(adminRelays) == 0 || len(followList) == 0 { if len(adminRelays) == 0 || len(followList) == 0 {
log.D.F("spider: no admin relays (%d) or follow list (%d) available", log.I.F("spider: no admin relays (%d) or follow list (%d) available - cannot create connections",
len(adminRelays), len(followList)) len(adminRelays), len(followList))
if len(adminRelays) == 0 {
log.I.F("spider: admin relays callback returned empty list - check if bootstrap relays are configured")
}
if len(followList) == 0 {
log.I.F("spider: follow list callback returned empty list - check if admin follow lists have been fetched")
}
return return
} }
// Update connections for current admin relays (filtering out self) // Update connections for current admin relays (filtering out self)
// Note: We release the mutex before the loop because isSelfRelay() needs to acquire it
log.I.F("spider: processing %d admin relays (will filter out self-relays)", len(adminRelays))
currentRelays := make(map[string]bool) currentRelays := make(map[string]bool)
for _, url := range adminRelays { for i, url := range adminRelays {
log.I.F("spider: checking relay %d/%d: %s", i+1, len(adminRelays), url)
// Check if this relay URL is ourselves // Check if this relay URL is ourselves
if s.isSelfRelay(url) { isSelf := s.isSelfRelay(url)
log.D.F("spider: skipping self-relay: %s", url) log.D.F("spider: isSelfRelay(%s) = %v", url, isSelf)
if isSelf {
log.I.F("spider: skipping self-relay: %s", url)
continue continue
} }
currentRelays[url] = true currentRelays[url] = true
if conn, exists := s.connections[url]; exists { // Acquire lock to check and modify connections
s.mu.Lock()
conn, exists := s.connections[url]
s.mu.Unlock()
if exists {
// Update existing connection // Update existing connection
log.I.F("spider: updating existing connection to %s", url)
conn.updateSubscriptions(followList) conn.updateSubscriptions(followList)
} else { } else {
// Create new connection // Create new connection
log.I.F("spider: creating new connection to %s", url)
s.createConnection(url, followList) s.createConnection(url, followList)
} }
} }
log.I.F("spider: processed %d relays, created/updated %d connections", len(adminRelays), len(currentRelays))
// Remove connections for relays no longer in admin list // Remove connections for relays no longer in admin list
s.mu.Lock()
for url, conn := range s.connections { for url, conn := range s.connections {
if !currentRelays[url] { if !currentRelays[url] {
log.I.F("spider: removing connection to %s (no longer in admin relays)", url) log.I.F("spider: removing connection to %s (no longer in admin relays)", url)
@ -296,6 +361,7 @@ func (s *Spider) updateConnections() {
delete(s.connections, url) delete(s.connections, url)
} }
} }
s.mu.Unlock()
} }
// createConnection creates a new relay connection // createConnection creates a new relay connection
@ -312,7 +378,10 @@ func (s *Spider) createConnection(url string, followList [][]byte) {
reconnectDelay: ReconnectDelay, reconnectDelay: ReconnectDelay,
} }
// Acquire lock to add connection
s.mu.Lock()
s.connections[url] = conn s.connections[url] = conn
s.mu.Unlock()
// Start connection in goroutine // Start connection in goroutine
go conn.manage(followList) go conn.manage(followList)

Loading…
Cancel
Save