Skip to content

Instantly share code, notes, and snippets.

View adohe-zz's full-sized avatar

TonyAdo adohe-zz

View GitHub Profile
@adohe-zz
adohe-zz / compare
Last active August 29, 2015 14:04
filesystem operations vs socket operations within libuv
The libuv filesystem operations are different from socket operations. Socket
operations use the non-blocking operations provided by the operating system.
Filesystem operations use blocking functions internally, but invoke these
functions in a thread pool and notify watchers registered with the event loop
when application interaction is required.
@adohe-zz
adohe-zz / libuv
Created July 24, 2014 16:24
the arch of libuv framework
So now the real architecture of libuv is:
IOCP(Windows) + epoll<with Edge triggered interface>(*nix) ===> libuv
@adohe-zz
adohe-zz / middleware
Created July 23, 2014 16:37
a typical connect middleware writing style
/**
* A typical connect middleware
*/
module.exports = function dist (/* options */) {
// stuff
return function (req, res, next) {
// stuff
next();
}
}
@adohe-zz
adohe-zz / connect
Last active August 29, 2015 14:04
use connect to parse upload...
var http = require('http'),
connect = require('connect'),
fs = require('fs'),
util = require('util'),
port = process.env.PORT || 8000,
form = fs.readFileSync('./form.html');
var app = connect();
app.use(function (req, res, next) {
@adohe-zz
adohe-zz / quickSort
Created July 22, 2014 03:06
Quick Sort algorithm implementation in JavaScript
(function()
{
var
defaultCompare,
defaultSwap,
partition,
quickSort;
Array.prototype.quickSort = function(compare, swap)
{
@adohe-zz
adohe-zz / poll
Last active August 29, 2015 14:04
Long polling in Node
var express = require('express'),
url = require('url'),
port = process.env.PORT | 8000,
events = {},
pending = {},
maxAge = 60,
lastRequestId = 0,
connectionTimeout = 60;
var app = express();
@adohe-zz
adohe-zz / MutableInteger
Created July 20, 2014 07:09
volatile and synchronization in Java
package com.ado.java;
/**
* Not Thread Safe
*/
public class MutableInteger {
private int value;
public int getValue() {
@adohe-zz
adohe-zz / SafeListener
Created July 20, 2014 07:07
publish and escape problem in Java
package com.ado.java;
class Event {
public Event() {
}
}
abstract class EventListener {
public abstract void onEvent(Event event);
}
@adohe-zz
adohe-zz / LeetCodeSix
Created July 17, 2014 09:59
Regular expression implementation in java
package com.westudio.java;
public class LeetCodeSix {
private static boolean isMatch(String s, String p) {
if (p.length() == 0) {
return s.length() == 0;
}
@adohe-zz
adohe-zz / post
Created July 17, 2014 06:06
A simple JavaScript to handle post request data
var http = require('http'),
iconv = require('iconv-lite');
var server = http.createServer(function (req, res) {
var chunks = [],
size = 0,
finish = function (res) {
res.end('beepboop\n');
};