Skip to content

Commit 4bfca28

Browse files
aleksej-paschenkoakos-tk
authored andcommitted
Add client.GetPoolStatus()
1 parent 40dcba5 commit 4bfca28

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

liteapi/client.go

+4
Original file line numberDiff line numberDiff line change
@@ -1089,3 +1089,7 @@ func (c *Client) GetNetworkGlobalID(ctx context.Context) (int32, error) {
10891089
c.networkGlobalID = &block.GlobalId
10901090
return block.GlobalId, nil
10911091
}
1092+
1093+
func (c *Client) GetPoolStatus() pool.Status {
1094+
return c.pool.Status()
1095+
}

liteapi/pool/conn_pool.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type conn interface {
5858
Run(ctx context.Context, detectArchive bool)
5959
IsArchiveNode() bool
6060
AverageRoundTrip() time.Duration
61+
Status() ConnStatus
6162
}
6263

6364
// New returns a new instance of a connections pool.
@@ -79,8 +80,9 @@ func (p *ConnPool) InitializeConnections(ctx context.Context, timeout time.Durat
7980
cli, _ := connect(ctx, timeout, server)
8081
// TODO: log error
8182
clientsCh <- clientWrapper{
82-
connID: connID,
83-
cli: cli,
83+
connID: connID,
84+
cli: cli,
85+
serverHost: server.Host,
8486
}
8587
}(connID, server)
8688
}
@@ -99,7 +101,7 @@ func (p *ConnPool) InitializeConnections(ctx context.Context, timeout time.Durat
99101
continue
100102
}
101103
if p.ConnectionsNumber() < maxConnections {
102-
c := p.addConnection(wrapper.connID, wrapper.cli)
104+
c := p.addConnection(wrapper.connID, wrapper.cli, wrapper.serverHost)
103105
go c.Run(context.TODO(), detectArchiveNodes)
104106
}
105107
if p.ConnectionsNumber() == maxConnections {
@@ -134,15 +136,17 @@ func connect(ctx context.Context, timeout time.Duration, server config.LiteServe
134136
}
135137

136138
type clientWrapper struct {
137-
connID int
138-
cli *liteclient.Client
139+
connID int
140+
cli *liteclient.Client
141+
serverHost string
139142
}
140143

141-
func (p *ConnPool) addConnection(connID int, cli *liteclient.Client) *connection {
144+
func (p *ConnPool) addConnection(connID int, cli *liteclient.Client, serverHost string) *connection {
142145
p.mu.Lock()
143146
defer p.mu.Unlock()
144147
c := &connection{
145148
id: connID,
149+
serverHost: serverHost,
146150
client: cli,
147151
masterHeadUpdatedCh: p.masterHeadUpdatedCh,
148152
}
@@ -369,3 +373,20 @@ func (p *ConnPool) WaitMasterchainSeqno(ctx context.Context, seqno uint32, timeo
369373
}
370374
}
371375
}
376+
377+
type Status struct {
378+
Connections []ConnStatus
379+
}
380+
381+
func (p *ConnPool) Status() Status {
382+
p.mu.Lock()
383+
defer p.mu.Unlock()
384+
385+
connStatuses := make([]ConnStatus, 0, len(p.conns))
386+
for _, c := range p.conns {
387+
connStatuses = append(connStatuses, c.Status())
388+
}
389+
return Status{
390+
Connections: connStatuses,
391+
}
392+
}

liteapi/pool/conn_pool_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (m *mockConn) Client() *liteclient.Client {
4747
panic("implement me")
4848
}
4949

50+
func (m *mockConn) Status() ConnStatus {
51+
panic("implement me")
52+
}
53+
5054
func (m *mockConn) Run(ctx context.Context, detectArchiveNodes bool) {
5155
}
5256

liteapi/pool/connection.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
)
1111

1212
type connection struct {
13-
id int
14-
client *liteclient.Client
13+
id int
14+
serverHost string
15+
client *liteclient.Client
1516

1617
// masterHeadUpdatedCh is used to send a notification when a known master head is changed.
1718
masterHeadUpdatedCh chan masterHeadUpdated
@@ -154,3 +155,17 @@ func (c *connection) setArchive(archive bool) {
154155
func (c *connection) AverageRoundTrip() time.Duration {
155156
return c.client.AverageRoundTrip()
156157
}
158+
159+
type ConnStatus struct {
160+
ServerHost string
161+
Connected bool
162+
Archive bool
163+
}
164+
165+
func (c *connection) Status() ConnStatus {
166+
return ConnStatus{
167+
ServerHost: c.serverHost,
168+
Connected: c.IsOK(),
169+
Archive: c.IsArchiveNode(),
170+
}
171+
}

0 commit comments

Comments
 (0)