Skip to content

[datastore] Generic 'Dial' function #1409

Open
the-glu wants to merge 2 commits intointeruss:masterfrom
Orbitalize:generic_dialer
Open

[datastore] Generic 'Dial' function #1409
the-glu wants to merge 2 commits intointeruss:masterfrom
Orbitalize:generic_dialer

Conversation

@the-glu
Copy link
Copy Markdown
Contributor

@the-glu the-glu commented Mar 31, 2026

Follow #1408 , extracted from #1403

Move to a generic dial function, normalizing datastore.Dial calls, checks of errors and (optional) cron creation.

scdV1Server, err = createSCDServer(ctx, logger)
if err != nil {
ridV1Server.Cron.Stop()
ridV2Server.Cron.Stop()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This will be fixed in a follow up PR. It's needed, but not implemented correctly (e.g. missing in scd)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mention this in a TODO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Already ready in #1410

@@ -0,0 +1,64 @@
package datastoreutils
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not in datastore package directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because that would create a loop: flags refrence datastore, and we need flags there :/

return err
}

func Dial(ctx context.Context, logger *zap.Logger, withCheckCron bool) (*Store, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Inline those Dial functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure how and why, what do you have in mind?

}
}

func DialStore[S any](ctx context.Context, dbName string, withCheckCron bool, newStore func(*datastore.Datastore) (S, error)) (S, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In the end, do we ever create stores without dialing them? If not, collapse DialStore and NewStore? (at least for what is exposed outside the package)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tests does, for now, as they want to override the clock.

We could add it as parameters, but that would be future PR no?

}
s, err := newStore(db)
if err != nil {
db.Pool.Close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are you sure this is safe to call here? (before it was within the next if block)
-> fix in a separate PR if it is

connectParameters := flags.ConnectParameters()
connectParameters.DBName = "aux"
datastore, err := datastore.Dial(ctx, connectParameters)
auxStore, err := auxc.Dial(ctx, logger, true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The withCronCheck should be false here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Are you sure? I would normalize scd/rid/aux there, no? Why aux wouldn't be checked like the others?

if withCheckCron {
c := cron.New()
if _, err := c.AddFunc("@every 1m", func() { checkDatabase(ctx, db, dbName) }); err != nil {
db.Pool.Close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This was not here before, is this safe to call?
-> fix in a separate PR if it is

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before it was only called in one return, so I normalized it. Since the db is going to be unused anymore, I do think it's ok and make sense to properly clean up in all cases.

scdV1Server, err = createSCDServer(ctx, logger)
if err != nil {
ridV1Server.Cron.Stop()
ridV2Server.Cron.Stop()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mention this in a TODO?

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.

2 participants