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

If disconnected from server. #3

Open
guijun opened this issue Oct 25, 2021 · 4 comments
Open

If disconnected from server. #3

guijun opened this issue Oct 25, 2021 · 4 comments

Comments

@guijun
Copy link

guijun commented Oct 25, 2021

Is there any notify or chan or something ?
and

We just need reconnect to server. should we close memdb ? anything else ?

@unit-adm
Copy link
Member

You could listen on <- ctx.Done() in a separate goroutine to know if context is cancelled.

You don't need to call close explicitly but you could call udb.Disconnect or udb.DisconnectContext as it is safe to call disconnect even when connection is already closed (as it internally check the disconnection status and gracefully returns).

@guijun
Copy link
Author

guijun commented Oct 26, 2021

thanks.

ctx.Done() doesn't work.

to avoid stopping my coding progress, I forked this repo and had added patch and watching if it works.

@unit-adm
Copy link
Member

Use ctx.Done() when connection is successful and you have other goroutine that need to be notified.

A simple example is given below to retry client connection before exiting the application.

// Client connection with retry
err = func(retry int) error {
r := 0
for range time.Tick(100 * time.Millisecond) {
r++
err = client.ConnectContext(ctx)
if err == nil {
return nil
}
if r >= retry {
return err
}
fmt.Println("client connection retry #", r)
}
return nil
}(3)
if err != nil {
log.Fatalf("err: %s", err)
}

@guijun
Copy link
Author

guijun commented Oct 27, 2021

I havn't readed all src codes. but I have to make my code works, So I had done some dirty patch, please check if they are ok:
guijun@115483a

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

No branches or pull requests

2 participants