3 secrets to build web APIs in Go Matt Aimonetti - splice.com - @mattetti

Matt Aimonetti

golangbootcamp.com

splice: Go, Ruby, JS, Obj-C, C#, C++, C

Splice

Splice

Architecture clients

APIs / core logic

web rendering

Architecture Obj-C

C Go

Web API: Synchronization File upload File download Notifications

C#

Architecture

file parsing/analysis (sessions, media) deduplication/storage intermediate representation

Architecture

internal/private API (application logic)

“public” API (states retrieval/change)

Architecture

Message Bus HTTP

Long running async tasks

Go vs Ruby

20-100x faster to run 10-20% slower to write 60-80% less maintenance required batteries included

• • •

Deployment Routing Monitoring

Deployment

Zero downtime deployments bitbucket.org/splice/go.grace

g h t i w l u f care

s e n i t orou

Deployment

Version Control

CI

Storage

Deployment

Deployment

current/api

symlink

2014-06-23-183000/api

2014-06-23-204500/api

Deployment

current/api

symlink

2014-06-23-183000/api

2014-06-23-204500/api

$ kill -USR2 {api PID}

Routing var V42Routes = Endpoints{ // create Ableton Live session { Verb: "POST", Path: "/v42/live/sessions", Handler: handlers.AlsUpload, RequiredParams: handlers.AlsUploadReqParams, Auth: splice.ClientAuth, }, // create Logic session { Verb: "POST", Path: "/v42/logic/sessions", Handler: handlers.LogicUpload, RequiredParams: handlers.LogicUploadReqParams, Auth: splice.ClientAuth, }, }

Routing

package router   type Endpoint struct { Verb string Path string RequiredParams []string Auth func(*splice.ReqEnv, bool) bool Handler splice.Handler }

Routing type Endpoints []Endpoint   // Activate all the endpoint API routes.

func (es Endpoints) Activate(p *pat.PatternServeMux) { for _, e := range es { // [..] set authentication calls // [..] check param requirements

handler := splice.ReqWrap(e.Handler, e.Auth) p.Add(e.Verb, e.Path, handler) // [..] collect all CORS verbs accepted for this endpoint so we can create OPTIONS routes.

} }

Routing func ReqWrap(handler *Handler, checkAuthFn func(*ReqEnv, bool) bool) http.Handler { !   return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() defer reportRequest(req, start, handler.Metrics) reqUuid := uuid.GenUUID() req := &ReqEnv{ ReqUuid: reqUuid, Request: r, Response: &statusResponseWriter{w, 0}, } // [..] set headers (content type, req ID, build) if !checkAuthFn(req, checkAuth) { return }

! ! !

Routing ! ! !

// We must return a 400 and stop here if there was a problem // parsing the request. err := req.ParseParams() if err != nil { // […]return 400 return } // convert a panic into a 500 defer req.handlePanic() handler.Handle(req) })

}

Monitoring

Monitor all the things!

runtime.ReadMemStats http://golang.org/pkg/runtime/#MemStats

Lessons learned

low memory usage

Lessons learned

detect issues

Lessons learned

detect issues

Low memory usage

use streaming io.Reader/io.Writer io.Copy

Encoder/Decoder instead of ReadAll and Unmarshal

Monitoring

Monitor all the things! Per endpoint: • # of requests • Status codes • Response times

@mattetti http://matt.aimonetti.net

go at splice - gosf

to build web APIs in Go. Matt Aimonetti - splice.com - @ ... Architecture. Web API: Synchronization. File upload. File download. Notifications. Obj-C. C#. C. Go ...

6MB Sizes 1 Downloads 98 Views

Recommend Documents

pdf splice
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. pdf splice.

comma splice worksheet pdf
Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. comma splice worksheet pdf. comma splice worksheet pdf. Open.

Comma Splice/Run-On.pdf
Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Comma Splice/Run-On.pdf. Comma Splice/Ru

ROOM SHEET SPLICE 2014 .pdf
Page 1 of 1 !!!!Articulate!project!space!3!0!12!January!2014. A project by five artists working in one space: Beata Geyer, Christine. Myerscough, Margaret Roberts ...

Loop Recognition in C++/Java/Go/Scala - Research at Google
ploys many language features, in particular, higher-level data structures (lists ... The benchmark points to very large differences in all examined dimensions of ...

Splice lean design canvas for media.pdf
A lean design canvas. for media startups. Page 1 of 3. Page 2 of 3. Page 3 of 3. Splice lean design canvas for media.pdf. Splice lean design canvas for media.

Zoom In Episode Splice 2010 in Streaming HD ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Zoom In Episode Splice 2010 in Streaming HD.MP4_______________________.pdf. Zoom In Episode Splice 2010 in S

Go santa go
Journey to Destruction in Steinbeck's Flightand London's To Build a Fire. Notmany people haveto face death in thecold ... Biology pdf 2015.Howdo they do its10e17.Go santa go.Usatop 40 single. charts.American black cop.3.3.5 no install.61) Fortunately

My go go
Kikiminajmaid.Theadventure of puss boots.The. legend ofzeldatwilight princess. ... go.One piece 607 [email protected] 2013 pdf.SECRETS OF THEBIBLE ...

Go Quick Reference Go Quick Reference Go Quick Reference - GitHub
Structure - Package package mylib func CallMeFromOutside. Format verbs. Simpler than Cās. MOAR TABLE package anothermain import (. "fmt". ) func main() {.

GO(P)
The Secretar5r, Kerala Public Service Commission (with C.L). The Registrar, University ... 'fhe Secretary, Kerala State Electricity l3oard (with C.l,). 'lhe Managing ...

GO 2759 Lifting of Ban on Promotions at Zonal Level - ap teacher.net
Jul 26, 2014 - GOVERNMENT OF ANDHRA PRADESH. ABSTRACT. A.P. Reorganization Act, 2014 – Ban imposed on all promotions, appointments ...

man-154\at-t-go-phone-customer-service-number.pdf
man-154\at-t-go-phone-customer-service-number.pdf. man-154\at-t-go-phone-customer-service-number.pdf. Open. Extract. Open with. Sign In. Main menu.

07-26-13 Modulars go in at Pleasant Valley, Daybreak, Tukes.pdf ...
PO Box 200 • Battle Ground, WA 98604-0200 • Office Location: 11104 NE 149th Street • Brush Prairie, Washington. District Switchboard: (360) 885-5300. July 26 ...

07-26-13 Modulars go in at Pleasant Valley, Daybreak, Tukes.pdf ...
Page 2 of 2. 07-26-13 Modulars go in at Pleasant Valley, Daybreak, Tukes.pdf. 07-26-13 Modulars go in at Pleasant Valley, Daybreak, Tukes.pdf. Open. Extract.