Skip to content

Instantly share code, notes, and snippets.

@spacejam
Last active March 29, 2017 21:40
Show Gist options
  • Save spacejam/01ef6707e69c8dda0d5201a139561b03 to your computer and use it in GitHub Desktop.
Save spacejam/01ef6707e69c8dda0d5201a139561b03 to your computer and use it in GitHub Desktop.

atomic

import "sync/atomic"

    func AddInt32(addr *int32, delta int32) (new int32)
    func AddInt64(addr *int64, delta int64) (new int64)
    func AddUint32(addr *uint32, delta uint32) (new uint32)
    func AddUint64(addr *uint64, delta uint64) (new uint64)
    func AddUintptr(addr *uintptr, delta uintptr) (new uintptr)
    func CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)
    func CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)
    func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapped bool)
    func CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)
    func CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)
    func CompareAndSwapUintptr(addr *uintptr, old, new uintptr) (swapped bool)
    func LoadInt32(addr *int32) (val int32)
    func LoadInt64(addr *int64) (val int64)
    func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
    func LoadUint32(addr *uint32) (val uint32)
    func LoadUint64(addr *uint64) (val uint64)
    func LoadUintptr(addr *uintptr) (val uintptr)
    func StoreInt32(addr *int32, val int32)
    func StoreInt64(addr *int64, val int64)
    func StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
    func StoreUint32(addr *uint32, val uint32)
    func StoreUint64(addr *uint64, val uint64)
    func StoreUintptr(addr *uintptr, val uintptr)
    func SwapInt32(addr *int32, new int32) (old int32)
    func SwapInt64(addr *int64, new int64) (old int64)
    func SwapPointer(addr *unsafe.Pointer, new unsafe.Pointer) (old unsafe.Pointer)
    func SwapUint32(addr *uint32, new uint32) (old uint32)
    func SwapUint64(addr *uint64, new uint64) (old uint64)
    func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)

json rest endpoint

import (                                                                                                                                                                                       
  "encoding/json"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  "net/http"
  "time"
)

type Request struct {
  Something uint64 `json:"something"`
}

type Response struct {
  Something string `json:"something"`
}

func serve(port int) {                                                                                                                
  mux := http.NewServeMux()                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                      
  static := http.FileServer(http.Dir("/path/to/static"))                                                                                                                                                 
  mux.Handle("/static/files", static)      
  
  mux.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {  
    req := &Request{}
    err := json.NewDecoder(r.Body).Decode(req)
    if err != nil {
      http.Error(w, err.Error(), http.StatusInternalServerError)
      return
    }   
    res := Response { Something: "yoooo" }
    rawRes, err := json.Marshal(s)                                                                                                                                              
    if err != nil {                                                                                                                                                                            
      http.Error(w, err, http.StatusInternalServerError)
      return
    }                                                                                                                                                                                            
    fmt.Fprint(w, string(rawRes))
  })   
  
  log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), mux))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
}

func post(url string) (*Response, error) {
    client := http.Client{                                                                                                                                                                     
      Timeout: time.Second * 5,                                                                                                                                                                    
    }         
    
    reqBytes := bytes.Buffer{}
    req := Request{ Something: 66 }
    json.NewEncoder(reqBytes).Encode(req)
   
    resp, err := client.Post(url, "application/json; charset=utf-8", reqBytes)                                                                                                                                          
    if err != nil {                                                                                                                                                                            
      return nil, err                                                                                                                                                                             
    }                                                                                                                                                                                          
    defer resp.Body.Close()                                                                                                                                                                    
                                                                                                                                                                                               
    body, err := ioutil.ReadAll(resp.Body)                                                                                                                                                     
    if err != nil {                                                                                                                                                                            
      return nil, err                                                                                                                                                                               
    }                                                                                                                                                                                                                                                                                                                                     
    response := &Response{}                                                                                                                                                             
    err = json.Unmarshal(body, response)                                                                                                                                                             
    if err != nil {                                                                                                                                                                            
      return nil, err                                                                                                                                                                               
    }
    
    return response, nil
}

func getWithRetries(uri string) (*Response, error) {                                                                                                                                                                                                                                                                     
  var (                                                                                                                                                                                        
    backoff = 1                                                                                                                                                                                
    client  = http.Client{                                                                                                                                                                     
      Timeout: time.Second * 5,                                                                                                                                                                    
    }                                                                                                                                                                                          
    response = &Response{}                                                                                                                                                               
    outerErr    error                                                                                                                                                                          
                                                                                                                                                                                               
    get = func() ([]byte, int, error) {                                                                                                                                              
      resp, err := client.Get(uri)                                                                                                                                                             
      if err != nil {                                                                                                                                                                          
        return nil, 0, err                                                                                                                                                                     
      }                                                                                                                                                                                        
      defer resp.Body.Close()                                                                                                                                                                  
      blob, err := ioutil.ReadAll(resp.Body)                                                                                                                                                   
      return blob, resp.StatusCode, err                                                                                                                                                        
    }                                                                                                                                                                                          
  )                                                                                                                                                                                            
  for retries := 0; retries < RPC_RETRIES; retries++ {                                                                                                                                         
    for {                                                                                                                                                                                      
      blob, code, err := get()                                                                                                                                  
      if err != nil {                                                                                                                                                                          
        outerErr = err                                                                                                                                                                         
        break                                                                                                                                                                                  
      }                                                                                                                                                                                                                                                                                                                                                                             
      err = json.Unmarshal(blob, response)                                                                                                                                                  
      if err == nil {                                                                                                                                                                          
        return response, nil                                                                                                                                                                
      }                                                                                                                                                                                        
      log.Error(err)                                                                                                                                                                           
    }                                                                                                                                                                                                                                                                                                                                    
    time.Sleep(time.Duration(backoff) * time.Second) 
    
    // truncated exponential backoff
    backoff = int(math.Min(float64(backoff<<1), 8))                                                                                                                                            
  }                                                                                                                                                                                            
  return nil, outerErr                                                                                                                                                                         
}    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment