HTTP with backup server(s)

Linux Build Status Windows Build status CRAN RStudio mirror downloads

An extremely simple fallback algorithm, to query a backup HTTP server, if the first choice HTTP server is down.

The algorithm works fine if a server is completely down, but might fail if the network is unreliable and/or slow. Consider this before you use this package. Suggestions for improvements are welcome.

Installation

library(devtools)
install_github("metacran/spareserver")

Usage

You can define services, and will use multiple servers. Each server has a default priority, and the servers are tried in the order of decreasing priority.

library(spareserver)
#> Loading required package: methods
add_service("cran-packages",
  server("http://cran.r-project.org/web/packages", priority = 10),
  server("http://cran.rstudio.com/web/packages", priority = 5)
)

Then you can make a robust query. Here we use the httr package.

library(httr)
spare_q("cran-packages", "/ggplot2/index.html", GET)
#> Response [http://cran.r-project.org/web/packages/ggplot2/index.html]
#>   Date: 2015-05-07 12:42
#>   Status: 200
#>   Content-Type: text/html
#>   Size: 26.4 kB
#> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3....
#> <html xmlns="http://www.w3.org/1999/xhtml">
#> <head>
#> <title>CRAN - Package ggplot2</title>
#> <link rel="stylesheet" type="text/css" href="../../CRAN_web.css" />
#> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
#> <meta name="citation_title" content="An Implementation of the Grammar of...
#> <meta name="citation_author" content="Hadley Wickham" />
#> <meta name="citation_author" content="Winston Chang" />
#> <meta name="citation_publication_date" content="2015-03-17" />
#> ...
remove_service("cran-packages")

In the next example, the first server is unreachable, so we will use the fallback server.

add_service("cran-packages",
  server("http://192.0.2.1/foobar", priority = 10),
  server("http://cran.rstudio.com/web/packages", priority = 5)
)
spare_q("cran-packages", "/ggplot2/index.html", GET)
#> Response [http://cran.rstudio.com/web/packages/ggplot2/index.html]
#>   Date: 2015-05-07 12:42
#>   Status: 200
#>   Content-Type: text/html
#>   Size: 26.4 kB
#> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3....
#> <html xmlns="http://www.w3.org/1999/xhtml">
#> <head>
#> <title>CRAN - Package ggplot2</title>
#> <link rel="stylesheet" type="text/css" href="../../CRAN_web.css" />
#> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
#> <meta name="citation_title" content="An Implementation of the Grammar of...
#> <meta name="citation_author" content="Hadley Wickham" />
#> <meta name="citation_author" content="Winston Chang" />
#> <meta name="citation_publication_date" content="2015-03-17" />
#> ...
remove_service("cran-packages")

Algorithm

If all servers are down, then we start over, with fifty times bigger timeout. We only do two rounds currently, and the statuses are reset between rounds.

License

MIT