Use fission.Cache for functionServiceMap
This commit is contained in:
@@ -81,6 +81,9 @@ func (rrt RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Response, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fh *functionHandler) tapService(serviceUrl *url.URL) {
|
func (fh *functionHandler) tapService(serviceUrl *url.URL) {
|
||||||
|
if fh.poolmgr == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
err := fh.poolmgr.TapService(serviceUrl)
|
err := fh.poolmgr.TapService(serviceUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("tap service error: %v", serviceUrl.String())
|
log.Printf("tap service error: %v", serviceUrl.String())
|
||||||
|
|||||||
@@ -17,97 +17,36 @@ limitations under the License.
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/platform9/fission"
|
"github.com/platform9/fission"
|
||||||
|
"github.com/platform9/fission/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type requestType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
LOOKUP requestType = iota // lookup the map
|
|
||||||
ASSIGN // assign function
|
|
||||||
NEXT_GEN // increment current generation
|
|
||||||
SWEEP // delete all but the current generation
|
|
||||||
)
|
|
||||||
|
|
||||||
type functionServiceMapResponse struct {
|
|
||||||
serviceUrl url.URL
|
|
||||||
error
|
|
||||||
}
|
|
||||||
type functionServiceMapRequest struct {
|
|
||||||
Function fission.Metadata
|
|
||||||
serviceUrl url.URL
|
|
||||||
requestType
|
|
||||||
responseChannel chan<- functionServiceMapResponse
|
|
||||||
}
|
|
||||||
type functionServiceMapEntry struct {
|
|
||||||
serviceUrl url.URL
|
|
||||||
generation uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
type functionServiceMap struct {
|
type functionServiceMap struct {
|
||||||
// map (funcname, uid) -> url
|
svc *cache.Cache // map[fission.Metadata]*url.URL
|
||||||
svc map[fission.Metadata]functionServiceMapEntry
|
|
||||||
currentGeneration uint64
|
|
||||||
requestChannel chan *functionServiceMapRequest
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeFunctionServiceMap() *functionServiceMap {
|
func makeFunctionServiceMap() *functionServiceMap {
|
||||||
fmap := &functionServiceMap{}
|
return &functionServiceMap{
|
||||||
fmap.requestChannel = make(chan *functionServiceMapRequest)
|
svc: cache.MakeCache(),
|
||||||
fmap.svc = make(map[fission.Metadata]functionServiceMapEntry)
|
|
||||||
go fmap.functionServiceMapWork()
|
|
||||||
return fmap
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fmap *functionServiceMap) functionServiceMapWork() {
|
|
||||||
for {
|
|
||||||
req := <-fmap.requestChannel
|
|
||||||
switch req.requestType {
|
|
||||||
case LOOKUP:
|
|
||||||
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] =
|
|
||||||
functionServiceMapEntry{serviceUrl: req.serviceUrl, generation: fmap.currentGeneration}
|
|
||||||
// no response
|
|
||||||
case NEXT_GEN:
|
|
||||||
fmap.currentGeneration++
|
|
||||||
// no response
|
|
||||||
case SWEEP:
|
|
||||||
log.Panic("not implemented")
|
|
||||||
default:
|
|
||||||
log.Panic("bad request")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fmap *functionServiceMap) lookup(f *fission.Metadata) (*url.URL, error) {
|
func (fmap *functionServiceMap) lookup(f *fission.Metadata) (*url.URL, error) {
|
||||||
respChannel := make(chan functionServiceMapResponse)
|
item, err := fmap.svc.Get(*f)
|
||||||
fmap.requestChannel <- &functionServiceMapRequest{Function: *f, requestType: LOOKUP, responseChannel: respChannel}
|
if err != nil {
|
||||||
resp := <-respChannel
|
return nil, err
|
||||||
if resp.error != nil {
|
|
||||||
return nil, resp.error
|
|
||||||
} else {
|
|
||||||
return &resp.serviceUrl, nil
|
|
||||||
}
|
}
|
||||||
|
u := item.(*url.URL)
|
||||||
|
return u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fmap *functionServiceMap) assign(f *fission.Metadata, serviceUrl *url.URL) {
|
func (fmap *functionServiceMap) assign(f *fission.Metadata, serviceUrl *url.URL) {
|
||||||
fmap.requestChannel <- &functionServiceMapRequest{Function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN}
|
//fmap.requestChannel <- &functionServiceMapRequest{Function: *f, serviceUrl: *serviceUrl, requestType: ASSIGN}
|
||||||
}
|
err := fmap.svc.Set(*f, serviceUrl)
|
||||||
|
if err != nil {
|
||||||
func (fmap *functionServiceMap) nextGen() {
|
log.Printf("error caching svc for function: %v", err)
|
||||||
fmap.requestChannel <- &functionServiceMapRequest{requestType: NEXT_GEN}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (fmap *functionServiceMap) sweep() {
|
|
||||||
fmap.requestChannel <- &functionServiceMapRequest{requestType: SWEEP}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user