Aggregate tap service request in interval (#229)
The poolmgr client now aggregates successive tapService() calls and sends them to the poolmgr service every 5 sec, instead of sending them immediately.
This commit is contained in:
@@ -20,29 +20,39 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fission/fission"
|
"github.com/fission/fission"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
poolmgrUrl string
|
poolmgrUrl string
|
||||||
|
tappedByUrl map[string]bool
|
||||||
|
requestChan chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeClient(poolmgrUrl string) *Client {
|
func MakeClient(poolmgrUrl string) *Client {
|
||||||
return &Client{poolmgrUrl: strings.TrimSuffix(poolmgrUrl, "/")}
|
c := &Client{
|
||||||
|
poolmgrUrl: strings.TrimSuffix(poolmgrUrl, "/"),
|
||||||
|
tappedByUrl: make(map[string]bool),
|
||||||
|
requestChan: make(chan string),
|
||||||
|
}
|
||||||
|
go c.service()
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetServiceForFunction(metadata *fission.Metadata) (string, error) {
|
func (c *Client) GetServiceForFunction(metadata *fission.Metadata) (string, error) {
|
||||||
url := c.poolmgrUrl + "/v1/getServiceForFunction"
|
poolmgrUrl := c.poolmgrUrl + "/v1/getServiceForFunction"
|
||||||
body, err := json.Marshal(metadata)
|
body, err := json.Marshal(metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
|
resp, err := http.Post(poolmgrUrl, "application/json", bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -60,12 +70,36 @@ func (c *Client) GetServiceForFunction(metadata *fission.Metadata) (string, erro
|
|||||||
return string(svcName), nil
|
return string(svcName), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) TapService(serviceUrl *url.URL) error {
|
func (c *Client) service() {
|
||||||
url := c.poolmgrUrl + "/v1/tapService"
|
ticker := time.NewTicker(time.Second * 5)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case serviceUrl := <-c.requestChan:
|
||||||
|
c.tappedByUrl[serviceUrl] = true
|
||||||
|
case <-ticker.C:
|
||||||
|
urls := c.tappedByUrl
|
||||||
|
c.tappedByUrl = make(map[string]bool)
|
||||||
|
if len(urls) > 0 {
|
||||||
|
go func() {
|
||||||
|
for u := range c.tappedByUrl {
|
||||||
|
c._tapService(u)
|
||||||
|
}
|
||||||
|
log.Printf("Tapped %v services in batch", len(urls))
|
||||||
|
}()
|
||||||
|
log.Printf("Tapped %v services in batch", len(urls))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serviceUrlStr := serviceUrl.String()
|
func (c *Client) TapService(serviceUrl *url.URL) {
|
||||||
|
c.requestChan <- serviceUrl.String()
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := http.Post(url, "application/octet-stream", bytes.NewReader([]byte(serviceUrlStr)))
|
func (c *Client) _tapService(serviceUrlStr string) error {
|
||||||
|
poolmgrUrl := c.poolmgrUrl + "/v1/tapService"
|
||||||
|
|
||||||
|
resp, err := http.Post(poolmgrUrl, "application/octet-stream", bytes.NewReader([]byte(serviceUrlStr)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,10 +84,7 @@ func (fh *functionHandler) tapService(serviceUrl *url.URL) {
|
|||||||
if fh.poolmgr == nil {
|
if fh.poolmgr == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := fh.poolmgr.TapService(serviceUrl)
|
fh.poolmgr.TapService(serviceUrl)
|
||||||
if err != nil {
|
|
||||||
log.Printf("tap service error for %v: %v", serviceUrl.String(), err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *http.Request) {
|
func (fh *functionHandler) handler(responseWriter http.ResponseWriter, request *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user