'how could I fix ssh: tcpChan: deadline not supported when I connect to a Redis Server over SSH

I use the example To connect over SSH channel by go-redis/redis/v8, but when I debug this example and ping the redis, I receive a panic say ssh: tcpChan: deadline not supported .Here is my segmental code.


var ctx = context.Background()
sshConfig := &ssh.ClientConfig{
    User:            "root",
    Auth:            []ssh.AuthMethod{ssh.Password("dengzuxuan20010404!")},
    HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    Timeout:         15 * time.Second,
}

sshClient, err := ssh.Dial("tcp", "8.140.38.47:22", sshConfig)
if err != nil {
    panic(err)
}
client := redis.NewClient(&redis.Options{
    Addr: net.JoinHostPort("127.0.0.1", "6379"),
    Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
        return sshClient.Dial(network, addr)
    },
    // Disable timeouts, because SSH does not support deadlines.
    ReadTimeout:  -1,
    WriteTimeout: -1,
})
_,err=client.Ping(ctx).Result()
if err!=nil{
    logging.Info(err)
    panic(err)
}

and this is the panic show



Solution 1:[1]

Check if this is related to go-redis/redis issue 2057, which is supposed to be fixed by PR 2060 and commit 3961b95 (not yet released)

     sshConfig := &ssh.ClientConfig{
        User:            "root",
        Auth:            []ssh.AuthMethod{ssh.Password("password")},
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
        Timeout:         15 * time.Second,
    }

    client, err := ssh.Dial("tcp", "remoteIP:22", sshConfig)
    if err != nil {
        log.Println(fmt.Errorf("%v", err))
    }

    cli := redis.NewClient(&redis.Options{
        Addr: net.JoinHostPort("127.0.0.1", "6379"),
        Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
            return client.Dial(network, addr)
        },
        ReadTimeout:  -1,
        WriteTimeout: -1,
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VonC