Skip to content

Convert config to a builder pattern - #11976

Merged
jgraham merged 6 commits into
masterfrom
config_builder
Jul 24, 2018
Merged

jgraham merged 6 commits into
masterfrom
config_builder

Conversation

@jgraham

@jgraham jgraham commented Jul 13, 2018 •

Copy link
Copy Markdown
Contributor

This solves several problems with the curent design, notably:

  • Config is created once and then shared, so multiple processses/threads can't race when creating the configuration data
  • It unbreaks the SSL environments that expect to be used as context managers

This change is Reviewable

Comment thread tools/wptserve/wptserve/config.py Outdated
class ConfigBuilder(object):
"""wptserve config

Inherits from Mapping for backwards compatibility with the old dict-based config"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This docstring should belong to Config. And ConfigBuilder needs a new docstring of itself.

hosts must be a list of all hosts to appear on the certificate, with
the primary hostname first."""
hosts = tuple(sorted(hosts, key=lambda x:-len(x)))
hosts = tuple(sorted(hosts, key=lambda x:len(x)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why this change? (Although I don't know why it was originally sorted inversely, either.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is expained in the commit message; we sort in order to get the top-level domain first. That wasn't what was happening ;)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ahh I didn't read the commit messages. Not sure if you plan to squash or rebase merge this, but as long as the message goes to the final history I'm happy.

**kwargs):

self.log_level = kwargs.get("log_level", "DEBUG")
self._data = self._default.copy()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: perhaps we can make self._default a frozendict for an extra layer of safety.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure it's worth implementing this or importing a third party package just for this.

Comment thread tools/serve/serve.py Outdated
class ConfigBuilder(config.ConfigBuilder):
"""serve config

this subclasses wptserve.config.Config to add serve config options"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

...subclasses wptserv.config.ConfigBuilder*...

Comment thread tools/ci/make_hosts_file.py Outdated
ssl={"type":"none"})

print(make_hosts_file(config, kwargs["address"]))
with config_builder.get() as config:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I might have missed something but where is ConfigBuilder.get defined?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's just a bug; the initial iteration had a .get method. I'm surprised the tests didn't catch this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry please disregard. I wrote too much code in blinkpy (which uses frozendict a lot) to the point I somehow thought PEP 416 was implemented...

@dheiberg dheiberg left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewable status: 0 of 22 files reviewed, 8 unresolved discussions (waiting on @Hexcles, @jgraham, @dhdavvie, and @gsnedders)


tools/wptrunner/wptrunner/environment.py, line 107 at r1 (raw file):

        self.cache_manager.__exit__(exc_type, exc_val, exc_tb)
        self.stash.__exit__()
        self.config_ctx.__exit__(exc_type, exc_val, exc_tb)

Is there any reason for this now being after self.stash.__exit__()? Or is the order arbitrary?


tools/wptrunner/wptrunner/wptrunner.py, line 39 at r1 (raw file):

"""

print sslutils.__file__

stray print statement by the looks of it

@jgraham
jgraham force-pushed the config_builder branch 3 times, most recently from 61f224e to e9d132b Compare July 17, 2018 16:45
Comment thread tools/serve/serve.py
def ws_doc_root(self):
if self._ws_doc_root is not None:
return self._ws_doc_root
def _get_ws_doc_root(self, data):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm confused by this; we appear to have a private getter never called from anywhere?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(Oh, it comes out of self.computed_properties. I'd much rather this more obvious rather than it appearing like this never gets called ever, especially when the call site is far away.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OK, so what do you suggest? The requirements are

  • Properties must be computed in a defined order, since later properties often depend on earlier ones.
  • Subclasses must be able to add to the list of properties in a relatively straightforward manner.

We could just have a list of methods to call on the class, but that's a bit ugly because they have to be defined at the bottom of the class definition whereas it's more usual to define data properties at the top.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A comment pointing at how it gets called and turned into an actual property would probably be useful?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added something to the docstring for the class describing the protocol.

The names are expected to be sorted shortest first, but had the order reversed.

@Hexcles Hexcles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 17 of 22 files at r1, 9 of 9 files at r2, 1 of 1 files at r3.
Reviewable status: 25 of 26 files reviewed, 3 unresolved discussions (waiting on @Hexcles and @jgraham)


tools/wpt/run.py, line 100 at r2 (raw file):

        with config_builder as config:
            expected_hosts = set(config.domains_set)
            is_windows = platform.uname()[0] == "Windows"

Could you move this statement back (out of the with block)?


tools/wptrunner/wptrunner/wptrunner.py, line 39 at r1 (raw file):

Previously, dhdavvie (David H) wrote…

stray print statement by the looks of it

ping @jgraham

jgraham added 5 commits July 18, 2018 18:41
We have two sets of problems with the config:

* It has to go to some lengths to be thread/process safe since several
  attributes are lazilly computed, and cached, by whichever thread
  happens to be first to access them.

* The OpenSSL-based certificate implementation is totally broken
  because this class needs to be used as a context manager in order to
  initialise some resources associated with the fake CA used to
  generate the certificates.

This suggests that the design is not really correct. We really want to
generate the configuration once in a single process, and then share the
generated configuration across multiple processes. We also want to
ensure that the ssl environment is initialised once in the main
process using a context manager.

This patch introduces a ConfigBuilder object which allows
configuration properties to be set. When used as a context manager, it
computes the generated configuration, initalises the SSL environment,
and updates the configuration with that data, and returns a config
object with the final, immutable, configuration which may then be
shared across processes.
This is fully obtainable from the config object.

@Hexcles Hexcles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewable status: 23 of 26 files reviewed, 2 unresolved discussions (waiting on @Hexcles, @jgraham, and @dhdavvie)

@jgraham
jgraham merged commit 8654332 into master Jul 24, 2018
Hexcles added a commit that referenced this pull request Jul 24, 2018
The suite was broken by PR #11976 where the config class was changed.
The suite uses a hack to get the default config of wptserve. This commit
fixes the hack, but unfortunately does not find a less hacky way to get
the default config.
foolip pushed a commit that referenced this pull request Jul 25, 2018
The suite was broken by PR #11976 where the config class was changed.
The suite uses a hack to get the default config of wptserve. This commit
fixes the hack, but unfortunately does not find a less hacky way to get
the default config.
@sideshowbarker
sideshowbarker deleted the config_builder branch November 22, 2018 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants