Skip to content

serve

omnitax serve [FILE] [--socket PATH] [--tcp HOST:PORT] [--no-export] [--log-level LEVEL]

Serves every taxonomy of a file built by build with the batched HTTP API. Needs the server extra (see Install). Clients (omnitax.connect, the query commands) run all algorithms themselves; the server only answers lookups, so it stays small and cheap.

Option Meaning
FILE the taxonomy file [$OMNITAX_DB]. Taxonomies added or replaced in it (by build or Taxonomy.save), and a file renamed over it, are served from the next request on, without a restart; a taxonomy that is no longer in it is a 404
--socket PATH Unix socket to listen on; the default when --tcp is not given: $XDG_RUNTIME_DIR/omnitax.sock
--tcp HOST:PORT also (or, without --socket, only) listen on TCP; [::1]:9000 and :9000 work
--no-export refuse the rows endpoints that stream a whole taxonomy (which subset, prune, stats and save of a remote taxonomy use)
--log-level LEVEL critical, error, warning, info or debug [info]

The Unix socket is created with mode 0660 (the owner and their group may connect), and removed on shutdown. A socket that another server is listening on is never replaced; a stale one is. --socket and --tcp together listen on both.

omnitax serve taxonomies.sqlite
omnitax serve taxonomies.sqlite --tcp 127.0.0.1:8765

While the file is written

The file is in WAL mode, so build or Taxonomy.save may write into it while the server runs: readers go on (a request that began before the commit answers from the file as it was) and the writer is not held up by a client that reads an export slowly. A file that cannot be read at all at that moment (missing, not of this layout, locked) is a 503 with Retry-After, never a hang or a 500; /health does not read the file and always answers. Keep the file on a local disk (not NFS), and see the database for the -wal and -shm files beside it. Serving from a read-only volume works (the file is then opened immutable).

Mounting into your own app

from starlette.applications import Starlette
from starlette.routing import Mount
from omnitax.server import Catalog, taxonomy_routes

app = Starlette(routes=[Mount("/taxonomy", routes=taxonomy_routes(Catalog("taxonomies.sqlite")))])

The routes close over their catalog and share nothing with the app (no app.state, no names to agree on), so the host app is free to add its own authentication and limits. Clients then connect to https://your.host/taxonomy, or on a Unix socket omnitax.connect(socket=..., prefix="/taxonomy") (--path-prefix /taxonomy on the command line). create_app(path) builds the standalone app of this command.

Running it publicly

omnitax has no authentication and no rate limit: anyone who can reach the socket or port can read every taxonomy and use as much CPU as they like. On a Unix socket or a trusted network that is fine. What is not bounded by the server, so that a proxy (or --no-export) has to: links_under of a root (every link of the taxonomy, streamed), subtree and rank/{rank} of the big ones (every node of the taxonomy), rows/*; a search with a short text and a large within (a letter in the whole of NCBI reads the best names of that letter, in order, until it has its limit), and a fuzzy search, which weighs up to 1000 vocabulary words for each of the first 8 words of its text (200 characters) and re-ranks 600 names: the most work one request can ask of the search (0.6 s measured on NCBI for a text of 175 characters and 18 words, on one core; a short query takes 0.1 s). Per request the server bounds a body (1 MiB), a batch (1000 entries), the hits of a search (100) and its text (200 characters); it bounds neither the number of requests nor how many run at once (the threads of the server, one connection each, up to eight kept ready). For a public server, listen on 127.0.0.1 (or a Unix socket) and put a reverse proxy in front that does what a proxy does well: TLS, authentication, rate limiting. The client retries the 429 and 503 a proxy sends (--retries N on the query commands, omnitax.connect(retries=N)), after Retry-After. If the proxy wants credentials, clients send them with --header 'Authorization: Bearer ...' or omnitax.connect(headers={...}).

nginx, 10 requests a second per client address with a burst of 20:

limit_req_zone $binary_remote_addr zone=omnitax:10m rate=10r/s;

server {
    listen 443 ssl;
    server_name taxonomy.example.org;
    # ssl_certificate ...; auth_basic or auth_request here if the server should not be open

    location /omnitax/ {
        limit_req zone=omnitax burst=20 nodelay;
        limit_req_status 429;
        proxy_pass http://127.0.0.1:8765/;
    }
}

Caddy with the third-party caddy-ratelimit module (caddy add-package github.com/mholt/caddy-ratelimit):

taxonomy.example.org {
    rate_limit {
        zone per_client {
            key    {remote_host}
            events 600
            window 1m
        }
    }
    handle_path /omnitax/* {
        reverse_proxy 127.0.0.1:8765
    }
}

Clients connect to https://taxonomy.example.org/omnitax (the path prefix is kept for every request). Consider --no-export for a public server: the rows endpoints stream a whole taxonomy.