From 796a9810a4d91bdb94fc22b6cf87c87b5c0ac2ad Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Fri, 9 Sep 2016 14:39:31 -0700 Subject: [PATCH 1/2] Capitalize type names to prepare for moving them to the top level package --- router/functionHandler.go | 8 ++++---- router/functionHandler_test.go | 4 ++-- router/functionServiceMap.go | 18 +++++++++--------- router/functionServiceMap_test.go | 4 ++-- router/httpTriggers.go | 8 ++++---- router/router.go | 16 ++++++++-------- router/router_test.go | 4 ++-- 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/router/functionHandler.go b/router/functionHandler.go index 8b93d89d..bb16c315 100644 --- a/router/functionHandler.go +++ b/router/functionHandler.go @@ -27,7 +27,7 @@ import ( type functionHandler struct { fmap *functionServiceMap poolManagerUrl string - function + Function } func (*functionHandler) getServiceForFunction() (*url.URL, error) { @@ -35,20 +35,20 @@ func (*functionHandler) getServiceForFunction() (*url.URL, error) { } func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *http.Request) { - serviceUrl, err := fh.fmap.lookup(&fh.function) + serviceUrl, err := fh.fmap.lookup(&fh.Function) if err != nil { // Cache miss: request the Pool Manager to make a new service. serviceUrl, poolErr := fh.getServiceForFunction() 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 } // add it to the map - fh.fmap.assign(&fh.function, serviceUrl) + fh.fmap.assign(&fh.Function, serviceUrl) } // Proxy off our request to the serviceUrl, and send the response back. diff --git a/router/functionHandler_test.go b/router/functionHandler_test.go index 72bfa1e7..fe794527 100644 --- a/router/functionHandler_test.go +++ b/router/functionHandler_test.go @@ -48,11 +48,11 @@ func TestFunctionProxying(t *testing.T) { backendURL := createBackendService(testResponseString) log.Printf("Created backend svc at %v", backendURL) - fn := &function{name: "foo", uid: "xxx"} + fn := &Function{Name: "foo", Uid: "xxx"} fmap := makeFunctionServiceMap() fmap.assign(fn, backendURL) - fh := &functionHandler{fmap: fmap, function: *fn} + fh := &functionHandler{fmap: fmap, Function: *fn} functionHandlerServer := httptest.NewServer(http.HandlerFunc(fh.handler)) fhURL := functionHandlerServer.URL diff --git a/router/functionServiceMap.go b/router/functionServiceMap.go index d667de67..780083be 100644 --- a/router/functionServiceMap.go +++ b/router/functionServiceMap.go @@ -36,7 +36,7 @@ type functionServiceMapResponse struct { error } type functionServiceMapRequest struct { - function + Function serviceUrl url.URL requestType responseChannel chan<- functionServiceMapResponse @@ -48,7 +48,7 @@ type functionServiceMapEntry struct { type functionServiceMap struct { // map (funcname, uid) -> url - svc map[function]functionServiceMapEntry + svc map[Function]functionServiceMapEntry currentGeneration uint64 requestChannel chan *functionServiceMapRequest } @@ -56,7 +56,7 @@ type functionServiceMap struct { func makeFunctionServiceMap() *functionServiceMap { fmap := &functionServiceMap{} fmap.requestChannel = make(chan *functionServiceMapRequest) - fmap.svc = make(map[function]functionServiceMapEntry) + fmap.svc = make(map[Function]functionServiceMapEntry) go fmap.functionServiceMapWork() return fmap } @@ -66,14 +66,14 @@ func (fmap *functionServiceMap) functionServiceMapWork() { req := <-fmap.requestChannel switch req.requestType { case LOOKUP: - e, present := fmap.svc[req.function] + e, present := fmap.svc[req.Function] if present { req.responseChannel <- functionServiceMapResponse{serviceUrl: e.serviceUrl} } else { req.responseChannel <- functionServiceMapResponse{error: errors.New("not found")} } case ASSIGN: - fmap.svc[req.function] = + fmap.svc[req.Function] = functionServiceMapEntry{serviceUrl: req.serviceUrl, generation: fmap.currentGeneration} // no response case NEXT_GEN: @@ -87,9 +87,9 @@ func (fmap *functionServiceMap) functionServiceMapWork() { } } -func (fmap *functionServiceMap) lookup(f *function) (*url.URL, error) { +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 { return nil, resp.error @@ -98,8 +98,8 @@ 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} +func (fmap *functionServiceMap) assign(f *Function, serviceUrl *url.URL) { + fmap.requestChannel <- &functionServiceMapRequest{Function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN} } func (fmap *functionServiceMap) nextGen() { diff --git a/router/functionServiceMap_test.go b/router/functionServiceMap_test.go index 48b12c52..ae20f789 100644 --- a/router/functionServiceMap_test.go +++ b/router/functionServiceMap_test.go @@ -23,7 +23,7 @@ import ( 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 { t.Errorf("can't parse url") @@ -39,7 +39,7 @@ func TestFunctionServiceMap(t *testing.T) { t.Errorf("Expected %#v, got %#v", u, v) } - fn.name = "bar" + fn.Name = "bar" _, err2 := m.lookup(fn) if err2 == nil { t.Errorf("No error on missing entry") diff --git a/router/httpTriggers.go b/router/httpTriggers.go index 7cdcb38b..d34989cf 100644 --- a/router/httpTriggers.go +++ b/router/httpTriggers.go @@ -25,11 +25,11 @@ type HTTPTriggerSet struct { *mutableRouter controllerUrl string poolManagerUrl string - triggers []httptrigger + triggers []HTTPTrigger } func makeHTTPTriggerSet(fmap *functionServiceMap, controllerUrl string, poolManagerUrl string) *HTTPTriggerSet { - triggers := make([]httptrigger, 1) + triggers := make([]HTTPTrigger, 1) return &HTTPTriggerSet{ functionServiceMap: fmap, triggers: triggers, @@ -49,10 +49,10 @@ func (triggers *HTTPTriggerSet) getRouterFromTriggers() *mux.Router { for _, trigger := range triggers.triggers { fh := &functionHandler{ fmap: triggers.functionServiceMap, - function: trigger.function, + Function: trigger.Function, poolManagerUrl: triggers.poolManagerUrl, } - muxRouter.HandleFunc(trigger.urlPattern, fh.handler) + muxRouter.HandleFunc(trigger.UrlPattern, fh.handler) } return muxRouter } diff --git a/router/router.go b/router/router.go index c1e77e35..28a7804a 100644 --- a/router/router.go +++ b/router/router.go @@ -47,14 +47,14 @@ import ( ) type ( - function struct { - name string - uid string + Function struct { + Name string + Uid string } - httptrigger struct { - urlPattern string - function + HTTPTrigger struct { + UrlPattern string + Function } options struct { @@ -65,9 +65,9 @@ type ( } ) -// request url ---[mux]---> function(name,uid) ----[fmap]----> k8s service url +// 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 +// request url ---[trigger]---> Function(name, deployment) ----[deployment]----> Function(name, uid) ----[pool mgr]---> k8s service url func router(httpTriggerSet *HTTPTriggerSet) *mutableRouter { muxRouter := mux.NewRouter() diff --git a/router/router_test.go b/router/router_test.go index 556a2849..2137933d 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -24,7 +24,7 @@ import ( func TestRouter(t *testing.T) { fmap := makeFunctionServiceMap() - fn := &function{name: "foo", uid: "xxx"} + fn := &Function{Name: "foo", Uid: "xxx"} testResponseString := "hi" testServiceUrl := createBackendService(testResponseString) @@ -33,7 +33,7 @@ func TestRouter(t *testing.T) { triggers := makeHTTPTriggerSet(fmap, "", "") triggerUrl := "/foo" - triggers.triggers = append(triggers.triggers, httptrigger{triggerUrl, *fn}) + triggers.triggers = append(triggers.triggers, HTTPTrigger{triggerUrl, *fn}) port := 4242 go server(port, triggers) From fb08d9803716115f005ff09c6de5abc884960d99 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Fri, 9 Sep 2016 15:04:15 -0700 Subject: [PATCH 2/2] Move Function and HTTPTrigger types to top level 'fission' package --- router/functionHandler.go | 4 +++- router/functionHandler_test.go | 7 ++++--- router/functionServiceMap.go | 12 +++++++----- router/functionServiceMap_test.go | 4 +++- router/httpTriggers.go | 5 +++-- router/router.go | 10 ---------- router/router_test.go | 6 ++++-- types.go | 29 +++++++++++++++++++++++++++++ 8 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 types.go diff --git a/router/functionHandler.go b/router/functionHandler.go index bb16c315..6464be63 100644 --- a/router/functionHandler.go +++ b/router/functionHandler.go @@ -22,12 +22,14 @@ import ( "net/http" "net/http/httputil" "net/url" + + "github.com/platform9/fission" ) type functionHandler struct { fmap *functionServiceMap poolManagerUrl string - Function + fission.Function } func (*functionHandler) getServiceForFunction() (*url.URL, error) { diff --git a/router/functionHandler_test.go b/router/functionHandler_test.go index fe794527..274b199a 100644 --- a/router/functionHandler_test.go +++ b/router/functionHandler_test.go @@ -19,10 +19,11 @@ package router import ( "log" "net/http" - "testing" - // "net/http/httputil" "net/http/httptest" "net/url" + "testing" + + "github.com/platform9/fission" ) func createBackendService(testResponseString string) *url.URL { @@ -48,7 +49,7 @@ func TestFunctionProxying(t *testing.T) { backendURL := createBackendService(testResponseString) log.Printf("Created backend svc at %v", backendURL) - fn := &Function{Name: "foo", Uid: "xxx"} + fn := &fission.Function{Name: "foo", Uid: "xxx"} fmap := makeFunctionServiceMap() fmap.assign(fn, backendURL) diff --git a/router/functionServiceMap.go b/router/functionServiceMap.go index 780083be..23394315 100644 --- a/router/functionServiceMap.go +++ b/router/functionServiceMap.go @@ -20,6 +20,8 @@ import ( "errors" "log" "net/url" + + "github.com/platform9/fission" ) type requestType int @@ -36,7 +38,7 @@ type functionServiceMapResponse struct { error } type functionServiceMapRequest struct { - Function + fission.Function serviceUrl url.URL requestType responseChannel chan<- functionServiceMapResponse @@ -48,7 +50,7 @@ type functionServiceMapEntry struct { type functionServiceMap struct { // map (funcname, uid) -> url - svc map[Function]functionServiceMapEntry + svc map[fission.Function]functionServiceMapEntry currentGeneration uint64 requestChannel chan *functionServiceMapRequest } @@ -56,7 +58,7 @@ type functionServiceMap struct { func makeFunctionServiceMap() *functionServiceMap { fmap := &functionServiceMap{} fmap.requestChannel = make(chan *functionServiceMapRequest) - fmap.svc = make(map[Function]functionServiceMapEntry) + fmap.svc = make(map[fission.Function]functionServiceMapEntry) go fmap.functionServiceMapWork() return fmap } @@ -87,7 +89,7 @@ func (fmap *functionServiceMap) functionServiceMapWork() { } } -func (fmap *functionServiceMap) lookup(f *Function) (*url.URL, error) { +func (fmap *functionServiceMap) lookup(f *fission.Function) (*url.URL, error) { respChannel := make(chan functionServiceMapResponse) fmap.requestChannel <- &functionServiceMapRequest{Function: *f, requestType: LOOKUP, responseChannel: respChannel} resp := <-respChannel @@ -98,7 +100,7 @@ func (fmap *functionServiceMap) lookup(f *Function) (*url.URL, error) { } } -func (fmap *functionServiceMap) assign(f *Function, serviceUrl *url.URL) { +func (fmap *functionServiceMap) assign(f *fission.Function, serviceUrl *url.URL) { fmap.requestChannel <- &functionServiceMapRequest{Function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN} } diff --git a/router/functionServiceMap_test.go b/router/functionServiceMap_test.go index ae20f789..94c8f9df 100644 --- a/router/functionServiceMap_test.go +++ b/router/functionServiceMap_test.go @@ -19,11 +19,13 @@ package router import ( "net/url" "testing" + + "github.com/platform9/fission" ) func TestFunctionServiceMap(t *testing.T) { m := makeFunctionServiceMap() - fn := &Function{Name: "foo", Uid: "012"} + fn := &fission.Function{Name: "foo", Uid: "012"} u, err := url.Parse("/foo012") if err != nil { t.Errorf("can't parse url") diff --git a/router/httpTriggers.go b/router/httpTriggers.go index d34989cf..5731ab5f 100644 --- a/router/httpTriggers.go +++ b/router/httpTriggers.go @@ -18,6 +18,7 @@ package router import ( "github.com/gorilla/mux" + "github.com/platform9/fission" ) type HTTPTriggerSet struct { @@ -25,11 +26,11 @@ type HTTPTriggerSet struct { *mutableRouter controllerUrl string poolManagerUrl string - triggers []HTTPTrigger + triggers []fission.HTTPTrigger } func makeHTTPTriggerSet(fmap *functionServiceMap, controllerUrl string, poolManagerUrl string) *HTTPTriggerSet { - triggers := make([]HTTPTrigger, 1) + triggers := make([]fission.HTTPTrigger, 1) return &HTTPTriggerSet{ functionServiceMap: fmap, triggers: triggers, diff --git a/router/router.go b/router/router.go index 28a7804a..7ea066ab 100644 --- a/router/router.go +++ b/router/router.go @@ -47,16 +47,6 @@ import ( ) type ( - Function struct { - Name string - Uid string - } - - HTTPTrigger struct { - UrlPattern string - Function - } - options struct { port int poolManagerUrl string diff --git a/router/router_test.go b/router/router_test.go index 2137933d..59aca9f0 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -20,11 +20,13 @@ import ( "fmt" "testing" "time" + + "github.com/platform9/fission" ) func TestRouter(t *testing.T) { fmap := makeFunctionServiceMap() - fn := &Function{Name: "foo", Uid: "xxx"} + fn := &fission.Function{Name: "foo", Uid: "xxx"} testResponseString := "hi" testServiceUrl := createBackendService(testResponseString) @@ -33,7 +35,7 @@ func TestRouter(t *testing.T) { triggers := makeHTTPTriggerSet(fmap, "", "") triggerUrl := "/foo" - triggers.triggers = append(triggers.triggers, HTTPTrigger{triggerUrl, *fn}) + triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{triggerUrl, *fn}) port := 4242 go server(port, triggers) diff --git a/types.go b/types.go new file mode 100644 index 00000000..ad4e252e --- /dev/null +++ b/types.go @@ -0,0 +1,29 @@ +/* +Copyright 2016 The Fission Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fission + +type ( + Function struct { + Name string + Uid string + } + + HTTPTrigger struct { + UrlPattern string + Function + } +)