diff --git a/knotmirror/knotstream/knotstream.go b/knotmirror/knotstream/knotstream.go index 22f16b9d..4b0d862e 100644 --- a/knotmirror/knotstream/knotstream.go +++ b/knotmirror/knotstream/knotstream.go @@ -66,7 +66,17 @@ func (s *KnotStream) SubscribeHost(ctx context.Context, hostname string, noSSL b if host.Status == models.HostStatusBanned { return fmt.Errorf("cannot subscribe to banned knot") } - return s.slurper.Subscribe(ctx, *host) + // `Subscribe` expects long-living context + if err := s.slurper.Subscribe(*host); err != nil { + return fmt.Errorf("slurper: %w", err) + } + + host.Status = models.HostStatusActive + if err := db.UpsertHost(ctx, s.db, host); err != nil { + return fmt.Errorf("upserting host status to db: %w", err) + } + + return nil } func (s *KnotStream) ResubscribeAllHosts(ctx context.Context) error { @@ -78,7 +88,7 @@ func (s *KnotStream) ResubscribeAllHosts(ctx context.Context) error { for _, host := range hosts { l := s.logger.With("hostname", host.Hostname) l.Info("re-subscribing to active host") - if err := s.slurper.Subscribe(ctx, host); err != nil { + if err := s.slurper.Subscribe(host); err != nil { l.Warn("failed to re-subscribe to host", "err", err) } // sleep for a very short period, so we don't open tons of sockets at the same time diff --git a/knotmirror/knotstream/slurper.go b/knotmirror/knotstream/slurper.go index e6299eac..b28ca1dc 100644 --- a/knotmirror/knotstream/slurper.go +++ b/knotmirror/knotstream/slurper.go @@ -89,7 +89,7 @@ func (s *KnotSlurper) persistCursors(ctx context.Context) error { return err } -func (s *KnotSlurper) Subscribe(ctx context.Context, host models.Host) error { +func (s *KnotSlurper) Subscribe(host models.Host) error { s.subsLk.Lock() defer s.subsLk.Unlock() @@ -109,6 +109,10 @@ func (s *KnotSlurper) Subscribe(ctx context.Context, host models.Host) error { } s.subs[host.Hostname] = sub + // TODO: use service level context, not the top-most one. + // Using top-most context should be avoided to do graceful shutdown. + ctx := context.TODO() + sub.scheduler.Start(ctx) go s.subscribeWithRedialer(ctx, host, sub) return nil @@ -120,6 +124,7 @@ func (s *KnotSlurper) subscribeWithRedialer(ctx context.Context, host models.Hos s.subsLk.Lock() defer s.subsLk.Unlock() + l.Info("unsubscribing knot") delete(s.subs, host.Hostname) }()