telegraf-session-redis icon indicating copy to clipboard operation
telegraf-session-redis copied to clipboard

Redis Cluster support - Local changes working

Open sekarpdkt opened this issue 5 years ago • 0 comments

Hi I am able to used redis cluster.

Step 1: Store cluster nodes as an array in a variable like

config.TELEGRAM_SESSION_REDIS_CLUSTER_DETAILS=[
  {
    port: 7000,
    host: "192.168.1.17"
  },
  {
    port: 7001,
    host: "192.168.1.17"
  },
  {
    port: 7002,
    host: "192.168.1.17"
  },
  {
    port: 7003,
    host: "192.168.1.17"
  }
]

Step 2: Initiate session like

    let session = new RedisSession({
    store: config.TELEGRAM_SESSION_REDIS_CLUSTER_DETAILS,ttl: 1800
    })

Step3: In telegraf-session-redis, use ioredis instead of redis like

const redis = require('ioredis')

Step4: In telegraf-session-redis, handle connection like below

  constructor (options) {
    this.options = Object.assign({
      property: 'session',
      getSessionKey: (ctx) => ctx.from && ctx.chat && `${ctx.from.id}:${ctx.chat.id}`,
      store: {}
    }, options)
    this.client={};
    console.log(options.store.length)
    if(this.options.store.length>1)
        this.client = new redis.Cluster(this.options.store)
    else{
        this.client = new redis.createClient({
            host: this.options.store[0].host,
            port: this.options.store[0].port
        })
    }   
  }


Note: if you don't have rediscluster, we need to add only one host in the array and above code will choose between createClient and Cluster to establish the connection.

I can generate a PR if required.

Thanks.

sekarpdkt avatar Jul 21 '19 16:07 sekarpdkt