Updated proxyType variable name for readability

This commit is contained in:
Cecylia Bocovich 2019-11-26 10:36:27 -05:00
parent 981abffbd9
commit 97554e03e4
5 changed files with 30 additions and 30 deletions

View file

@ -97,16 +97,16 @@ func (mh MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Proxies may poll for client offers concurrently. // Proxies may poll for client offers concurrently.
type ProxyPoll struct { type ProxyPoll struct {
id string id string
ptype string proxyType string
offerChannel chan []byte offerChannel chan []byte
} }
// Registers a Snowflake and waits for some Client to send an offer, // Registers a Snowflake and waits for some Client to send an offer,
// as part of the polling logic of the proxy handler. // as part of the polling logic of the proxy handler.
func (ctx *BrokerContext) RequestOffer(id string, ptype string) []byte { func (ctx *BrokerContext) RequestOffer(id string, proxyType string) []byte {
request := new(ProxyPoll) request := new(ProxyPoll)
request.id = id request.id = id
request.ptype = ptype request.proxyType = proxyType
request.offerChannel = make(chan []byte) request.offerChannel = make(chan []byte)
ctx.proxyPolls <- request ctx.proxyPolls <- request
// Block until an offer is available, or timeout which sends a nil offer. // Block until an offer is available, or timeout which sends a nil offer.
@ -119,7 +119,7 @@ func (ctx *BrokerContext) RequestOffer(id string, ptype string) []byte {
// client offer or nil on timeout / none are available. // client offer or nil on timeout / none are available.
func (ctx *BrokerContext) Broker() { func (ctx *BrokerContext) Broker() {
for request := range ctx.proxyPolls { for request := range ctx.proxyPolls {
snowflake := ctx.AddSnowflake(request.id, request.ptype) snowflake := ctx.AddSnowflake(request.id, request.proxyType)
// Wait for a client to avail an offer to the snowflake. // Wait for a client to avail an offer to the snowflake.
go func(request *ProxyPoll) { go func(request *ProxyPoll) {
select { select {
@ -139,11 +139,11 @@ func (ctx *BrokerContext) Broker() {
// Create and add a Snowflake to the heap. // Create and add a Snowflake to the heap.
// Required to keep track of proxies between providing them // Required to keep track of proxies between providing them
// with an offer and awaiting their second POST with an answer. // with an offer and awaiting their second POST with an answer.
func (ctx *BrokerContext) AddSnowflake(id string, ptype string) *Snowflake { func (ctx *BrokerContext) AddSnowflake(id string, proxyType string) *Snowflake {
snowflake := new(Snowflake) snowflake := new(Snowflake)
snowflake.id = id snowflake.id = id
snowflake.clients = 0 snowflake.clients = 0
snowflake.ptype = ptype snowflake.proxyType = proxyType
snowflake.offerChannel = make(chan []byte) snowflake.offerChannel = make(chan []byte)
snowflake.answerChannel = make(chan []byte) snowflake.answerChannel = make(chan []byte)
heap.Push(ctx.snowflakes, snowflake) heap.Push(ctx.snowflakes, snowflake)
@ -162,7 +162,7 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
return return
} }
sid, ptype, err := messages.DecodePollRequest(body) sid, proxyType, err := messages.DecodePollRequest(body)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
@ -173,11 +173,11 @@ func proxyPolls(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Println("Error processing proxy IP: ", err.Error()) log.Println("Error processing proxy IP: ", err.Error())
} else { } else {
ctx.metrics.UpdateCountryStats(remoteIP, ptype) ctx.metrics.UpdateCountryStats(remoteIP, proxyType)
} }
// Wait for a client to avail an offer to the snowflake, or timeout if nil. // Wait for a client to avail an offer to the snowflake, or timeout if nil.
offer := ctx.RequestOffer(sid, ptype) offer := ctx.RequestOffer(sid, proxyType)
var b []byte var b []byte
if nil == offer { if nil == offer {
ctx.metrics.proxyIdleCount++ ctx.metrics.proxyIdleCount++
@ -291,11 +291,11 @@ func debugHandler(ctx *BrokerContext, w http.ResponseWriter, r *http.Request) {
var webexts, browsers, standalones, unknowns int var webexts, browsers, standalones, unknowns int
for _, snowflake := range ctx.idToSnowflake { for _, snowflake := range ctx.idToSnowflake {
if snowflake.ptype == "badge" { if snowflake.proxyType == "badge" {
browsers++ browsers++
} else if snowflake.ptype == "webext" { } else if snowflake.proxyType == "webext" {
webexts++ webexts++
} else if snowflake.ptype == "standalone" { } else if snowflake.proxyType == "standalone" {
standalones++ standalones++
} else { } else {
unknowns++ unknowns++

View file

@ -110,20 +110,20 @@ func (s CountryStats) Display() string {
return output return output
} }
func (m *Metrics) UpdateCountryStats(addr string, ptype string) { func (m *Metrics) UpdateCountryStats(addr string, proxyType string) {
var country string var country string
var ok bool var ok bool
if ptype == "standalone" { if proxyType == "standalone" {
if m.countryStats.standalone[addr] { if m.countryStats.standalone[addr] {
return return
} }
} else if ptype == "badge" { } else if proxyType == "badge" {
if m.countryStats.badge[addr] { if m.countryStats.badge[addr] {
return return
} }
} else if ptype == "webext" { } else if proxyType == "webext" {
if m.countryStats.webext[addr] { if m.countryStats.webext[addr] {
return return
} }
@ -153,11 +153,11 @@ func (m *Metrics) UpdateCountryStats(addr string, ptype string) {
//update map of unique ips and counts //update map of unique ips and counts
m.countryStats.counts[country]++ m.countryStats.counts[country]++
if ptype == "standalone" { if proxyType == "standalone" {
m.countryStats.standalone[addr] = true m.countryStats.standalone[addr] = true
} else if ptype == "badge" { } else if proxyType == "badge" {
m.countryStats.badge[addr] = true m.countryStats.badge[addr] = true
} else if ptype == "webext" { } else if proxyType == "webext" {
m.countryStats.webext[addr] = true m.countryStats.webext[addr] = true
} else { } else {
m.countryStats.unknown[addr] = true m.countryStats.unknown[addr] = true

View file

@ -10,7 +10,7 @@ over the offer and answer channels.
*/ */
type Snowflake struct { type Snowflake struct {
id string id string
ptype string proxyType string
offerChannel chan []byte offerChannel chan []byte
answerChannel chan []byte answerChannel chan []byte
clients int clients int

View file

@ -78,11 +78,11 @@ type ProxyPollRequest struct {
Type string Type string
} }
func EncodePollRequest(sid string, ptype string) ([]byte, error) { func EncodePollRequest(sid string, proxyType string) ([]byte, error) {
return json.Marshal(ProxyPollRequest{ return json.Marshal(ProxyPollRequest{
Sid: sid, Sid: sid,
Version: version, Version: version,
Type: ptype, Type: proxyType,
}) })
} }

View file

@ -12,7 +12,7 @@ func TestDecodeProxyPollRequest(t *testing.T) {
Convey("Context", t, func() { Convey("Context", t, func() {
for _, test := range []struct { for _, test := range []struct {
sid string sid string
ptype string proxyType string
data string data string
err error err error
}{ }{
@ -62,9 +62,9 @@ func TestDecodeProxyPollRequest(t *testing.T) {
fmt.Errorf(""), fmt.Errorf(""),
}, },
} { } {
sid, ptype, err := DecodePollRequest([]byte(test.data)) sid, proxyType, err := DecodePollRequest([]byte(test.data))
So(sid, ShouldResemble, test.sid) So(sid, ShouldResemble, test.sid)
So(ptype, ShouldResemble, test.ptype) So(proxyType, ShouldResemble, test.proxyType)
So(err, ShouldHaveSameTypeAs, test.err) So(err, ShouldHaveSameTypeAs, test.err)
} }
@ -75,9 +75,9 @@ func TestEncodeProxyPollRequests(t *testing.T) {
Convey("Context", t, func() { Convey("Context", t, func() {
b, err := EncodePollRequest("ymbcCMto7KHNGYlp", "standalone") b, err := EncodePollRequest("ymbcCMto7KHNGYlp", "standalone")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
sid, ptype, err := DecodePollRequest(b) sid, proxyType, err := DecodePollRequest(b)
So(sid, ShouldEqual, "ymbcCMto7KHNGYlp") So(sid, ShouldEqual, "ymbcCMto7KHNGYlp")
So(ptype, ShouldEqual, "standalone") So(proxyType, ShouldEqual, "standalone")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
}) })
} }