Skip to content

Commit

Permalink
Add client.GetPoolStatus()
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko authored and akos-tk committed Nov 29, 2024
1 parent 40dcba5 commit 4bfca28
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions liteapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,3 +1089,7 @@ func (c *Client) GetNetworkGlobalID(ctx context.Context) (int32, error) {
c.networkGlobalID = &block.GlobalId
return block.GlobalId, nil
}

func (c *Client) GetPoolStatus() pool.Status {
return c.pool.Status()
}
33 changes: 27 additions & 6 deletions liteapi/pool/conn_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type conn interface {
Run(ctx context.Context, detectArchive bool)
IsArchiveNode() bool
AverageRoundTrip() time.Duration
Status() ConnStatus
}

// New returns a new instance of a connections pool.
Expand All @@ -79,8 +80,9 @@ func (p *ConnPool) InitializeConnections(ctx context.Context, timeout time.Durat
cli, _ := connect(ctx, timeout, server)
// TODO: log error
clientsCh <- clientWrapper{
connID: connID,
cli: cli,
connID: connID,
cli: cli,
serverHost: server.Host,
}
}(connID, server)
}
Expand All @@ -99,7 +101,7 @@ func (p *ConnPool) InitializeConnections(ctx context.Context, timeout time.Durat
continue
}
if p.ConnectionsNumber() < maxConnections {
c := p.addConnection(wrapper.connID, wrapper.cli)
c := p.addConnection(wrapper.connID, wrapper.cli, wrapper.serverHost)
go c.Run(context.TODO(), detectArchiveNodes)
}
if p.ConnectionsNumber() == maxConnections {
Expand Down Expand Up @@ -134,15 +136,17 @@ func connect(ctx context.Context, timeout time.Duration, server config.LiteServe
}

type clientWrapper struct {
connID int
cli *liteclient.Client
connID int
cli *liteclient.Client
serverHost string
}

func (p *ConnPool) addConnection(connID int, cli *liteclient.Client) *connection {
func (p *ConnPool) addConnection(connID int, cli *liteclient.Client, serverHost string) *connection {
p.mu.Lock()
defer p.mu.Unlock()
c := &connection{
id: connID,
serverHost: serverHost,
client: cli,
masterHeadUpdatedCh: p.masterHeadUpdatedCh,
}
Expand Down Expand Up @@ -369,3 +373,20 @@ func (p *ConnPool) WaitMasterchainSeqno(ctx context.Context, seqno uint32, timeo
}
}
}

type Status struct {
Connections []ConnStatus
}

func (p *ConnPool) Status() Status {
p.mu.Lock()
defer p.mu.Unlock()

connStatuses := make([]ConnStatus, 0, len(p.conns))
for _, c := range p.conns {
connStatuses = append(connStatuses, c.Status())
}
return Status{
Connections: connStatuses,
}
}
4 changes: 4 additions & 0 deletions liteapi/pool/conn_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (m *mockConn) Client() *liteclient.Client {
panic("implement me")
}

func (m *mockConn) Status() ConnStatus {
panic("implement me")
}

func (m *mockConn) Run(ctx context.Context, detectArchiveNodes bool) {
}

Expand Down
19 changes: 17 additions & 2 deletions liteapi/pool/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type connection struct {
id int
client *liteclient.Client
id int
serverHost string
client *liteclient.Client

// masterHeadUpdatedCh is used to send a notification when a known master head is changed.
masterHeadUpdatedCh chan masterHeadUpdated
Expand Down Expand Up @@ -154,3 +155,17 @@ func (c *connection) setArchive(archive bool) {
func (c *connection) AverageRoundTrip() time.Duration {
return c.client.AverageRoundTrip()
}

type ConnStatus struct {
ServerHost string
Connected bool
Archive bool
}

func (c *connection) Status() ConnStatus {
return ConnStatus{
ServerHost: c.serverHost,
Connected: c.IsOK(),
Archive: c.IsArchiveNode(),
}
}

0 comments on commit 4bfca28

Please sign in to comment.