Skip to content

Instantly share code, notes, and snippets.

View barinek's full-sized avatar

Barinek barinek

  • Initial Capacity
  • Boulder, CO
View GitHub Profile
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)
@barinek
barinek / guice.rb
Created September 4, 2012 15:35
Guice-ish
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
@barinek
barinek / about.md
Created August 30, 2011 02:06 — forked from jasonrudolph/about.md
Programming Achievements: How to Level Up as a Developer
@barinek
barinek / noop handler
Created March 24, 2011 13:01
noop handler jetty handler
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;
@barinek
barinek / nginx
Created October 26, 2010 03:05 — forked from thewebfellas/nginx
#! /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