Function service map: cache parsed URLs instead of strings

This commit is contained in:
Soam Vasani
2016-08-30 16:39:39 -07:00
parent d990a0b9af
commit b9b7f9bbfc
2 changed files with 18 additions and 13 deletions
+9 -8
View File
@@ -19,6 +19,7 @@ package router
import (
"errors"
"log"
"net/url"
)
type requestType int
@@ -30,17 +31,17 @@ const (
)
type functionServiceMapResponse struct {
serviceUrl string
serviceUrl url.URL
error
}
type functionServiceMapRequest struct {
function
serviceUrl string
serviceUrl url.URL
requestType
responseChannel chan<- functionServiceMapResponse
}
type functionServiceMapEntry struct {
serviceUrl string
serviceUrl url.URL
generation uint64
}
@@ -85,21 +86,21 @@ func (fmap *functionServiceMap) functionServiceMapWork() {
}
}
func (fmap *functionServiceMap) lookup(f *function) (string, error) {
func (fmap *functionServiceMap) lookup(f *function) (*url.URL, error) {
respChannel := make(chan functionServiceMapResponse)
fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, requestType: LOOKUP, responseChannel: respChannel }
resp := <-respChannel
if (resp.error != nil) {
return "", resp.error
return nil, resp.error
} else {
return resp.serviceUrl, nil
return &resp.serviceUrl, nil
}
}
func (fmap *functionServiceMap) assign(f *function, serviceUrl string) {
func (fmap *functionServiceMap) assign(f *function, serviceUrl *url.URL) {
fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, serviceUrl: serviceUrl, requestType: ASSIGN }
&functionServiceMapRequest{ function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN }
}
func (fmap *functionServiceMap) nextGen() {
+9 -5
View File
@@ -18,21 +18,25 @@ package router
import (
"testing"
"net/url"
)
func TestFunctionServiceMap(t *testing.T) {
m := makeFunctionServiceMap()
fn := &function{ name: "foo", uid: "012" }
url := "/foo012"
u, err := url.Parse("/foo012")
if (err != nil) {
t.Errorf("can't parse url")
}
m.assign(fn, url)
m.assign(fn, u)
v, err := m.lookup(fn)
if (err != nil) {
t.Errorf("Lookup error: %s", err)
t.Errorf("Lookup error: %v", err)
}
if (v != url) {
t.Errorf("Expected %s, got %s", url, v)
if (*v != *u) {
t.Errorf("Expected %#v, got %#v", u, v)
}
fn.name = "bar"