8000 🤗 [Question]: Can we add an option to database storages accept a already initialized connection? · Issue #967 · gofiber/storage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
🤗 [Question]: Can we add an option to database storages accept a already initialized connection? #967
Open
@mirusky

Description

@mirusky

Question Description

For example my app already opens a connection with redis and I'd like to reuse the same connection, since it's simple to create a health check endpoint that's only checks one redis connection instead of 2 or more.

Currently I'm opening 3 connections since my code opens 1 and I call redis.New twice, so I need to get the remaining connections using store.Conn and pass it as dependency to my health check controller to check all connections.

I'd like to know if it's possible to add an option to database driven storages like "Conn" that we can pass a already established connection. Something simillar to GORM/Existing database connection

Code Snippet (optional)

package main

import (
	"github.com/redis/go-redis/v9"

	fiberredis "github.com/gofiber/storage/redis"
)

func main() {
	url := "redis://localhost:6379?password=hello&protocol=3"
	opts, err := redis.ParseURL(url)
	if err != nil {
		panic(err)
	}
	rdb := redis.NewClient(opts) // Already connected

	store1 := fiberredis.New(redis.Config{URL: config.RedisURL + "/1"}) // New Conn
	store2 := fiberredis.New(redis.Config{URL: config.RedisURL + "/2"}) // New Conn

	// I'm thinking something like this:
        // Ref: https://github.com/redis/go-redis/issues/1018#issuecomment-517928987
	// db1 := rdb.Conn()
	// db1.Select(context.Background(), 1)
	// db2 := rdb.Conn()
	// db2.Select(context.Background(), 2)
	// store1 := fiberredis.New(redis.Config{Conn: db1})
	// store2 := fiberredis.New(redis.Config{Conn: db2})
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0