Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subscriptions slow to update #54

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions service/adapters/relay_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,29 @@ import (
"github.com/planetary-social/nos-crossposting-service/service/domain"
)

const (
reconnectAfter = 1 * time.Minute
)

type RelayConnection struct {
address domain.RelayAddress
logger logging.Logger

state app.RelayConnectionState
stateMutex sync.Mutex

subscriptions map[string]subscription
subscriptionsMutex sync.Mutex
subscriptions map[string]subscription
subscriptionsUpdatedCh chan struct{}
subscriptionsUpdatedChClosed bool
subscriptionsMutex sync.Mutex
}

func NewRelayConnection(address domain.RelayAddress, logger logging.Logger) *RelayConnection {
return &RelayConnection{
address: address,
logger: logger.New(fmt.Sprintf("relayConnection(%s)", address.String())),
subscriptions: make(map[string]subscription),
address: address,
logger: logger.New(fmt.Sprintf("relayConnection(%s)", address.String())),
subscriptions: make(map[string]subscription),
subscriptionsUpdatedCh: make(chan struct{}),
}
}

Expand Down Expand Up @@ -82,6 +89,8 @@ func (r *RelayConnection) GetEvents(ctx context.Context, publicKey domain.Public
maxAge: maxAge,
}

r.triggerSubscriptionUpdate()

go func() {
<-ctx.Done()
if err := r.removeChannel(ch); err != nil {
Expand Down Expand Up @@ -110,13 +119,26 @@ func (r *RelayConnection) removeChannel(chToRemove chan app.EventOrEndOfSavedEve
if chToRemove == subscription.ch {
close(subscription.ch)
delete(r.subscriptions, uuid)
r.triggerSubscriptionUpdate()
return nil
}
}

return errors.New("somehow the channel was already removed")
}

func (r *RelayConnection) triggerSubscriptionUpdate() {
if !r.subscriptionsUpdatedChClosed {
r.subscriptionsUpdatedChClosed = true
close(r.subscriptionsUpdatedCh)
}
}

func (r *RelayConnection) resetSubscriptionUpdateCh() {
r.subscriptionsUpdatedChClosed = false
r.subscriptionsUpdatedCh = make(chan struct{})
}

func (r *RelayConnection) run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down Expand Up @@ -227,7 +249,7 @@ func (r *RelayConnection) manageSubs(
}

select {
case <-time.After(manageSubscriptionsEvery):
case <-r.subscriptionsUpdatedCh:
continue
case <-ctx.Done():
return ctx.Err()
Expand All @@ -242,6 +264,8 @@ func (r *RelayConnection) updateSubs(
r.subscriptionsMutex.Lock()
defer r.subscriptionsMutex.Unlock()

r.resetSubscriptionUpdateCh()

for _, uuid := range activeSubscriptions.List() {
if _, ok := r.subscriptions[uuid]; !ok {
r.logger.Trace().
Expand Down
3 changes: 0 additions & 3 deletions service/adapters/relay_event_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (

const (
storeMetricsEvery = 30 * time.Second

reconnectAfter = 1 * time.Minute
manageSubscriptionsEvery = 10 * time.Second
)

type RelayEventDownloader struct {
Expand Down