apply gofmt
This commit is contained in:
@@ -17,10 +17,10 @@ limitations under the License.
|
||||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"errors"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -18,14 +18,13 @@ package router
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"net/http"
|
||||
"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))
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
)
|
||||
|
||||
type requestType int
|
||||
|
||||
const (
|
||||
LOOKUP requestType = iota // lookup the map
|
||||
ASSIGN // assign function
|
||||
@@ -52,7 +53,7 @@ type functionServiceMap struct {
|
||||
requestChannel chan *functionServiceMapRequest
|
||||
}
|
||||
|
||||
func makeFunctionServiceMap() (*functionServiceMap) {
|
||||
func makeFunctionServiceMap() *functionServiceMap {
|
||||
fmap := &functionServiceMap{}
|
||||
fmap.requestChannel = make(chan *functionServiceMapRequest)
|
||||
fmap.svc = make(map[function]functionServiceMapEntry)
|
||||
@@ -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}
|
||||
}
|
||||
|
||||
@@ -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"}
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -17,10 +17,10 @@ limitations under the License.
|
||||
package router
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -40,7 +40,6 @@ func startServer(mr *mutableRouter) {
|
||||
http.ListenAndServe(":3333", mr)
|
||||
}
|
||||
|
||||
|
||||
func spamServer(quit chan bool) {
|
||||
i := 0
|
||||
for {
|
||||
@@ -50,7 +49,7 @@ func spamServer(quit chan bool) {
|
||||
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()
|
||||
|
||||
@@ -37,14 +37,13 @@ Its job is to:
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
flag "github.com/ogier/pflag"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -64,14 +63,13 @@ type (
|
||||
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")
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user