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
// SetAlive for this backend
func (b *Backend) SetAlive(alive bool) {
b.mux.Lock()
b.Alive = alive
b.mux.Unlock()
}
// IsAlive returns true when backend is alive
func (b *Backend) IsAlive() (alive bool) {
b.mux.RLock()
// GetNextPeer returns next active peer to take a connection
func (s *ServerPool) GetNextPeer() *Backend {
// loop entire backends to find out an Alive backend
next := s.NextIndex()
l := len(s.backends) + next // start from next and move a full cycle
for i := next; i < l; i++ {
idx := i % len(s.backends) // take an index by modding
if s.backends[idx].IsAlive() { // if we have an alive backend, use it and store if its not the original one
if i != next {
atomic.StoreUint64(&s.current, uint64(idx))
type Backend struct {
URL *url.URL
Alive bool
mux sync.RWMutex
ReverseProxy *httputil.ReverseProxy
}
//+build ignore
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
func FormatByteSlice(sl []byte) string {
builder := strings.Builder{}
for _, v := range sl {
builder.WriteString(fmt.Sprintf("%d,", int(v)))
}
return builder.String()
}
var packageTemplate = template.Must(template.New("").Funcs(map[string]interface{}{"conv": FormatByteSlice}).Parse(`// Code generated by go generate; DO NOT EDIT.
// generated using files from resources directory
// DO NOT COMMIT this file
package box
func init(){
{{- range $name, $file := . }}
resources.Add("{{ $name }}", []byte{ {{ conv $file }} })
{{- end }}
}
resources := make(map[string][]byte)
err := filepath.Walk("resources", func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("Error :", err)
return err
}
relativePath := filepath.ToSlash(strings.TrimPrefix(path, "resources"))
if info.IsDir() {
log.Println(path, "is a directory, skipping...")
return nil
@kasvith
kasvith / defaults.go
Last active July 26, 2019 14:27
defaults.go
//go:generate go run gen.go
package box
type resourceBox struct {
storage map[string][]byte
}
func newResourceBox() *resourceBox {
return &resourceBox{storage: make(map[string][]byte)}
// define default values, other resources in default package
package defaults
var ConfigTemplate = []byte(`
// content goes here
`)
// In some another package
func somefunc(){
ioutil.WriteFile("/path/to/dest", defaults.ConfigTemplate, os.ModePerm)
}
@kasvith
kasvith / api_params.yaml
Created July 17, 2019 06:01
Reference for api_params.yaml
environments: # Define environments as a list
- name: dev # Name of the environment
gatewayEnvironments: # define gateway environments
- "Production and Sandbox" # List gateways
- "Another Gateway"
certs: # define certificates for endpoints as a list
- hostName: 'https://dev.wso2.com' # hostname for the certificate
alias: 'Dev Certificate' # Alias for the certificate
path: '~/certs/dev.pem' # path to public certificate file
endpoints: # Define endpoints for environment