diff --git a/common.go b/common.go new file mode 100644 index 00000000..51dcbb63 --- /dev/null +++ b/common.go @@ -0,0 +1,25 @@ +/* +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 + +import ( + "fmt" +) + +func UrlForFunction(m *Metadata) string { + return fmt.Sprintf("/fission-function/%v/%v", m.Name, m.Uid) +} diff --git a/router/httpTriggers.go b/router/httpTriggers.go index c9df3008..685efaf4 100644 --- a/router/httpTriggers.go +++ b/router/httpTriggers.go @@ -35,6 +35,7 @@ type HTTPTriggerSet struct { controller *controllerClient.Client poolmgr *poolmgrClient.Client triggers []fission.HTTPTrigger + functions []fission.Function } func makeHTTPTriggerSet(fmap *functionServiceMap, controller *controllerClient.Client, poolmgr *poolmgrClient.Client) *HTTPTriggerSet { @@ -49,12 +50,14 @@ func makeHTTPTriggerSet(fmap *functionServiceMap, controller *controllerClient.C func (ts *HTTPTriggerSet) subscribeRouter(mr *mutableRouter) { ts.mutableRouter = mr - mr.updateRouter(ts.getRouterFromTriggers()) + mr.updateRouter(ts.getRouter()) go ts.watchTriggers() } -func (ts *HTTPTriggerSet) getRouterFromTriggers() *mux.Router { +func (ts *HTTPTriggerSet) getRouter() *mux.Router { muxRouter := mux.NewRouter() + + // HTTP triggers setup by the user for _, trigger := range ts.triggers { fh := &functionHandler{ fmap: ts.functionServiceMap, @@ -63,6 +66,18 @@ func (ts *HTTPTriggerSet) getRouterFromTriggers() *mux.Router { } muxRouter.HandleFunc(trigger.UrlPattern, fh.handler) } + + // Internal triggers for each function + for _, function := range ts.functions { + fh := &functionHandler{ + fmap: ts.functionServiceMap, + Function: function.Metadata, + poolmgr: ts.poolmgr, + } + muxRouter.HandleFunc(fission.UrlForFunction(&function.Metadata), + fh.handler) + } + return muxRouter } @@ -102,9 +117,18 @@ func (ts *HTTPTriggerSet) watchTriggers() { if failureCount >= maxFailures { log.Fatalf("Failed to connect to controller after %v retries: %v", failureCount, err) } + time.Sleep(time.Duration(pollSleepSec) * time.Second) + continue } ts.triggers = triggers - ts.mutableRouter.updateRouter(ts.getRouterFromTriggers()) + + functions, err := ts.controller.FunctionList() + if err != nil { + log.Fatalf("Failed to get function list") + } + ts.functions = functions + + ts.mutableRouter.updateRouter(ts.getRouter()) time.Sleep(time.Duration(pollSleepSec) * time.Second) } }