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

internal errors should be wrap or encapsulated? #93

Open
lingfengChen03 opened this issue Sep 2, 2022 · 3 comments
Open

internal errors should be wrap or encapsulated? #93

lingfengChen03 opened this issue Sep 2, 2022 · 3 comments

Comments

@lingfengChen03
Copy link

in the process of using Sometimes we needs to determine if the status of mmdb is closed or not.

errors.New("cannot call Lookup on a closed database")

Now that I'm out there judging strings, there should be a more elegant way to do it?
Looking forward to the author's recovery~

@oschwald
Copy link
Owner

oschwald commented Sep 2, 2022

Although I would accept a PR to make a more specific error type for this case, could you expand a bit on how you end up in the situation where you don't know if the reader is closed or not? In my own use, this would generally be indicative of a programming bug in the application.

@lingfengChen03
Copy link
Author

Although I would accept a PR to make a more specific error type for this case, could you expand a bit on how you end up in the situation where you don't know if the reader is closed or not? In my own use, this would generally be indicative of a programming bug in the application.

Thank you for your reply~
In the code, there is a timer that updates the mmdb regularly, meaning that one closes the old mmdb and opens the new one, so if another read-goroutine reads it, it may return an exception, so I want to get the current mmdb status for the subsequent logic.

	go func() {
		for range ticker.C {
			if MmdbReader != nil {
				MmdbReader.Close()
			}
			// init mmdb
			MmdbReader, err = maxminddb.Open(Conf.MaxmindDb)
                         ....
		}
	}()

@oschwald
Copy link
Owner

I see. For what it is worth, in situations like this one, I atomically replace the reader and rely on the finalizer to eventually close the old one. If you don't want to rely on the finalizer, you could also start a new goroutine to close it after an appropriate amount of time has passed for your use case.

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