sanic icon indicating copy to clipboard operation
sanic copied to clipboard

Remove usage of ctx in routing

Open ahopkins opened this issue 2 years ago • 2 comments

TLDR

Remove all Sanic reliance upon ctx values in the routing module.

Rationale

There are several locations throughout Sanic where the developer is provided with a ctx object to inject arbitrary values. The two most visible locations are Sanic.ctx and Request.ctx. However, this also exists for the router, routes, blueprints, and connections.

Only in routing does Sanic actually use the object for storing internal state. It will be much more consistent if Sanic never touches these objects, and therefore they can serve their original purpose: injection of arbitrary values without the worry of conflict.


Implementation

The reason that Sanic has used these values is so that sanic-routing can exist independently of sanic. So, from the perspective of sanic-routing, these objects are fulfilling their purpose and not being overburdened with values. Ultimately, however, this is somewhat moot as far as the developer is concerned as they need to be careful not to overwrite these values.

Therefore, the simplest approach is probably to add a state object into sanic-routing that looks and functions the same was as ctx, but is specifically intended for arbitrary "stateful" information coming from Sanic.

See #2300 and #2302

ahopkins avatar Nov 07 '21 12:11 ahopkins

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

stale[bot] avatar Mar 02 '22 08:03 stale[bot]

...

ahopkins avatar Mar 02 '22 09:03 ahopkins

route.extra was added https://github.com/sanic-org/sanic-routing/pull/65

ahopkins avatar Sep 28 '22 20:09 ahopkins