mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Separate broker HTTP and snowflake heap go files
This commit is contained in:
parent
30bfeb247e
commit
a6f6284229
2 changed files with 50 additions and 49 deletions
|
@ -11,7 +11,6 @@ package snowflake_broker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/heap"
|
"container/heap"
|
||||||
// "fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
@ -19,58 +18,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is an intermediate step - a basic hardcoded appengine rendezvous
|
|
||||||
// to a single browser snowflake.
|
|
||||||
|
|
||||||
// This is minimum viable client-proxy registration.
|
// This is minimum viable client-proxy registration.
|
||||||
// TODO: better, more secure registration corresponding to what's in
|
// TODO(#13): better, more secure registration corresponding to what's in
|
||||||
// the python flashproxy facilitator.
|
// the python flashproxy facilitator.
|
||||||
|
|
||||||
// Slice of available snowflake proxies.
|
|
||||||
// var snowflakes []chan []byte
|
|
||||||
|
|
||||||
type Snowflake struct {
|
|
||||||
id string
|
|
||||||
offerChannel chan []byte
|
|
||||||
answerChannel chan []byte
|
|
||||||
clients int
|
|
||||||
index int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implements heap.Interface, and holds Snowflakes.
|
|
||||||
type SnowflakeHeap []*Snowflake
|
|
||||||
|
|
||||||
func (sh SnowflakeHeap) Len() int { return len(sh) }
|
|
||||||
|
|
||||||
func (sh SnowflakeHeap) Less(i, j int) bool {
|
|
||||||
// Snowflakes serving less clients should sort earlier.
|
|
||||||
return sh[i].clients < sh[j].clients
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sh SnowflakeHeap) Swap(i, j int) {
|
|
||||||
sh[i], sh[j] = sh[j], sh[i]
|
|
||||||
sh[i].index = i
|
|
||||||
sh[j].index = j
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sh *SnowflakeHeap) Push(s interface{}) {
|
|
||||||
n := len(*sh)
|
|
||||||
snowflake := s.(*Snowflake)
|
|
||||||
snowflake.index = n
|
|
||||||
*sh = append(*sh, snowflake)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only valid when Len() > 0.
|
|
||||||
func (sh *SnowflakeHeap) Pop() interface{} {
|
|
||||||
flakes := *sh
|
|
||||||
n := len(flakes)
|
|
||||||
snowflake := flakes[n-1]
|
|
||||||
snowflake.index = -1
|
|
||||||
*sh = flakes[0 : n-1]
|
|
||||||
return snowflake
|
|
||||||
}
|
|
||||||
|
|
||||||
var snowflakes *SnowflakeHeap
|
var snowflakes *SnowflakeHeap
|
||||||
|
|
||||||
|
// Map keeping track of snowflakeIDs required to match SDP answers from
|
||||||
|
// the second http POST.
|
||||||
var snowflakeMap map[string]*Snowflake
|
var snowflakeMap map[string]*Snowflake
|
||||||
|
|
||||||
// Create and add a Snowflake to the heap.
|
// Create and add a Snowflake to the heap.
|
46
broker/snowflake-heap.go
Normal file
46
broker/snowflake-heap.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
Keeping track of pending available snowflake proxies.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package snowflake_broker
|
||||||
|
|
||||||
|
type Snowflake struct {
|
||||||
|
id string
|
||||||
|
offerChannel chan []byte
|
||||||
|
answerChannel chan []byte
|
||||||
|
clients int
|
||||||
|
index int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements heap.Interface, and holds Snowflakes.
|
||||||
|
type SnowflakeHeap []*Snowflake
|
||||||
|
|
||||||
|
func (sh SnowflakeHeap) Len() int { return len(sh) }
|
||||||
|
|
||||||
|
func (sh SnowflakeHeap) Less(i, j int) bool {
|
||||||
|
// Snowflakes serving less clients should sort earlier.
|
||||||
|
return sh[i].clients < sh[j].clients
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh SnowflakeHeap) Swap(i, j int) {
|
||||||
|
sh[i], sh[j] = sh[j], sh[i]
|
||||||
|
sh[i].index = i
|
||||||
|
sh[j].index = j
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sh *SnowflakeHeap) Push(s interface{}) {
|
||||||
|
n := len(*sh)
|
||||||
|
snowflake := s.(*Snowflake)
|
||||||
|
snowflake.index = n
|
||||||
|
*sh = append(*sh, snowflake)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only valid when Len() > 0.
|
||||||
|
func (sh *SnowflakeHeap) Pop() interface{} {
|
||||||
|
flakes := *sh
|
||||||
|
n := len(flakes)
|
||||||
|
snowflake := flakes[n-1]
|
||||||
|
snowflake.index = -1
|
||||||
|
*sh = flakes[0 : n-1]
|
||||||
|
return snowflake
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue