redis.js 3.4 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
'use strict';

3
4
const config = require('config');

mntmn's avatar
mntmn committed
5
6
// this is a mock version of the Redis API,
// emulating Redis if it is not available locally
mntmn's avatar
mntmn committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var notRedis = {
  state: {},
  topics: {},
  
  publish: function(topic, msg, cb) {
    if (!this.topics[topic]) {
      this.topics[topic] = {
        subscribers: []
      };
    }
    var t=this.topics[topic];
    for (var i=0; i<t.subscribers.length; i++) {
      var s=t.subscribers[i];
      if (s.handler) {
        s.handler(topic, msg);
      }
    }
    if (cb) cb(null);
  },

  subscribe: function(topics, cb) {
    var handle = {
      handler: null,
      on: function(evt, cb) {
        if (evt == "message") {
          this.handler = cb;
        }
      }
    };
    
    for (var i=0; i<topics.length; i++) {
      var topic = topics[i];
      if (!this.topics[topic]) {
        this.topics[topic] = {
          subscribers: []
        };
      }
    
      var t=this.topics[topic];
      t.subscribers.push(handle);
    }

mntmn's avatar
mntmn committed
49
    cb(null, topics.length);
mntmn's avatar
mntmn committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    return handle;
  },

  get: function(key, cb) {
    cb(null, this.state[key]);
    return this.state[key];
  },

  set: function(key, val, cb) {
    this.state[key] = val;
    cb();
  },

  del: function(key, cb) {
    delete this.state[key];
    cb(null);
  },

  sadd: function(key, skey, cb) {
    if (!this.state[key]) this.state[key] = {};
    this.state[key][skey] = true;
    cb(null);
  },

  srem: function(key, skey, cb) {
    if (this.state[key]) {
      delete this.state[key][skey];
    }
    cb(null);
  },

  smembers: function(key, cb) {
    cb(null, Object.keys(this.state[key]));
  },

  incr: function(key, cb) {
    if (!this.state[key]) this.state[key] = 0;
    this.state[key]++;
mntmn's avatar
mntmn committed
88
    cb(null, this.state[key]);
mntmn's avatar
mntmn committed
89
90
91
92
93
  },

  expire: function() {
  },
}
mntmn's avatar
mntmn committed
94
95

module.exports = {
mntmn's avatar
mntmn committed
96
  connectRedis: function() {
97
98
99
100
101
102
    if (config.get("redis_mock")) {
      this.connection = notRedis;
    } else {
      const redisHost = process.env.REDIS_PORT_6379_TCP_ADDR || 'sync';
      this.connection = new RedisConnection(6379, redisHost);
    }
mntmn's avatar
mntmn committed
103
  },
mntmn's avatar
mntmn committed
104
105
106
107
108
  getConnection: function() {
    this.connectRedis();
    return this.connection;
  },
  sendMessage: function(action, model, attributes, channelId) {
mntmn's avatar
mntmn committed
109
110
111
112
113
114
115
116
    const data = JSON.stringify({
      channel_id: channelId,
      action: action,
      model: model,
      object: attributes
    });
    this.connection.publish('updates', data);
  },
mntmn's avatar
mntmn committed
117
  logIp: function(ip, cb) {
mntmn's avatar
mntmn committed
118
119
120
121
    this.connection.incr("ip_"+ ip, (err, socketCounter) => {
      cb();
    });
  },
mntmn's avatar
mntmn committed
122
  rateLimit: function(namespace, ip, cb) {
mntmn's avatar
mntmn committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    const key = "limit_"+ namespace + "_"+ ip;
    const redis = this.connection;

    redis.get(key, (err, count)=> {
      if (count) {
        if(count < 150) {
          redis.incr(key, (err, newCount) => {
            if (newCount==150) {
              // limit
            }
            cb(true);
          });
        } else {
          cb(false);
        }
      } else {
        redis.set(key, 1, (err, count) => {
          redis.expire(key, 1800, (err, expResult) => {
            cb(true);
          });
        });
      }
    });
  }, 
mntmn's avatar
mntmn committed
147
  isOnlineInSpace: function(user, space, cb) {
mntmn's avatar
mntmn committed
148
149
150
151
152
153
154
155
156
157
158
    this.connection.smembers("space_" + space._id.toString(), function(err, list) {
      if (err) cb(err);
      else {
        var users = list.filter(function(item) {
          return user._id.toString() === item;
        });
        cb(null, (users.length > 0));
      }
    });
  }
};
mntmn's avatar
mntmn committed
159
160
161

return module.exports;