Angel
1.x
1.x
  • Introduction
  • Example Projects
  • Awesome Angel
  • 1.1.0 Migration Guide
  • Social
    • Angel on Gitter
    • Angel on Medium
    • Angel on YouTube
  • The Basics
    • Installation & Setup
      • Without the Boilerplate
    • Requests & Responses
    • Dependency Injection
    • Basic Routing
    • Request Lifecycle
    • Middleware
    • Controllers
    • Handling File Uploads
    • Using Plug-ins
    • Rendering Views
    • REST Client
    • Testing
    • Error Handling
    • Pattern Matching and Parameter
    • Command Line
  • Flutter
    • Writing a Chat App
    • Flutter helper widgets
  • Services
    • Service Basics
    • TypedService
    • In-Memory
    • Custom Services
    • Hooks
      • Bundled Hooks
    • Database-Agnostic Relations
    • Database Adapters
      • MongoDB
      • RethinkDB
      • JSON File-based
  • Plug-ins
    • Authentication
    • Configuration
    • Diagnostics & Logging
    • Reverse Proxy
    • Service Seeder
    • Static Files
    • Validation
    • Websockets
    • Server-sent Events
    • Toggle-able Services
  • Middleware/Finalizers
    • CORS
    • Response Compression
    • Security
    • File Upload Security
    • shelf Integration
    • User Agents
    • Pagination
    • Range, If-Range, Accept-Ranges support
  • PostgreSQL ORM
    • Model Serialization
    • Query Builder + ORM
    • Migrations
  • Deployment
    • Running in Isolates
    • Configuring SSL
    • HTTP/2 Support
    • Ubuntu and nginx
    • AppEngine
    • Production Mode
  • Front-end
    • Mustache Templates
    • Jael template engine
      • Github
      • Basics
      • Custom Elements
      • Strict Resolution
      • Directive: declare
      • Directive: for-each
      • Directive: extend
      • Directive: if
      • Directive: include
      • Directive: switch
    • compiled_mustache-based engine
    • html_builder-based engine
    • Markdown template engine
    • Using Angel with Angular
  • Advanced
    • API Documentation
    • Contribute to Angel
    • Scaling & Load Balancing
    • Standalone Router
    • Writing a Plugin
    • Task Engine
    • Hot Reloading
    • Real-time polling
Powered by GitBook
On this page
  1. Deployment

Running in Isolates

How to run Angel in multiple isolates.

The concept is pretty simple. A normal server would look like this:

var app = new Angel();

If you use the Angel.custom constructor, you can provide a custom ServerGenerator, which is a typedef for a function that binds an HTTP server:

typedef Future<HttpServer> ServerGenerator(InternetAddress address, int port);

With this in mind, you can start a server passing the shared argument to HttpServer.bind:

new Angel.custom((address, port) => HttpServer.bind(address, port, shared: true));

startShared is a function that accomplishes this for you, since it's commonly-used functionality:

new Angel.custom(startShared);

Multiple Isolates

To run in multiple isolates, the concept is simple as well:

import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';

main(List<String> args) async {
  int concurrency = Platform.numberOfProcessors;

  // Start child isolates...
  for (int i = 1; i < concurrency; i++) {
    Isolate.spawn(serverMain, i);
  }

  // Spawn a server in the main isolate.
  serverMain(concurrency);
}

void serverMain(int id) {
  // Start shared!!!
  var app = new Angel();
  var http = new AngelHttp.custom(app, startShared);

  app.get('/json', () => {'hello': 'world'});

  app.get('/db', () async {
    // Run a query...
    var connection = new PostgreSQLConnection(
        '127.0.0.1', 5432, 'wrk_benchmark',
        username: 'postgres', password: 'password');
    await connection.open();
    var rows = await connection.query('SELECT id, text from notes;');
    return rows.map((row) => {'id': row[0], 'text': row[1]});
  });

  http.startServer(InternetAddress.ANY_IP_V4, 3000).then((server) {
    print(
        'Instance #$id listening at http://${server.address.address}:${server.port}');
  });
}
PreviousDeploymentNextConfiguring SSL

Last updated 6 years ago