Skip to content

Commit

Permalink
refactor for findServerCert
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Oct 16, 2023
1 parent e80e822 commit 50df648
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions server/pairing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"sync"

"go.uber.org/zap"

Expand Down Expand Up @@ -42,30 +41,29 @@ type BaseClient struct {
func findServerCert(c *ConnectionParams, reachableIPs []net.IP) (*url.URL, *x509.Certificate, error) {
var baseAddress *url.URL
var serverCert *x509.Certificate
var once sync.Once
errCh := make(chan struct {

type connectionError struct {
ip net.IP
err error
}, len(reachableIPs))
certCh := make(chan *x509.Certificate, len(reachableIPs))
urlCh := make(chan *url.URL, len(reachableIPs))
done := make(chan bool)
}
errCh := make(chan connectionError, len(reachableIPs))

type result struct {
u *url.URL
cert *x509.Certificate
}
successCh := make(chan result, 1) // as we close on the first success

for _, ip := range reachableIPs {
go func(ip net.IP) {
u := c.BuildURL(ip)
cert, err := getServerCert(u)
if err != nil {
errCh <- struct {
ip net.IP
err error
}{ip: ip, err: fmt.Errorf("connecting to '%s' failed: %s", u, err.Error())}
errCh <- connectionError{ip: ip, err: fmt.Errorf("connecting to '%s' failed: %s", u, err.Error())}
return
}
// If no error, send the results to their respective channels
urlCh <- u
certCh <- cert
once.Do(func() { close(done) }) // signal success and close the done channel
// If no error, send the results to the success channel
successCh <- result{u: u, cert: cert}
}(ip)
}

Expand All @@ -74,9 +72,9 @@ func findServerCert(c *ConnectionParams, reachableIPs []net.IP) (*url.URL, *x509
var combinedErrors string
for {
select {
case <-done:
baseAddress = <-urlCh
serverCert = <-certCh
case success := <-successCh:
baseAddress = success.u
serverCert = success.cert
return baseAddress, serverCert, nil
case ipErr := <-errCh:
errorCount++
Expand Down

0 comments on commit 50df648

Please sign in to comment.