Package: tino
TinoThe fastest, cutest, little API you ever did seeWhat is Tino?Tino is a framework to remotely call functions. It builds both clients and servers.Tradidtional APIs are JSON over HTTP. Tino is Msgpack over the Redis Serialization Protocol. This makes it go faster, use less bandwidth, and its binary format easier to understand.Does Tino use Redis?No. Tino uses RESP, the Redis Serialization Protocol, to encode messages over TCP. Tino only needs Redis if the service you build adds it.Generalizing the Redis ProtocolTino was born after a long search to find an alternative to HTTP. The protocol is great for the web, but for backend server-to-server services, it is a bit overkill. HTTP2 tried to fix some of the problems, but ended up as is a mess of statemachines, multiplexing, and header compression algorithms that proper clients took years to come out and some languages still haven't implemented it fully.RESP, the Redis Serialization Protocol, is a Request-Response model with a very simple human readable syntax. It is also very fast to parse which makes it a great candidate for use in an API. After aweekend of hacking, a proof of concept was born and Tino quickly followed.MessagePack for SerializationIt is fast, can enable zero-copy string and bytes decoding, and the most important, it isonly an afternoon of hackingto get a serializer and parser going.MessagePack paired with RESP means that you can implement the entire stack, protocol and serialization, by yourself from scratch if you needed to without too much trouble. And it will be fast.The BasicsTino follows closely the design ofFastAPI. Type annotations are required for both arguments and return values so that values can automatically be parsed and serialized. In Redis all commands are arrays. The position of your argument in the signature of the function matches the position of the string array of the redis command. Tino commands can not contain keyword arguments.# server.pyfromtinoimportTinoapp=Tino()@app.commanddefadd(a:int,b:int)->int:returna+bif__name__=="__main__":app.run()Now you can run commands against the server using any Redis api in any language as long as the client supports custom Redis commands (most do).Or you can use Tino's builtin high-performance client:# client.pyimportasynciofromserverimportapp# import the app from aboveasyncdefgo():client=app.client()awaitclient.connect()three=awaitclient.add(1,2)client.close()awaitclient.wait_closed()if__name__=="__main__":asyncio.run(go())AuthorizationTino has authorization by addingAuthRequiredto the type signature of the methods you want to protect and supplying theTinoobject with anauth_func. Theauth_functakes abytesobject and returnsNoneif the connection failed authorization or any other value symbolizing the authorization state if they succeeded.fromtinoimportTinoKEYS={b'tinaspassword':'tina'}defauth_func(password:bytes):returnKEYS.get(password,None)app=Tino(auth_func=auth_func)@app.commanddefadd(a:int,b:int,auth:AuthRequired)->int:print(auth.value)returna+bAnd pass the password to theclient.connect.asyncdefdo():client=app.client()awaitclient.connect(password="tinaspassword")Other Magic ArgumentsBesidesAuthRequiredyou can also addAuth(whereauth.valuecan be None) andConnStateto get the state if you also supply astate_factory. This state is mutatable and is private to the connection.fromtinoimportTinoasyncdefstate_factory():return0app=Tino(state_factory=state_factory)@app.commanddefadd(a:int,b:int,auth:Auth,conn:ConnState)->int:# Count the number of unauthorized calls on this connection.ifauth.valueisNone:conn.value+=1returna+bIs Tino Secure?Probably the biggest vulnerability is a DDOS attack. More testing needs to be done to see how Tino behaves under large message sizes. Currently placing views behindAuthRequireddoes not protect against this because the entire message is parsed. So for the time being, Tino should only be considered for private connections. This can be improved however, by parsing the command first, doing the permission check then reading and parsing the body.What about Databases?For SQL I recommend using thedatabasesproject with SQLAlchemy to get true asyncio support. This example is borrowed fromfastapifromtinoimportTinoimportdatabasesimportsqlalchemyfromtypingimportList# SQLAlchemy specific code, as with any other appDATABASE_URL="sqlite:///./test.db"# DATABASE_URL = "postgresql://user:password@postgresserver/db"database=databases.Database(DATABASE_URL)metadata=sqlalchemy.MetaData()notes=sqlalchemy.Table("notes",metadata,sqlalchemy.Column("id",sqlalchemy.Integer,primary_key=True),sqlalchemy.Column("text",sqlalchemy.String),sqlalchemy.Column("completed",sqlalchemy.Boolean),)engine=sqlalchemy.create_engine(DATABASE_URL,connect_args={"check_same_thread":False})metadata.create_all(engine)classNoteIn(BaseModel):text:strcompleted:boolclassNote(BaseModel):id:inttext:strcompleted:boolapp=Tino()@app.on_startupasyncdefstartup():awaitdatabase.connect()@app.on_shutdownasyncdefshutdown():awaitdatabase.disconnect()@app.commandasyncdefread_notes()->List[Note]:query=notes.select()rows=awaitdatabase.fetch_all(query)return[Note(**n)forninrows]@app.commandasyncdefcreate_note(note:NoteIn)->Note:query=notes.insert().values(text=note.text,completed=note.completed)last_record_id=awaitdatabase.execute(query)returnNote(id=last_record_id,**note.dict())if__name__=="__main__":app.run()Should I use Tino in Production?Its not ready for public consumption at the moment, but if you want my help to run it, just drop me a line.TLS SupportIts probably easiest to deploy Tino behind a TCP loadbalancer that already supports TLS. You can pass in theSSLContextto theclient.connectfunction as kwargs to the Redis connection pool.Coming SoonIterators