@@ -84,29 +84,33 @@ public enum Lambda {
84
84
// for testing and internal use
85
85
@discardableResult
86
86
internal static func run( configuration: Configuration = . init( ) , factory: @escaping HandlerFactory ) -> Result < Int , Error > {
87
- do {
88
- let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 ) // only need one thread, will improve performance
89
- defer { try ! eventLoopGroup. syncShutdownGracefully ( ) }
90
- let result = try self . runAsync ( eventLoopGroup: eventLoopGroup, configuration: configuration, factory: factory) . wait ( )
91
- return . success( result)
92
- } catch {
93
- return . failure( error)
94
- }
95
- }
96
-
97
- internal static func runAsync( eventLoopGroup: EventLoopGroup , configuration: Configuration , factory: @escaping HandlerFactory ) -> EventLoopFuture < Int > {
98
87
Backtrace . install ( )
99
88
var logger = Logger ( label: " Lambda " )
100
89
logger. logLevel = configuration. general. logLevel
101
- let lifecycle = Lifecycle ( eventLoop: eventLoopGroup. next ( ) , logger: logger, configuration: configuration, factory: factory)
102
- let signalSource = trap ( signal: configuration. lifecycle. stopSignal) { signal in
103
- logger. info ( " intercepted signal: \( signal) " )
104
- lifecycle. shutdown ( )
105
- }
106
- return lifecycle. start ( ) . flatMap {
107
- return lifecycle. shutdownFuture. always { _ in
90
+
91
+ var r : Result < Int , Error > ?
92
+ MultiThreadedEventLoopGroup . makeEventLoopFromCallingThread { eventLoop in
93
+ let lifecycle = Lifecycle ( eventLoop: eventLoop, logger: logger, configuration: configuration, factory: factory)
94
+ let signalSource = trap ( signal: configuration. lifecycle. stopSignal) { signal in
95
+ logger. info ( " intercepted signal: \( signal) " )
96
+ lifecycle. shutdown ( )
97
+ }
98
+
99
+ _ = lifecycle. start ( ) . flatMap {
100
+ lifecycle. shutdownFuture
101
+ }
102
+ . always { result in
108
103
signalSource. cancel ( )
104
+ eventLoop. shutdownGracefully { _ in
105
+ logger. info ( " shutdown " )
106
+ }
107
+
108
+ r = result
109
109
}
110
110
}
111
+
112
+ logger. info ( " shutdown completed " )
113
+
114
+ return r!
111
115
}
112
116
}
0 commit comments