Skip to content

Commit

Permalink
on check db use main app db connection to avoid open new
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Aug 22, 2024
1 parent 8a37c46 commit 674a44d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func main() {

clientM := websocket.NewClientManager(rdb, int(queueConfig.ClientTTL))

app := websocket.NewApp(websocket.NewPool(), rdb, metrics, histories, clientM, queueConn)
app := websocket.NewApp(websocket.NewPool(), rdb, mdb, metrics, histories, clientM, queueConn)
app.StartConnectionsHeartbeat()
websocket.SetupRoutes(app)

Expand Down
4 changes: 3 additions & 1 deletion pkg/websocket/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import (
"github.com/ilhasoft/wwcs/pkg/metric"
"github.com/ilhasoft/wwcs/pkg/queue"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)

// App encapsulates application with resources.
type App struct {
ClientPool *ClientPool
RDB *redis.Client
MDB *mongo.Database
Metrics *metric.Service
Histories history.Service
ClientManager ClientManager
QueueConnectionManager queue.Connection
}

// Create new App instance.
func NewApp(pool *ClientPool, rdb *redis.Client, metrics *metric.Service, histories history.Service, clientM ClientManager, qconnM queue.Connection) *App {
func NewApp(pool *ClientPool, rdb *redis.Client, mdb *mongo.Database, metrics *metric.Service, histories history.Service, clientM ClientManager, qconnM queue.Connection) *App {
return &App{
ClientPool: pool,
RDB: rdb,
Expand Down
10 changes: 5 additions & 5 deletions pkg/websocket/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestParsePayload(t *testing.T) {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 3})
defer rdb.FlushAll(context.TODO())
cm := NewClientManager(rdb, 4)
app := NewApp(NewPool(), rdb, nil, nil, cm, nil)
app := NewApp(NewPool(), rdb, nil, nil, nil, cm, nil)
client, ws, s := newTestClient(t)
defer client.Conn.Close()
defer ws.Close()
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestCloseSession(t *testing.T) {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 3})
defer rdb.FlushAll(context.TODO())
cm := NewClientManager(rdb, 4)
app := NewApp(NewPool(), rdb, nil, nil, cm, nil)
app := NewApp(NewPool(), rdb, nil, nil, nil, cm, nil)
conn := NewOpenConnection(t)

client := &Client{
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestClientRegister(t *testing.T) {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 3})
defer rdb.FlushAll(context.TODO())
cm := NewClientManager(rdb, 4)
app := NewApp(NewPool(), rdb, nil, nil, cm, nil)
app := NewApp(NewPool(), rdb, nil, nil, nil, cm, nil)
var poolSize int

client, ws, s := newTestClient(t)
Expand Down Expand Up @@ -430,7 +430,7 @@ func TestRedirect(t *testing.T) {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 3})
defer rdb.FlushAll(context.TODO())
cm := NewClientManager(rdb, 4)
app := NewApp(NewPool(), rdb, nil, nil, cm, nil)
app := NewApp(NewPool(), rdb, nil, nil, nil, cm, nil)
c, ws, s := newTestClient(t)
defer c.Conn.Close()
defer ws.Close()
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestGetHistory(t *testing.T) {
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 3})
defer rdb.FlushAll(context.TODO())
cm := NewClientManager(rdb, 4)
_ = NewApp(NewPool(), rdb, nil, nil, cm, nil)
_ = NewApp(NewPool(), rdb, nil, nil, nil, cm, nil)
client, ws, s := newTestClient(t)
defer client.Conn.Close()
defer ws.Close()
Expand Down
14 changes: 2 additions & 12 deletions pkg/websocket/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"time"

log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -219,20 +217,12 @@ func CheckRedis(app *App) error {
}

func CheckDB(app *App) error {
cfg := config.Get().DB
dbURI := cfg.URI
options := options.Client().ApplyURI(dbURI)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
connection, err := mongo.Connect(ctx, options)
if err != nil {
log.Error("fail to connect to MongoDB", err.Error())
return err
}
if err := connection.Ping(ctx, nil); err != nil {
conn := app.MDB.Client()
if err := conn.Ping(ctx, nil); err != nil {
log.Error("fail to ping MongoDB", err.Error())
return err
}
connection.Disconnect(context.TODO())
return nil
}

0 comments on commit 674a44d

Please sign in to comment.