redis.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. const config = require('config');
  3. // this is a mock version of the Redis API,
  4. // emulating Redis if it is not available locally
  5. var notRedis = {
  6. state: {},
  7. topics: {},
  8. publish: function(topic, msg, cb) {
  9. if (!this.topics[topic]) {
  10. this.topics[topic] = {
  11. subscribers: []
  12. };
  13. }
  14. var t=this.topics[topic];
  15. for (var i=0; i<t.subscribers.length; i++) {
  16. var s=t.subscribers[i];
  17. if (s.handler) {
  18. s.handler(topic, msg);
  19. }
  20. }
  21. if (cb) cb(null);
  22. },
  23. subscribe: function(topics, cb) {
  24. var handle = {
  25. handler: null,
  26. on: function(evt, cb) {
  27. if (evt == "message") {
  28. this.handler = cb;
  29. }
  30. }
  31. };
  32. for (var i=0; i<topics.length; i++) {
  33. var topic = topics[i];
  34. if (!this.topics[topic]) {
  35. this.topics[topic] = {
  36. subscribers: []
  37. };
  38. }
  39. var t=this.topics[topic];
  40. t.subscribers.push(handle);
  41. }
  42. cb(null, topics.length);
  43. return handle;
  44. },
  45. get: function(key, cb) {
  46. cb(null, this.state[key]);
  47. return this.state[key];
  48. },
  49. set: function(key, val, cb) {
  50. this.state[key] = val;
  51. cb();
  52. },
  53. del: function(key, cb) {
  54. delete this.state[key];
  55. cb(null);
  56. },
  57. sadd: function(key, skey, cb) {
  58. if (!this.state[key]) this.state[key] = {};
  59. this.state[key][skey] = true;
  60. cb(null);
  61. },
  62. srem: function(key, skey, cb) {
  63. if (this.state[key]) {
  64. delete this.state[key][skey];
  65. }
  66. cb(null);
  67. },
  68. smembers: function(key, cb) {
  69. cb(null, Object.keys(this.state[key]));
  70. },
  71. incr: function(key, cb) {
  72. if (!this.state[key]) this.state[key] = 0;
  73. this.state[key]++;
  74. cb(null, this.state[key]);
  75. },
  76. expire: function() {
  77. },
  78. }
  79. module.exports = {
  80. connectRedis: function() {
  81. if (config.get("redis_mock")) {
  82. this.connection = notRedis;
  83. } else {
  84. const redisHost = process.env.REDIS_PORT_6379_TCP_ADDR || 'sync';
  85. this.connection = new RedisConnection(6379, redisHost);
  86. }
  87. },
  88. getConnection: function() {
  89. this.connectRedis();
  90. return this.connection;
  91. },
  92. sendMessage: function(action, model, attributes, channelId) {
  93. const data = JSON.stringify({
  94. channel_id: channelId,
  95. action: action,
  96. model: model,
  97. object: attributes
  98. });
  99. this.connection.publish('updates', data);
  100. },
  101. logIp: function(ip, cb) {
  102. this.connection.incr("ip_"+ ip, (err, socketCounter) => {
  103. cb();
  104. });
  105. },
  106. rateLimit: function(namespace, ip, cb) {
  107. const key = "limit_"+ namespace + "_"+ ip;
  108. const redis = this.connection;
  109. redis.get(key, (err, count)=> {
  110. if (count) {
  111. if(count < 150) {
  112. redis.incr(key, (err, newCount) => {
  113. if (newCount==150) {
  114. // limit
  115. }
  116. cb(true);
  117. });
  118. } else {
  119. cb(false);
  120. }
  121. } else {
  122. redis.set(key, 1, (err, count) => {
  123. redis.expire(key, 1800, (err, expResult) => {
  124. cb(true);
  125. });
  126. });
  127. }
  128. });
  129. },
  130. isOnlineInSpace: function(user, space, cb) {
  131. this.connection.smembers("space_" + space._id.toString(), function(err, list) {
  132. if (err) cb(err);
  133. else {
  134. var users = list.filter(function(item) {
  135. return user._id.toString() === item;
  136. });
  137. cb(null, (users.length > 0));
  138. }
  139. });
  140. }
  141. };
  142. return module.exports;