SlideShare a Scribd company logo
@孢⼦子响⻢马 
2014.10.25 
Javascript on Fiber
什么是 fibjs? 
• 服务器端 Javascript 开发 
• 基于 Google v8 引擎构建 
• 完全⾮非阻塞,fiber 驱动,拒绝回调,类似 goroutine 
• CommonJS 模块系统 
• 80,000+ ⾏行 c/c++ 代码,充分挖掘多核性能 
• 27 个基本模块,覆盖常⻅见服务器应⽤用场景
27 个内置模块
82 个公开对象类型 
object 
+ dispose() 
+ toString() 
+ toJSON() 
+ valueOf() 
Buffer 
+ operator[] 
+ length 
+ Buffer() 
+ Buffer() 
+ Buffer() 
+ resize() 
+ write() 
+ write() 
+ write() 
+ readUInt8() 
+ readUInt16LE() 
+ readUInt16BE() 
+ readUInt32LE() 
+ readUInt32BE() 
+ readInt8() 
+ readInt16LE() 
+ readInt16BE() 
+ readInt32LE() 
+ readInt32BE() 
+ readInt64LE() 
+ readInt64BE() 
+ readFloatLE() 
+ readFloatBE() 
+ readDoubleLE() 
+ readDoubleBE() 
+ writeUInt8() 
+ writeUInt16LE() 
+ writeUInt16BE() 
+ writeUInt32LE() 
+ writeUInt32BE() 
+ writeInt8() 
+ writeInt16LE() 
+ writeInt16BE() 
+ writeInt32LE() 
+ writeInt32BE() 
+ writeInt64LE() 
+ writeInt64BE() 
+ writeFloatLE() 
+ writeFloatBE() 
+ writeDoubleLE() 
+ writeDoubleBE() 
+ slice() 
+ hex() 
+ base64() 
+ toString() 
+ toString() 
Cipher 
+ name 
+ keySize 
+ ivSize 
+ blockSize 
+ Cipher() 
+ Cipher() 
+ Cipher() 
+ paddingMode() 
+ encrypt() 
+ decrypt() 
DbConnection 
+ close() 
+ begin() 
+ commit() 
+ rollback() 
+ execute() 
+ execute() 
+ format() 
DBRow 
+ operator[] 
+ operator[String] 
Digest 
+ size 
+ update() 
+ digest() 
+ digest() 
Expect 
+ to 
+ be 
+ been 
+ is 
+ that 
+ and 
+ have 
+ with 
+ at 
+ of 
+ same 
+ not 
+ deep 
+ ok 
+ true 
+ false 
+ null 
+ undefined 
+ function 
+ object 
+ array 
+ string 
+ number 
+ boolean 
+ exist 
+ a() 
+ an() 
+ equal() 
+ eql() 
+ above() 
+ greaterThan() 
+ least() 
+ below() 
+ lessThan() 
+ most() 
+ property() 
+ property() 
+ closeTo() 
Fiber 
+ caller 
+ join() 
Function 
+ start() 
GridFS 
+ files 
+ chunks 
+ retrieve() 
+ store() 
+ store() 
+ exists() 
+ remove() 
Handler 
+ invoke() 
HttpCollection 
+ operator[String] 
+ clear() 
+ has() 
+ first() 
+ all() 
+ add() 
+ add() 
+ set() 
+ set() 
+ remove() 
HttpCookie 
+ name 
+ value 
+ domain 
+ path 
+ expires 
+ httpOnly 
+ secure 
+ HttpCookie() 
+ HttpCookie() 
+ parse() 
+ match() 
HttpUploadData 
+ fileName 
+ contentType 
+ contentTransferEncoding 
+ body 
Image 
+ width 
+ height 
+ format 
+ type 
+ colorsTotal 
+ transparent 
+ alphaBlending 
+ getData() 
+ save() 
+ save() 
+ colorAllocate() 
+ colorAllocate() 
+ colorAllocateAlpha() 
+ colorAllocateAlpha() 
+ colorClosest() 
+ colorClosest() 
+ colorClosestHWB() 
+ colorClosestHWB() 
+ colorClosestAlpha() 
+ colorClosestAlpha() 
+ colorExact() 
+ colorExact() 
+ colorExactAlpha() 
+ colorExactAlpha() 
+ colorResolve() 
+ colorResolve() 
+ colorResolveAlpha() 
+ colorResolveAlpha() 
+ colorDeallocate() 
+ clip() 
+ getPixel() 
+ getTrueColorPixel() 
+ setPixel() 
+ setThickness() 
+ line() 
+ rectangle() 
+ filledRectangle() 
+ polygon() 
+ openPolygon() 
+ filledPolygon() 
+ ellipse() 
+ filledEllipse() 
+ arc() 
+ filledArc() 
+ fill() 
+ fillToBorder() 
+ colorReplace() 
+ clone() 
+ resample() 
+ crop() 
+ flip() 
+ rotate() 
+ convert() 
+ copy() 
+ copyMerge() 
+ copyMergeGray() 
+ copyResized() 
+ copyResampled() 
+ copyRotated() 
Int64 
+ hi 
+ lo 
+ Int64() 
+ Int64() 
+ Int64() 
+ Int64() 
+ equal() 
+ compare() 
+ shiftLeft() 
+ shiftRight() 
+ and() 
+ or() 
+ xor() 
+ add() 
+ sub() 
+ toNumber() 
+ toString() 
LevelDB 
+ has() 
+ get() 
+ mget() 
+ set() 
+ mset() 
+ remove() 
+ remove() 
+ forEach() 
+ between() 
+ begin() 
+ commit() 
+ close() 
List 
+ operator[] 
+ length 
+ List() 
+ resize() 
+ push() 
+ push() 
+ pop() 
+ slice() 
+ concat() 
+ every() 
+ filter() 
+ forEach() 
+ map() 
+ toArray() 
Lock 
+ Lock() 
+ acquire() 
+ release() 
LruCache 
+ size 
+ LruCache() 
+ clear() 
+ has() 
+ get() 
+ get() 
+ set() 
+ put() 
+ put() 
+ remove() 
+ isEmpty() 
Map 
+ size 
+ operator[String] 
+ Map() 
+ clear() 
+ has() 
+ get() 
+ put() 
+ put() 
+ remove() 
+ isEmpty() 
Message 
+ value 
+ params 
+ result 
+ body 
+ length 
+ stream 
+ response 
+ Message() 
+ read() 
+ readAll() 
+ write() 
+ clear() 
+ sendTo() 
+ readFrom() 
MongoCollection 
+ operator[String] 
+ find() 
+ findOne() 
+ findAndModify() 
+ insert() 
+ insert() 
+ save() 
+ update() 
+ update() 
+ remove() 
+ runCommand() 
+ runCommand() 
+ drop() 
+ ensureIndex() 
+ reIndex() 
+ dropIndex() 
+ dropIndexes() 
+ getIndexes() 
+ getCollection() 
MongoCursor 
+ skip() 
+ limit() 
+ sort() 
+ hasNext() 
+ next() 
+ count() 
+ size() 
+ forEach() 
+ map() 
+ toArray() 
+ hint() 
MongoDB 
+ operator[String] 
+ fs 
+ getCollection() 
+ runCommand() 
+ runCommand() 
+ oid() 
+ close() 
MongoID 
PKey 
+ name 
+ keySize 
+ publicKey 
+ PKey() 
+ genRsaKey() 
+ genEcKey() 
+ isPrivate() 
+ clone() 
+ importKey() 
+ importKey() 
+ exportPem() 
+ exportDer() 
+ encrypt() 
+ decrypt() 
+ sign() 
+ verify() 
Queue 
+ length 
+ Queue() 
+ add() 
+ offer() 
+ remove() 
+ poll() 
+ element() 
+ peek() 
+ clear() 
+ toArray() 
Redis 
+ command() 
+ set() 
+ setNX() 
+ setXX() 
+ mset() 
+ mset() 
+ msetNX() 
+ msetNX() 
+ append() 
+ setRange() 
+ getRange() 
+ strlen() 
+ bitcount() 
+ get() 
+ mget() 
+ mget() 
+ getset() 
+ decr() 
+ incr() 
+ setBit() 
+ getBit() 
+ exists() 
+ type() 
+ keys() 
+ del() 
+ del() 
+ expire() 
+ ttl() 
+ persist() 
+ rename() 
+ renameNX() 
+ sub() 
+ sub() 
+ unsub() 
+ unsub() 
+ unsub() 
+ unsub() 
+ psub() 
+ psub() 
+ unpsub() 
+ unpsub() 
+ unpsub() 
+ unpsub() 
+ onsuberror() 
+ pub() 
+ getHash() 
+ getList() 
+ getSet() 
+ getSortedSet() 
+ dump() 
+ restore() 
+ close() 
RedisHash 
+ set() 
+ setNX() 
+ mset() 
+ mset() 
+ get() 
+ mget() 
+ mget() 
+ incr() 
+ getAll() 
+ keys() 
+ len() 
+ exists() 
+ del() 
+ del() 
RedisList 
+ push() 
+ push() 
+ pop() 
+ rpush() 
+ rpush() 
+ rpop() 
+ set() 
+ get() 
+ insertBefore() 
+ insertAfter() 
+ remove() 
+ trim() 
+ len() 
+ range() 
RedisSet 
+ add() 
+ add() 
+ remove() 
+ remove() 
+ len() 
+ exists() 
+ members() 
+ pop() 
+ randMember() 
+ randMember() 
RedisSortedSet 
+ add() 
+ add() 
+ score() 
+ incr() 
+ remove() 
+ remove() 
+ len() 
+ count() 
+ range() 
+ rangeRev() 
+ rank() 
+ rankRev() 
Regex 
+ lastIndex 
+ global 
+ ignoreCase 
+ multiline 
+ exec() 
+ test() 
SandBox 
+ SandBox() 
+ SandBox() 
+ add() 
+ add() 
+ addScript() 
+ remove() 
+ run() 
+ require() 
Smtp 
+ socket 
+ Smtp() 
+ connect() 
+ command() 
+ hello() 
+ login() 
+ from() 
+ to() 
+ data() 
+ quit() 
Stat 
+ name 
+ size 
+ mode 
+ mtime 
+ atime 
+ ctime 
+ isWritable() 
+ isReadable() 
+ isExecutable() 
+ isHidden() 
+ isDirectory() 
+ isFile() 
+ isSymbolicLink() 
+ isMemory() 
+ isSocket() 
Stats 
+ operator[String] 
+ Stats() 
+ Stats() 
+ inc() 
+ dec() 
+ add() 
+ reset() 
+ uptime() 
Stream 
+ read() 
+ write() 
+ close() 
+ copyTo() 
TcpServer 
+ socket 
+ handler 
+ stats 
+ TcpServer() 
+ TcpServer() 
+ run() 
+ asyncRun() 
+ stop() 
TextColor 
+ notice 
+ warn 
+ error 
+ highLight 
Trigger 
+ Trigger() 
+ on() 
+ on() 
+ once() 
+ once() 
+ off() 
+ off() 
+ off() 
+ trigger() 
Url 
+ href 
+ protocol 
+ slashes 
+ auth 
+ username 
+ password 
+ host 
+ hostname 
+ port 
+ path 
+ pathname 
+ search 
+ query 
+ hash 
+ Url() 
+ Url() 
+ parse() 
+ format() 
+ resolve() 
+ normalize() 
uuidValue 
+ data() 
+ detail() 
X509Cert 
+ version 
+ serial 
+ issuer 
+ subject 
+ notBefore 
+ notAfter 
+ ca 
+ pathlen 
+ usage 
+ type 
+ publicKey 
+ next 
+ X509Cert() 
+ load() 
+ load() 
+ loadFile() 
+ loadRootCerts() 
+ verify() 
+ dump() 
+ clear() 
X509Crl 
+ X509Crl() 
+ load() 
+ load() 
+ loadFile() 
+ dump() 
+ clear() 
X509Req 
+ subject 
+ publicKey 
+ X509Req() 
+ X509Req() 
+ load() 
+ load() 
+ loadFile() 
+ exportPem() 
+ exportDer() 
+ sign() 
XmlNode 
+ nodeType 
+ nodeName 
+ nodeValue 
+ ownerDocument 
+ parentNode 
+ childNodes 
+ firstChild 
+ lastChild 
+ previousSibling 
+ nextSibling 
+ hasChildNodes() 
+ normalize() 
+ cloneNode() 
+ lookupPrefix() 
+ lookupNamespaceURI() 
+ insertBefore() 
+ insertAfter() 
+ appendChild() 
+ replaceChild() 
+ removeChild() 
XmlNodeList 
+ length 
+ operator[] 
+ item() 
MySQL 
+ rxBufferSize 
+ txBufferSize 
+ use() 
SQLite 
+ fileName 
+ timeout 
+ backup() 
AsyncWait 
+ end() 
Chain 
+ Chain() 
+ append() 
+ append() 
HttpHandler 
+ crossDomain 
+ forceGZIP 
+ maxHeadersCount 
+ maxUploadSize 
+ handler 
+ stats 
+ HttpHandler() 
PacketHandler 
+ maxSize 
+ handler 
+ stats 
+ PacketHandler() 
Routing 
+ Routing() 
+ append() 
+ append() 
SslHandler 
+ verification 
+ ca 
+ handler 
+ SslHandler() 
+ SslHandler() 
DBResult 
+ insertId 
+ affected 
+ fields 
Condition 
+ Condition() 
+ Condition() 
+ wait() 
+ notify() 
+ notifyAll() 
Event 
+ Event() 
+ isSet() 
+ set() 
+ pulse() 
+ clear() 
+ wait() 
Semaphore 
+ Semaphore() 
+ wait() 
+ post() 
+ trywait() 
HttpMessage 
+ protocol 
+ headers 
+ keepAlive 
+ maxHeadersCount 
+ maxUploadSize 
+ hasHeader() 
+ firstHeader() 
+ allHeader() 
+ addHeader() 
+ addHeader() 
+ setHeader() 
+ setHeader() 
+ removeHeader() 
PacketMessage 
+ maxSize 
+ PacketMessage() 
HttpRequest 
+ method 
+ address 
+ queryString 
+ cookies 
+ form 
+ query 
+ HttpRequest() 
HttpResponse 
+ status 
+ cookies 
+ HttpResponse() 
+ addCookie() 
+ redirect() 
BlockQueue 
+ BlockQueue() 
+ put() 
+ take() 
BufferedStream 
+ stream 
+ charset 
+ EOL 
+ BufferedStream() 
+ readText() 
+ readLine() 
+ readLines() 
+ readUntil() 
+ readPacket() 
+ writeText() 
+ writeLine() 
+ writePacket() 
SeekableStream 
+ seek() 
+ tell() 
+ rewind() 
+ size() 
+ readAll() 
+ stat() 
Socket 
+ family 
+ type 
+ remoteAddress 
+ remotePort 
+ localAddress 
+ localPort 
+ Socket() 
+ connect() 
+ bind() 
+ bind() 
+ listen() 
+ accept() 
+ recv() 
+ recvFrom() 
+ send() 
+ sendto() 
SslSocket 
+ verification 
+ ca 
+ peerCert 
+ SslSocket() 
+ SslSocket() 
+ connect() 
+ accept() 
File 
+ name 
+ truncate() 
+ eof() 
+ flush() 
+ chmod() 
MemoryStream 
+ MemoryStream() 
+ setTime() 
+ clone() 
+ clear() 
HttpServer 
+ crossDomain 
+ forceGZIP 
+ maxHeadersCount 
+ maxUploadSize 
+ httpStats 
+ HttpServer() 
+ HttpServer() 
SslServer 
+ verification 
+ ca 
+ SslServer() 
+ SslServer() 
+ SslServer() 
+ SslServer() 
HttpsServer 
+ verification 
+ ca 
+ HttpsServer() 
+ HttpsServer() 
+ HttpsServer() 
+ HttpsServer() 
XmlCharacterData 
+ data 
+ length 
+ substringData() 
+ appendData() 
+ insertData() 
+ deleteData() 
+ replaceData() 
XmlDocument 
+ inputEncoding 
+ xmlStandalone 
+ xmlVersion 
+ doctype 
+ documentElement 
+ head 
+ title 
+ body 
+ XmlDocument() 
+ load() 
+ getElementsByTagName() 
+ getElementsByTagNameNS() 
+ createElement() 
+ createElementNS() 
+ createTextNode() 
+ createComment() 
+ createCDATASection() 
+ createProcessingInstruction() 
XmlDocumentType 
+ name 
+ publicId 
+ systemId 
XmlElement 
+ namespaceURI 
+ prefix 
+ localName 
+ tagName 
+ id 
+ textContent 
+ innerHTML 
+ className 
+ attributes 
+ getAttribute() 
+ getAttributeNS() 
+ setAttribute() 
+ setAttributeNS() 
+ removeAttribute() 
+ removeAttributeNS() 
+ hasAttribute() 
+ hasAttributeNS() 
+ getElementsByTagName() 
+ getElementsByTagNameNS() 
XmlProcessingInstruction 
+ target 
+ data 
XmlComment XmlText 
+ splitText() 
XmlCDATASection
更多模块持续发展中
fibjs 和 nodejs 有什么不同?
来看⼀一段常⽤用的数据库操作 
nodejs fibjs
等⼀一下!!!!!! 
nodejs 告诉我们: 
会阻塞,导致其他任务不能运⾏行。 
写这样的代码难道不会被打吗?
nodejs ⼀一直说: sync 是邪恶的
祂们说: 
In busy processes, the programmer is strongly 
encouraged to use the asynchronous versions of these 
calls. The synchronous versions will block the entire 
process until they complete--halting all connections.
⽽而 module.js ⾥里华丽丽滴写着这样的代码
因此 nodejs 在 require 时不响应请求
fibjs 不会出现这样的尴尬 
当调⽤用阻塞⽅方法时, 
fibjs 会保护现场,挂起当前 
fiber,并将 Javascript 引擎切 
换⾄至下⼀一个排队的 fiber。挂起 
的 fiber 在⼯工作完成后会进⼊入 
排队队列等待恢复
fibjs 有三种类型的线程(nodejs 类似) 
Javascript 线程⼯工作线程池异步 io 线程
Javascript 线程是 fibjs 的主线程 
Javascript 线程• ⼀一个 fibjs 进程只有⼀一个 Javascript 线程 
• Javascript 线程内会运⾏行多个 fiber 
• Javascript 代码在 fiber 内运⾏行 
• 同⼀一时刻只会有⼀一个 fiber 激活 
• 当前 fiber 休眠时其它 fiber 才会恢复 
• fiber 不释放就会把 Javascript 塞住
socket 操作会委托给异步 io 线程处理 
Javascript 线程异步 io 线程 
sock.recv 
return
阻塞操作也会委托给⼯工作线程池处理 
Javascript 线程⼯工作线程池 
fs.readFile 
return
最终避免 fiber 阻塞导致 Javascript 被挂起
fibjs 的并发:轻量的⽤用户空间线程 fiber 
• 操作系统线程的切换成本昂贵 
• fiber 是在应⽤用级的线程系统 
• 运⾏行现场完整保护,对应⽤用开发透明 
• 可以直接使⽤用的编程逻辑,包括 try/catch 
• 基于堆栈切换现场,模块调⽤用和返回更⾼高效 
• ⾮非抢先,⽆无需内存级锁,并发逻辑简单
再回顾⼀一下这两段代码,是不是很爽 
nodejs fibjs
nodejs 常说: 使⽤用回调,所以更快?
祂们⽤用 Apache 和 nginx 的对⽐比来证明
⽽而事实上,异步并不等同于回调
这是⼀一个 web 服务器基准测试 
每秒请求数 
60,000 
50,000 
40,000 
30,000 
20,000 
10,000 
0 
fibjs nodejs 
100 500 900 1300 1700 2100 2500 2900 3300 3700 4100 4500 4900 
并发连接
平均 7.25 倍
有⺴⽹网友祭出 cluster ⼤大法
但是 fibjs 真的是多线程吗?
fibjs 同样是单线程的 
• fibjs 和 nodejs 都是以 v8 为脚本引擎 
• v8 本⾝身被设计为 isolate 不⽀支持多线程 
• nodejs 使⽤用异步回调复⽤用 v8 线程 
• fibjs 使⽤用 fiber 复⽤用 v8 线程 
• 因此 fibjs 和 nodejs 都是单线程
那么 fibjs 快在哪⾥里呢?
Javascript 线程是珍贵的计算资源 
Javascript 线程
fibjs 将更多的计算移⾄至⼯工作线程池 
Javascript 线程⼯工作线程池异步 io 线程
⼯工作线程不再仅仅完成阻塞操作 
⼯工作线程池 
• 同步 io 操作,⽐比如磁盘操作,⽂文件处理 
• 计算密集的⽅方法,⽐比如加密,压缩,图像 
• 异步 io 基础上的复杂逻辑,⽐比如协议处理 
ps: 异步 io 基础上的计算,全部以异步⽅方式完成,并 
发性能更⾼高。
从⽽而提⾼高 Javascript 线程利⽤用率 
Javascript 线程Javascript 线程
多线程的⼯工作线程池也可以发挥多核性能 
Javascript 线程⼯工作线程池异步 io 线程
fibjs 是更彻底的异步 
并将异步延伸⾄至⼯工作线程池 
只是不在 js 层表现出来
fibjs 的应⽤用逻辑是在太过于直⽩白和简单 
毫⽆无 nodejs 回调处理的各种惊艳技巧 
就不再展⽰示那些毫⽆无趣味的⽰示例代码了
良好的⽣生态从积极参与和分享开始 
⺴⽹网站: http://fibjs.org/ 
代码: https://github.com/xicilion/fibjs 
社区: http://baoz.cn/fibjs
fibjs ⼯工程实践:孢⼦子社区 baoz.cn
孢⼦子社区的 fibjs 实践 
• 基于 http ⻓长连接的客户端消息推送 
• 基于 tcp 的移动 app 消息推送 
• 基于 fiber 的数据并⾏行读取 
• 基于 fiber 的后台运算异步化 
• 基于 SandBox 的服务器端模块 app 化与数据隔离 
• 基于 SandBox 的服务器代码热升级
欢迎加⼊入孢⼦子团队 
简历请寄 info@baoz.cn
Javascript on Fiber

More Related Content

PDF
Python速成指南
PDF
Ooredis
PDF
Python 温故
PPTX
Python入門:5大概念初心者必備
PPTX
Python入門:5大概念初心者必備 2021/11/18
PPT
设计模式分享
PDF
Mongo db introduction
PDF
Advanced heap exploitaion
Python速成指南
Ooredis
Python 温故
Python入門:5大概念初心者必備
Python入門:5大概念初心者必備 2021/11/18
设计模式分享
Mongo db introduction
Advanced heap exploitaion

Viewers also liked (6)

PDF
Bt hack-full-disclosure Uncovered – //NONSA//NOGCHQ//NOGOV - CC BY-ND
PDF
The Rise of BaaS A Utopia for Client-Side Developers
PDF
Firmen die Monsanto verwenden
PDF
PTS- Wissenschaftliche Grundlagen des Analysesystems
PDF
Future topic geoengineering
PDF
Wirtschaftsspionage in Zeiten von Vollüberwachung
Bt hack-full-disclosure Uncovered – //NONSA//NOGCHQ//NOGOV - CC BY-ND
The Rise of BaaS A Utopia for Client-Side Developers
Firmen die Monsanto verwenden
PTS- Wissenschaftliche Grundlagen des Analysesystems
Future topic geoengineering
Wirtschaftsspionage in Zeiten von Vollüberwachung
Ad

More from Marc Manthey (20)

PDF
Ecolog2000
PDF
Wi fi technology - an uncontrolled global experiment on the health of mankind...
PDF
Wifi - a thalidomide in the making - who cares
PDF
Silent weapons for quiet wars
PDF
Nasa the futureof war
PDF
Xkeyscore
PDF
Vision 2015 - Geheimdienste Global verknüpfen
PDF
Wenn diese Petition es vor den Petitionsausschuss schafft geb ich einen Aus
PDF
Top Secret National Reconnaissance Office declassification guideline
PDF
Third part nations - S-CCO leaked document 1
PDF
Der spiegel 28-2013_-_nur_der_snowden_und_nsa_part
PDF
CoCCA OpenReg development, registry software
PDF
PDF
UNICEF Terms of use
PDF
Piraten analyse
PDF
Multistakeholder processes
PDF
Internetstadt köln-veranstaltung
PDF
Petitionsausschuss
PDF
Web rtc videoconference
PDF
A swarm intelligence-based unicast routing protocol for hybrid ad hoc networks
Ecolog2000
Wi fi technology - an uncontrolled global experiment on the health of mankind...
Wifi - a thalidomide in the making - who cares
Silent weapons for quiet wars
Nasa the futureof war
Xkeyscore
Vision 2015 - Geheimdienste Global verknüpfen
Wenn diese Petition es vor den Petitionsausschuss schafft geb ich einen Aus
Top Secret National Reconnaissance Office declassification guideline
Third part nations - S-CCO leaked document 1
Der spiegel 28-2013_-_nur_der_snowden_und_nsa_part
CoCCA OpenReg development, registry software
UNICEF Terms of use
Piraten analyse
Multistakeholder processes
Internetstadt köln-veranstaltung
Petitionsausschuss
Web rtc videoconference
A swarm intelligence-based unicast routing protocol for hybrid ad hoc networks
Ad

Javascript on Fiber - http://fibjs.org

  • 2. 什么是 fibjs? • 服务器端 Javascript 开发 • 基于 Google v8 引擎构建 • 完全⾮非阻塞,fiber 驱动,拒绝回调,类似 goroutine • CommonJS 模块系统 • 80,000+ ⾏行 c/c++ 代码,充分挖掘多核性能 • 27 个基本模块,覆盖常⻅见服务器应⽤用场景
  • 4. 82 个公开对象类型 object + dispose() + toString() + toJSON() + valueOf() Buffer + operator[] + length + Buffer() + Buffer() + Buffer() + resize() + write() + write() + write() + readUInt8() + readUInt16LE() + readUInt16BE() + readUInt32LE() + readUInt32BE() + readInt8() + readInt16LE() + readInt16BE() + readInt32LE() + readInt32BE() + readInt64LE() + readInt64BE() + readFloatLE() + readFloatBE() + readDoubleLE() + readDoubleBE() + writeUInt8() + writeUInt16LE() + writeUInt16BE() + writeUInt32LE() + writeUInt32BE() + writeInt8() + writeInt16LE() + writeInt16BE() + writeInt32LE() + writeInt32BE() + writeInt64LE() + writeInt64BE() + writeFloatLE() + writeFloatBE() + writeDoubleLE() + writeDoubleBE() + slice() + hex() + base64() + toString() + toString() Cipher + name + keySize + ivSize + blockSize + Cipher() + Cipher() + Cipher() + paddingMode() + encrypt() + decrypt() DbConnection + close() + begin() + commit() + rollback() + execute() + execute() + format() DBRow + operator[] + operator[String] Digest + size + update() + digest() + digest() Expect + to + be + been + is + that + and + have + with + at + of + same + not + deep + ok + true + false + null + undefined + function + object + array + string + number + boolean + exist + a() + an() + equal() + eql() + above() + greaterThan() + least() + below() + lessThan() + most() + property() + property() + closeTo() Fiber + caller + join() Function + start() GridFS + files + chunks + retrieve() + store() + store() + exists() + remove() Handler + invoke() HttpCollection + operator[String] + clear() + has() + first() + all() + add() + add() + set() + set() + remove() HttpCookie + name + value + domain + path + expires + httpOnly + secure + HttpCookie() + HttpCookie() + parse() + match() HttpUploadData + fileName + contentType + contentTransferEncoding + body Image + width + height + format + type + colorsTotal + transparent + alphaBlending + getData() + save() + save() + colorAllocate() + colorAllocate() + colorAllocateAlpha() + colorAllocateAlpha() + colorClosest() + colorClosest() + colorClosestHWB() + colorClosestHWB() + colorClosestAlpha() + colorClosestAlpha() + colorExact() + colorExact() + colorExactAlpha() + colorExactAlpha() + colorResolve() + colorResolve() + colorResolveAlpha() + colorResolveAlpha() + colorDeallocate() + clip() + getPixel() + getTrueColorPixel() + setPixel() + setThickness() + line() + rectangle() + filledRectangle() + polygon() + openPolygon() + filledPolygon() + ellipse() + filledEllipse() + arc() + filledArc() + fill() + fillToBorder() + colorReplace() + clone() + resample() + crop() + flip() + rotate() + convert() + copy() + copyMerge() + copyMergeGray() + copyResized() + copyResampled() + copyRotated() Int64 + hi + lo + Int64() + Int64() + Int64() + Int64() + equal() + compare() + shiftLeft() + shiftRight() + and() + or() + xor() + add() + sub() + toNumber() + toString() LevelDB + has() + get() + mget() + set() + mset() + remove() + remove() + forEach() + between() + begin() + commit() + close() List + operator[] + length + List() + resize() + push() + push() + pop() + slice() + concat() + every() + filter() + forEach() + map() + toArray() Lock + Lock() + acquire() + release() LruCache + size + LruCache() + clear() + has() + get() + get() + set() + put() + put() + remove() + isEmpty() Map + size + operator[String] + Map() + clear() + has() + get() + put() + put() + remove() + isEmpty() Message + value + params + result + body + length + stream + response + Message() + read() + readAll() + write() + clear() + sendTo() + readFrom() MongoCollection + operator[String] + find() + findOne() + findAndModify() + insert() + insert() + save() + update() + update() + remove() + runCommand() + runCommand() + drop() + ensureIndex() + reIndex() + dropIndex() + dropIndexes() + getIndexes() + getCollection() MongoCursor + skip() + limit() + sort() + hasNext() + next() + count() + size() + forEach() + map() + toArray() + hint() MongoDB + operator[String] + fs + getCollection() + runCommand() + runCommand() + oid() + close() MongoID PKey + name + keySize + publicKey + PKey() + genRsaKey() + genEcKey() + isPrivate() + clone() + importKey() + importKey() + exportPem() + exportDer() + encrypt() + decrypt() + sign() + verify() Queue + length + Queue() + add() + offer() + remove() + poll() + element() + peek() + clear() + toArray() Redis + command() + set() + setNX() + setXX() + mset() + mset() + msetNX() + msetNX() + append() + setRange() + getRange() + strlen() + bitcount() + get() + mget() + mget() + getset() + decr() + incr() + setBit() + getBit() + exists() + type() + keys() + del() + del() + expire() + ttl() + persist() + rename() + renameNX() + sub() + sub() + unsub() + unsub() + unsub() + unsub() + psub() + psub() + unpsub() + unpsub() + unpsub() + unpsub() + onsuberror() + pub() + getHash() + getList() + getSet() + getSortedSet() + dump() + restore() + close() RedisHash + set() + setNX() + mset() + mset() + get() + mget() + mget() + incr() + getAll() + keys() + len() + exists() + del() + del() RedisList + push() + push() + pop() + rpush() + rpush() + rpop() + set() + get() + insertBefore() + insertAfter() + remove() + trim() + len() + range() RedisSet + add() + add() + remove() + remove() + len() + exists() + members() + pop() + randMember() + randMember() RedisSortedSet + add() + add() + score() + incr() + remove() + remove() + len() + count() + range() + rangeRev() + rank() + rankRev() Regex + lastIndex + global + ignoreCase + multiline + exec() + test() SandBox + SandBox() + SandBox() + add() + add() + addScript() + remove() + run() + require() Smtp + socket + Smtp() + connect() + command() + hello() + login() + from() + to() + data() + quit() Stat + name + size + mode + mtime + atime + ctime + isWritable() + isReadable() + isExecutable() + isHidden() + isDirectory() + isFile() + isSymbolicLink() + isMemory() + isSocket() Stats + operator[String] + Stats() + Stats() + inc() + dec() + add() + reset() + uptime() Stream + read() + write() + close() + copyTo() TcpServer + socket + handler + stats + TcpServer() + TcpServer() + run() + asyncRun() + stop() TextColor + notice + warn + error + highLight Trigger + Trigger() + on() + on() + once() + once() + off() + off() + off() + trigger() Url + href + protocol + slashes + auth + username + password + host + hostname + port + path + pathname + search + query + hash + Url() + Url() + parse() + format() + resolve() + normalize() uuidValue + data() + detail() X509Cert + version + serial + issuer + subject + notBefore + notAfter + ca + pathlen + usage + type + publicKey + next + X509Cert() + load() + load() + loadFile() + loadRootCerts() + verify() + dump() + clear() X509Crl + X509Crl() + load() + load() + loadFile() + dump() + clear() X509Req + subject + publicKey + X509Req() + X509Req() + load() + load() + loadFile() + exportPem() + exportDer() + sign() XmlNode + nodeType + nodeName + nodeValue + ownerDocument + parentNode + childNodes + firstChild + lastChild + previousSibling + nextSibling + hasChildNodes() + normalize() + cloneNode() + lookupPrefix() + lookupNamespaceURI() + insertBefore() + insertAfter() + appendChild() + replaceChild() + removeChild() XmlNodeList + length + operator[] + item() MySQL + rxBufferSize + txBufferSize + use() SQLite + fileName + timeout + backup() AsyncWait + end() Chain + Chain() + append() + append() HttpHandler + crossDomain + forceGZIP + maxHeadersCount + maxUploadSize + handler + stats + HttpHandler() PacketHandler + maxSize + handler + stats + PacketHandler() Routing + Routing() + append() + append() SslHandler + verification + ca + handler + SslHandler() + SslHandler() DBResult + insertId + affected + fields Condition + Condition() + Condition() + wait() + notify() + notifyAll() Event + Event() + isSet() + set() + pulse() + clear() + wait() Semaphore + Semaphore() + wait() + post() + trywait() HttpMessage + protocol + headers + keepAlive + maxHeadersCount + maxUploadSize + hasHeader() + firstHeader() + allHeader() + addHeader() + addHeader() + setHeader() + setHeader() + removeHeader() PacketMessage + maxSize + PacketMessage() HttpRequest + method + address + queryString + cookies + form + query + HttpRequest() HttpResponse + status + cookies + HttpResponse() + addCookie() + redirect() BlockQueue + BlockQueue() + put() + take() BufferedStream + stream + charset + EOL + BufferedStream() + readText() + readLine() + readLines() + readUntil() + readPacket() + writeText() + writeLine() + writePacket() SeekableStream + seek() + tell() + rewind() + size() + readAll() + stat() Socket + family + type + remoteAddress + remotePort + localAddress + localPort + Socket() + connect() + bind() + bind() + listen() + accept() + recv() + recvFrom() + send() + sendto() SslSocket + verification + ca + peerCert + SslSocket() + SslSocket() + connect() + accept() File + name + truncate() + eof() + flush() + chmod() MemoryStream + MemoryStream() + setTime() + clone() + clear() HttpServer + crossDomain + forceGZIP + maxHeadersCount + maxUploadSize + httpStats + HttpServer() + HttpServer() SslServer + verification + ca + SslServer() + SslServer() + SslServer() + SslServer() HttpsServer + verification + ca + HttpsServer() + HttpsServer() + HttpsServer() + HttpsServer() XmlCharacterData + data + length + substringData() + appendData() + insertData() + deleteData() + replaceData() XmlDocument + inputEncoding + xmlStandalone + xmlVersion + doctype + documentElement + head + title + body + XmlDocument() + load() + getElementsByTagName() + getElementsByTagNameNS() + createElement() + createElementNS() + createTextNode() + createComment() + createCDATASection() + createProcessingInstruction() XmlDocumentType + name + publicId + systemId XmlElement + namespaceURI + prefix + localName + tagName + id + textContent + innerHTML + className + attributes + getAttribute() + getAttributeNS() + setAttribute() + setAttributeNS() + removeAttribute() + removeAttributeNS() + hasAttribute() + hasAttributeNS() + getElementsByTagName() + getElementsByTagNameNS() XmlProcessingInstruction + target + data XmlComment XmlText + splitText() XmlCDATASection
  • 6. fibjs 和 nodejs 有什么不同?
  • 8. 等⼀一下!!!!!! nodejs 告诉我们: 会阻塞,导致其他任务不能运⾏行。 写这样的代码难道不会被打吗?
  • 10. 祂们说: In busy processes, the programmer is strongly encouraged to use the asynchronous versions of these calls. The synchronous versions will block the entire process until they complete--halting all connections.
  • 12. 因此 nodejs 在 require 时不响应请求
  • 13. fibjs 不会出现这样的尴尬 当调⽤用阻塞⽅方法时, fibjs 会保护现场,挂起当前 fiber,并将 Javascript 引擎切 换⾄至下⼀一个排队的 fiber。挂起 的 fiber 在⼯工作完成后会进⼊入 排队队列等待恢复
  • 14. fibjs 有三种类型的线程(nodejs 类似) Javascript 线程⼯工作线程池异步 io 线程
  • 15. Javascript 线程是 fibjs 的主线程 Javascript 线程• ⼀一个 fibjs 进程只有⼀一个 Javascript 线程 • Javascript 线程内会运⾏行多个 fiber • Javascript 代码在 fiber 内运⾏行 • 同⼀一时刻只会有⼀一个 fiber 激活 • 当前 fiber 休眠时其它 fiber 才会恢复 • fiber 不释放就会把 Javascript 塞住
  • 16. socket 操作会委托给异步 io 线程处理 Javascript 线程异步 io 线程 sock.recv return
  • 18. 最终避免 fiber 阻塞导致 Javascript 被挂起
  • 19. fibjs 的并发:轻量的⽤用户空间线程 fiber • 操作系统线程的切换成本昂贵 • fiber 是在应⽤用级的线程系统 • 运⾏行现场完整保护,对应⽤用开发透明 • 可以直接使⽤用的编程逻辑,包括 try/catch • 基于堆栈切换现场,模块调⽤用和返回更⾼高效 • ⾮非抢先,⽆无需内存级锁,并发逻辑简单
  • 22. 祂们⽤用 Apache 和 nginx 的对⽐比来证明
  • 24. 这是⼀一个 web 服务器基准测试 每秒请求数 60,000 50,000 40,000 30,000 20,000 10,000 0 fibjs nodejs 100 500 900 1300 1700 2100 2500 2900 3300 3700 4100 4500 4900 并发连接
  • 28. fibjs 同样是单线程的 • fibjs 和 nodejs 都是以 v8 为脚本引擎 • v8 本⾝身被设计为 isolate 不⽀支持多线程 • nodejs 使⽤用异步回调复⽤用 v8 线程 • fibjs 使⽤用 fiber 复⽤用 v8 线程 • 因此 fibjs 和 nodejs 都是单线程
  • 31. fibjs 将更多的计算移⾄至⼯工作线程池 Javascript 线程⼯工作线程池异步 io 线程
  • 32. ⼯工作线程不再仅仅完成阻塞操作 ⼯工作线程池 • 同步 io 操作,⽐比如磁盘操作,⽂文件处理 • 计算密集的⽅方法,⽐比如加密,压缩,图像 • 异步 io 基础上的复杂逻辑,⽐比如协议处理 ps: 异步 io 基础上的计算,全部以异步⽅方式完成,并 发性能更⾼高。
  • 33. 从⽽而提⾼高 Javascript 线程利⽤用率 Javascript 线程Javascript 线程
  • 36. fibjs 的应⽤用逻辑是在太过于直⽩白和简单 毫⽆无 nodejs 回调处理的各种惊艳技巧 就不再展⽰示那些毫⽆无趣味的⽰示例代码了
  • 37. 良好的⽣生态从积极参与和分享开始 ⺴⽹网站: http://fibjs.org/ 代码: https://github.com/xicilion/fibjs 社区: http://baoz.cn/fibjs
  • 39. 孢⼦子社区的 fibjs 实践 • 基于 http ⻓长连接的客户端消息推送 • 基于 tcp 的移动 app 消息推送 • 基于 fiber 的数据并⾏行读取 • 基于 fiber 的后台运算异步化 • 基于 SandBox 的服务器端模块 app 化与数据隔离 • 基于 SandBox 的服务器代码热升级