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

feat: reconnection in nip47 #72

Merged
merged 19 commits into from
Jun 1, 2024
Merged

feat: reconnection in nip47 #72

merged 19 commits into from
Jun 1, 2024

Conversation

im-adithya
Copy link
Member

Closes #66

  • Uses the subscription handler methods to make sure we reconnect to the relay
  • Adds some request event related attributes to the Subscription struct (omitted from the db) to ensure DRY code
  • Subscriptions are not stored in the db from now on for NIP47 requests
  • Publish is done after EOS is received in processEvents (conditionally for NIP47 requests)

@bumi bumi requested a review from rolznz May 29, 2024 13:38
Since time.Time
Until time.Time
Limit int
Search string
CreatedAt time.Time
UpdatedAt time.Time
Uuid string `gorm:"type:uuid;default:gen_random_uuid()"`
Uuid string `gorm:"type:uuid;default:gen_random_uuid()"`
EventChan chan *nostr.Event `gorm:"-"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to split the DB model from the application model. Then we do not need these gorm hints

REQUEST_EVENT_PUBLISH_CONFIRMED = "confirmed"
REQUEST_EVENT_PUBLISH_FAILED = "failed"
REQUEST_EVENT_PUBLISH_CONFIRMED = "CONFIRMED"
REQUEST_EVENT_PUBLISH_FAILED = "FAILED"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these still used and is it ok to change the casing? will the previous values be manually migrated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, we can do it this way instead of writing a migration

UPDATE request_events
SET state = UPPER(state);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@im-adithya keep in mind for an open source project we typically also have to think about other people who might run this.
it's fine here, but keep this in mind.

internal/nostr/models.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
}
svc.postEventToWebhook(event, requestData.WebhookUrl)
}()
go svc.startSubscription(svc.Ctx, &subscription)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bumi mentioned:

I might think a bit too complicated there and maybe the two options (webhook + sync) is not needed

But aren't they both separate, and good usecases? otherwise you are forced to use a webhook?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both are good and important use cases. not sure if we should continue to have it in one endpoint though if it's too complicated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we simplified code, it's actually easier to support both in a single endpoint...

internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
svc.db.Save(subscription)
svc.db.Save(requestEvent)
}()
relay, isCustomRelay, err := svc.getRelayConnection(ctx, subscription.RelayUrl)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking here: the isCustomRelay logic looks quite dangerous, it disconnects the relay after each request and in multiple places. If there are two simultaneous requests to the same custom relay, this could break

internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
internal/nostr/nostr.go Outdated Show resolved Hide resolved
requestEvent := RequestEvent{}
findRequestResult := svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).Find(&requestEvent)

if findRequestResult.RowsAffected != 0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code all looks duplicated from another method - can you try DRY it up?

internal/nostr/nostr.go Outdated Show resolved Hide resolved
@im-adithya
Copy link
Member Author

Right now we are only storing the subscription in the DB to get an ID from there which we then use to store subscriptions in the svc, if we use a uuid generator then we can stop storing the subscriptions which basically bloats the DB for no reason. Should I proceed with that?

@bumi bumi merged commit 78a4d60 into main Jun 1, 2024
1 check passed
@bumi bumi deleted the task-nip47 branch June 1, 2024 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review/Refactor NIP-47 Handler
3 participants