|
553 | 553 | "from pypdf import PdfReader, PdfWriter\n", |
554 | 554 | "from unstructured.partition.auto import partition\n", |
555 | 555 | "from unstructured.staging.base import convert_to_isd, convert_to_dataframe, elements_from_json\n", |
| 556 | + "import psutil\n", |
556 | 557 | "import requests\n", |
557 | 558 | "import time" |
558 | 559 | ] |
|
767 | 768 | " \"m_xml_keep_tags\": m_xml_keep_tags,\n", |
768 | 769 | " }, default=str)\n", |
769 | 770 | " ))\n", |
| 771 | + "\n", |
| 772 | + " # If this var is set, reject traffic when free memory is below minimum\n", |
| 773 | + " # Allow internal requests - these are parallel calls already in progress\n", |
| 774 | + " mem = psutil.virtual_memory()\n", |
| 775 | + " memory_free_minimum = int(os.environ.get(\"UNSTRUCTURED_MEMORY_FREE_MINIMUM_MB\", 0))\n", |
| 776 | + "\n", |
| 777 | + " if memory_free_minimum > 0 and mem.available <= memory_free_minimum * 1024 * 1024:\n", |
| 778 | + " # Note(yuming): Use X-Forwarded-For header to find the orginal IP for external API\n", |
| 779 | + " # requests,since LB forwards requests in AWS\n", |
| 780 | + " origin_ip = request.headers.get(\"X-Forwarded-For\") or request.client.host\n", |
| 781 | + "\n", |
| 782 | + " if not origin_ip.startswith(\"10.\"):\n", |
| 783 | + " raise HTTPException(\n", |
| 784 | + " status_code=503, detail=\"Server is under heavy load. Please try again later.\"\n", |
| 785 | + " )\n", |
| 786 | + "\n", |
770 | 787 | " if filename.endswith(\".msg\"):\n", |
771 | 788 | " # Note(yuming): convert file type for msg files\n", |
772 | 789 | " # since fast api might sent the wrong one.\n", |
|
0 commit comments