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

fix memory leak and optimize cpu usage #12

Merged
merged 11 commits into from
Nov 29, 2023
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
11 changes: 0 additions & 11 deletions metrics/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ var (
Name: "send_blockings",
Help: "Number of send blocking connections",
})

countMessageFromClosed = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: promSubsystem,
Name: "sending_on_closed",
Help: "Number of sending on closed connections",
})
)

func IncNewConnection() {
Expand All @@ -51,10 +44,6 @@ func IncSendBlocking() {
countSendBlocking.Inc()
}

func IncMessageFromClosed() {
countMessageFromClosed.Inc()
}

func SetCurrentConnections(num int) {
gaugeCurrentConnections.Set(float64(num))
}
Expand Down
112 changes: 0 additions & 112 deletions relay/pendingSession.go

This file was deleted.

76 changes: 0 additions & 76 deletions relay/pendingSession_test.go

This file was deleted.

20 changes: 20 additions & 0 deletions relay/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package relay

import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"strings"
"sync"
Expand Down Expand Up @@ -159,6 +161,16 @@ func NewTopicSet() *TopicSet {
}
}

func (tm TopicSet) Get() map[string]struct{} {
tm.Lock()
defer tm.Unlock()
newmap := make(map[string]struct{})
for key, value := range tm.Data {
newmap[key] = value
}
return newmap
}

func (tm TopicSet) Set(topic string) {
tm.Lock()
defer tm.Unlock()
Expand All @@ -178,3 +190,11 @@ type ClientUnregisterEvent struct {
client *client
reason error
}

func generateRandomBytes16() string {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(buf)
}
24 changes: 24 additions & 0 deletions relay/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,27 @@ func TestGetTopicsByClientAndClear(t *testing.T) {
t.Errorf("length error, expected: %v, actual: %v", expectedLen, actualLen)
}
}

func TestTopicGet(t *testing.T) {
ts := NewTopicSet()

ts.Set("hello")
ts.Set("hello1")
ts.Set("hello2")

topics := ts.Get()
actualLen := len(topics)
expectedLen := 3
if actualLen != expectedLen {
t.Errorf("length error, expected: %v, actual: %v", expectedLen, actualLen)
}
if _, ok := topics["hello"]; !ok {
t.Errorf("key does not exists")
}
if _, ok := topics["hello1"]; !ok {
t.Errorf("key does not exists")
}
if _, ok := topics["hello2"]; !ok {
t.Errorf("key does not exists")
}
}
14 changes: 0 additions & 14 deletions relay/util.go

This file was deleted.

4 changes: 0 additions & 4 deletions relay/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package relay
import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/RabbyHub/derelay/log"
Expand All @@ -19,7 +18,6 @@ type client struct {

id string // randomly generate, just for logging
role RoleType // dapp or wallet
session string // session id
pubTopics *TopicSet
subTopics *TopicSet

Expand All @@ -31,7 +29,6 @@ func (c *client) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
if c != nil {
encoder.AddString("id", c.id)
encoder.AddString("role", string(c.role))
encoder.AddString("session", string(c.session))
encoder.AddArray("pubTopics", c.pubTopics)
encoder.AddArray("subTopics", c.subTopics)
}
Expand Down Expand Up @@ -87,7 +84,6 @@ func (c *client) send(message SocketMessage) {
case c.sendbuf <- message:
default:
metrics.IncSendBlocking()
log.Error("client sendbuf full", fmt.Errorf(""), zap.Any("client", c), zap.Any("len(sendbuf)", len(c.sendbuf)), zap.Any("message", message))
}
}

Expand Down
Loading
Loading