当前位置: 首页 > news >正文

python: Fail-Fast Pattern

项目结构:

# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:03 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : fail_fast.py class ResourceCheckFailedError(Exception): """ 快速失败:资源不可用" """ pass # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:04 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : inventory_repo.py class InventoryRepository: """ """ def __init__(self): self.inventory = { "DIA001": 5, "DIA002": 0, "GOLD001": 20 } def get_stock(self, product_id: str)->int|None: """ :param product_id: :return: """ return self.inventory.get(product_id, -1) def deduct_stock(self, product_id: str, quantity: int)->int|None: """ :param product_id: :param quantity: :return: """ self.inventory[product_id] -= quantity # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:05 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : external_services_repo.py class ExternalServices: """ """ def __init__(self): self.inventory_service = True self.payment_gateway = True self.quality_inspection = True self.invoice_service = True # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:05 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : validation_service.py from FailFastPattern.exceptions.fail_fast import ResourceCheckFailedError from FailFastPattern.repository.external_services_repo import ExternalServices from FailFastPattern.repository.inventory_repo import InventoryRepository class ValidationService: """ """ def __init__( self, inventory_repo: InventoryRepository, ext_services: ExternalServices ): self.inventory_repo = inventory_repo self.ext_services = ext_services def fail_fast_check(self, customer_id: str, product_id: str, quantity: int): """ Fail-Fast 核心:全部资源一次性检查,不满足立即失败 :param customer_id: :param product_id: :param quantity: :return: """ # 1. 顾客检查 if not customer_id or len(customer_id) < 3: raise ResourceCheckFailedError("顾客信息无效") # 2. 商品存在检查 stock = self.inventory_repo.get_stock(product_id) if stock < 0: raise ResourceCheckFailedError(f"商品 {product_id} 不存在") # 3. 库存足够检查 if stock < quantity: raise ResourceCheckFailedError(f"商品 {product_id} 库存不足") # 4. 外部服务检查 if not self.ext_services.inventory_service: raise ResourceCheckFailedError("库存服务不可用") if not self.ext_services.payment_gateway: raise ResourceCheckFailedError("支付网关不可用") if not self.ext_services.quality_inspection: raise ResourceCheckFailedError("质检系统不可用") if not self.ext_services.invoice_service: raise ResourceCheckFailedError("发票系统不可用") # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:06 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : order_service.py from FailFastPattern.exceptions.fail_fast import ResourceCheckFailedError from FailFastPattern.repository.external_services_repo import ExternalServices from FailFastPattern.repository.inventory_repo import InventoryRepository from FailFastPattern.service.validation_service import ValidationService from ParallelismPattern.models.entities import Order class OrderService: """ """ def __init__( self, validation_service: ValidationService, inventory_repo: InventoryRepository, ext_services: ExternalServices ): self.validation = validation_service self.inventory_repo = inventory_repo self.ext_services = ext_services def create_order(self, customer_id: str, product_id: str, quantity: int)->dict: """ :param customer_id: :param product_id: :param quantity: :return: """ try: # ========== 第一步:快速失败检查 ========== self.validation.fail_fast_check(customer_id, product_id, quantity) # ========== 检查通过:执行业务 ========== self.inventory_repo.deduct_stock(product_id, quantity) return { "order_id": "ORD-JWL-9999", "product_id": product_id, "quantity": quantity, "remaining_stock": self.inventory_repo.get_stock(product_id), "status": "success" } except ResourceCheckFailedError as e: raise e # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:06 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : order_controller.py from FailFastPattern.service.order_service import OrderService class OrderController: """ """ def __init__(self, order_service: OrderService): self.order_service = order_service def create_jewelry_order(self, customer_id: str, product_id: str, quantity: int)->dict: """ :param customer_id: :param product_id: :param quantity: :return: """ try: result = self.order_service.create_order(customer_id, product_id, quantity) return {"success": True, "data": result} except Exception as e: return {"success": False, "error": f"快速失败: {str(e)}"}

调用:

# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述:Fail-Fast Pattern 快速失败模式 # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/6/30 7:15 # User : geovindu # Product : PyCharm # Project : pydesginpattern # File : FailFastBll.py from FailFastPattern.api.order_controller import OrderController from FailFastPattern.repository.external_services_repo import ExternalServices from FailFastPattern.repository.inventory_repo import InventoryRepository from FailFastPattern.service.order_service import OrderService from FailFastPattern.service.validation_service import ValidationService class FailFastBll(object): """ """ def demo(self): """ :return: """ # 初始化依赖 inventory_repo = InventoryRepository() ext_services = ExternalServices() validation_service = ValidationService(inventory_repo, ext_services) order_service = OrderService(validation_service, inventory_repo, ext_services) controller = OrderController(order_service) print("==== 场景1:正常下单 ===") print(controller.create_jewelry_order("C1001", "DIA001", 1)) print("\n==== 场景2:库存不足 ===") print(controller.create_jewelry_order("C1001", "DIA002", 1)) print("\n==== 场景3:顾客无效 ===") print(controller.create_jewelry_order("C", "DIA001", 1)) print("\n==== 场景4:支付服务挂了 ===") ext_services.payment_gateway = False print(controller.create_jewelry_order("C1001", "DIA001", 1))

输出:

http://www.jsqmd.com/news/1099752/

相关文章:

  • AI优化mRNA翻译效率:从密码子优化到深度学习驱动的序列设计
  • AI工具集
  • 【计算机毕业设计案例】基于 SpringBoot 的高校外卖配送调度监控系统的设计与实现 基于 SpringBoot 的校园餐饮消费配送管理系统(程序+文档+讲解+定制)
  • JAVA注解(简单版)
  • 2026-06-30 后端启动异常排查记录
  • Java毕设项目:基于 SpringBoot 的电竞周边用品交易管理系统的设计与实现 基于 SpringBoot 的小众游戏周边购物服务系统 (源码+文档,讲解、调试运行,定制等)
  • 基于FFmpeg的直播视频录制工具StreamCap
  • 【毕业设计】基于 SpringBoot 的高校学生心理预警干预系统的设计与实现 基于 SpringBoot 的大学生心理状态跟踪管理系统(源码+文档+远程调试,全bao定制等)
  • 企业做GEO内容发布,哪些做法容易出风险?
  • CPT Markets:把多语言支持做扎实,注重效率的使用者更容易感受到的框架
  • Vol.57|接新IM渠道还要改代码?现在填几个字段就行
  • 无人机视角航拍违建违章建筑识别数据集labelme格式245张2类别
  • CAD 图纸批量处理:用 OpenClaw 实现图纸格式转换、批量打印、版本号自动标注
  • Spring Cloud分布式事务快速上手(基于Seata AT模式,集成Nacos)--学习版
  • Manim 节奏控制指南 (Rate Functions)
  • Java计算机毕设之基于 SpringBoot 的学生心理咨询预约管理系统的设计与实现 基于 SpringBoot 的高校心理健康信息管理系统(完整前后端代码+说明文档+LW,调试定制等)
  • 按照这个方法真的领到了8元,千问新用户专属220372
  • YOLO-World实战:零样本目标检测,一句话实现开放词汇检测
  • AI 建议用 Redis `SETNX` 防重复提交,为什么锁过期后仍可能创建两条记录
  • CPT Markets:把外汇投教内容建设做到位——标准观察与提示整理
  • 6G网络中大模型技术与多模态感知通信的融合应用
  • Java毕业设计-基于 SpringBoot 的农机 4S 店综合管理系统的设计与实现 基于 SpringBoot 的农作物机械管理系统的设计与实源码+LW+部署文档+全bao+远程调试+代码讲解等)
  • 数值优化方法:信任域与无导数技术详解
  • 注解的基本语法
  • FreeRTOS学习笔记(二)
  • [对比学习LangChain和MAF-16]基于Checkpoint的持久化
  • C中单向链表之增删改查
  • 导入Seata-server所需SQL
  • 四川大学《微积分I-1》期末试卷及答案2016-2025学年PDF
  • OpenHarness源码研究-5-基础设施