apply gofmt

This commit is contained in:
Soam Vasani
2016-09-01 16:32:32 -07:00
parent 2c872642f3
commit 8c56818e0a
8 changed files with 69 additions and 77 deletions
+5 -5
View File
@@ -17,15 +17,15 @@ limitations under the License.
package router
import (
"errors"
"log"
"net/http"
"net/http/httputil"
"errors"
"net/url"
)
type functionHandler struct {
fmap *functionServiceMap
fmap *functionServiceMap
poolManagerUrl string
function
}
@@ -36,13 +36,13 @@ func (*functionHandler) getServiceForFunction() (*url.URL, error) {
func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *http.Request) {
serviceUrl, err := fh.fmap.lookup(&fh.function)
if (err != nil) {
if err != nil {
// Cache miss: request the Pool Manager to make a new service.
serviceUrl, poolErr := fh.getServiceForFunction()
if (poolErr != nil) {
if poolErr != nil {
// now we're really screwed
log.Printf("Failed to get service for function (%v,%v): %v",
fh.function.name, fh.function.uid, poolErr);
fh.function.name, fh.function.uid, poolErr)
responseWriter.WriteHeader(500) // TODO: make this smarter based on the actual error
return
}
+6 -7
View File
@@ -18,14 +18,13 @@ package router
import (
"log"
"testing"
"net/http"
// "net/http/httputil"
"testing"
// "net/http/httputil"
"net/http/httptest"
"net/url"
)
func createBackendService(testResponseString string) *url.URL {
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(testResponseString))
@@ -39,10 +38,10 @@ func createBackendService(testResponseString string) *url.URL {
}
/*
1. Create a service at some URL
2. Add it to the function service map
3. Create a http server with some trigger url pointed at function handler
4. Send a request to that server, ensure it reaches the first service.
1. Create a service at some URL
2. Add it to the function service map
3. Create a http server with some trigger url pointed at function handler
4. Send a request to that server, ensure it reaches the first service.
*/
func TestFunctionProxying(t *testing.T) {
testResponseString := "hi"
+17 -20
View File
@@ -23,11 +23,12 @@ import (
)
type requestType int
const (
LOOKUP requestType = iota // lookup the map
ASSIGN // assign function
NEXT_GEN // increment current generation
SWEEP // delete all but the current generation
LOOKUP requestType = iota // lookup the map
ASSIGN // assign function
NEXT_GEN // increment current generation
SWEEP // delete all but the current generation
)
type functionServiceMapResponse struct {
@@ -47,12 +48,12 @@ type functionServiceMapEntry struct {
type functionServiceMap struct {
// map (funcname, uid) -> url
svc map[function]functionServiceMapEntry
svc map[function]functionServiceMapEntry
currentGeneration uint64
requestChannel chan *functionServiceMapRequest
requestChannel chan *functionServiceMapRequest
}
func makeFunctionServiceMap() (*functionServiceMap) {
func makeFunctionServiceMap() *functionServiceMap {
fmap := &functionServiceMap{}
fmap.requestChannel = make(chan *functionServiceMapRequest)
fmap.svc = make(map[function]functionServiceMapEntry)
@@ -62,18 +63,18 @@ func makeFunctionServiceMap() (*functionServiceMap) {
func (fmap *functionServiceMap) functionServiceMapWork() {
for {
req := <- fmap.requestChannel
req := <-fmap.requestChannel
switch req.requestType {
case LOOKUP:
e, present := fmap.svc[req.function]
if present {
req.responseChannel <- functionServiceMapResponse{ serviceUrl: e.serviceUrl }
req.responseChannel <- functionServiceMapResponse{serviceUrl: e.serviceUrl}
} else {
req.responseChannel <- functionServiceMapResponse{ error: errors.New("not found") }
req.responseChannel <- functionServiceMapResponse{error: errors.New("not found")}
}
case ASSIGN:
fmap.svc[req.function] =
functionServiceMapEntry{ serviceUrl: req.serviceUrl, generation: fmap.currentGeneration }
functionServiceMapEntry{serviceUrl: req.serviceUrl, generation: fmap.currentGeneration}
// no response
case NEXT_GEN:
fmap.currentGeneration++
@@ -88,10 +89,9 @@ func (fmap *functionServiceMap) functionServiceMapWork() {
func (fmap *functionServiceMap) lookup(f *function) (*url.URL, error) {
respChannel := make(chan functionServiceMapResponse)
fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, requestType: LOOKUP, responseChannel: respChannel }
fmap.requestChannel <- &functionServiceMapRequest{function: *f, requestType: LOOKUP, responseChannel: respChannel}
resp := <-respChannel
if (resp.error != nil) {
if resp.error != nil {
return nil, resp.error
} else {
return &resp.serviceUrl, nil
@@ -99,16 +99,13 @@ func (fmap *functionServiceMap) lookup(f *function) (*url.URL, error) {
}
func (fmap *functionServiceMap) assign(f *function, serviceUrl *url.URL) {
fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN }
fmap.requestChannel <- &functionServiceMapRequest{function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN}
}
func (fmap *functionServiceMap) nextGen() {
fmap.requestChannel <-
&functionServiceMapRequest{ requestType: NEXT_GEN }
fmap.requestChannel <- &functionServiceMapRequest{requestType: NEXT_GEN}
}
func (fmap *functionServiceMap) sweep() {
fmap.requestChannel <-
&functionServiceMapRequest{ requestType: SWEEP }
fmap.requestChannel <- &functionServiceMapRequest{requestType: SWEEP}
}
+6 -6
View File
@@ -17,31 +17,31 @@ limitations under the License.
package router
import (
"testing"
"net/url"
"testing"
)
func TestFunctionServiceMap(t *testing.T) {
m := makeFunctionServiceMap()
fn := &function{ name: "foo", uid: "012" }
fn := &function{name: "foo", uid: "012"}
u, err := url.Parse("/foo012")
if (err != nil) {
if err != nil {
t.Errorf("can't parse url")
}
m.assign(fn, u)
v, err := m.lookup(fn)
if (err != nil) {
if err != nil {
t.Errorf("Lookup error: %v", err)
}
if (*v != *u) {
if *v != *u {
t.Errorf("Expected %#v, got %#v", u, v)
}
fn.name = "bar"
_, err2 := m.lookup(fn)
if (err2 == nil) {
if err2 == nil {
t.Errorf("No error on missing entry")
}
}
+5 -5
View File
@@ -17,10 +17,10 @@ limitations under the License.
package router
import (
"net/http"
"sync/atomic"
"github.com/gorilla/mux"
"log"
"net/http"
"sync/atomic"
)
//
@@ -32,8 +32,8 @@ type mutableRouter struct {
router atomic.Value // mux.Router
}
func NewMutableRouter(handler *mux.Router) (*mutableRouter) {
mr := mutableRouter{};
func NewMutableRouter(handler *mux.Router) *mutableRouter {
mr := mutableRouter{}
mr.router.Store(handler)
return &mr
}
@@ -42,7 +42,7 @@ func (mr *mutableRouter) ServeHTTP(responseWriter http.ResponseWriter, request *
// Atomically grab the underlying mux router and call it.
routerValue := mr.router.Load()
router, ok := routerValue.(*mux.Router)
if (!ok) {
if !ok {
log.Panic("Invalid router type")
}
router.ServeHTTP(responseWriter, request)
+4 -5
View File
@@ -17,10 +17,10 @@ limitations under the License.
package router
import (
"testing"
"net/http"
"github.com/gorilla/mux"
"log"
"net/http"
"testing"
"time"
)
@@ -40,17 +40,16 @@ func startServer(mr *mutableRouter) {
http.ListenAndServe(":3333", mr)
}
func spamServer(quit chan bool) {
i := 0
for {
select {
case <- quit:
case <-quit:
break
default:
i = i + 1
resp, err := http.Get("http://localhost:3333")
if (err != nil) {
if err != nil {
log.Panicf("failed to make get request %v: %v", i, err)
}
resp.Body.Close()
+6 -8
View File
@@ -37,20 +37,19 @@ Its job is to:
*/
package router
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
flag "github.com/ogier/pflag"
"net/http"
)
type (
function struct {
name string
uid string
uid string
}
httptrigger struct {
@@ -59,19 +58,18 @@ type (
}
options struct {
port int
port int
poolManagerUrl string
controllerUrl string
controllerUrl string
//...
}
)
// request url ---[mux]---> function(name,uid) ----[fmap]----> k8s service url
// request url ---[trigger]---> function(name, deployment) ----[deployment]----> function(name, uid) ----[pool mgr]---> k8s service url
func router(httpTriggerSet *HTTPTriggerSet) (*mutableRouter) {
func router(httpTriggerSet *HTTPTriggerSet) *mutableRouter {
muxRouter := mux.NewRouter()
mr := NewMutableRouter(muxRouter)
httpTriggerSet.subscribeRouter(mr)
@@ -84,7 +82,7 @@ func server(port int, httpTriggerSet *HTTPTriggerSet) {
http.ListenAndServe(url, mr)
}
func getOptions() (*options) {
func getOptions() *options {
options := &options{}
flag.IntVar(&options.port, "port", 80, "Port to listen on")
+6 -7
View File
@@ -1,31 +1,30 @@
package router
import (
"net/http"
"log"
"io/ioutil"
"log"
"net/http"
)
func testRequest(targetUrl string, expectedResponse string) {
resp, err := http.Get(targetUrl)
if (err != nil) {
if err != nil {
log.Panicf("failed to make get request: %v", err)
}
defer resp.Body.Close()
if (resp.StatusCode != 200) {
if resp.StatusCode != 200 {
log.Panicf("response status: %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if (err != nil) {
if err != nil {
log.Panic("failed to read response")
}
bodyStr := string(body)
log.Printf("Server responded with %v", bodyStr)
if (bodyStr != expectedResponse) {
if bodyStr != expectedResponse {
log.Panic("Unexpected response")
}
}