-
Notifications
You must be signed in to change notification settings - Fork 81
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
Misc fixes #560
base: main
Are you sure you want to change the base?
Misc fixes #560
Conversation
Since Go 1.21, there is a slices package in the standard library, that implements the same functionality more comprehensively. Replace the uses of ArrayContains with slices.Contains.
576cf4b
to
4044eb6
Compare
okay, now for the scariest part… |
d45c407
to
07ca3f2
Compare
…ne_importers/internal
…ne_exporters/internal
A number of these can probably be added to the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to submit these changes! A lot of them are agreeable and certainly improve the code quality, though several don't appear to consider why a particular decision was made.
I've left comments by going through the commits individually. For comments which affect the whole commit, I've arbitrarily left the comment on the first file so there's some visible threading in the github UI. I suggest reviewing this review by going through the commits individually.
It also appears as though some commits include unrelated changes, making it hard to accept this commit by commit. I would suggest making the changes recommended in this review and opening a dedicated PR for each task instead, encouraging commits to be distinct and limited in scope. This will also help with the eventual merge strategy, as MMR's policy is to squash merge rather than cherry pick or create a merge commit.
If you have any questions about the feedback here, please feel free to ask either here or in the support room.
|
||
// var NumericIdRegex = regexp.MustCompile("[0-9]+") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this got moved down incorrectly
@@ -48,8 +52,8 @@ func GuestAuthFailed() *ErrorResponse { | |||
return &ErrorResponse{common.ErrCodeNoGuests, "Guests cannot use this endpoint", common.ErrCodeNoGuests} | |||
} | |||
|
|||
func BadRequest(message string) *ErrorResponse { | |||
return &ErrorResponse{common.ErrCodeUnknown, message, common.ErrCodeBadRequest} | |||
func BadRequest(err error) *ErrorResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions deliberately do not take error objects to avoid accidentally leaking internal details to callers - please revert this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's an exception to this rule later on in the federation test/info endpoints, as those endpoints are primarily used for debugging or by admins rather than random consumers.
@@ -70,59 +70,59 @@ func openDatabase(connectionString string, maxConns int, maxIdleConns int) error | |||
var err error | |||
|
|||
if d.conn, err = sql.Open("postgres", connectionString); err != nil { | |||
return errors.New("error connecting to db: " + err.Error()) | |||
return fmt.Errorf("error connecting to db: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably be using errors.Join()
in these cases to avoid losing context on the stack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%w
is a special formatting instruction for fmt.Errorf()
that wraps the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, that feels a bit magic. errors.Join
is more clear.
var apiUrlCacheInstance *cache.Cache | ||
var apiUrlSingletonLock = &sync.Once{} | ||
var ( | ||
apiUrlCacheInstance *cache.Cache | ||
apiUrlSingletonLock = &sync.Once{} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please undo this change
mediaChan <- media | ||
}() | ||
mediaChan <- media | ||
close(mediaChan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a bit uncomfortable to me to have the close in the goroutine when the channel is created externally - please restore the defer close(mediaChan)
to avoid potential resource leaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is a bug. Recovering from a panic does not remedy unclear channel ownership. The go routine for writing is no longer necessary since this is a buffered channel now. And the channel can just be closed afterwards. A simple look at the possible code paths confirms that there is no way the channel could remain unclosed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The panic recovery is indeed a bug, though the defer close(mediaChan)
needs to be moved up near the channel creation please. The remainder of the change is fine, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. This is a concurrency bug. The channel closing can be deferred in the writing function, but never outside of it. Otherwise, you end with a potential write on a closed channel. And since uploading likely takes a few milliseconds more than thumbnailing (at least on reasonably fast hardware), this will happen more often than not. So the best I could do is:
go func(){
defer close(mediachan)
// upload and stuff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be blocking on a read from the channel before the defer
would fire - if we're not, then the concurrency is broken for sure. I'm not really comfortable using a different close style in a single section of the code, and would rather go out of the way to make defer close()
immediately after creation work reliably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert 6fc49c2 - the package naming is deliberate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert dc277e9 - the package naming is deliberate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert 1f3380e - the package naming is deliberate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert a31465b - the package naming is deliberate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert 0c957d8 - the package naming is deliberate
Just some minor things I've noticed while perusing the codebase. This includes:
util.ArrayContains
)strings.HasSuffix
)thumbnailing/{i,m,u}
,**/_*
)regexp.MustCompile
)time.Time
type to represent times