Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #52 from planetscale/fatih/add-local-addr
Browse files Browse the repository at this point in the history
proxy: add LocalAddr() method
  • Loading branch information
fatih authored Mar 2, 2021
2 parents 1ed032f + c82065c commit fd7bb8b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -52,6 +53,10 @@ type Client struct {
// configCache contains the TLS certificate chache for each indiviual
// database
configCache *tlsCache

listener net.Listener
// done is closed after a successfull net.Listen bind.
done chan struct{}
}

// Options are the options for creating a new Client.
Expand Down Expand Up @@ -89,6 +94,7 @@ func NewClient(opts Options) (*Client, error) {
remoteAddr: opts.RemoteAddr,
instance: opts.Instance,
configCache: newtlsCache(),
done: make(chan struct{}),
}

if opts.Logger != nil {
Expand Down Expand Up @@ -129,9 +135,24 @@ func (c *Client) Run(ctx context.Context) error {
}
defer c.log.Sync() // nolint: errcheck

c.listener = l
close(c.done)

return c.run(ctx, l)
}

// LocalAddr returns the address of the local listener. This is by default
// blocking and will only return if the proxy is invoked with the Run() method.
func (c *Client) LocalAddr() (net.Addr, error) {
<-c.done

if c.listener == nil {
return nil, errors.New("listener is not set")

}
return c.listener.Addr(), nil
}

func (c *Client) getListener() (net.Listener, error) {
if strings.HasPrefix(c.localAddr, "unix://") {
p := strings.TrimPrefix(c.localAddr, "unix://")
Expand Down

0 comments on commit fd7bb8b

Please sign in to comment.