Simplify proxy poll handler, and broker match test

This commit is contained in:
Serene Han 2016-02-16 20:50:00 -08:00
parent 9fd471b4c4
commit 791f6925ec
3 changed files with 74 additions and 52 deletions

View file

@ -28,17 +28,17 @@ type BrokerContext struct {
snowflakes *SnowflakeHeap snowflakes *SnowflakeHeap
// Map keeping track of snowflakeIDs required to match SDP answers from // Map keeping track of snowflakeIDs required to match SDP answers from
// the second http POST. // the second http POST.
snowflakeMap map[string]*Snowflake snowflakeMap map[string]*Snowflake
createChannel chan *ProxyRequest proxyPolls chan *ProxyPoll
} }
func NewBrokerContext() *BrokerContext { func NewBrokerContext() *BrokerContext {
snowflakes := new(SnowflakeHeap) snowflakes := new(SnowflakeHeap)
heap.Init(snowflakes) heap.Init(snowflakes)
return &BrokerContext{ return &BrokerContext{
snowflakes: snowflakes, snowflakes: snowflakes,
snowflakeMap: make(map[string]*Snowflake), snowflakeMap: make(map[string]*Snowflake),
createChannel: make(chan *ProxyRequest), proxyPolls: make(chan *ProxyPoll),
} }
} }
@ -51,46 +51,58 @@ func (sh SnowflakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sh.h(sh.BrokerContext, w, r) sh.h(sh.BrokerContext, w, r)
} }
type ProxyRequest struct { // Proxies may poll for client offers concurrently.
id string type ProxyPoll struct {
offerChan chan []byte id string
offerChannel chan []byte
}
// Registers a Snowflake and waits for some Client to send an offer,
// as part of the polling logic of the proxy handler.
func (ctx *BrokerContext) RequestOffer(id string) []byte {
request := new(ProxyPoll)
request.id = id
request.offerChannel = make(chan []byte)
ctx.proxyPolls <- request
// Block until an offer is available...
offer := <-request.offerChannel
return offer
}
// goroutine which match proxies to clients.
// Safely processes proxy requests, responding to them with either an available
// client offer or nil on timeout / none are available.
func (ctx *BrokerContext) Broker() {
for request := range ctx.proxyPolls {
snowflake := ctx.AddSnowflake(request.id)
// Wait for a client to avail an offer to the snowflake.
go func(request *ProxyPoll) {
select {
case offer := <-snowflake.offerChannel:
log.Println("Passing client offer to snowflake proxy.")
request.offerChannel <- offer
case <-time.After(time.Second * ProxyTimeout):
// This snowflake is no longer available to serve clients.
heap.Remove(ctx.snowflakes, snowflake.index)
delete(ctx.snowflakeMap, snowflake.id)
request.offerChannel <- nil
}
}(request)
}
} }
// Create and add a Snowflake to the heap. // Create and add a Snowflake to the heap.
func (sc *BrokerContext) AddSnowflake(id string) *Snowflake { func (ctx *BrokerContext) AddSnowflake(id string) *Snowflake {
snowflake := new(Snowflake) snowflake := new(Snowflake)
snowflake.id = id snowflake.id = id
snowflake.clients = 0 snowflake.clients = 0
snowflake.offerChannel = make(chan []byte) snowflake.offerChannel = make(chan []byte)
snowflake.answerChannel = make(chan []byte) snowflake.answerChannel = make(chan []byte)
heap.Push(sc.snowflakes, snowflake) heap.Push(ctx.snowflakes, snowflake)
sc.snowflakeMap[id] = snowflake ctx.snowflakeMap[id] = snowflake
return snowflake return snowflake
} }
// Match proxies to clients.
// func (ctx *BrokerContext) Broker(proxies <-chan *ProxyRequest) {
func (ctx *BrokerContext) Broker() {
// for p := range proxies {
for p := range ctx.createChannel {
snowflake := ctx.AddSnowflake(p.id)
// Wait for a client to avail an offer to the snowflake, or timeout
// and ask the snowflake to poll later.
go func(p *ProxyRequest) {
select {
case offer := <-snowflake.offerChannel:
log.Println("Passing client offer to snowflake.")
p.offerChan <- offer
case <-time.After(time.Second * ProxyTimeout):
// This snowflake is no longer available to serve clients.
heap.Remove(ctx.snowflakes, snowflake.index)
delete(ctx.snowflakeMap, snowflake.id)
p.offerChan <- nil
}
}(p)
}
}
func robotsTxtHandler(w http.ResponseWriter, r *http.Request) { func robotsTxtHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte("User-agent: *\nDisallow:\n")) w.Write([]byte("User-agent: *\nDisallow:\n"))
@ -145,14 +157,15 @@ func clientHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
case answer := <-snowflake.answerChannel: case answer := <-snowflake.answerChannel:
log.Println("Client: Retrieving answer") log.Println("Client: Retrieving answer")
w.Write(answer) w.Write(answer)
// Only remove from the snowflake map once the answer is set.
delete(ctx.snowflakeMap, snowflake.id)
case <-time.After(time.Second * ClientTimeout): case <-time.After(time.Second * ClientTimeout):
log.Println("Client: Timed out.") log.Println("Client: Timed out.")
w.WriteHeader(http.StatusGatewayTimeout) w.WriteHeader(http.StatusGatewayTimeout)
w.Write([]byte("timed out waiting for answer!")) w.Write([]byte("timed out waiting for answer!"))
} }
// Remove from the snowflake map whether answer was sent or not, because
// this client request is now over.
delete(ctx.snowflakeMap, snowflake.id)
} }
/* /*
@ -172,17 +185,9 @@ func proxyHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
if string(body) != id { // Mismatched IDs! if string(body) != id { // Mismatched IDs!
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
} }
// Maybe confirm that X-Session-ID is the same.
log.Println("Received snowflake: ", id) log.Println("Received snowflake: ", id)
// Wait for a client to avail an offer to the snowflake, or timeout if nil.
p := new(ProxyRequest) offer := ctx.RequestOffer(id)
p.id = id
p.offerChan = make(chan []byte)
ctx.createChannel <- p
// Wait for a client to avail an offer to the snowflake, or timeout
// and ask the snowflake to poll later.
offer := <-p.offerChan
if nil == offer { if nil == offer {
log.Println("Proxy " + id + " did not receive a Client offer.") log.Println("Proxy " + id + " did not receive a Client offer.")
w.WriteHeader(http.StatusGatewayTimeout) w.WriteHeader(http.StatusGatewayTimeout)

View file

@ -22,6 +22,19 @@ func TestBroker(t *testing.T) {
So(len(ctx.snowflakeMap), ShouldEqual, 1) So(len(ctx.snowflakeMap), ShouldEqual, 1)
}) })
Convey("Broker goroutine matches clients with proxies", func() {
p := new(ProxyPoll)
p.id = "test"
p.offerChannel = make(chan []byte)
go func() {
ctx.proxyPolls <- p
close(ctx.proxyPolls)
}()
ctx.Broker()
So(ctx.snowflakes.Len(), ShouldEqual, 1)
So(ctx.snowflakes.Len(), ShouldEqual, 1)
})
Convey("Responds to client offers...", func() { Convey("Responds to client offers...", func() {
w := httptest.NewRecorder() w := httptest.NewRecorder()
data := bytes.NewReader([]byte("test")) data := bytes.NewReader([]byte("test"))
@ -83,9 +96,9 @@ func TestBroker(t *testing.T) {
done <- true done <- true
}(ctx) }(ctx)
// Pass a fake client offer to this proxy // Pass a fake client offer to this proxy
p := <-ctx.createChannel p := <-ctx.proxyPolls
So(p.id, ShouldEqual, "test") So(p.id, ShouldEqual, "test")
p.offerChan <- []byte("fake offer") p.offerChannel <- []byte("fake offer")
<-done <-done
So(w.Code, ShouldEqual, http.StatusOK) So(w.Code, ShouldEqual, http.StatusOK)
So(w.Body.String(), ShouldEqual, "fake offer") So(w.Body.String(), ShouldEqual, "fake offer")
@ -96,10 +109,10 @@ func TestBroker(t *testing.T) {
proxyHandler(ctx, w, r) proxyHandler(ctx, w, r)
done <- true done <- true
}(ctx) }(ctx)
p := <-ctx.createChannel p := <-ctx.proxyPolls
So(p.id, ShouldEqual, "test") So(p.id, ShouldEqual, "test")
// nil means timeout // nil means timeout
p.offerChan <- nil p.offerChannel <- nil
<-done <-done
So(w.Body.String(), ShouldEqual, "") So(w.Body.String(), ShouldEqual, "")
So(w.Code, ShouldEqual, http.StatusGatewayTimeout) So(w.Code, ShouldEqual, http.StatusGatewayTimeout)
@ -159,12 +172,12 @@ func TestBroker(t *testing.T) {
}() }()
// Manually do the Broker goroutine action here for full control. // Manually do the Broker goroutine action here for full control.
p := <-ctx.createChannel p := <-ctx.proxyPolls
So(p.id, ShouldEqual, "test") So(p.id, ShouldEqual, "test")
s := ctx.AddSnowflake(p.id) s := ctx.AddSnowflake(p.id)
go func() { go func() {
offer := <-s.offerChannel offer := <-s.offerChannel
p.offerChan <- offer p.offerChannel <- offer
}() }()
So(ctx.snowflakeMap["test"], ShouldNotBeNil) So(ctx.snowflakeMap["test"], ShouldNotBeNil)

View file

@ -4,6 +4,10 @@ Keeping track of pending available snowflake proxies.
package snowflake_broker package snowflake_broker
/*
The Snowflake struct contains a single interaction
over the offer and answer channels.
*/
type Snowflake struct { type Snowflake struct {
id string id string
offerChannel chan []byte offerChannel chan []byte