Merge remote-tracking branch 'gitlab/mr/187'

This commit is contained in:
meskio 2023-10-26 18:47:01 +02:00
commit 778e3af09a
No known key found for this signature in database
GPG key ID: 52B8F5AC97A2DA86
2 changed files with 33 additions and 12 deletions

View file

@ -233,22 +233,19 @@ func (i *IPC) ClientOffers(arg messages.Arg, response *[]byte) error {
}
func (i *IPC) matchSnowflake(natType string) *Snowflake {
// Only hand out known restricted snowflakes to unrestricted clients
var snowflakeHeap *SnowflakeHeap
if natType == NATUnrestricted {
snowflakeHeap = i.ctx.restrictedSnowflakes
} else {
snowflakeHeap = i.ctx.snowflakes
}
i.ctx.snowflakeLock.Lock()
defer i.ctx.snowflakeLock.Unlock()
if snowflakeHeap.Len() > 0 {
return heap.Pop(snowflakeHeap).(*Snowflake)
} else {
return nil
// Proiritize known restricted snowflakes for unrestricted clients
if natType == NATUnrestricted && i.ctx.restrictedSnowflakes.Len() > 0 {
return heap.Pop(i.ctx.restrictedSnowflakes).(*Snowflake)
}
if i.ctx.snowflakes.Len() > 0 {
return heap.Pop(i.ctx.snowflakes).(*Snowflake)
}
return nil
}
func (i *IPC) ProxyAnswers(arg messages.Arg, response *[]byte) error {

View file

@ -167,6 +167,30 @@ func TestBroker(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("with unrestricted proxy to unrestricted client if there are no restricted proxies", func() {
snowflake := ctx.AddSnowflake("test", "", NATUnrestricted, 0)
offerData, err := createClientOffer(sdp, NATUnrestricted, "")
So(err, ShouldBeNil)
r, err := http.NewRequest("POST", "snowflake.broker/client", offerData)
done := make(chan bool)
go func() {
clientOffers(i, w, r)
done <- true
}()
select {
case <-snowflake.offerChannel:
case <-time.After(250 * time.Millisecond):
So(false, ShouldBeTrue)
return
}
snowflake.answerChannel <- "test answer"
<-done
So(w.Body.String(), ShouldEqual, `{"answer":"test answer"}`)
})
Convey("Times out when no proxy responds.", func() {
if testing.Short() {
return