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

Refactor k6/http and k6/html to the new JS Module API #2226

Merged
merged 1 commit into from
Dec 13, 2021
Merged

Conversation

na--
Copy link
Member

@na-- na-- commented Nov 8, 2021

I thought I needed to refactor both k6/html and k6/http together, since the latter depends on the former, but turns out that wasn't quite the case and the panic was caused by something else... 😅 Still, it doesn't hurt and the k6/html changes are relatively minor...

I like how this looks like for now, and I'll probably make another PR after this to move some things out of the js.Runner.newVu() and lib.State and into the k6/http module, for example:

  • TLSConfig, Transport and CookieJar probably should go in the Client and be initialized in a constructor, which will use the global lib.Options as the default options for its initialization
  • BPool can either be in the Client or ModuleInstance, depends
  • RPSLimit should probably be in the RootModule

As a prerequisite to these though, other built-in modules (and xk6 extensions) should be able to essentially do the equivalent of require('k6/http') internally, i.e. get the ModuleInstance for their VU. And to do that without Go import loops, we probably have to move some of the logic from js/InitContext to js/common.InitEnvironment

@na-- na-- added this to the v0.36.0 milestone Nov 8, 2021
@na-- na-- requested a review from mstoykov November 8, 2021 07:35
@CLAassistant
Copy link

CLAassistant commented Nov 16, 2021

CLA assistant check
All committers have signed the CLA.

@na-- na-- changed the title [WIP] Refactor k6/http to ModuleV2 [WIP] Refactor k6/http and k6/html to the new JS Module API Nov 16, 2021
@na-- na-- changed the title [WIP] Refactor k6/http and k6/html to the new JS Module API Refactor k6/http and k6/html to the new JS Module API Nov 16, 2021
@na-- na-- marked this pull request as ready for review November 16, 2021 14:39
@na--
Copy link
Member Author

na-- commented Nov 16, 2021

I'll fix the linter errors and squash the commits later, but the code should be reasonably ready for review now.

Copy link
Collaborator

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just some small suggestions, not too enthusiastic about the ModuleInstance <-> Client cycle dependency but I think we can't do better for now.

js/modules/k6/html/html.go Show resolved Hide resolved
js/modules/k6/http/http.go Show resolved Hide resolved
oleiade
oleiade previously approved these changes Nov 23, 2021
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 A couple of nitpicks that you should feel free to ignore.

A general remark about a pattern I've observed in other places in the code: I often see private and public functions/methods/types appearing in a seamingly unordered fashion. I was always told, and exposed to the idea that especially in Go (as naming has an impact on the privateness/publicness of the code), it's important to have types/functions/methods appear in order of importance: public first, then private. Most essential/important first, less important/implementation details last. In my experience, being intentional about it makes a codebase much easier to understand, navigate and maintain.

What do you folks think?

js/modules/k6/http/request.go Outdated Show resolved Hide resolved
js/modules/k6/http/request.go Show resolved Hide resolved
mstoykov
mstoykov previously approved these changes Nov 24, 2021
Copy link
Collaborator

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general, I left some comments I do think we should discuss what we want to do with Exports more in general soon so that we can align all internal modules within this release

js/modules/k6/http/request_test.go Show resolved Hide resolved
js/modules/k6/http/request.go Outdated Show resolved Hide resolved
lib/state.go Outdated Show resolved Hide resolved
samples/http_get.js Show resolved Hide resolved
js/modules/k6/http/http.go Outdated Show resolved Hide resolved
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Default: mi.exports,
// TODO: add new HTTP APIs like Client, Request, etc. as named exports?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #2258 as it was more or less written for this module 🤣

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started by reading that issue, but I am still not sure exactly what you mean, sorry 😞 Do you mean I should just return everything as named exports here and rely on this?

k6/js/initcontext.go

Lines 180 to 182 in 5c14dae

if exp.Default == nil {
return exp.Named
}

Comment on lines +123 to +134
mustAddProp := func(name, val string) {
err := mi.exports.DefineDataProperty(
name, rt.ToValue(val), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE,
)
if err != nil {
common.Throw(rt, err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is the one use case for using goja.Object as the exports - being able to make things not act just as a simple map, as in this case not being able to overwrite this constants.

The question is should you not be able to overwrite this but be able to overwrite the functions ?
Should we return something different that is more robust in handling such cases

See #2241 (comment) (And above comments) for discussion on why I prefer map[string]interface{} for most cases

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm 🤔 I can understand the map[string]interface{} preference, but goja will just convert that to an Object anyway. And I like being able to have constants be constant 😅

Do you know hot this should work with proper ES modules? If you have export const something, does that actually make it constant in the code that imports it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have export const something, does that actually make it constant in the code that imports it?

yes

Copy link
Collaborator

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the effort in doing it, I mostly omitted to comment the parts related to the exports because I would wait for updates from @mstoykov experiments before providing any input there.

The rest is mostly asking for clarifications about TODOs.

.golangci.yml Outdated Show resolved Hide resolved
js/modules/k6/http/file.go Outdated Show resolved Hide resolved
js/modules/k6/http/http.go Outdated Show resolved Hide resolved
js/modules/k6/http/cookiejar.go Show resolved Hide resolved
js/modules/k6/http/http.go Outdated Show resolved Hide resolved
js/modules/k6/http/http.go Show resolved Hide resolved
lib/state.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from the tests changes. I would expect that there will be some trouble with the tests either way so 🤷

mstoykov
mstoykov previously approved these changes Dec 13, 2021
Copy link
Collaborator

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one missing change, the rest LGTM

js/modules/k6/http/http.go Outdated Show resolved Hide resolved
codebien
codebien previously approved these changes Dec 13, 2021
Copy link
Collaborator

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (ofc with green CI), thanks for doing it 🙏

mstoykov
mstoykov previously approved these changes Dec 13, 2021
oleiade
oleiade previously approved these changes Dec 13, 2021
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just went through the PR again. Realized I forgot to approve it in the first place. Sorry for that 🙏🏻 Great work 🤝 🎉

@na-- na-- merged commit 3bcdabb into master Dec 13, 2021
@na-- na-- deleted the http-js-refactor branch December 13, 2021 15:31
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

Successfully merging this pull request may close these issues.

None yet

5 participants