Nameserver Configuration

Nameserver Configuration#

A nameserver configuration is necessary when:

  • The machine is hosting a nameserver.

  • The machine is hosting services that will be registered with a nameserver.

In either case, it uses the information stored in a NameServerConfiguration object to setup or locate nameservers.

Let’s take a look at the nameserver configuration class.


class NameServerConfiguration(_env_file: DotenvType | None = '<object object>', _env_file_encoding: str | None = None, _env_nested_delimiter: str | None = None, _secrets_dir: StrPath | None = None, *, host: str = 'localhost', ns_port: int = 9090, broadcast: bool = False, ns_bchost: bool | None = None, ns_bcport: int = 9091, ns_autoclean: float = 0.0, storage: str = 'memory')[source]

Bases: BaseSettings, PyroConfigMixin, YAMLMixin

The NameServer Settings class.

Contains all applicable configuration parameters for running a nameserver.

Parameters:
hoststr, optional

The hostname of the nameserver. Defaults to “localhost” for security. Can be set to “public”, which is dynamically translated to the machine’s ip address when the nameserver is started.

ns_portint, optional

The port of the nameserver. Defaults to 9090.

broadcastbool, optional

Whether to launch a broadcast server. Defaults to False.

ns_bchoststr, optional

The hostname of the broadcast server. Defaults to None.

ns_bcportint, optional

The port of the broadcast server. Defaults to 9091.

ns_autocleanfloat, optional

The interval in seconds at which the nameserver will ping registered objects and clean up unresponsive ones. Default is 0.0 (off).

storagestr, optional

A Pyro5-style storage string. You have several options:

  • memory: Fast, volatile in-memory database. This is the default.

  • dbm[:dbfile]: Persistent database using dbm. Optionally provide the filename to use (ignore for PyroLab to create automatically). This storage type does not support metadata.

  • sql[:dbfile]: Persistent database using sqlite. Optionally provide the filename to use (ignore for PyroLab to create automatically).

Examples

The following are examples of valid YAML configurations for nameservers. Keys not defined assume the default values.

Example 1. Basic configuration.

host: localhost
ns_port: 9090
ns_autoclean: 0.0
storage: memory

Example 2. Nameserver publicly accessible.

host: public
ns_port: 9100
broadcast: false
ns_bchost: null
ns_bcport: 9091
ns_autoclean: 15.0
storage: sql
__init__(_env_file: str | PathLike | List[str | PathLike] | Tuple[str | PathLike, ...] | None = '<object object>', _env_file_encoding: str | None = None, _env_nested_delimiter: str | None = None, _secrets_dir: str | PathLike | None = None, **values: Any) None

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.


Again, notice that all the configuration values have “sensible” defaults. PyroLab by default keeps everything on localhost for security’s sake.

As with the server configuration, in the YAML representation of the nameserver’s configuration, only the keys that are different from the defaults need to be define. Undefined fields assume their default values.

We can work with NameServerConfiguration objects either through a text YAML file, or by using the actual Python object:

Listing 5 The default nameserver configuration, interacting directly with the Python object.#
>>> from pyrolab.configure import NameServerConfiguration
>>> config = NameServerConfiguration()
>>> config.host, config.ns_port
('localhost', 9090)
Listing 6 Nameserver configuration, interacting with the Python object, with values modified from the default.#
>>> from pyrolab.configure import NameServerConfiguration
>>> config = NameServerConfiguration(host="public", ns_port=2022, ns_autoclean=30.0, storage="sql")
>>> config.host, config.ns_port
('public', 2022)

These objects have a yaml() method that can be used to dump the configuration to a valid YAML file. It accepts a parameter that allows you to dump only the values that differ from the default:

Listing 7 The default nameserver configuration, interacting with the YAML representation.#
>>> from pyrolab.configure import NameServerConfiguration
>>> config = NameServerConfiguration()
>>> print(config.yaml())
host: localhost
ns_port: 9090
broadcast: false
ns_bchost: null
ns_bcport: 9091
ns_autoclean: 0.0
storage: memory
Listing 8 Modified nameserver configuration, interacting with the YAML representation, with defaults excluded.#
>>> from pyrolab.configure import NameServerConfiguration
>>> config = NameServerConfiguration(host="public", ns_port=2022, ns_autoclean=30.0, storage="sql")
>>> print(config.yaml(exclude_defaults=True))
host: public
ns_port: 2022
ns_autoclean: 30.0
storage: sql

You can launch a simple nameserver by simply passing one of these objects to pyrolab.api.start_ns_loop().

Listing 9 Starting a nameserver, continuing using the config from above.#
>>> from pyrolab.nameserver import start_ns_loop
>>> start_ns_loop(config)

So a complete script for starting up a nameserver that is visible to other machines on the network would look like this:

Listing 10 Simplest, complete script for running a nameserver visible to other machines on the network.#
>>> from pyrolab.configure import NameServerConfiguration
>>> from pyrolab.nameserver import start_ns_loop
>>> config = NameServerConfiguration(host="public")
>>> start_ns_loop(config)