在Smalltalk中重试块的优雅方式
Smalltalk通过使用on:retry方法定义重试之间的行为来支持重试异常。我想实现由两个附加操作组成的重试行为:delayInterval、maxAttempts在Smalltalk中重试块的优雅方式,smalltalk,Smalltalk,Smalltalk通过使用on:retry方法定义重试之间的行为来支持重试异常。我想实现由两个附加操作组成的重试行为:delayInterval、maxAttempts [ operations ] on: Exception maxAttempts: 10 delayInterval: 5 seconds do: [ : e | Transcript show: 'Trying...'; cr ] 有什么优雅的方法可以做到这一点吗 on: exceptionClass maxAttempts
[ operations ]
on: Exception
maxAttempts: 10
delayInterval: 5 seconds
do: [ : e | Transcript show: 'Trying...'; cr ]
有什么优雅的方法可以做到这一点吗
on: exceptionClass maxAttempts: anInteger delayInterval: seconds do: aBlock
max := anInteger.
exception := false.
[ result := self on: exceptionClass do: [ :ex | aBlock value: ex. exception := true. max := max - 1 ].
exception and: [max > 0 ] ] whileTrue: [ exception := false ].
^result
“优雅”是有争议的…卡洛斯的方法很好,但我看到它的问题是,它多次发送
\on:do:
消息。这是不必要的,因为异常
理解#重试
消息。因此,我们可以在处理块内循环,而不是将所有内容都包含在一个循环中,如下所示:
BlockClosure >> on: aClass do: aBlock maxAttempts: anInteger
| counter |
counter := anInteger.
^self
on: aClass
do: [:ex | | result |
result := aBlock value: ex.
counter := counter - 1.
counter > 0 ifTrue:[ex retry].
ex return: result]
请注意,此代码中没有“语法”循环。然而,执行流将根据ex retry
消息再次评估接收块(不到达ex return:result
行)。只有当计数器
达到0
时,处理程序才会“放弃”并返回异常块的值aBlock
同样的想法现在也可用于引入超时:
on: aClass
do: aBlock
maxAttempts: anInteger
timeout: anotherInteger
| counter timeout |
counter := anInteger.
timeout := TimeStamp now asMilliseconds + anotherInteger "dialect dependent".
^self
on: aClass
do: [:ex | | result |
result := aBlock value: ex.
counter := counter - 1.
(counter > 0 and: [TimeStamp now asMilliseconds < timeout])
ifTrue: [ex retry].
ex return: result]
on:aClass
do:一个锁
maxtures:一个整数
超时:另一个Integer
|计数器超时|
计数器:=整数。
超时:=时间戳现在为毫秒+另一个Integer“方言相关”。
^自我
关于:aClass
do:[:ex | result|
结果:=锁定值:ex。
计数器:=计数器-1。
(计数器>0和:[时间戳现在为毫秒<超时])
ifTrue:[重试后]。
交回:结果]
同样的考虑也适用于这里。唯一的区别是,
重试
条件也考虑了超时限制。您尝试过什么吗?是否希望在每次重试之前执行do:
之后的块?比如,操作,延迟,do,操作,延迟,do,…?你针对的是哪种Smalltalk方言?任何方言都可以