dongshuohuan5291 2018-11-18 04:58
浏览 454
已采纳

Redis在Docker容器中失去连接

I am making a PubSub using redigo and the connection is created by redis pool.

This is the Redis Pool code:

package main

import (
    "os"
    "os/signal"
    "syscall"
    "time"

    "github.com/gomodule/redigo/redis"
)

type IRedis interface {
    Addr() string
    Conn() redis.Conn
    Set(key string, body string) error
    Close()
}

type Redis struct {
    addr string
    pool *redis.Pool
}

func NewRedis(addr string) *Redis {
    r := &Redis{
        addr,
        &redis.Pool{
            MaxIdle:     50000,
            IdleTimeout: 240 * time.Second,
            Dial: func() (redis.Conn, error) {
                c, err := redis.Dial("tcp", addr)
                if err != nil {
                    return nil, err
                }
                return c, err
            },
            TestOnBorrow: func(c redis.Conn, t time.Time) error {
                _, err := c.Do("PING")
                return err
            },
        },
    }

    r.cleanupHook()
    return r
}

func (r *Redis) cleanupHook() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)
    signal.Notify(c, syscall.SIGTERM)
    signal.Notify(c, syscall.SIGKILL)
    go func() {
        <-c
        r.pool.Close()
        os.Exit(0)
    }()
}

func (r *Redis) Addr() string {
    return r.addr
}

func (r *Redis) Conn() redis.Conn {
    return r.pool.Get()
}

func (r *Redis) Set(key string, body string) error {
    p := r.pool.Get()
    defer p.Close()

    _, err := p.Do("SET", key, body)
    return err
}

func (r *Redis) Close() {
    r.pool.Close()
}

This is my PubSub code:

package main

import (
    "log"

    "github.com/gomodule/redigo/redis"
)

type PubSub struct {
    send chan string
    conn *redis.PubSubConn
}

func NewPubSub(s chan string, c *redis.PubSubConn) (*PubSub, error) {
    err := c.Subscribe("urls")
    if err != nil {
        return nil, err
    }

    return &PubSub{s, c}, nil
}

func (ps *PubSub) Start() {
    for {
        switch v := ps.conn.Receive().(type) {
        case redis.Message:
            data := string(v.Data)
            ps.send <- data

        case redis.Subscription:
            log.Printf("subscription message: %s: %s %d
", v.Channel, v.Kind, v.Count)

        case error:
            log.Println("error:", v)
            return
        }
    }
}

func (ps *PubSub) Close() {
    ps.conn.Close()
}

and after I receive 10k messages in my PubSub channel the connection is lost and I receive an redis.Error with the message EOF

Any thoughts why this is happen? Even when I run locally the problem happens

  • 写回答

1条回答 默认 最新

  • doujiao1949 2018-11-18 22:32
    关注

    The problem was with parameter in redis conf called client-output-buffer-limit pubsub which the default value is 32mb 8mb 60 and when the limit is reached redis closes the connection with the pubsub. To solve it I've increased the value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)