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 ( import (
"errors" "errors"
"log" "log"
"net/url"
) )
type requestType int type requestType int
@@ -30,17 +31,17 @@ const (
) )
type functionServiceMapResponse struct { type functionServiceMapResponse struct {
serviceUrl string serviceUrl url.URL
error error
} }
type functionServiceMapRequest struct { type functionServiceMapRequest struct {
function function
serviceUrl string serviceUrl url.URL
requestType requestType
responseChannel chan<- functionServiceMapResponse responseChannel chan<- functionServiceMapResponse
} }
type functionServiceMapEntry struct { type functionServiceMapEntry struct {
serviceUrl string serviceUrl url.URL
generation uint64 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) respChannel := make(chan functionServiceMapResponse)
fmap.requestChannel <- fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, requestType: LOOKUP, responseChannel: respChannel } &functionServiceMapRequest{ function: *f, requestType: LOOKUP, responseChannel: respChannel }
resp := <-respChannel resp := <-respChannel
if (resp.error != nil) { if (resp.error != nil) {
return "", resp.error return nil, resp.error
} else { } 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 <- fmap.requestChannel <-
&functionServiceMapRequest{ function: *f, serviceUrl: serviceUrl, requestType: ASSIGN } &functionServiceMapRequest{ function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN }
} }
func (fmap *functionServiceMap) nextGen() { func (fmap *functionServiceMap) nextGen() {
+9 -5
View File
@@ -18,21 +18,25 @@ package router
import ( import (
"testing" "testing"
"net/url"
) )
func TestFunctionServiceMap(t *testing.T) { func TestFunctionServiceMap(t *testing.T) {
m := makeFunctionServiceMap() m := makeFunctionServiceMap()
fn := &function{ name: "foo", uid: "012" } 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) v, err := m.lookup(fn)
if (err != nil) { if (err != nil) {
t.Errorf("Lookup error: %s", err) t.Errorf("Lookup error: %v", err)
} }
if (v != url) { if (*v != *u) {
t.Errorf("Expected %s, got %s", url, v) t.Errorf("Expected %#v, got %#v", u, v)
} }
fn.name = "bar" fn.name = "bar"