Vapor4.0概念

最近打算学习一下后端的知识,心想怎样Javascript是绕不开的,就学Nodejs吧。学习了一个星期的Javascript和Nodejs,最后读了《深入浅出Node.js》有关Nodejs的历史,转身就跑去学Vapor了。

平台的发展都需要一定的时间,也有人坚定的从一开始走过来。Swift用起来那么顺,又经过几年的沉淀。现在是时候认真搞一搞了。

现在的想法是先建个博客练练手,后端用Vapor4.0,前端用Vue.js。两个技术一起学。将来可能把这个博客迁移过去。

Vapor4.0的文档还不够完善,大佬们建议从vapor / api-template这里搞起,记得分支选4

对Vapor4要有大概的了解才能继续下去。看到Nodejs主要是通过EventLoop来接收请求的。就问问大佬们Vapor是怎么个机制。热心的大佬回复说Vapor底层用的SwiftNIO,其底层的逻辑和Nodejs差不多,SwiftNIO的这个机制叫做EventLoopFuture。网上查了些资料, 摘录如下:

The Event Loop

Efficiency is an important goal when writing server-side code. The more efficient your code, the more users it can help concurrently. Where an unoptimized app can only help ten to a hundred users at a time, highly optimized apps are able to help a hundred-thousand concurrent users on the same hardware.

The main reason for this difference is architectural. A poorly optimized app will be idle most of the time, whereas a highly optimized app will try to spend its time cleverly and actively. The brains behind this architecture in server-side Swift is provided by SwiftNIO, Apple’s cross-platform event-driven networking framework, by a type called EventLoop.

EventLoop is, in its essence, a while loop that keeps looking to do work when an external factor, such as a user uploading a file, has made progress.

If one thread writes to a variable, and another thread tries to read it or write to it at the same time, a race condition occurs, which will crash your app. One method you can use to prevent race conditions from occurring is using locks for each of these variables. You’ll lock this lock before accessing a variable and unlock is after accessing it is completed, so any future accesses to the resource are prevented until you’ll unlock the lock. The main downside of this approach is its complexity and impact on performance.

Using a single thread to access this information is another way to tackle the issue of race conditions. Doing this means all read and write operations are done by this thread. One example of this is the database you’ll find in the sample project. Using this method requires you to give the read/write thread a way to return the result to the event loop that requested it.

If you respond to a request from a different EventLoop, SwiftNIO will crash the app to prevent undefined behaviour from causing trouble.

Futures and Promises

Future is a type used to describe information that does not exist yet, but will exist in the future. Writing asynchronous code with futures means directing the futures to respond to the successful results or to the failures. You can create a future with a predefined result: either success or failure. However, if the result is not known yet, you need to create a Promise.

These promises and futures need to be created from the EventLoop since the future type will always return to the event loop it originated from. A future can only hold one result at a time, at which point it is considered completed. Completion can be either successful or unsuccessful, thus failed futures are also considered completed. Finally, if a promise is creates, it must be completed.

现在就开始建立自己的新项目吧。