refactor: extract duplicated code patterns into shared utilities - #2
Open
devin-ai-integration[bot] wants to merge 1 commit into
Open
devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
main.pyhad several code patterns copy-pasted across the WebSocket relay, HTTP proxy, and link-management endpoints. This refactor extracts them into small shared helpers, leaving behavior unchanged (verified with a functional smoke test over login / link CRUD / stats / health). Net effect: the same logic now lives in one place, so accounting/serialization changes only need to be made once.Helpers introduced (all in the Helpers section):
format_uuid_hex(h)— canonical8-4-4-4-12formatting, previously duplicated as an inline f-string in bothgenerate_uuid()andformat_link_uuid().record_traffic(size, *, conn_id=None, link_uid=None, count_request=True)— the 4–5 line byte-accounting block (total_bytes/total_requests/connections[id]["bytes"]/hourly_traffic/add_usage) was repeated 4× inws_to_tcp,tcp_to_ws,websocket_tunnel, andhttp_proxy, each a slightly different subset. Flags capture the differences:record_error(exc, **extra)— replaces thestats["total_errors"] += 1; error_logs.append({"error": ..., "time": ...})pair duplicated inwebsocket_tunnelandhttp_proxy;**extracarries the proxy'surl=.enforce_quota(websocket, link_uid, size)— the "check quota, elsews.close(1008)and break" block duplicated in both relay directions, nowif not await enforce_quota(...): break.limit_bytes_from_body(body)— thelimit_value/limit_unit→ bytes parsing duplicated increate_linkandtoggle_link.new_link_record(label, limit_bytes)/serialize_link(uid, data, host)— the link dict literal (built inensure_default_link+create_link) and the outward JSON shape (built increate_link+list_links) are now single-sourced.filter_headers(headers, *, drop_host=False)— the hop-by-hop header-stripping comprehension used twice inhttp_proxy.No new dependencies; no endpoint signatures or response shapes changed.
Link to Devin session: https://app.devin.ai/sessions/70a0c90760234f1280795234854adb87
Requested by: @hmilan1366