-
Notifications
You must be signed in to change notification settings - Fork 55
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
Fix request scoped transactions #3103
Conversation
a40b397
to
ab282d5
Compare
"github.com/infrahq/infra/internal/access" | ||
"github.com/infrahq/infra/internal/server/models" | ||
) | ||
|
||
func pprofHandler(c *gin.Context) { | ||
func pprofHandler(c *gin.Context, _ *api.EmptyRequest) (*api.EmptyResponse, 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.
Now that the database transaction comes from wrapRoute
this handler had to be updated to fit the interface required by wrapRoute
.
@@ -108,30 +108,6 @@ func TestBindsEmptyRequest(t *testing.T) { | |||
assert.NilError(t, err) | |||
} | |||
|
|||
func TestGetRoute(t *testing.T) { |
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.
I think this test was probably useful before, but now it does not really do much. We have good test coverage for all of our real production routes, so we don't need to test an artificial route.
ab282d5
to
789a386
Compare
@@ -70,40 +70,31 @@ func handleInfraDestinationHeader(c *gin.Context) error { | |||
// authenticatedMiddleware is applied to all routes that require authentication. | |||
// It validates the access key, and updates the lastSeenAt of the user, and | |||
// possibly also of the destination. | |||
func authenticatedMiddleware(srv *Server) gin.HandlerFunc { |
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.
I think the name "authenticatedMiddleware" is confusing here where this is just an auth check and scope now, it doesnt actually pass the request through any middleware
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.
Very true, I'll rename this to remove "middlware", and I should also update the godoc at the same time.
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.
I pushed a commit that renames these to authenticateRequest
, and validateRequestOrganization
. What do you think of those names?
bindRoute(a, group.RouterGroup, route.method, route.path, handler) | ||
} | ||
|
||
func wrapRoute[Req, Res any](a *API, route route[Req, Res]) func(*gin.Context) 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.
I would like to see a comment explaining that wrapRoute
effectively functions as a middleware for requests, there is a lot going on in here
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.
Good call, there is a lot going on here. #3104 is a follow up PR that reduces it a bit.
More godoc here would be better. I'll add that now.
92a7486
to
a77eec0
Compare
d3984ae
to
b1171c9
Compare
a77eec0
to
233ad27
Compare
And handle sendAPIError once, instead of on every error.
To store the behaviour of different routes. This is temporary until we can set these on the route definition directly.
This commit fixes two bugs related to request scoped transactions.
Now that they are no longer middleware. Also document wrapRoute.
b1171c9
to
a2e50cf
Compare
Summary
Branched from #3028 this PR cleans up the commits from #3032 to fix both #2697 and #3076
We had two problems with our request scoped transactions:
This PR addresses the problems by:
wrapRoute
, which has access to the error from both the middleware and the handlerswrapRoute
add a defer which will rollback if commit has not happenedRelated Issues
Resolves #2697
Resolves #3076