<commit_before>// 缓存 {id:{name:////}}
let cahce = {};

// 登录则建立连接，初始化 cache

// 创建:则插入cache，并推送至另一端

// 中断:插入数据库

// 结束:插入数据库

// 断开连接，则删除cache

let dataModels = require("./model/mongo.js");
let tomatomodel = dataModels.tomatomodel;
let toamato_hash = {};
/**
 * 视频聊天中转服务器
*/
module.exports = io => {
	io.of("/tomato").on("connection", socket => {
		/**
         * 加载已有番茄钟
         */
		socket.on("load_tomato", (userid, endname) => {
            let hash = toamato_hash[userid];
            if (hash && hash.tomato){
                io
                .of("/tomato")
                .to(socket.id)
                .emit("load_tomato_succeed", hash.tomato);
            }
        });

		/**
         * 开始
         */
		socket.on("start_tomato", (userid, endname, tomato) => {
			let hash = toamato_hash[userid];
			hash.end = endname;
			let socketList = hash.socketList;
			hash.tomato = tomato;
			for (let so of socketList) {
				if (so != socket.id) {
					io
						.of("/tomato")
						.to(so)
						.emit("other_end_start_tomato", tomato);
				}
			}
		});

		/**
         * 番茄钟中断
         */
		socket.on("break_tomato", async (userid, endname, tomato) => {
			let hash = toamato_hash[userid];
			hash.end = endname;
			let socketList = hash.socketList;
			hash.tomato = tomato;

			// 保存当前番茄钟
			const result = await model.create(tomato);
			if (result) {
				for (let so of socketList) {
					if (so != socket.id) {
						io
							.of("/tomato")
							.to(so)
							.emit("other_end_break_tomato", tomato);
					}
				}
			}
		});

		/**
         * 番茄钟完成
         */
		// socket.on("finish", (userid, tomato) => {

        // });

		/**
		 * 离线处理
		 */
		socket.on("disconnect", () => {
			let hash = toamato_hash[userid];
			let socketList = hash.socketList;
			if (result) {
				var index = socketList.indexOf(socket.id);
                if (index > -1) {
                    socketList.splice(index, 1);
                }
			}
		});
	});
};<commit_msg>add timeout strategy<commit_after>// 缓存 {id:{name:////}}
let cahce = {};
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);

let dataModels = require("./model/mongo.js");
let tomatomodel = dataModels.tomatomodel;
let toamato_hash = {};
/**
 * 视频聊天中转服务器
*/
module.exports = io => {
	io.of("/tomato").on("connection", socket => {
		
		/**
         * 加载已有番茄钟
         */
		socket.on("load_tomato", (userid, endname) => {
            let hash = toamato_hash[userid];
            if (hash && hash.tomato){
                io
                .of("/tomato")
                .to(socket.id)
                .emit("load_tomato_succeed", hash.tomato);
            }
        });

		/**
         * 开始
         */
		socket.on("start_tomato", (userid, endname, tomato) => {
			let hash = toamato_hash[userid];
			hash.end = endname;
			let socketList = hash.socketList;
			hash.tomato = tomato;
			// 设定番茄钟任务
			TIME_OUT_ID = setTimeoutPromise(1000*60*25,userid).then((userid) => {
				let thash = toamato_hash[userid];
				let tomato = thash.tomato;
				tomato.endTime = new Date();
				tomato.succeed = 1;
				await model.create(tomato);
				thash.tomato = null;
			});
			hash.TIME_OUT_ID= TIME_OUT_ID;
			for (let so of socketList) {
				if (so != socket.id) {
					io
						.of("/tomato")
						.to(so)
						.emit("other_end_start_tomato", tomato);
				}
			}
		});

		/**
         * 番茄钟中断
         */
		socket.on("break_tomato", async (userid, endname) => {
			let hash = toamato_hash[userid];
			hash.end = endname;
			let socketList = hash.socketList;
			let tomato = hash.tomato;
			tomato.endTime = new Date();
			tomato.succeed = 0;
			clearTimeout(hash.TIME_OUT_ID);
			// 保存当前番茄钟
			const result = await model.create(tomato);
			if (result) {
				for (let so of socketList) {
					if (so != socket.id) {
						io
							.of("/tomato")
							.to(so)
							.emit("other_end_break_tomato", tomato);
					}
				}
			}
		});
		
		/**
		 * 离线处理
		 */
		socket.on("disconnect", () => {
			let hash = toamato_hash[userid];
			let socketList = hash.socketList;
			if (result) {
				var index = socketList.indexOf(socket.id);
                if (index > -1) {
                    socketList.splice(index, 1);
                }
			}
		});

		/**
         * 番茄钟完成
         */
		// socket.on("finish", (userid, tomato) => {
        // });

	});
};
{'fraction_non_alphanumeric': 0.16589755422242733, 'fraction_numerical': 0.003230272265805261, 'mean_word_length': 1.8842315369261478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6220831', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1689, 'n_words': 278}