Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client.GetPoolStatus() #317

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions liteapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,10 @@ func (c *Client) GetNetworkGlobalID(ctx context.Context) (int32, error) {
return block.GlobalId, nil
}

func (c *Client) GetPoolStatus() pool.Status {
return c.pool.Status()
}

func (c *Client) WaitMasterchainBlock(ctx context.Context, seqno uint32, timeout time.Duration) (ton.BlockIDExt, error) {
t := uint32(timeout.Milliseconds())
client, _, err := c.pool.BestMasterchainClient(ctx)
Expand Down
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(),
}
}