├── _config.yml ├── project ├── build.properties └── plugins.sbt ├── .gitignore ├── .travis.yml ├── .scalafmt.conf ├── ratelimiter4s └── src │ ├── main │ └── scala │ │ └── ratelimiter4s │ │ └── core │ │ ├── FRateLimiter.scala │ │ └── FunctionLimiter.scala │ └── test │ └── scala │ └── ratelimiter4s │ ├── Configs.scala │ └── core │ ├── FRateLimiterUnsafeRunSpec.scala │ └── FRateLimiterSpec.scala ├── ratelimiter4sZio └── src │ ├── main │ └── scala │ │ └── ratelimiter4s │ │ └── zio │ │ ├── ZIORateLimiter.scala │ │ └── FunctionLimiter.scala │ └── test │ └── scala │ └── ratelimiter4s │ ├── Configs.scala │ └── zio │ ├── ZIORateLimiterUnsafeRunSpec.scala │ └── ZIORateLimiterSpec.scala ├── ratelimiter4sCats └── src │ ├── main │ └── scala │ │ └── ratelimiter4s │ │ └── cats │ │ ├── CatsRateLimiter.scala │ │ └── FunctionLimiter.scala │ └── test │ └── scala │ └── ratelimiter4s │ ├── Configs.scala │ └── cats │ ├── CatsRateLimiterUnsafeRunSpec.scala │ └── CatsRateLimiterSpec.scala ├── README.md └── LICENSE /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 1.2.8 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | project/project 2 | project/target 3 | target 4 | .idea 5 | 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | scala: 3 | - 2.12.8 4 | script: 5 | - sbt clean test 6 | jdk: 7 | - openjdk8 -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | logLevel := Level.Warn 2 | 3 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1") 4 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.22") 5 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = "2.0.0" 2 | maxColumn = 120 3 | align = most 4 | continuationIndent.defnSite = 2 5 | assumeStandardLibraryStripMargin = true 6 | docstrings = JavaDoc 7 | lineEndings = preserve 8 | includeCurlyBraceInSelectChains = false 9 | danglingParentheses = true 10 | spaces { 11 | inImportCurlyBraces = true 12 | } 13 | optIn.annotationNewlines = true 14 | 15 | rewrite.rules = [SortImports, RedundantBraces] -------------------------------------------------------------------------------- /ratelimiter4s/src/main/scala/ratelimiter4s/core/FRateLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.core 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | 5 | /** 6 | * @author Ayush Mittal 7 | */ 8 | 9 | object FRateLimiter { 10 | 11 | def limit[A](function0: () => A, rateLimiter: RateLimiter): Function0Limiter[A] = 12 | new Function0Limiter[A](rateLimiter, function0) 13 | 14 | def limit[A,B](function1: B => A, rateLimiter: RateLimiter): Function1Limiter[A, B] = 15 | new Function1Limiter[A, B](rateLimiter, function1) 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ratelimiter4sZio/src/main/scala/ratelimiter4s/zio/ZIORateLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.zio 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | 5 | /** 6 | * @author Ayush Mittal 7 | */ 8 | object ZIORateLimiter { 9 | 10 | def limit[A](function0: () => A, rateLimiter: RateLimiter): ZFunction0Limiter[A] = 11 | new ZFunction0Limiter[A](rateLimiter, function0) 12 | 13 | 14 | def limit[A,B](function1: B => A, rateLimiter: RateLimiter): ZFunction1Limiter[A, B] = 15 | new ZFunction1Limiter[A, B](rateLimiter, function1) 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ratelimiter4sCats/src/main/scala/ratelimiter4s/cats/CatsRateLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.cats 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | 5 | /** 6 | * @author Ayush Mittal 7 | */ 8 | object CatsRateLimiter { 9 | 10 | def limit[A](function0: () => A, rateLimiter: RateLimiter): CatsFunction0Limiter[A] = 11 | new CatsFunction0Limiter[A](rateLimiter, function0) 12 | 13 | def limit[A,B](function1: B => A, rateLimiter: RateLimiter): CatsFunction1Limiter[A, B] = 14 | new CatsFunction1Limiter[A, B](rateLimiter, function1) 15 | 16 | } 17 | -------------------------------------------------------------------------------- /ratelimiter4sCats/src/test/scala/ratelimiter4s/Configs.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s 2 | 3 | import java.time.Duration 4 | 5 | import io.github.resilience4j.ratelimiter.RateLimiterConfig 6 | 7 | /** 8 | * @author Ayush Mittal 9 | */ 10 | trait Configs { 11 | 12 | val onePerMillis: RateLimiterConfig = RateLimiterConfig 13 | .custom() 14 | .timeoutDuration(Duration.ofMillis(100)) 15 | .limitRefreshPeriod(Duration.ofSeconds(1)) 16 | .limitForPeriod(1) 17 | .build() 18 | 19 | val twoPerMillis: RateLimiterConfig = RateLimiterConfig 20 | .custom() 21 | .timeoutDuration(Duration.ofMillis(100)) 22 | .limitRefreshPeriod(Duration.ofSeconds(1)) 23 | .limitForPeriod(2) 24 | .build() 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ratelimiter4sZio/src/test/scala/ratelimiter4s/Configs.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s 2 | 3 | import java.time.Duration 4 | 5 | import io.github.resilience4j.ratelimiter.RateLimiterConfig 6 | 7 | /** 8 | * @author Ayush Mittal 9 | */ 10 | trait Configs { 11 | 12 | val onePerMillis: RateLimiterConfig = RateLimiterConfig 13 | .custom() 14 | .timeoutDuration(Duration.ofMillis(100)) 15 | .limitRefreshPeriod(Duration.ofSeconds(1)) 16 | .limitForPeriod(1) 17 | .build() 18 | 19 | val twoPerMillis: RateLimiterConfig = RateLimiterConfig 20 | .custom() 21 | .timeoutDuration(Duration.ofMillis(100)) 22 | .limitRefreshPeriod(Duration.ofSeconds(1)) 23 | .limitForPeriod(2) 24 | .build() 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ratelimiter4s/src/test/scala/ratelimiter4s/Configs.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s 2 | 3 | import java.time.Duration 4 | 5 | import io.github.resilience4j.ratelimiter.RateLimiterConfig 6 | 7 | /** 8 | * @author Ayush Mittal 9 | */ 10 | trait Configs { 11 | 12 | val onePerMillis: RateLimiterConfig = RateLimiterConfig 13 | .custom() 14 | .timeoutDuration(Duration.ofMillis(100)) 15 | .limitRefreshPeriod(Duration.ofSeconds(1)) 16 | .limitForPeriod(1) 17 | .build() 18 | 19 | 20 | 21 | val twoPerMillis: RateLimiterConfig = RateLimiterConfig 22 | .custom() 23 | .timeoutDuration(Duration.ofMillis(100)) 24 | .limitRefreshPeriod(Duration.ofSeconds(1)) 25 | .limitForPeriod(2) 26 | .build() 27 | 28 | } 29 | -------------------------------------------------------------------------------- /ratelimiter4sCats/src/main/scala/ratelimiter4s/cats/FunctionLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.cats 2 | 3 | import cats.data.EitherT 4 | import cats.effect.IO 5 | import io.github.resilience4j.ratelimiter.{ RateLimiter, RequestNotPermitted } 6 | import ratelimiter4s.core.{ Function0Limiter, Function1Limiter } 7 | 8 | /** 9 | * @author Ayush Mittal 10 | */ 11 | class CatsFunction0Limiter[A](rateLimiter: RateLimiter, function0: () => A) extends (() => EitherT[IO, Throwable, A]) { 12 | def pure: EitherT[IO, RequestNotPermitted, A] = 13 | EitherT.fromEither { 14 | Function0Limiter.pure(rateLimiter, function0) 15 | } 16 | 17 | def apply: EitherT[IO, Throwable, A] = 18 | EitherT(IO(Function0Limiter(rateLimiter, function0))) 19 | } 20 | 21 | class CatsFunction1Limiter[A, T](rateLimiter: RateLimiter, function1: T => A) extends (T => EitherT[IO, Throwable, A]) { 22 | def pure(t: T): EitherT[IO, RequestNotPermitted, A] = 23 | EitherT.fromEither { 24 | Function1Limiter.pure(rateLimiter, function1, t) 25 | } 26 | 27 | def apply(t: T): EitherT[IO, Throwable, A] = 28 | EitherT(IO(Function1Limiter(rateLimiter, function1, t))) 29 | } 30 | -------------------------------------------------------------------------------- /ratelimiter4sZio/src/main/scala/ratelimiter4s/zio/FunctionLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.zio 2 | 3 | import io.github.resilience4j.ratelimiter.{ RateLimiter, RequestNotPermitted } 4 | import ratelimiter4s.core.{ Function0Limiter, Function1Limiter } 5 | import zio.{ IO, Task, ZIO } 6 | 7 | /** 8 | * @author Ayush Mittal 9 | */ 10 | class ZFunction0Limiter[A](rateLimiter: RateLimiter, function0: () => A) extends (() => Task[A]) { 11 | def pure: IO[RequestNotPermitted, A] = 12 | ZIO.fromEither { 13 | Function0Limiter.pure(rateLimiter, function0) 14 | } 15 | 16 | def apply: Task[A] = 17 | ZIO.effect(Function0Limiter(rateLimiter, function0)).flatMap { 18 | case Left(error) => ZIO.fail(error) 19 | case Right(a) => ZIO.succeed(a) 20 | } 21 | } 22 | 23 | class ZFunction1Limiter[A, T](rateLimiter: RateLimiter, function1: T => A) extends (T => Task[A]) { 24 | def pure(t: T): IO[RequestNotPermitted, A] = 25 | ZIO.fromEither { 26 | Function1Limiter.pure(rateLimiter, function1, t) 27 | } 28 | 29 | def apply(t: T): Task[A] = 30 | ZIO.effect(Function1Limiter(rateLimiter, function1, t)).flatMap { 31 | case Left(error) => ZIO.fail(error) 32 | case Right(a) => ZIO.succeed(a) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ratelimiter4s/src/main/scala/ratelimiter4s/core/FunctionLimiter.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.core 2 | 3 | import io.github.resilience4j.ratelimiter.{ RateLimiter, RequestNotPermitted } 4 | 5 | /** 6 | * @author Ayush Mittal 7 | */ 8 | class Function0Limiter[A](rateLimiter: RateLimiter, function0: () => A) extends (() => Either[Throwable, A]) { 9 | def apply: Either[Throwable, A] = 10 | Function0Limiter.apply[A](rateLimiter, function0) 11 | 12 | def pure: Either[RequestNotPermitted, A] = 13 | Function0Limiter.pure[A](rateLimiter, function0) 14 | } 15 | 16 | object Function0Limiter { 17 | 18 | def pure[A](rateLimiter: RateLimiter, function0: () => A): Either[RequestNotPermitted, A] = 19 | try { 20 | RateLimiter.waitForPermission(rateLimiter) 21 | Right(function0.apply()) 22 | } catch { 23 | case e: RequestNotPermitted => Left(e) 24 | } 25 | 26 | def apply[A](rateLimiter: RateLimiter, function0: () => A): Either[Throwable, A] = 27 | try { 28 | RateLimiter.waitForPermission(rateLimiter) 29 | Right(function0.apply()) 30 | } catch { 31 | case e: Throwable => Left(e) 32 | } 33 | } 34 | 35 | class Function1Limiter[A, T](rateLimiter: RateLimiter, function1: T => A) extends (T => Either[Throwable, A]) { 36 | def apply(t: T): Either[Throwable, A] = 37 | Function1Limiter.apply(rateLimiter, function1, t) 38 | 39 | def pure(t: T): Either[RequestNotPermitted, A] = 40 | Function1Limiter.pure(rateLimiter, function1, t) 41 | } 42 | 43 | object Function1Limiter { 44 | def pure[A, T](rateLimiter: RateLimiter, function1: T => A, t: T): Either[RequestNotPermitted, A] = 45 | try { 46 | RateLimiter.waitForPermission(rateLimiter) 47 | Right(function1.apply(t)) 48 | } catch { 49 | case e: RequestNotPermitted => Left(e) 50 | } 51 | 52 | def apply[A, T](rateLimiter: RateLimiter, function1: T => A, t: T): Either[Throwable, A] = 53 | try { 54 | RateLimiter.waitForPermission(rateLimiter) 55 | Right(function1.apply(t)) 56 | } catch { 57 | case e: Throwable => Left(e) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /ratelimiter4s/src/test/scala/ratelimiter4s/core/FRateLimiterUnsafeRunSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.core 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | import org.scalatest.{ EitherValues, Matchers, WordSpec } 5 | import ratelimiter4s.Configs 6 | import FRateLimiter._ 7 | 8 | /** 9 | * @author Ayush Mittal 10 | */ 11 | class FRateLimiterUnsafeRunSpec extends WordSpec with Matchers with EitherValues with Configs { 12 | 13 | "Rate limiter" when { 14 | 15 | "Function1 is rate limitedd" should { 16 | 17 | def service(name: String): String = { 18 | if (name.exists(_.isDigit)) { 19 | throw new RuntimeException("error") 20 | } 21 | s"hello $name" 22 | } 23 | 24 | "return RequestNotPermitted when rate limit is breached" in { 25 | 26 | val onePerSecondLimiter = 27 | RateLimiter.of("onePerSecondLimiter", onePerMillis) 28 | 29 | val rateLimitedService = limit(service _, onePerSecondLimiter) 30 | 31 | val result = for { 32 | _ <- rateLimitedService("John") 33 | result2 <- rateLimitedService("Bob") 34 | } yield result2 35 | 36 | result.isLeft shouldBe true 37 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 38 | } 39 | 40 | "return value when rate limit is not breached" in { 41 | 42 | val twoPerMillisLimiter = 43 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 44 | 45 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 46 | 47 | val result = for { 48 | _ <- rateLimitedService("John") 49 | result2 <- rateLimitedService("Bob") 50 | } yield result2 51 | 52 | result.isRight shouldBe true 53 | result.right.value shouldBe "hello Bob" 54 | 55 | } 56 | 57 | "return exception when it is thrown" in { 58 | 59 | val twoPerMillisLimiter = 60 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 61 | 62 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 63 | 64 | val result = for { 65 | _ <- rateLimitedService("1") 66 | result2 <- rateLimitedService("Bob") 67 | } yield result2 68 | 69 | result.isLeft shouldBe true 70 | result.left.value.getMessage shouldBe "error" 71 | } 72 | } 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ratelimiter4sCats/src/test/scala/ratelimiter4s/cats/CatsRateLimiterUnsafeRunSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.cats 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | import org.scalatest.{ EitherValues, Matchers, WordSpec } 5 | import ratelimiter4s.Configs 6 | import ratelimiter4s.cats.CatsRateLimiter._ 7 | 8 | /** 9 | * @author Ayush Mittal 10 | */ 11 | class CatsRateLimiterUnsafeRunSpec extends WordSpec with Matchers with EitherValues with Configs { 12 | 13 | "Rate limiter" when { 14 | 15 | "Function1 is rate limitedd" should { 16 | 17 | def service(name: String): String = { 18 | if (name.exists(_.isDigit)) { 19 | throw new RuntimeException("error") 20 | } 21 | s"hello $name" 22 | } 23 | 24 | "return RequestNotPermitted when rate limit is breached" in { 25 | 26 | val onePerSecondLimiter = 27 | RateLimiter.of("onePerSecondLimiter", onePerMillis) 28 | 29 | val rateLimitedService = limit(service _, onePerSecondLimiter) 30 | 31 | val prog = for { 32 | _ <- rateLimitedService("John") 33 | result2 <- rateLimitedService("Bob") 34 | } yield result2 35 | 36 | val result = prog.value.unsafeRunSync() 37 | result.isLeft shouldBe true 38 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 39 | } 40 | 41 | "return value when rate limit is not breached" in { 42 | 43 | val twoPerMillisLimiter = 44 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 45 | 46 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 47 | 48 | val prog = for { 49 | _ <- rateLimitedService("John") 50 | result2 <- rateLimitedService("Bob") 51 | } yield result2 52 | 53 | val result = prog.value.unsafeRunSync() 54 | result.isRight shouldBe true 55 | result.right.value shouldBe "hello Bob" 56 | 57 | } 58 | 59 | "return exception when it is thrown" in { 60 | 61 | val twoPerMillisLimiter = 62 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 63 | 64 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 65 | 66 | val prog = for { 67 | _ <- rateLimitedService("1") 68 | result2 <- rateLimitedService("Bob") 69 | } yield result2 70 | 71 | val result = prog.value.unsafeRunSync() 72 | result.isLeft shouldBe true 73 | result.left.value.getMessage shouldBe "error" 74 | } 75 | } 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /ratelimiter4sZio/src/test/scala/ratelimiter4s/zio/ZIORateLimiterUnsafeRunSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.zio 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | import org.scalatest.{ EitherValues, Matchers, WordSpec } 5 | import ratelimiter4s.Configs 6 | import ratelimiter4s.zio.ZIORateLimiter._ 7 | import zio.DefaultRuntime 8 | 9 | /** 10 | * @author Ayush Mittal 11 | */ 12 | class ZIORateLimiterUnsafeRunSpec extends WordSpec with Matchers with EitherValues with Configs { 13 | 14 | val runtime = new DefaultRuntime {} 15 | 16 | "Rate limiter" when { 17 | 18 | "Function1 is rate limitedd" should { 19 | 20 | def service(name: String): String = { 21 | if (name.exists(_.isDigit)) { 22 | throw new RuntimeException("error") 23 | } 24 | s"hello $name" 25 | } 26 | 27 | "return RequestNotPermitted when rate limit is breached" in { 28 | 29 | val onePerSecondLimiter = 30 | RateLimiter.of("onePerSecondLimiter", onePerMillis) 31 | 32 | val rateLimitedService = limit(service _, onePerSecondLimiter) 33 | 34 | val prog = for { 35 | _ <- rateLimitedService("John") 36 | result2 <- rateLimitedService("Bob") 37 | } yield result2 38 | 39 | val result = runtime.unsafeRun(prog.either) 40 | result.isLeft shouldBe true 41 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 42 | } 43 | 44 | "return value when rate limit is not breached" in { 45 | 46 | val twoPerMillisLimiter = 47 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 48 | 49 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 50 | 51 | val prog = for { 52 | _ <- rateLimitedService("John") 53 | result2 <- rateLimitedService("Bob") 54 | } yield result2 55 | 56 | val result = runtime.unsafeRun(prog.either) 57 | result.isRight shouldBe true 58 | result.right.value shouldBe "hello Bob" 59 | 60 | } 61 | 62 | "return exception when it is thrown" in { 63 | 64 | val twoPerMillisLimiter = 65 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 66 | 67 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 68 | 69 | val prog = for { 70 | _ <- rateLimitedService("1") 71 | result2 <- rateLimitedService("Bob") 72 | } yield result2 73 | 74 | val result = runtime.unsafeRun(prog.either) 75 | result.isLeft shouldBe true 76 | result.left.value.getMessage shouldBe "error" 77 | } 78 | } 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /ratelimiter4s/src/test/scala/ratelimiter4s/core/FRateLimiterSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.core 2 | 3 | import io.github.resilience4j.ratelimiter.{RateLimiter} 4 | import org.scalatest.{EitherValues, Matchers, WordSpec} 5 | import ratelimiter4s.Configs 6 | import ratelimiter4s.core.FRateLimiter._ 7 | 8 | /** 9 | * @author Ayush Mittal 10 | */ 11 | class FRateLimiterSpec extends WordSpec with Matchers with EitherValues with Configs { 12 | 13 | "Rate limiter" when { 14 | 15 | "Function0 is rate limitedd" should { 16 | 17 | def service: String = "value" 18 | 19 | "return RequestNotPermitted when rate limit is breached" in { 20 | 21 | val onePerMilliSecondLimiter = RateLimiter.of("onePerMilliSecondLimiter", onePerMillis) 22 | 23 | def rateLimitedService: () => Either[Throwable, String] = limit(service _, onePerMilliSecondLimiter) 24 | 25 | rateLimitedService.apply() shouldBe Right("value") 26 | rateLimitedService.apply().left.value.getMessage shouldBe "RateLimiter 'onePerMilliSecondLimiter' does not permit further calls" 27 | } 28 | 29 | "return value when rate limit is not breached" in { 30 | 31 | val twoPerMilliSecondLimiter = RateLimiter.of("twoPerMilliSecondLimiter", twoPerMillis) 32 | 33 | val rateLimitedService = limit(service _, twoPerMilliSecondLimiter) 34 | 35 | rateLimitedService.pure shouldBe Right("value") 36 | rateLimitedService.pure shouldBe Right("value") 37 | 38 | } 39 | 40 | "throw exception if thrown from service" in { 41 | 42 | def service: String = throw new RuntimeException("error") 43 | 44 | val onePerMilliSecondLimiter = RateLimiter.of("onePerMilliSecondLimiter", onePerMillis) 45 | 46 | val rateLimitedService = limit(service _, onePerMilliSecondLimiter) 47 | 48 | val exception = intercept[RuntimeException] { 49 | rateLimitedService.pure 50 | } 51 | 52 | exception.getMessage shouldBe "error" 53 | } 54 | 55 | } 56 | 57 | "Function1 is rate limitedd" should { 58 | 59 | def service(name: String): String = 60 | s"Hello $name" 61 | 62 | "return RequestNotPermitted when rate limit is breached" in { 63 | 64 | val onePerMilliSecondLimiter = RateLimiter.of("onePerMilliSecondLimiter", onePerMillis) 65 | 66 | val rateLimitedService = limit(service _, onePerMilliSecondLimiter) 67 | 68 | rateLimitedService.pure("John") shouldBe Right("Hello John") 69 | rateLimitedService.pure("Bob").left.value.getMessage shouldBe "RateLimiter 'onePerMilliSecondLimiter' does not permit further calls" 70 | } 71 | 72 | "return value when rate limit is not breached" in { 73 | 74 | val twoPerMilliSecondLimiter = RateLimiter.of("twoPerMilliSecondLimiter", twoPerMillis) 75 | 76 | val rateLimitedService = limit(service _, twoPerMilliSecondLimiter) 77 | 78 | 79 | rateLimitedService.pure("John") shouldBe Right("Hello John") 80 | rateLimitedService.pure("Bob") shouldBe Right("Hello Bob") 81 | 82 | } 83 | 84 | "throw exception if thrown from service" in { 85 | 86 | def service(name: String): String = throw new RuntimeException("error") 87 | 88 | val onePerMilliSecondLimiter = RateLimiter.of("onePerMilliSecondLimiter", onePerMillis) 89 | 90 | val rateLimitedService = limit(service _, onePerMilliSecondLimiter) 91 | 92 | val exception = intercept[RuntimeException] { 93 | rateLimitedService.pure("John") 94 | } 95 | 96 | exception.getMessage shouldBe "error" 97 | } 98 | 99 | } 100 | 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /ratelimiter4sCats/src/test/scala/ratelimiter4s/cats/CatsRateLimiterSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.cats 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | import org.scalatest.{ EitherValues, Matchers, WordSpec } 5 | import ratelimiter4s.Configs 6 | import ratelimiter4s.cats.CatsRateLimiter._ 7 | 8 | /** 9 | * @author Ayush Mittal 10 | */ 11 | class CatsRateLimiterSpec extends WordSpec with Matchers with EitherValues with Configs { 12 | 13 | "Rate limiter" when { 14 | 15 | "Function0 is rate limitedd" should { 16 | 17 | def service: String = "value" 18 | 19 | "return RequestNotPermitted when rate limit is breached" in { 20 | 21 | val onePerSecondLimiter = RateLimiter.of("onePerSecondLimiter", onePerMillis) 22 | 23 | val rateLimitedService: CatsFunction0Limiter[String] = limit(service _, onePerSecondLimiter) 24 | 25 | val prog = for { 26 | _ <- rateLimitedService.pure 27 | result2 <- rateLimitedService.pure 28 | } yield result2 29 | 30 | val result = prog.value.unsafeRunSync 31 | result.isLeft shouldBe true 32 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 33 | } 34 | 35 | "return value when rate limit is not breached" in { 36 | 37 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 38 | 39 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 40 | 41 | val prog = for { 42 | _ <- rateLimitedService.pure 43 | result2 <- rateLimitedService.pure 44 | } yield result2 45 | 46 | val result = prog.value.unsafeRunSync 47 | result.isRight shouldBe true 48 | result.right.value shouldBe "value" 49 | 50 | } 51 | 52 | "throw exception if thrown from service" in { 53 | 54 | def service: String = throw new RuntimeException("error") 55 | 56 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 57 | 58 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 59 | 60 | val exception = intercept[RuntimeException] { 61 | for { 62 | _ <- rateLimitedService.pure 63 | result2 <- rateLimitedService.pure 64 | } yield result2 65 | } 66 | 67 | exception.getMessage shouldBe "error" 68 | } 69 | 70 | } 71 | 72 | "Function1 is rate limitedd" should { 73 | 74 | def service(name: String): String = 75 | s"hello $name" 76 | 77 | "return RequestNotPermitted when rate limit is breached" in { 78 | 79 | val onePerSecondLimiter = RateLimiter.of("onePerSecondLimiter", onePerMillis) 80 | 81 | val rateLimitedService = limit(service _, onePerSecondLimiter) 82 | 83 | val prog = for { 84 | _ <- rateLimitedService.pure("John") 85 | result2 <- rateLimitedService.pure("Bob") 86 | } yield result2 87 | 88 | val result = prog.value.unsafeRunSync 89 | result.isLeft shouldBe true 90 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 91 | } 92 | 93 | "return value when rate limit is not breached" in { 94 | 95 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 96 | 97 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 98 | 99 | val prog = for { 100 | _ <- rateLimitedService.pure("John") 101 | result2 <- rateLimitedService.pure("Bob") 102 | } yield result2 103 | 104 | val result = prog.value.unsafeRunSync 105 | result.isRight shouldBe true 106 | result.right.value shouldBe "hello Bob" 107 | 108 | } 109 | 110 | "throw exception if thrown from service" in { 111 | 112 | def service(name: String): String = throw new RuntimeException("error") 113 | 114 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 115 | 116 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 117 | 118 | val exception = intercept[RuntimeException] { 119 | for { 120 | _ <- rateLimitedService.pure("John") 121 | result2 <- rateLimitedService.pure("Bob") 122 | } yield result2 123 | } 124 | 125 | exception.getMessage shouldBe "error" 126 | } 127 | 128 | } 129 | 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /ratelimiter4sZio/src/test/scala/ratelimiter4s/zio/ZIORateLimiterSpec.scala: -------------------------------------------------------------------------------- 1 | package ratelimiter4s.zio 2 | 3 | import io.github.resilience4j.ratelimiter.RateLimiter 4 | import org.scalatest.{ EitherValues, Matchers, WordSpec } 5 | import ratelimiter4s.Configs 6 | import ratelimiter4s.zio.ZIORateLimiter._ 7 | import zio.{ DefaultRuntime, FiberFailure } 8 | 9 | /** 10 | * @author Ayush Mittal 11 | */ 12 | class ZIORateLimiterSpec extends WordSpec with Matchers with EitherValues with Configs { 13 | 14 | val runtime = new DefaultRuntime {} 15 | 16 | "Rate limiter" when { 17 | 18 | "Function0 is rate limitedd" should { 19 | 20 | def service: String = "value" 21 | 22 | "return RequestNotPermitted when rate limit is breached" in { 23 | 24 | val onePerMillisLimiter = 25 | RateLimiter.of("onePerMillisLimiter", onePerMillis) 26 | 27 | val rateLimitedService: ZFunction0Limiter[String] = limit(service _, onePerMillisLimiter) 28 | 29 | val prog = for { 30 | _ <- rateLimitedService.pure 31 | result2 <- rateLimitedService.pure 32 | } yield result2 33 | 34 | val result = runtime.unsafeRun(prog.either) 35 | result.isLeft shouldBe true 36 | result.left.value.getMessage shouldBe "RateLimiter 'onePerMillisLimiter' does not permit further calls" 37 | } 38 | 39 | "return value when rate limit is not breached" in { 40 | 41 | val twoPerMillisLimiter = 42 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 43 | 44 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 45 | 46 | val prog = for { 47 | _ <- rateLimitedService.pure 48 | result2 <- rateLimitedService.pure 49 | } yield result2 50 | 51 | val result = runtime.unsafeRun(prog.either) 52 | result.isRight shouldBe true 53 | result.right.value shouldBe "value" 54 | } 55 | 56 | "throw exception if thrown from service" in { 57 | 58 | def service: String = throw new RuntimeException("error") 59 | 60 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 61 | 62 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 63 | 64 | val prog = for { 65 | _ <- rateLimitedService.pure 66 | result2 <- rateLimitedService.pure 67 | } yield result2 68 | 69 | intercept[FiberFailure](runtime.unsafeRun(prog.either)) 70 | 71 | } 72 | 73 | } 74 | 75 | "Function1 is rate limitedd" should { 76 | 77 | def service(name: String): String = 78 | s"hello $name" 79 | 80 | "return RequestNotPermitted when rate limit is breached" in { 81 | 82 | val onePerSecondLimiter = 83 | RateLimiter.of("onePerSecondLimiter", onePerMillis) 84 | 85 | val rateLimitedService = limit(service _, onePerSecondLimiter) 86 | 87 | val prog = for { 88 | _ <- rateLimitedService.pure("John") 89 | result2 <- rateLimitedService.pure("Bob") 90 | } yield result2 91 | 92 | val result = runtime.unsafeRun(prog.either) 93 | result.isLeft shouldBe true 94 | result.left.value.getMessage shouldBe "RateLimiter 'onePerSecondLimiter' does not permit further calls" 95 | } 96 | 97 | "return value when rate limit is not breached" in { 98 | 99 | val twoPerMillisLimiter = 100 | RateLimiter.of("twoPerMillisLimiter", twoPerMillis) 101 | 102 | val rateLimitedService = limit(service _, twoPerMillisLimiter) 103 | 104 | val prog = for { 105 | _ <- rateLimitedService.pure("John") 106 | result2 <- rateLimitedService.pure("Bob") 107 | } yield result2 108 | 109 | val result = runtime.unsafeRun(prog.either) 110 | result.isRight shouldBe true 111 | result.right.value shouldBe "hello Bob" 112 | 113 | } 114 | 115 | "throw exception if thrown from service" in { 116 | 117 | def service(name: String): String = throw new RuntimeException("error") 118 | 119 | val twoPerSecondLimiter = RateLimiter.of("twoPerSecondLimiter", twoPerMillis) 120 | 121 | val rateLimitedService = limit(service _, twoPerSecondLimiter) 122 | 123 | val prog = for { 124 | _ <- rateLimitedService.pure("John") 125 | result2 <- rateLimitedService.pure("Bob") 126 | } yield result2 127 | 128 | intercept[FiberFailure](runtime.unsafeRun(prog.either)) 129 | } 130 | 131 | } 132 | 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rate limiter library designed for Scala. 2 | 3 | [![Build Status](https://travis-ci.com/ayushworks/ratelimiter4s.svg?branch=master)](https://travis-ci.com/ayushworks/ratelimiter4s) 4 | 5 | **ratelimter4s** is lightweight rate limiter library designed for Scala. It provides wrappers to enhance any `Function` with rate limiting capabilities. The wrappers are available in 3 flavours 6 | * FRateLimiter : Rate limited method returns a `Either` type 7 | * CatsRateLimiter : Rate limited method returns a `EitherT` type 8 | * ZIORateLimiter : Rate limited method returns a `Task` type 9 | 10 | ratelimiter4s uses [resilience4j rate limiter](https://resilience4j.readme.io/docs/ratelimiter) to create the 11 | underlying rate limiting policies. Resilience4j is a lightweight fault tolerance library inspired by [Netflix Hystrix](https://github.com/Netflix/Hystrix), but designed for Java 8. 12 | 13 | The heroes of our story are `FRateLimiter`, `CatsRateLimiter` and `ZIORateLimiter` **classes** which provide `limit` method to rate limit. 14 | Any scala `Function` can be rate limited. 15 | 16 | Lets check out `FRateLimiter` in action : 17 | 18 | 19 | Consider a public service which takes a character name and returns an `Artist`. 20 | ```scala 21 | trait ArtistService { 22 | def getArtist(character: String): Artist 23 | } 24 | ``` 25 | 26 | Rules dictate that calling rate for `getArtist` to be not higher than 3 requests/second. 27 | 28 | We can define a `RateLimiter` config defined like this 29 | 30 | ```scala 31 | val threePerSecondConfig: RateLimiterConfig = RateLimiterConfig.custom() 32 | .limitRefreshPeriod(Duration.ofSeconds(1)) 33 | .limitForPeriod(3) 34 | .timeoutDuration(Duration.ofMillis(25)) 35 | .build() 36 | val threePerSecondLimiter = RateLimiter.of("3/second", threePerSecondConfig) 37 | ``` 38 | The details of the limiter config are described in detail [here](https://resilience4j.readme.io/docs/ratelimiter) 39 | 40 | We can use this config and add rate-limiting capability to our service. We would need to import the `FRateLimiter` class that provides the method `limit`. 41 | 42 | ```scala 43 | import ratelimiter4s.core.FRateLimiter._ 44 | 45 | val rateLimitedService = limit(getArtist _, threePerSecondLimiter) 46 | ``` 47 | 48 | we can also say 49 | 50 | ```scala 51 | import ratelimiter4s.core.FRateLimiter._ 52 | 53 | def rateLimitedService = limit(getArtist _, threePerSecondLimiter) 54 | ``` 55 | 56 | Lets checkout a rate limited service in action 57 | 58 | ```scala 59 | //first attempt 60 | rateLimitedService("Sheldon") == Right(Artist("Jim Parsons")) 61 | rateLimitedService("Penny") == Right(Artist("Kaley Cuoco")) 62 | rateLimitedService("Leonard") == Right(Artist("Johnny Galecki")) 63 | 64 | //fourth attempt will fail with a RequestNotPermitted 65 | rateLimitedService("Kripke") == Left(RequestNotPermitted) 66 | ``` 67 | **And that is it! We have achieved the objective of limiting the requests to 3/second.** 68 | 69 | 70 | `limit` methods return a `ratelimiter4s.core.FunctionNLimiter` instance. To be more accurate: 71 | 72 | * limiting a `scala.Function0` returns a `Function0Limiter` 73 | * limiting a `scala.Function1` returns a `Function1Limiter` 74 | * and so on for N till 22 75 | 76 | #### Pure and Impure 77 | 78 | A pure scala function is side-effect free, plus the result does not depend on anything other than its inputs. 79 | 80 | `FunctionNLimiter` provides a pure apply for every rate-limited function. The return type of `pure` 81 | is `Either[RequestNotPermitted,A]`. 82 | 83 | ```scala 84 | def greeter(guest: String) : String = s"Hello $guest" 85 | ``` 86 | Decorating `greeter` with a rate limiter 87 | 88 | ```scala 89 | val rateLimitedGreeter = limit(getArtist _, rateLimiter) 90 | ``` 91 | 92 | We can call `pure` on the rate limited method. 93 | 94 | ```scala 95 | val result: Either[RequestNotPermitted, String] = rateLimitedGreeter.pure("World") 96 | ``` 97 | 98 | Any exceptions thrown by the original `greeter` method are unhandled. So any instances of `Left` can only have a `RequestNotPermitted` type inside. 99 | 100 | However many useful methods that we would like to rate limit interact with outside world, mutate state and 101 | would not qualify as pure functions. Such a rate limited method could throw exceptions which are caused by the actual business logic. 102 | 103 | Consider the following example as a tweak to the original `greeter`. 104 | ```scala 105 | def greeter(guest: String) : String = { 106 | 107 | if(!guestList.contains(guest)) throw new UninvitedGuestException(guest) 108 | 109 | s"Hello $guest" 110 | } 111 | ``` 112 | 113 | Decorating the impure `greeter` with a rate limiter would remain the same. 114 | 115 | ```scala 116 | val rateLimitedGreeter = limit(getArtist _, rateLimiter) 117 | ``` 118 | 119 | **We should call an `apply` on the rate limited method instead of pure**. This means that we are also expecting `greeter` to fail for reasons other than rate limitations. 120 | 121 | ```scala 122 | val result: Either[Throwable, String] = rateLimitedGreeter("World") 123 | ``` 124 | The `Left` is of `Throwable` type in this case which is self-explanatory. 125 | 126 | #### Support for cats effect 127 | 128 | We can also capture the result of a rate limited method using [cats](https://typelevel.org/cats/) and its monad transformer instance `EitherT` 129 | 130 | Consider an image recognition service. 131 | 132 | ```scala 133 | trait RecognitionService { 134 | def recognizeImage(image: URL): ImageType 135 | } 136 | ``` 137 | 138 | Lets rate limit the `recognizeImage` method using `CatsFunctionLimiter` instances available in the `CatsRateLimiter` **class** 139 | 140 | ```scala 141 | import ratelimiter4s.cats.CatsRateLimiter._ 142 | 143 | val rateLimitedService = limit(getArtist _, rateLimiter) 144 | ``` 145 | 146 | Calling this rate limited return an `EitherT` bounded to an `IO` effect type 147 | 148 | ```scala 149 | val result : EitherT[IO, Throwable, ImageType] = rateLimitedService("https://samples.clarifai.com/metro-north.jpg") 150 | ``` 151 | 152 | The result is an `EitherT[IO, Throwable, ImageType`] . The `left` is a throwable because the method can fail due to other reasons apart from `RequestNotPermitted`. If that is not the case, we can use `pure`. 153 | 154 | ```scala 155 | val result : EitherT[IO, RequestNotPermitted, ImageType] = rateLimitedService.pure("https://samples.clarifai.com/metro-north.jpg") 156 | ``` 157 | 158 | the type clearly indicates that the failure can only be caused by a `RequestNotPermitted` error. 159 | 160 | #### Support for ZIO : 161 | 162 | We can also capture the result of a rate limited method using [zio](https://zio.dev/) types. 163 | 164 | Lets consider the image recognition service again. 165 | 166 | ```scala 167 | trait RecognitionService { 168 | def recognizeImage(image: URL): ImageType 169 | } 170 | ``` 171 | 172 | We rate limit this time using the `ZFunctionLimiter` instances available inside the `ZIORateLimiter` **class**. 173 | 174 | ```scala 175 | import ratelimiter4s.zio.ZIORateLimiter._ 176 | 177 | val rateLimitedService = limit(getArtist _, rateLimiter) 178 | ``` 179 | 180 | Calling this rate limited return an `Task[ImageType]` result which is shorthand for `ZIO[Any, Throwable, ImageType]` 181 | 182 | ```scala 183 | val result : Task[ImageType] = rateLimitedService("https://samples.clarifai.com/metro-north.jpg") 184 | ``` 185 | 186 | The result is `Task` type because the method can fail due to other reasons apart from `RequestNotPermitted`. If that is not the case , we can use `pure`. 187 | 188 | ```scala 189 | val result : IO[RequestNotPermitted, ImageType] = rateLimitedService.pure("https://samples.clarifai.com/metro-north.jpg") 190 | ``` 191 | 192 | `IO[RequestNotPermitted, ImageType]` which is a shorthand for `ZIO[Any, RequestNotPermitted, ImageType]` clearly indicates that the failure can only be caused by a `RequestNotPermitted` error. 193 | 194 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------