Vapor 源码阅读方法

11/2/2020 swiftvapor

Vapor 源码阅读有方法:抓主干,补血肉

# 如何抓住主干?

Vapor是在SwiftNIO之上的。那么他们两者的联系点是啥?弄清这个问题,就可以抓住主干。

首先,放下Vapor, 了解SwiftNIO。比较快速的了解SwiftNIO,可以跟着下面这篇文章实战一番。

A µTutorial on SwiftNIO 2 (opens new window)

let bootstrap = ServerBootstrap(group: loopGroup)
      .serverChannelOption(ChannelOptions.backlog, value: 256)
      .serverChannelOption(reuseAddrOpt, value: 1)
      
      .childChannelInitializer { channel in
        channel.pipeline.configureHTTPServerPipeline()
        
        // this is where the action is going to be!
      }
      
      .childChannelOption(ChannelOptions.socket(
                            IPPROTO_TCP, TCP_NODELAY), value: 1)
      .childChannelOption(reuseAddrOpt, value: 1)
      .childChannelOption(ChannelOptions.maxMessagesPerRead, 
                          value: 1)

上面就是SwiftNIO处理HTTP的主体流程。

# 找到主干

Vapor源码中找到ServerBootstrap初始化的地方。即HTTPServer.swift

private final class HTTPServerConnection {
    let channel: Channel
    let quiesce: ServerQuiescingHelper
    
    static func start(
        application: Application,
        responder: Responder,
        configuration: HTTPServer.Configuration,
        on eventLoopGroup: EventLoopGroup
    ) -> EventLoopFuture<HTTPServerConnection> {
        let quiesce = ServerQuiescingHelper(group: eventLoopGroup)
        let bootstrap = ServerBootstrap(group: eventLoopGroup)
            // Specify backlog and enable SO_REUSEADDR for the server itself
            .serverChannelOption(ChannelOptions.backlog, value: Int32(configuration.backlog))
            .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: configuration.reuseAddress ? SocketOptionValue(1) : SocketOptionValue(0))
            
            // Set handlers that are applied to the Server's channel
            .serverChannelInitializer { channel in
                channel.pipeline.addHandler(quiesce.makeServerChannelHandler(channel: channel))
            }

这就是Vapor的七寸。恭喜你,主干已获取。

# 补充血肉

从主干代码不断的分析各个支流,整个项目的架构就比较清晰了。当然可以画画思维导图,类图,写写扩展,或者贡献自己的代码。然后你就是大佬了(头发也就少了)。

上次更新: 1/12/2022, 7:28:48 AM