Experiments and implementations · Intermediate

Compare refreshed and propagated deadlines across three services

Inject delays into three local HTTP processes and compare per-hop timeout renewal with one inherited endpoint, checking budgets and effects.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Inject work delays into three local services
  2. Fix the single-host clock assumption
  3. Trace checks before further work
  4. Compare effects as well as responses
  5. The cancellation check has a specific execution boundary
  6. Solve the effect-before-check exercise
  7. Ask Logeia about this articleCheck your understanding

Inject work delays into three local services

Start HTTP services A, B and C as separate processes, calling A to B to C. Inject modeled work delays of eighty, 120 and 150 ms with an entry budget of 300 ms. Compare reset, which creates a fresh 300 ms at each stage, with propagate, which retains the endpoint. Read deadline propagation first. These waits model work; startup synchronization instead uses explicit pipe readiness notifications.

C's effect count is a local model, not a real payment. Reset can complete every individual stage within its local allowance while exceeding the entry budget overall. Propagation leaves less time for C and returns expiration with zero effects. Check shrinking budget, response and effect together rather than expecting identical decimal timings. Operating-system scheduling means the test does not assert exact 300.00 ms completion.

Fix the single-host clock assumption

Save deadline.py and run it as a file with Python 3.12 or later. All processes compare one host's time.monotonic domain. This does not establish that raw monotonic endpoints are comparable across machines. A production RPC extension must follow its documented deadline encoding and clock-offset policy. The JSON deadline here makes a local boundary explicit under a deliberately narrower assumption.

Start C, then B with C's port, then A with B's port. Each binds an available loopback port and reports it before calls begin. HTTPServer handles two trials per process, with bounded waits. The parent joins children and finally terminates survivors on failure. Preserve the main guard for spawn. Save source and environment together so the clock domain and process topology remain reviewable, rather than treating a local numeric timestamp as a general distributed protocol.

Trace checks before further work

Each server chooses either a fresh 300 ms endpoint or the inherited one, computes remaining time and waits for the smaller of modeled cost and remainder. After waiting, expiration prevents another downstream call or final effect. A and B pass the same endpoint forward in propagate mode. C produces one effect only if its modeled work finishes before that endpoint. This model deliberately checks cancellation before effect creation.

The HTTP client's five-second timeout bounds the test harness, not the business budget. Cutting only the parent wait at 300 ms could hide continued downstream work. Here the parent waits for the result so that difference remains observable. Expired is carried in a status-200 JSON response for the experiment; this is not a recommendation that all production deadline failures use HTTP 200. Separate harness controls, modeled policy and actual API semantics when adapting the example.

python
import json,multiprocessing as mp,threading,time,urllib.request
from http.server import BaseHTTPRequestHandler,HTTPServer

def service(index,next_port,ready):
    costs=[0.080,0.120,0.150]
    class Handler(BaseHTTPRequestHandler):
        def log_message(self,*args):pass
        def do_POST(self):
            request=json.loads(self.rfile.read(int(self.headers['Content-Length'])))
            start=time.monotonic();deadline=start+0.300 if request['mode']=='reset' else request['deadline']
            remaining=max(0,deadline-start)
            threading.Event().wait(min(costs[index],remaining))
            if time.monotonic()>=deadline:
                response={'expired':True,'effects':0,'at':index,'trace':[]}
            elif next_port:
                request['deadline']=deadline
                response=call(next_port,request)
            else:response={'expired':False,'effects':1,'at':index,'trace':[]}
            response['trace'].insert(0,{'service':index,'incoming_ms':round(remaining*1000,2)})
            body=json.dumps(response).encode();self.send_response(200);self.send_header('Content-Length',str(len(body)));self.end_headers();self.wfile.write(body)
    server=HTTPServer(('127.0.0.1',0),Handler);server.timeout=10;ready.send(server.server_port);ready.close()
    try:
        for _ in range(2):server.handle_request()
    finally:server.server_close()

def call(port,payload):
    data=json.dumps(payload).encode();request=urllib.request.Request(f'http://127.0.0.1:{port}',data=data,headers={'Content-Type':'application/json'})
    with urllib.request.urlopen(request,timeout=5) as response:return json.load(response)

if __name__=='__main__':
    context=mp.get_context('spawn');workers=[];ports=[];parents=[];next_port=0
    try:
        for index in [2,1,0]:
            parent,child=context.Pipe(duplex=False);worker=context.Process(target=service,args=(index,next_port,child));worker.start();child.close();workers.append(worker);parents.append(parent)
            assert parent.poll(10);next_port=parent.recv();ports.append(next_port)
        for mode in ['reset','propagate']:
            start=time.monotonic();result=call(ports[-1],{'mode':mode,'deadline':start+0.300});elapsed=time.monotonic()-start
            assert result['effects']==(1 if mode=='reset' else 0)
            assert result['expired']==(mode=='propagate')
            assert len(result['trace'])==3
            print(json.dumps({'mode':mode,'elapsed_ms':round(elapsed*1000,2),**result}))
        for worker in workers:worker.join(12);assert worker.exitcode==0
    finally:
        for worker in workers:
            if worker.is_alive():worker.terminate();worker.join()
        for parent in parents:parent.close()

Compare effects as well as responses

Assert reset returns not expired with one effect and propagate returns expired with zero effects, each with all three services in its trace. One recorded run took about 369 ms for reset and 302 ms for propagation; those observations vary by environment. Fixed expectations concern effect and expiration behavior, not exact duration. Incoming budgets in that run remain 300 per reset stage and shrink to roughly 299, 218 and 97 under propagation.

A heavily delayed A or B could expire before reaching C, causing the three-stage assertion to fail. Inspect the trace and execution conditions instead of immediately blaming business logic. This is a controlled-history example, not a guarantee under every possible scheduler delay. The printed duration is not decomposed into precise network latency. Keep the structural fact of endpoint renewal separate from the measured intervals observed during this run.

The cancellation check has a specific execution boundary

This model checks the endpoint after its modeled wait and before C's effect. It does not implement interruption of an already started external API. A version dividing work into smaller units could check between units, changing the maximum leftover computation. Record notification and actual stopping separately. A client-side timeout is only one observation; a completed cleanup path or an effect counter supplies different evidence.

The three services also perform only one modeled serial operation per trial. There is no background fan-out whose children might outlive a response. Adding parallel calls would require propagating the same deadline to each and joining or cancelling their remaining work under explicit rules. A fast parent response would not prove those children stopped. Keep the original test as a controlled endpoint-renewal comparison while creating separate assertions for any extended topology. This preserves the meaning of its effect count and prevents the five-second harness timeout from being mistaken for the application's cancellation mechanism.

An extended cancellation test should record the time and amount of work remaining after expiration. Logging a shrinking budget does not itself stop a task. Match the final effect and exit boundary to the propagated signal before claiming successful cancellation.

Solve the effect-before-check exercise

Move C's effect before its deadline check. It can then return expired even though an effect already exists. The original zero-effect result depended on the explicit pre-effect check; real databases and external APIs need their own verified boundary. Trace those lines to explain why cancellation is not equivalent to undoing a committed business action.

After eighty and 120 ms, reserving another twenty for response work leaves at most eighty for C rather than the approximately one hundred used by this simple endpoint experiment. Passing one endpoint does not automatically reserve that margin. Specify it separately. All children stop at completion and no database or persistent file is created. Extensions should retain admission, work, effect and response control points as separately labeled trials.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Understand the systems behind the web · STEP 16

Choose a learning path