Skip to content

Commit

Permalink
fix: guard Substarte connection state in GetClient() and Close()
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed Nov 26, 2024
1 parent b8d84f3 commit c6328aa
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions clients/tfchain-client-go/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var (
ErrUnknownVersion = fmt.Errorf("unknown version")
//ErrNotFound is returned if an object is not found
ErrNotFound = fmt.Errorf("object not found")
//ErrClosed is returned if the client is closed
ErrClosed = fmt.Errorf("client closed")
)

// Versioned base for all types
Expand Down Expand Up @@ -145,6 +147,9 @@ func (p *mgrImpl) Raw() (Conn, Meta, error) {
return cl, meta, err
}

// connect connects to the next endpoint in roundrobin fashion
// and replaces the current connection with the new one.
// need to be called while lock is acquired.
func (p *mgrImpl) connect(s *Substrate) error {
cl, meta, err := p.Raw()
if err != nil {
Expand Down Expand Up @@ -176,8 +181,10 @@ func (p *mgrImpl) put(s *Substrate) {

// Substrate client
type Substrate struct {
cl Conn
meta Meta
mu sync.Mutex
cl Conn
meta Meta
closed bool

close func(s *Substrate)
connect func(s *Substrate) error
Expand All @@ -189,10 +196,23 @@ func newSubstrate(cl Conn, meta Meta, close func(*Substrate), connect func(s *Su
}

func (s *Substrate) Close() {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return
}
s.close(s)
s.closed = true
}

func (s *Substrate) GetClient() (Conn, Meta, error) {
s.mu.Lock()
defer s.mu.Unlock()

if s.closed {
return nil, nil, ErrClosed
}

// check if connection is healthy
if _, err := getTime(s.cl, s.meta); err != nil {
log.Info().Str("url", s.cl.Client.URL()).Msg("connection unhealthy, attempting failover")
Expand Down

0 comments on commit c6328aa

Please sign in to comment.