Skip to content

Instantly share code, notes, and snippets.

View kasvith's full-sized avatar
Working from home

Kasun Vithanage kasvith

Working from home
View GitHub Profile
kasvith / vscode-extensions.txt
Created January 15, 2023 15:17
My VSCode extensions
code --install-extension 13xforever.language-x86-64-assembly
code --install-extension 42Crunch.vscode-openapi
code --install-extension aaron-bond.better-comments
code --install-extension AbhijoyBasak.nestjs-files
code --install-extension adpyke.vscode-sql-formatter
code --install-extension ahmadalli.vscode-nginx-conf
code --install-extension ahmadawais.shades-of-purple
code --install-extension akamud.vscode-theme-onedark
code --install-extension alefragnani.Bookmarks
code --install-extension amandeepmittal.pug
kasvith / list.ex
Last active January 23, 2021 10:58
Dave Thomas Programming Elixir Exercise: ListsAndRecursion-2 without Kernel.max
defmodule MyList do
def max([]), do: nil
def max([head | tail]), do: findmax(head, head, tail)
defp findmax(current, _next, [head | tail]) do
if head > current, do: findmax(head, current, tail), else: findmax(current, current, tail)
defp findmax(current, next, []) do
if current > next, do: current, else: next
package main
import (
_ "fmt"
func main() {
FROM golang:1.13 AS builder
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o agent .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/agent .
ENTRYPOINT [ "/root/agent" ]
'use strict'
const httpStatus = require('http-status')
const errorCodes = require('./errorCodes')
module.exports = function APIError (
status = httpStatus.INTERNAL_SERVER_ERROR,
errorCode = errorCodes.UnkownError,
errors = null
) {
// HealthCheck pings the backends and update the status
func (s *ServerPool) HealthCheck() {
for _, b := range s.backends {
status := "up"
alive := isBackendAlive(b.URL)
if !alive {
status = "down"
log.Printf("%s [%s]\n", b.URL, status)
// isAlive checks whether a backend is Alive by establishing a TCP connection
func isBackendAlive(u *url.URL) bool {
timeout := 2 * time.Second
conn, err := net.DialTimeout("tcp", u.Host, timeout)
if err != nil {
log.Println("Site unreachable, error: ", err)
return false
_ = conn.Close() // close it, we dont need to maintain this connection
return true
// lb load balances the incoming request
func lb(w http.ResponseWriter, r *http.Request) {
attempts := GetAttemptsFromContext(r)
if attempts > 3 {
log.Printf("%s(%s) Max attempts reached, terminating\n", r.RemoteAddr, r.URL.Path)
http.Error(w, "Service not available", http.StatusServiceUnavailable)
peer := serverPool.GetNextPeer()
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
log.Printf("[%s] %s\n", serverUrl.Host, e.Error())
retries := GetRetryFromContext(request)
if retries < 3 {
select {
case <-time.After(10 * time.Millisecond):
ctx := context.WithValue(request.Context(), Retry, retries+1)
proxy.ServeHTTP(writer, request.WithContext(ctx))
// lb load balances the incoming request
func lb(w http.ResponseWriter, r *http.Request) {
peer := serverPool.GetNextPeer()
if peer != nil {
peer.ReverseProxy.ServeHTTP(w, r)
http.Error(w, "Service not available", http.StatusServiceUnavailable)