限流器工作流
patterns
此工作流模拟防止过多请求被处理。
完整 FlowZap 代码
Client { # Client
n1: circle label:"Start"
n2: rectangle label:"Send API request"
n3: rectangle label:"Receive response"
n4: circle label:"End"
n1.handle(right) -> n2.handle(left)
n2.handle(bottom) -> RateLimiter.n5.handle(top) [label="Request"]
n3.handle(right) -> n4.handle(left)
}
RateLimiter { # Rate Limiter
n5: rectangle label:"Extract client identifier"
n6: rectangle label:"Get current window count"
n7: diamond label:"Within rate limit?"
n8: rectangle label:"Increment request count"
n9: rectangle label:"Return 429 Too Many Requests"
n10: diamond label:"Algorithm type?"
n11: rectangle label:"Token bucket check"
n12: rectangle label:"Sliding window check"
n5.handle(right) -> n6.handle(left)
n6.handle(right) -> n10.handle(left)
n10.handle(right) -> n11.handle(left) [label="Token bucket"]
n10.handle(bottom) -> n12.handle(top) [label="Sliding window"]
n11.handle(right) -> n7.handle(left)
n12.handle(right) -> n7.handle(top)
n7.handle(right) -> n8.handle(left) [label="Yes"]
n7.handle(bottom) -> n9.handle(top) [label="No"]
n8.handle(bottom) -> Backend.n13.handle(top) [label="Allow"]
n9.handle(top) -> Client.n3.handle(bottom) [label="Rejected"]
}
Backend { # Backend Service
n13: rectangle label:"Process request"
n14: rectangle label:"Execute business logic"
n15: diamond label:"Request successful?"
n16: rectangle label:"Return 200 OK"
n17: rectangle label:"Return error response"
n18: rectangle label:"Add rate limit headers"
n13.handle(right) -> n14.handle(left)
n14.handle(right) -> n15.handle(left)
n15.handle(right) -> n16.handle(left) [label="Yes"]
n15.handle(bottom) -> n17.handle(top) [label="No"]
n16.handle(right) -> n18.handle(left)
n17.handle(right) -> n18.handle(top)
n18.handle(top) -> Client.n3.handle(bottom) [label="Response"]
}