Skip to content

Commit de51855

Browse files
authored
Merge pull request #634 from pastelnetwork/PSL-894_trackOpenConnections
[PSL-894] implement gRPC open connection tracker
2 parents d52cfe5 + 1699f04 commit de51855

File tree

8 files changed

+142
-21
lines changed

8 files changed

+142
-21
lines changed

supernode/cmd/app.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ func runApp(ctx context.Context, config *configs.Config) error {
277277
// statsMngr.Add("mdl", metadb)
278278
statsMngr.Add("pasteld", healthcheck_lib.NewPastelStatsClient(pastelClient))
279279

280-
// Debug service
281-
debugSerivce := debug.NewService(config.DebugService, p2p, storageChallenger)
282-
283280
// server
284281
grpc := server.New(config.Server,
285282
"service",
@@ -301,6 +298,11 @@ func runApp(ctx context.Context, config *configs.Config) error {
301298
hermes.NewHermes(p2p),
302299
)
303300

301+
openConsMap := grpc.GetConnTrackerMap()
302+
303+
// Debug service
304+
debugSerivce := debug.NewService(config.DebugService, p2p, storageChallenger, openConsMap)
305+
304306
log.WithContext(ctx).Infof("Config: %s", config)
305307

306308
go func() {

supernode/debug/debug.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@ package debug
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"time"
78

8-
json "github.com/json-iterator/go"
9-
10-
"github.com/pastelnetwork/gonode/supernode/services/common"
11-
12-
"github.com/pastelnetwork/gonode/supernode/services/storagechallenge"
13-
14-
"github.com/pastelnetwork/gonode/common/storage/local"
15-
169
"github.com/gorilla/mux"
10+
json "github.com/json-iterator/go"
1711
"github.com/pastelnetwork/gonode/common/log"
12+
"github.com/pastelnetwork/gonode/common/storage/local"
1813
"github.com/pastelnetwork/gonode/common/version"
1914
"github.com/pastelnetwork/gonode/p2p"
20-
21-
"net/http"
15+
"github.com/pastelnetwork/gonode/supernode/node/grpc/server"
16+
"github.com/pastelnetwork/gonode/supernode/services/common"
17+
"github.com/pastelnetwork/gonode/supernode/services/storagechallenge"
2218
)
2319

2420
const (
@@ -70,14 +66,16 @@ type Service struct {
7066
httpServer *http.Server
7167
cleanTracker *CleanTracker
7268
scService *storagechallenge.SCService
69+
trackedCon map[string][]*server.TrackedConn
7370
}
7471

7572
// NewService returns debug service
76-
func NewService(config *Config, p2pClient p2p.Client, srvc *storagechallenge.SCService) *Service {
73+
func NewService(config *Config, p2pClient p2p.Client, srvc *storagechallenge.SCService, openCons map[string][]*server.TrackedConn) *Service {
7774
service := &Service{
78-
config: config,
79-
p2pClient: p2pClient,
80-
scService: srvc,
75+
config: config,
76+
p2pClient: p2pClient,
77+
scService: srvc,
78+
trackedCon: openCons,
8179
}
8280

8381
router := mux.NewRouter()
@@ -93,6 +91,7 @@ func NewService(config *Config, p2pClient p2p.Client, srvc *storagechallenge.SCS
9391
router.HandleFunc("/selfhealing/cleanup", service.selfHealingChallengeCleanup).Methods(http.MethodDelete) // cleanup self-healing data from history db
9492
router.HandleFunc("/health", service.p2pHealth).Methods(http.MethodGet)
9593
router.HandleFunc("/storage/initiate", service.storageInitiate).Methods(http.MethodGet)
94+
router.HandleFunc("/open/cons", service.getOpenCons).Methods(http.MethodGet)
9695

9796
service.httpServer = &http.Server{
9897
Addr: fmt.Sprintf("%s:%d", defaultListenAddr, config.HTTPPort),
@@ -227,6 +226,13 @@ func (service *Service) storageInitiate(writer http.ResponseWriter, _ *http.Requ
227226
responseWithJSON(writer, http.StatusOK, fmt.Sprintf("ok %s", "storage challenge initiated"))
228227
}
229228

229+
func (service *Service) getOpenCons(writer http.ResponseWriter, _ *http.Request) {
230+
ctx := context.Background()
231+
log.WithContext(ctx).Info("returning open cons")
232+
233+
responseWithJSON(writer, http.StatusOK, service.trackedCon)
234+
}
235+
230236
func (service *Service) p2pHealth(writer http.ResponseWriter, request *http.Request) {
231237
ctx := service.contextWithLogPrefix(request.Context())
232238
log.WithContext(ctx).Info("p2pHealth")
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package server
2+
3+
import (
4+
"net"
5+
"sync"
6+
"time"
7+
8+
"github.com/pastelnetwork/gonode/common/log"
9+
)
10+
11+
type connTrackListener struct {
12+
net.Listener
13+
connTrack *ConnectionTracker
14+
}
15+
16+
// Accept wraps the Listener.Accept method and adds the connection reference to the map.
17+
func (l *connTrackListener) Accept() (net.Conn, error) {
18+
c, err := l.Listener.Accept()
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
conn := &TrackedConn{
24+
Conn: c,
25+
connTrack: l.connTrack,
26+
OpenedAt: time.Now().UTC(),
27+
}
28+
log.WithField("con", c.RemoteAddr()).Info("new connection request received")
29+
30+
l.connTrack.add(c.RemoteAddr().String(), conn)
31+
32+
return conn, nil
33+
}
34+
35+
// TrackedConn represents an open conn
36+
type TrackedConn struct {
37+
net.Conn `json:"-"`
38+
connTrack *ConnectionTracker `json:"-"`
39+
OpenedAt time.Time `json:"opened_at"`
40+
}
41+
42+
// Close wraps the Conn.Close method and removes the connection reference from the map.
43+
func (c *TrackedConn) Close() error {
44+
err := c.Conn.Close()
45+
if err != nil {
46+
log.Warnf("Error closing connection: %v, from: %v\n", err, c.RemoteAddr())
47+
}
48+
c.connTrack.remove(c.LocalAddr().String(), c)
49+
return err
50+
}
51+
52+
// ConnectionTracker keeps track of open connection
53+
type ConnectionTracker struct {
54+
mu sync.Mutex
55+
conns map[string][]*TrackedConn
56+
}
57+
58+
// NewConnectionTracker initializes the connection tracker map.
59+
func NewConnectionTracker() *ConnectionTracker {
60+
return &ConnectionTracker{
61+
conns: make(map[string][]*TrackedConn),
62+
}
63+
}
64+
65+
// add inserts a new connection reference into the in-memory map.
66+
func (ct *ConnectionTracker) add(serverAddr string, c *TrackedConn) {
67+
ct.mu.Lock()
68+
defer ct.mu.Unlock()
69+
70+
ct.conns[serverAddr] = append(ct.conns[serverAddr], c)
71+
log.Infof("New connection: %v, total: %d for server: %s\n", c.RemoteAddr(), len(ct.conns[serverAddr]), serverAddr)
72+
}
73+
74+
// remove deletes a connection reference from the map.
75+
func (ct *ConnectionTracker) remove(serverAddr string, c *TrackedConn) {
76+
ct.mu.Lock()
77+
defer ct.mu.Unlock()
78+
79+
connsForServer, ok := ct.conns[serverAddr]
80+
if !ok {
81+
return
82+
}
83+
84+
for i, conn := range connsForServer {
85+
if conn == c {
86+
ct.conns[serverAddr] = append(connsForServer[:i], connsForServer[i+1:]...)
87+
break
88+
}
89+
}
90+
91+
if len(ct.conns[serverAddr]) == 0 {
92+
delete(ct.conns, serverAddr)
93+
}
94+
95+
log.Infof("Closed connection: %v, total: %d for server: %s\n", c.RemoteAddr(), len(ct.conns[serverAddr]), serverAddr)
96+
}

supernode/node/grpc/server/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Server struct {
3030
name string
3131
secClient alts.SecClient
3232
secInfo *alts.SecInfo
33+
connTrack *ConnectionTracker // connection tracker
3334
}
3435

3536
// Run starts the server
@@ -62,6 +63,12 @@ func (server *Server) listen(ctx context.Context, address string, grpcServer *gr
6263
return errors.Errorf("listen: %w", err).WithField("address", address)
6364
}
6465

66+
// The listener that will track connections.
67+
listen = &connTrackListener{
68+
Listener: listen,
69+
connTrack: server.connTrack, // connection tracker
70+
}
71+
6572
errCh := make(chan error, 1)
6673
go func() {
6774
defer errors.Recover(func(recErr error) { err = recErr })
@@ -131,5 +138,11 @@ func New(config *Config, name string, secClient alts.SecClient, secInfo *alts.Se
131138
secInfo: secInfo,
132139
services: services,
133140
name: name,
141+
connTrack: NewConnectionTracker(), // create a new connection tracker
134142
}
135143
}
144+
145+
// GetConnTrackerMap returns the map keeping track of open cons
146+
func (server *Server) GetConnTrackerMap() map[string][]*TrackedConn {
147+
return server.connTrack.conns
148+
}

tools/download_thumbnail/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/gorilla/websocket v1.5.0
99
github.com/pastelnetwork/gonode/walletnode v0.0.0-20210723172801-5d493665cdd7
1010
github.com/pkg/errors v0.9.1
11-
goa.design/goa/v3 v3.13.0
11+
goa.design/goa/v3 v3.13.1
1212
)
1313

1414
require (

tools/download_thumbnail/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ goa.design/goa/v3 v3.3.1/go.mod h1:Br8id+evuay7oUIK3BXr6iFJVLxAiirb+FPzfYUYOWg=
590590
goa.design/goa/v3 v3.4.3 h1:MLZAuJGNVSSxXgwVPiQun33wrCXvb94Ka1w4GyENHwE=
591591
goa.design/goa/v3 v3.4.3/go.mod h1:ebbEQvCugeHeWM6Vu6zqdmiCHDICiHVNZf3L58a87J0=
592592
goa.design/goa/v3 v3.13.0/go.mod h1:dZd1Xib+mDb49UIwlRY1xOvSXPeCbA0z5K/303LHvBk=
593+
goa.design/goa/v3 v3.13.1/go.mod h1:v8tZEXhHSRXTsgtA9rK6tlVKA+7L6S0vqu8x6U9a17c=
593594
goa.design/plugins/v3 v3.3.1/go.mod h1:wJqL5wrmKUk4TW6M2tVxqwZfKwW1DWTxJUQL1ytWdHk=
594595
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
595596
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

tools/pastel-api/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ require (
4040
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
4141
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
4242
golang.org/x/crypto v0.11.0 // indirect
43-
golang.org/x/sys v0.11.0 // indirect
44-
golang.org/x/term v0.10.0 // indirect
45-
golang.org/x/text v0.11.0 // indirect
43+
golang.org/x/sys v0.12.0 // indirect
44+
golang.org/x/term v0.12.0 // indirect
45+
golang.org/x/text v0.13.0 // indirect
4646
gopkg.in/ini.v1 v1.67.0 // indirect
4747
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
4848
gopkg.in/yaml.v3 v3.0.1 // indirect

tools/pastel-api/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,11 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
381381
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
382382
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
383383
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
384+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
384385
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
385386
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
386387
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
388+
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
387389
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
388390
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
389391
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -394,6 +396,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
394396
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
395397
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
396398
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
399+
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
397400
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
398401
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
399402
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

0 commit comments

Comments
 (0)