Skip to content

refactor: extract duplicated code patterns into shared utilities - #2

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1783577474-dedup-shared-utils
Open

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1783577474-dedup-shared-utils

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

main.py had 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) — canonical 8-4-4-4-12 formatting, previously duplicated as an inline f-string in both generate_uuid() and format_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× in ws_to_tcp, tcp_to_ws, websocket_tunnel, and http_proxy, each a slightly different subset. Flags capture the differences:
    await record_traffic(size, conn_id=conn_id, link_uid=uuid)                    # ws upstream / first chunk
    await record_traffic(size, conn_id=conn_id, link_uid=uuid, count_request=False)  # tcp downstream
    await record_traffic(len(resp.content))                                      # http proxy (no conn/link)
  • record_error(exc, **extra) — replaces the stats["total_errors"] += 1; error_logs.append({"error": ..., "time": ...}) pair duplicated in websocket_tunnel and http_proxy; **extra carries the proxy's url=.
  • enforce_quota(websocket, link_uid, size) — the "check quota, else ws.close(1008) and break" block duplicated in both relay directions, now if not await enforce_quota(...): break.
  • limit_bytes_from_body(body) — the limit_value/limit_unit → bytes parsing duplicated in create_link and toggle_link.
  • new_link_record(label, limit_bytes) / serialize_link(uid, data, host) — the link dict literal (built in ensure_default_link + create_link) and the outward JSON shape (built in create_link + list_links) are now single-sourced.
  • filter_headers(headers, *, drop_host=False) — the hop-by-hop header-stripping comprehension used twice in http_proxy.

No new dependencies; no endpoint signatures or response shapes changed.

Link to Devin session: https://app.devin.ai/sessions/70a0c90760234f1280795234854adb87
Requested by: @hmilan1366

@hmilan1366 hmilan1366 self-assigned this Jul 9, 2026
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants