为pgxpool.Config设置DialFunc

huangapple go评论153阅读模式
英文:

Setting a DialFunc for pgxpool.Config

问题

我在使用"github.com/jackc/pgx/v4/pgxpool"库通过SSH隧道连接到Postgres数据库时遇到了问题。我似乎无法弄清楚如何设置DialFunc以建立连接所需的pgxpool.Config。这是我尝试的代码:

sshConnStr := mkPostgresXViaSSHConnStr(config, tz)
config, err := parseSshConnStr(sshConnStr)
sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", config.tunnelHost, config.tunnelConfig), &config.tunnelConfig)
cfg, err = pgxpool.ParseConfig(config.pgConnStr)
cfg.ConnConfig.DialFunc = func(network, addr string) (net.Conn, error) {
    return sshcon.Dial(network, addr)
}

当我尝试设置DialFunc时,我收到以下错误:cannot use (func(network, addr string) (net.Conn, error) literal) (value of type func(network string, addr string) (net.Conn, error)) as pgconn.DialFunc value in assignment

关于如何使其工作,有什么建议吗?

英文:

I am having trouble creating a connection to a postgres database via ssh tunneling with the "github.com/jackc/pgx/v4/pgxpool" library. I cannot seem to figure out how to set the DialFunc for the *pgxpool.Config that I would like to use to establish a connection. This is what I'm trying to do:

        sshConnStr := mkPostgresXViaSSHConnStr(config, tz)
		
		config, err := parseSshConnStr(sshConnStr)
        // var config *pgsshConfig

		sshcon, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", config.tunnelHost, config.tunnelConfig), &config.tunnelConfig)
        // var sshcon *ssh.Client

		cfg, err = pgxpool.ParseConfig(config.pgConnStr)
        // var cfg *pgxpool.Config

		cfg.ConnConfig.DialFunc = func(network, addr string) (net.Conn, error) {
			return sshcon.Dial(network, addr)
		}

When I try to set the DialFunc I get the following error: cannot use (func(network, addr string) (net.Conn, error) literal) (value of type func(network string, addr string) (net.Conn, error)) as pgconn.DialFunc value in assignment

Any suggestions about how to get this to work?

答案1

得分: 0

将上述代码添加上下文并转换为pgconn.DialFunc

package main

import (
	"context"
	"net"

	"github.com/jackc/pgconn"
	"github.com/jackc/pgx/v4/pgxpool"
)

func main() {
	cfg, _ := pgxpool.ParseConfig("")
	myDial := func(ctx context.Context, network, addr string) (net.Conn, error) {
		return nil, nil
	}
	cfg.ConnConfig.DialFunc = pgconn.DialFunc(myDial)
}

请注意,这段代码的作用是将自定义的myDial函数赋值给cfg.ConnConfig.DialFunc,以便在建立与PostgreSQL数据库的连接时使用自定义的拨号函数。

英文:

Add context and cast to pgconn.DialFunc:

package main

import (
        "context"
        "net"

        "github.com/jackc/pgconn"
        "github.com/jackc/pgx/v4/pgxpool"
)

func main() {
        cfg, _ := pgxpool.ParseConfig("")
        myDial := func(ctx context.Context, network, addr string) (net.Conn, error) {
                return nil, nil
        }
        cfg.ConnConfig.DialFunc = pgconn.DialFunc(myDial)
}

答案2

得分: 0

我看错了DialFunc的类型。对于pgx/v3.6.2,只需要网络和地址作为参数。在更新的版本中,pgx/v4还需要传递context.Context作为变量(无论是否实际使用)。修复错误的更新如下:


cfg.ConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
			return sshcon.Dial(network, addr)
}

英文:

I was looking at the wrong type for the DialFunc. For pgx/v3.6.2 only the network and address were needed as parameters. In the updated version, pgx/v4 context.Context needs to be passed as a variable as well (whether or not it is actually used). The update which fixed the error is this:


cfg.ConnConfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
			return sshcon.Dial(network, addr)
}

huangapple
  • 本文由 发表于 2022年1月20日 01:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70774969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定