How do I set the Netstring MAX_LENGTH in txjsonrpc.netstring.jsonrpc.QueryProtocol?

Asked by Duncan McGreggor

Support for easily overriding the MAX_LENGTH attribute on the Twisted NetstringReceiver class was added in txJSON-RPC 0.3 (see bug #454335; http://bugs.launchpad.net/landscape/+bug/454335). Until that, it was only possible with some monkeypatching.

Revision history for this message
Duncan McGreggor (oubiwann) said :
#1

This is a bit of a tricky problem, since MAX_LENGTH is a class attribute on the
NetstringReceiver class. If you set it to one thing on one method call, it's
going to be set for all instances that are created after that (in the same
process). As a result, simply passing an override value will end up creating
more problems than it solves.

The way around this takes two steps:

1. Users/developers create a subclass of the QueryFactory, overriding any class
   attributes or methods they want to, and then

2. Pass the subclass when creating a Proxy or when calling Proxy.callRemote.

Here are some example usages:

1. On a per-call basis:

class MyFactory(QueryFactory):
    MAX_LENGTH = 1234

proxyInstance.remoteCall(method name, arg1, arg2..., factoryClass=MyFactory)

2. On a per-instance basis:

class MyFactory(QueryFactory):
    MAX_LENGTH = 1234

proxyInstance = Proxy(host, port, factoryClass=MyFactory)

This will set the default maxLength for all calls to 1000. Note that an
override at the method-level supersedes an override at the instance-level.
FAQ #770: “How do I set the Netstring MAX_LENGTH in txjsonrpc.netstring.jsonrpc.QueryProtocol?”.