This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
| open class DiscoveryClient(val mapper: ObjectMapper, val template: RestTemplate) { | |
| private fun <E> List<E>.random(random: java.util.Random): E? = if (size > 0) get(random.nextInt(size)) else null | |
| fun getUrl(appId: String): String? { | |
| val endpoint = System.getenv("DISCOVERY_SERVER_ENDPOINT") | |
| val response = template.get("$endpoint/discovery/apps", "application/json", BasicNameValuePair("appId", appId)) | |
| val instances: List<InstanceInfo> = mapper.readValue(response, object : TypeReference<List<InstanceInfo>>() {}) | |
| return when { | |
| instances.isEmpty() -> null | |
| else -> instances.random(Random())!!.url |
| open class ProjectClient(val mapper: ObjectMapper, val template: RestTemplate) { | |
| open fun getProject(projectId: Long): ProjectInfo? { | |
| val endpoint = System.getenv("REGISTRATION_SERVER_ENDPOINT") | |
| val response = template.get("$endpoint/project", "application/vnd.appcontinuum.v2+json", BasicNameValuePair("projectId", projectId.toString())) | |
| when { | |
| response.isBlank() -> return null | |
| else -> return mapper.readValue(response, ProjectInfo::class.java) | |
| } | |
| } |
| class InstanceDataGateway(val pool: JedisPool, val timeToLiveInMillis: Long) { | |
| fun heartbeat(appId: String, url: String): InstanceRecord { | |
| val resource = pool.resource | |
| resource.psetex("$appId:$url", timeToLiveInMillis, url) | |
| resource.close() | |
| return InstanceRecord(appId, url) | |
| } | |
| fun findBy(appId: String): List<InstanceRecord> { | |
| val list = mutableListOf<InstanceRecord>() |
| class CircuitBreaker(val timeoutInMillis: Long = 200, val maxFailures: Int = 3, val retryIntervalInMillis: Long = 300) { | |
| fun <T> withCircuitBreaker(function: () -> T, fallback: () -> T): T { | |
| if (open() && !shouldRetry()) return fallback() | |
| val future = Executors.newSingleThreadExecutor().submit(function) | |
| return try { | |
| future.get(timeoutInMillis, TimeUnit.MILLISECONDS).apply { |
| fun <T> create(connection: Connection, sql: String, id: (Long) -> T, vararg params: Any): T { | |
| return connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS).use { statement -> | |
| for (i in params.indices) { | |
| val param = params[i] | |
| val parameterIndex = i + 1 | |
| when (param) { | |
| is String -> statement.setString(parameterIndex, param) | |
| is Int -> statement.setInt(parameterIndex, param) | |
| is Long -> statement.setLong(parameterIndex, param) |
| require "singleton" | |
| require "i18n" | |
| require "active_support" | |
| require "active_support/core_ext/string" | |
| module Guice | |
| def inject(*klass_symbols) | |
| @injections = Hash.new unless @injections | |
| @injections[self.name] = klass_symbols | |
| end |
This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
| package com.barinek; | |
| import org.eclipse.jetty.server.Request; | |
| import org.eclipse.jetty.server.Server; | |
| import org.eclipse.jetty.server.handler.AbstractHandler; | |
| import org.eclipse.jetty.server.nio.SelectChannelConnector; | |
| import org.eclipse.jetty.util.thread.QueuedThreadPool; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServletRequest; |
| #! /bin/sh | |
| # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and | |
| # run 'sudo update-rc.d nginx defaults', or use the appropriate command on your | |
| # distro. | |
| # | |
| # Author: Ryan Norbauer <ryan.norbauer@gmail.com> | |
| # Modified: Geoffrey Grosenbach http://topfunky.com | |
| # |
| rvm_archflags="-arch x86_64" | |
| rvm 1.8.7-p174@mahan |