Install a Library or Service

Easily install TinyChain services on your host or cluster without restarting

Arguably TinyChain's most powerful feature is the ability to install a Library or Service at runtime without restarting. For example, TinyChain's linear algebra and machine learning services are distributed as part of the Python client (math.linalg.LinearAlgebra, ml.NeuralNets, and ml.Optimizers). Installing them is as easy as:

import rjwt  # this library can be used to construct and sign auth tokens
import tinychain as tc

# the lead replica (can be a load balancer)
LEAD = tc.URI("http://your.tinychain.cluster")

# this is just for illustration--never save a private key in source code!
actor = rjwt.Actor(private_key=b"cluster's private key")

host = tc.Host(LEAD)
host.install(actor, tc.math.linalg.LinearAlgebra())
assert host.get("/lib/math/linalg", '/')

Install your Service

Of course the built-in Services aren't much use without being able to install your own!

# ...

class MyService(tc.service.Service):
    NS = "my_namespace"
    NAME = "my_service"
    VERSION = tc.Version("0.0.0")

    __uri__ = tc.service.service_uri(LEAD, NS, NAME, VERSION)

    @tc.get
    def hello(self, name: tc.String) -> tc.String:
        return tc.String("Hello, {{name}}!").render(name=name)


assert tc.URI(MyService).path() == "/service/my_namespace/my_service/0.0.0"

host.install(actor, MyService())

assert host.get(tc.URI(MyService).path() + "/hello", "World") == "Hello, World!"

Last updated