-
Notifications
You must be signed in to change notification settings - Fork 985
lxd: Pass around the request context instead of the request #15529
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
Conversation
f5d93ea to
4196fa2
Compare
4196fa2 to
5cb758a
Compare
|
I've had a quick look over this out of curiosity and I have a few initial thoughts:
type RequestContext interface {
context.Context
ID() string // ID of request
IsAuthenticated() bool
AuthenticationMethod() string // Always returning the authentication method of the original requestor, even if forwarded
Identifier() string // Always returning the identifier of the original requestor, even if forwarded
IsForwarded() bool // Indicate that the request has been forwarded.
ForwardedFrom() string
/* etc. */
}Then, when a request arrives, an implementation of this interface is instantiated and passed around as a |
|
Thanks for taking a look at this 🚀 We previously briefly spoken about that and I completely agree. Also we avoid confusion where functions accept multiple contexts And this still leaves us the room to pass the request context as
Could you elaborate on this one a bit? I'm a bit confused because those smaller functions generally accept different input arguments, and we would introduce an interface that just maps our current implementation? |
|
I'm not entirely convinced on the need for an interface (as opposed to a concrete struct), is there going to be multiple implementations? If not then a concrete struct should be sufficient to hold the functions/variables, rather than needing to mask it behind an interface. See https://medium.com/@rahulgorai/why-interfaces-can-be-expensive-in-go-and-when-you-should-still-use-them-a1510c162851 |
Currently we have handlers that accept various parameters and return different things based on path arguments and query parameters. My meaning here is the business logic should never return e.g. GetInstanceNames(ctx context.Context, projectName string) ([]string, error)
GetInstances(ctx context.Context, projectName string) ([]api.Instance, error)
GetInstancesFull(ctx context.Context, projectName string) ([]api.InstanceFull, error)
GetInstanceNamesAllProjects(ctx context.Context) ([]string, error)
GetInstancesAllProjects(ctx context.Context) ([]api.Instance, error)
GetInstanceFullAllProjects(ctx context.Context) ([]api.InstanceFull, error)Depending on the initial request parsing, the handler will call out to one of these functions. These functions would be almost identical to those defined in
I agree, there's no reason for the interface. Instead we could just have a struct for this that is set in the request context. If this struct is not present then it is an internal request. This would just bundle the various existing context keys together to make them easier to use. |
Yep, that's the goal. Effectively replacing EDIT: Although the function names can be adjusted according to your suggestion. Currently I went with |
a619cc3 to
6869fd5
Compare
6869fd5 to
aebb719
Compare
0e8c145 to
224e2da
Compare
|
@markylaing @tomponline Removed handler refactoring logic and just left the bits that pass around the request context instead of the http.Request. The request info is coming in a followup PR. |
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
…quest Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
…a request Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
…equest Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
…uest Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
… of a request Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
…request Signed-off-by: Din Music <[email protected]>
…equest Signed-off-by: Din Music <[email protected]>
…f a request Signed-off-by: Din Music <[email protected]>
…of a request Signed-off-by: Din Music <[email protected]>
…instead of a request Signed-off-by: Din Music <[email protected]>
…equest Signed-off-by: Din Music <[email protected]>
…instead of a request Signed-off-by: Din Music <[email protected]>
a237414 to
3ebb69a
Compare
Signed-off-by: Din Music <[email protected]>
Signed-off-by: Din Music <[email protected]>
3ebb69a to
2440371
Compare
tomponline
left a comment
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.
LGTM thanks!
This PR aims to reduce the usage of
http.Requestoutside the API handlers. The main requirement for this is to stop passing an actual request around, and pass only the request context instead.The second change addresses the types returned from the BL functions. Instead of returning the responses, we want to get concrete types and a potential error. This also affected forwarding response, which is forwards the request when the response is rendered. Now, we return aForwardRequiredErrorinstead, which is handled byresponse.SmartError.Only a subset of handlers are addressed, and the remaining will be done once we agree upon this approach.