当前位置: 首页>>代码示例>>Scala>>正文


Scala Time类代码示例

本文整理汇总了Scala中com.twitter.util.Time的典型用法代码示例。如果您正苦于以下问题:Scala Time类的具体用法?Scala Time怎么用?Scala Time使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Time类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Client

//设置package包名称以及导入依赖的类
package roc
package postgresql

import com.twitter.finagle._
import com.twitter.finagle.ServiceFactory
import com.twitter.util.{Closable, Future, Time}

object Client {
  def apply (factory: ServiceFactory[Request, Result]): Client = 
    new StdClient(factory)
}

trait Client extends Closable {
  def query(req: Request): Future[Result]
}

final class StdClient(val factory: ServiceFactory[Request, Result]) extends Client {
  private[this] val service = factory.toService

  def query(req: Request): Future[Result] = {
    val query = new Query(req.query)
    service(req)
  }

  def close(deadline: Time): Future[Unit] = service.close(deadline)
} 
开发者ID:finagle,项目名称:roc,代码行数:27,代码来源:Client.scala

示例2: MockService

//设置package包名称以及导入依赖的类
package com.twitter.finagle.mysql

import com.twitter.finagle.{ClientConnection, ServiceFactory, Service}
import com.twitter.util.{Time, Future}


class MockService extends Service[Request, Result] {
  var requests = List[Request]()
  val resultSet = new ResultSet(fields=Seq(), rows=Seq())

  def apply(request: Request): Future[Result] = {
    requests = requests ++ List(request)
    Future.value(resultSet)
  }
}

class MockServiceFactory(service: Service[Request, Result]) extends ServiceFactory[Request, Result] {
  def apply(conn: ClientConnection): Future[Service[Request, Result]] = Future.value(service)
  def close(deadline: Time): Future[Unit] = Future.Unit
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:21,代码来源:MockService.scala

示例3: ClientTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.mysql

import com.twitter.finagle.Mysql
import com.twitter.finagle.mysql.transport.TransportImpl
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{MustMatchers, FunSuite}
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers._
import com.twitter.util.Time


@RunWith(classOf[JUnitRunner])
class ClientTest extends FunSuite with MockitoSugar with MustMatchers {
  private val sqlQuery = "SELECT * FROM FOO"

  test("basic test creates a new service for each query") {
    val service = new MockService()
    val factory = spy(new MockServiceFactory(service))
    val client = spy(Client(factory))

    client.query(sqlQuery)
    client.query(sqlQuery)

    service.requests must equal (List(
     sqlQuery, sqlQuery
    ).map(QueryRequest(_)))

    verify(client, times(2)).query(sqlQuery)
    verify(factory, times(2)).apply()
    verify(factory, times(0)).close(any[Time])
  }

  test("Client uses Netty3 by default, but can be toggled to netty4") {
    val params = Mysql.client.params

    assert(params[TransportImpl].transporter(params).toString.equals("Netty3Transporter"))
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:41,代码来源:ClientTest.scala

示例4: close

//设置package包名称以及导入依赖的类
package com.twitter.finagle.loadbalancer

import com.twitter.finagle.ServiceFactory
import com.twitter.finagle.util.OnReady
import com.twitter.util.{Time, Activity, Future, Promise}


  private[this] val observation = activity.states.respond {
    case Activity.Pending =>

    case Activity.Ok(newList) =>
      update(newList)
      ready.setDone()

    case Activity.Failed(_) =>
      // On resolution failure, consider the
      // load balancer ready (to serve errors).
      ready.setDone()
  }

  override def close(deadline: Time): Future[Unit] = {
    observation.close(deadline).transform { _ =>
      super.close(deadline)
    }.ensure {
      ready.setDone()
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:29,代码来源:Updating.scala

示例5: rng

//设置package包名称以及导入依赖的类
package com.twitter.finagle.loadbalancer

import com.twitter.finagle.{ClientConnection, Service, ServiceFactory, ServiceFactoryProxy, ServiceProxy}
import com.twitter.finagle.service.FailingFactory
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.finagle.util.Rng
import com.twitter.util.{Throw, Time, Future, Return}
import java.util.concurrent.atomic.AtomicInteger


private[loadbalancer] trait LeastLoaded[Req, Rep] { self: Balancer[Req, Rep] =>
  protected def rng: Rng

  protected case class Node(
      factory: ServiceFactory[Req, Rep],
      counter: AtomicInteger,
      token: Int)
    extends ServiceFactoryProxy[Req, Rep](factory)
    with NodeT[Req, Rep] {

    type This = Node

    def load: Double = counter.get
    def pending: Int = counter.get

    override def apply(conn: ClientConnection): Future[Service[Req, Rep]] = {
      counter.incrementAndGet()
      super.apply(conn).transform {
        case Return(svc) =>
          Future.value(new ServiceProxy(svc) {
            override def close(deadline: Time) =
              super.close(deadline).ensure {
                counter.decrementAndGet()
              }
          })

        case [email protected](_) =>
          counter.decrementAndGet()
          Future.const(t)
      }
    }
  }

  protected def newNode(factory: ServiceFactory[Req, Rep], statsReceiver: StatsReceiver) =
    Node(factory, new AtomicInteger(0), rng.nextInt())

  private[this] val failingLoad = new AtomicInteger(0)
  protected def failingNode(cause: Throwable) = Node(new FailingFactory(cause), failingLoad, 0)
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:50,代码来源:LeastLoaded.scala

示例6: MkJvmFilter

//设置package包名称以及导入依赖的类
package com.twitter.finagle.filter

import com.twitter.util.{Time, Future}
import com.twitter.jvm.Jvm
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.finagle.tracing.{Trace, Record, Annotation}
import com.twitter.conversions.time._


class MkJvmFilter(jvm: Jvm) {
  private[this] val buffer = jvm.monitorGcs(1.minute)

  def apply[Req, Rep](): SimpleFilter[Req, Rep] = new SimpleFilter[Req, Rep] {
    def apply(req: Req, service: Service[Req, Rep]): Future[Rep] = {
      val begin = Time.now
      if (Trace.isActivelyTracing) {
        service(req) ensure {
          buffer(begin) foreach { gc =>
            Trace.record {
              Record(Trace.id, gc.timestamp, Annotation.Message(gc.toString), Some(gc.duration))
            }
          }
        }
      }
      else
        service(req)
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:30,代码来源:JvmFilter.scala

示例7: ExpiringServerDispatcher

//设置package包名称以及导入依赖的类
package com.twitter.finagle.dispatch

import com.twitter.finagle.Service
import com.twitter.finagle.service.ExpiringService
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.finagle.transport.Transport
import com.twitter.util.{Duration, Timer, Time, Closable}

object ExpiringServerDispatcher {
  def apply[Req, Rep, In, Out](
    maxIdleTime: Option[Duration],
    maxLifeTime: Option[Duration],
    timer: Timer,
    statsReceiver: StatsReceiver,
    newDispatcher: (Transport[In, Out], Service[Req, Rep]) => Closable
  ): (Transport[In, Out], Service[Req, Rep]) => Closable =
    (transport: Transport[In, Out], service: Service[Req, Rep]) =>
        new ExpiringService(service, maxIdleTime, maxLifeTime, timer, statsReceiver) {
          private[this] val dispatcher = newDispatcher(transport, this)
          protected def onExpire() { dispatcher.close(Time.now) }
        }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:23,代码来源:ExpiringServerDispatcher.scala

示例8: onClose

//设置package包名称以及导入依赖的类
package com.twitter.finagle.util

import com.twitter.util.{Closable, Future, Promise, Return, Time}


    def onClose(h: => Unit) = {
      if (closing.isDefined)
        h
      else
        closeHandlers ::= { () => h }
    }

     // Invokes close handlers in reverse order from which they were added.
    closing ensure { closeHandlers foreach { handler =>
      handler()
    }}
  }

  def makeLifoCloser(): CloseNotifier with Closable = new CloseNotifier with Closable {
    private[this] val closing = new Promise[Unit]
    private[this] val notifier = makeLifo(closing)

    def close(deadline: Time) = {
      closing.updateIfEmpty(Return(()))
      Future.Done
    }
    def onClose(h: => Unit) = notifier.onClose(h)
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:30,代码来源:CloseNotifier.scala

示例9: WriteCompletionTimeoutHandler

//设置package包名称以及导入依赖的类
package com.twitter.finagle.netty3.channel

import com.twitter.finagle.WriteTimedOutException
import com.twitter.util.{Time, Duration, Timer}
import org.jboss.netty.channel._


private[finagle] class WriteCompletionTimeoutHandler(timer: Timer, timeout: Duration)
  extends SimpleChannelDownstreamHandler
{
  override def writeRequested(ctx: ChannelHandlerContext, e: MessageEvent) {
    val task = timer.schedule(Time.now + timeout) {
      val channel = ctx.getChannel
      Channels.fireExceptionCaught(
        channel, new WriteTimedOutException(if (channel != null) channel.getRemoteAddress else null))
    }
    e.getFuture.addListener(new ChannelFutureListener {
      override def operationComplete(f: ChannelFuture): Unit =
        if (!f.isCancelled) { // on success or failure
          task.cancel()
        }
    })

    super.writeRequested(ctx, e)
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:27,代码来源:WriteCompletionTimeoutHandler.scala

示例10: DeadlineStatsFilter

//设置package包名称以及导入依赖的类
package com.twitter.finagle.service

import com.twitter.finagle._
import com.twitter.finagle.context.Deadline
import com.twitter.finagle.stats.StatsReceiver
import com.twitter.util.{Duration, Future, Time}

object DeadlineStatsFilter {

  val role = new Stack.Role("DeadlineStatsFilter")

  
private[finagle] class DeadlineStatsFilter[Req, Rep](statsReceiver: StatsReceiver)
    extends SimpleFilter[Req, Rep] {

  private[this] val exceededStat = statsReceiver.counter("exceeded")
  private[this] val transitTimeStat = statsReceiver.stat("transit_latency_ms")
  private[this] val expiredTimeStat = statsReceiver.stat("expired_ms")

  def apply(request: Req, service: Service[Req, Rep]): Future[Rep] = {
    Deadline.current match {
      case Some(deadline) =>
        val now = Time.now
        if (deadline.deadline < now) {
          val exceeded = now - deadline.deadline
          expiredTimeStat.add(exceeded.inMilliseconds)
        }
        transitTimeStat.add((now - deadline.timestamp).max(Duration.Zero).inMilliseconds)

        if (deadline.expired)
          exceededStat.incr()

      case None =>
    }
    service(request)
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:38,代码来源:DeadlineStatsFilter.scala

示例11: RefcountedService

//设置package包名称以及导入依赖的类
package com.twitter.finagle.service

import com.twitter.finagle.{Service, ServiceProxy}
import com.twitter.finagle.util.AsyncLatch
import com.twitter.util.{Future, Promise, Time, Try}


private[finagle] class RefcountedService[Req, Rep](underlying: Service[Req, Rep])
  extends ServiceProxy[Req, Rep](underlying) {

  private[this] val replyLatch = new AsyncLatch()

  private[this] val decrLatchFn: Try[Rep] => Unit =
    _ => replyLatch.decr()

  override def apply(request: Req): Future[Rep] = {
    replyLatch.incr()
    underlying(request).respond(decrLatchFn)
  }

  override final def close(deadline: Time): Future[Unit] = {
    val p = new Promise[Unit]
    replyLatch.await {
      p.become(underlying.close(deadline))
    }
    p
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:29,代码来源:RefcountedService.scala

示例12: SingletonFactory

//设置package包名称以及导入依赖的类
package com.twitter.finagle.service

import com.twitter.finagle.util.AsyncLatch
import com.twitter.finagle.{Service, ServiceFactory, ClientConnection}
import com.twitter.util.{Future, Promise, Time}


class SingletonFactory[Req, Rep](service: Service[Req, Rep])
  extends ServiceFactory[Req, Rep]
{
  private[this] var latch = new AsyncLatch

  def apply(conn: ClientConnection) = Future {
    latch.incr()
    new Service[Req, Rep] {
      def apply(request: Req) = service(request)
      override def close(deadline: Time) = { latch.decr(); Future.Done }
    }
  }

  def close(deadline: Time) = {
    val p = new Promise[Unit]
    latch.await {
      service.close()
      p.setDone()
    }
    p
  }

  override def status = service.status

  override val toString = "singleton_factory_%s".format(service.toString)
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:34,代码来源:SingletonFactory.scala

示例13: CloseOnReleaseService

//设置package包名称以及导入依赖的类
package com.twitter.finagle.service

import com.twitter.finagle.{Status, Service, ServiceClosedException, ServiceProxy, WriteException}
import com.twitter.util.{Future, Time}
import java.util.concurrent.atomic.AtomicBoolean


private[finagle] class CloseOnReleaseService[Req, Rep](underlying: Service[Req, Rep])
  extends ServiceProxy[Req, Rep](underlying)
{
  private[this] val wasReleased = new AtomicBoolean(false)

  override def apply(request: Req) = {
    if (!wasReleased.get) {
      super.apply(request)
    } else {
      Future.exception(
        WriteException(new ServiceClosedException))
    }
  }

  override def close(deadline: Time) = {
    if (wasReleased.compareAndSet(false, true))
      super.close(deadline)
    else
      Future.Done
  }

  override def status = 
    if (wasReleased.get) Status.Closed
    else super.status
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:33,代码来源:CloseOnReleaseService.scala

示例14: ServerStatsFilterTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.filter

import com.twitter.finagle.Service
import com.twitter.finagle.stats.InMemoryStatsReceiver
import com.twitter.util.{Stopwatch, Time, Future}
import com.twitter.conversions.time._
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class ServerStatsFilterTest extends FunSuite {
  test("Records handletime for a service") {
    Time.withCurrentTimeFrozen { ctl =>
      val inMemory = new InMemoryStatsReceiver
      val svc = Service.mk[Unit, Unit] { unit =>
        ctl.advance(5.microseconds)
        Future.never
      }
      val filter = new ServerStatsFilter[Unit, Unit](inMemory, Stopwatch.timeNanos)
      filter.andThen(svc)(())
      val expected = 5
      val actual = inMemory.stats(Seq("handletime_us"))(0)
      assert(actual == expected)
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:28,代码来源:ServerStatsFilterTest.scala

示例15: DeadlineTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.context

import com.twitter.util.{Time, Duration, Return}
import org.junit.runner.RunWith
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.junit.{AssertionsForJUnit, JUnitRunner}
import org.scalatest.prop.GeneratorDrivenPropertyChecks

@RunWith(classOf[JUnitRunner])
class DeadlineTest 
  extends FunSuite 
  with AssertionsForJUnit
  with GeneratorDrivenPropertyChecks {

  val time = for (t <- Gen.choose(0L, Long.MaxValue)) yield Time.fromNanoseconds(t)
  val dur = for (d <- Gen.choose(0L, Long.MaxValue)) yield Duration.fromNanoseconds(d)
  val deadline = for (t <- time; d <- dur) yield Deadline(t, t + d)
  val deadlineWithoutTop  = deadline.filter(_.deadline != Time.Top)

  test("Deadline marshalling") {
    // won't pass Time.Top as deadline for marshalling
    forAll(deadlineWithoutTop) { d =>
      assert(Deadline.tryUnmarshal(Deadline.marshal(d)) == Return(d))
    }
  }

  test("Deadline.combined") {
    forAll(deadline, deadline) { (d1, d2) =>
      assert(Deadline.combined(d1, d2).timestamp == (d1.timestamp max d2.timestamp))
      assert(Deadline.combined(d1, d2).deadline == (d1.deadline min d2.deadline))
      assert(Deadline.combined(d1, d2) == Deadline.combined(d2, d1))
    }
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:36,代码来源:DeadlineTest.scala


注:本文中的com.twitter.util.Time类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。