Access rate limit exceeded. Please log in to continue.
1231
import std.time.*
import soulsoft_strategy.interfaces.*
main() {
// 构建管线
let pipeline = StrategyPipelineBuilder<Unit>()
.addStopWatchStrategy() // 对所有策略计时
.addRetryStrategy(10)
.build()
// 策略上下文
let context = StrategyContext()
// 使用给定的上下文
pipeline.execute(context) { context =>
let timstamp = DateTime.now().toUnixTimeStamp().toMicroseconds()
let flag = timstamp % 2 == 0
println("working:${timstamp},${flag}...")
if (!flag) {
throw UnsupportedException()
}
}
// 打印耗时
println("耗时:${context.get<Duration>('StopWatchStrategy:result')}")
return 0
}
// 定义策略
public class StopWatchStrategyExecutor<TResult> <: StrategyExecutor<TResult> {
public func execute(context: IStrategyContext, callback: (IStrategyContext) -> TResult): TResult {
let start = DateTime.now()
let result = callback(context)
let span = DateTime.now() - start
context.set("StopWatchStrategy:result", span)
return result
}
}
// 扩展到管道
extend<TResult> StrategyPipelineBuilder<TResult> {
public func addStopWatchStrategy() {
let executor = StopWatchStrategyExecutor<TResult>()
this.addStrategy(executor)
return this
}
}