Skip to content

Commit

Permalink
Add Guard and rename variable
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Sep 1, 2016
1 parent f475436 commit 62f3ea0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
34 changes: 34 additions & 0 deletions wvlet-log/src/main/scala/wvlet/log/Guard.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.log

import java.util.concurrent.locks.ReentrantLock

/**
*
*/
trait Guard {
private[this] val lock = new ReentrantLock()
protected def newCondition = lock.newCondition()

def guard[U](body: => U) : U = {
lock.lockInterruptibly()
try {
body
}
finally {
lock.unlock()
}
}
}
11 changes: 5 additions & 6 deletions wvlet-log/src/main/scala/wvlet/log/LogLevelScanner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,23 @@ private[log] class LogLevelScanner extends Guard {
state.set(STOPPING)
}

private var lastCheckedMillis: Option[Long] = None
private var lastScheduledMillis: Option[Long] = None
private var lastScannedMillis: Option[Long] = None

private def run {
while (state.get() != STOPPING) {
// Periodically run
val currentTimeMillis = System.currentTimeMillis()
val scanIntervalMillis = getConfig.scanInterval.toMillis
if (lastCheckedMillis.isEmpty || currentTimeMillis - lastCheckedMillis.get > scanIntervalMillis) {
if (lastScheduledMillis.isEmpty || currentTimeMillis - lastScheduledMillis.get > scanIntervalMillis) {
val updatedLastScannedMillis = scan(getConfig.logLevelFileCandidates, lastScannedMillis)
guard {
lastScannedMillis = updatedLastScannedMillis
}
lastCheckedMillis = Some(currentTimeMillis)
lastScheduledMillis = Some(currentTimeMillis)
}
// wait
val sleepTime = scanIntervalMillis - math.max(0, math.min(scanIntervalMillis, currentTimeMillis - lastCheckedMillis.get))

// wait until next scheduled time
val sleepTime = scanIntervalMillis - math.max(0, math.min(scanIntervalMillis, currentTimeMillis - lastScheduledMillis.get))
guard {
if (configChanged.await(sleepTime, TimeUnit.MILLISECONDS)) {
// awaken due to config change
Expand Down

0 comments on commit 62f3ea0

Please sign in to comment.