This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDateTime | |
import java.util.{Deque, LinkedList} | |
import scala.concurrent.{Future, Promise} | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.util.{Failure, Success} | |
class ApiService[T](limit: Long, timeFrame: FiniteDuration) { | |
case class RequestQueueItem(f: () => Future[T], p: Promise[T]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
object MockAPI { | |
private val rand = new scala.util.Random() | |
class RateLimitException(msg: String) extends Throwable(msg, null) | |
def simpleService: Future[String] = Future { "1, 2, 3, 4, 5, 6" } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiService[T](limit: Long, timeFrame: FiniteDuration) { | |
// other code ... | |
type LimitDetector = PartialFunction[Either[Throwable, T], Boolean] | |
case class RequestQueueItem(f: () => Future[T], | |
p: Promise[T], | |
limitDetector: LimitDetector) | |
private var limitDetection: LimitDetector = { | |
case Left(_) => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Either[Throwable, T] => Boolean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApiService[T](limit: Long, timeFrame: FiniteDuration) { | |
// previous code ... | |
private def hasCapacity(): Boolean = { | |
val now = LocalDateTime.now() | |
if (now.isAfter(windowStopTime)) { | |
windowStopTime = now.plusSeconds(timeFrame.toSeconds) | |
requestCount = 0 | |
true | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.ExecutionContext.Implicits.global | |
class ApiService[T](limit: Long, timeFrame: FiniteDuration) { | |
// previous code ... | |
private def makeRequest(): Unit = requestQueue.pollFirst() match { | |
case null => // do nothing | |
case req => | |
requestCount += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDateTime | |
import java.util.{Deque, LinkedList} | |
import scala.concurrent.{Future, Promise} | |
import scala.concurrent.duration._ | |
class ApiService[T](limit: Long, timeFrame: FiniteDuration) { | |
case class RequestQueueItem(f: () => Future[T], p: Promise[T]) | |
private var requestQueue: Deque[RequestQueueItem] = new LinkedList[RequestQueueItem]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
language: go | |
go: | |
- 1.8 | |
- 1.9.x | |
- master | |
matrix: | |
allow_failures: | |
go: master |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script: | |
- go test -v ./... | |
- gometalinter.v2 --vendor --disable-all --enable=errcheck --enable=vet --enable=deadcode ./... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script: | |
- go test -v ./... | |
- gometalinter.v2 --vendor ./... |