Dart Isolate, Port and Capabilities

Dart Isolate, Port and Capabilities

Dart's basic concurrent language construct Isolate and communication through ReceivePort/SendPort. and Capability based authentication way.

5d429874243e641a1684a364211ddc16?s=128

UEHARA Junji

July 18, 2018
Tweet

Transcript

  1. Dartのアイソレートとケイ パビリティ、Portなど NTTςΫϊΫϩεגࣜձࣾ ্ݪ५ೋ Dart Meetup Tokyo #5 2018/7/18

  2. ࣗݾ঺հ ্ݪ५ೋ(@uehaj) ॴଐ: NTTςΫϊΫϩεגࣜձࣾ ޷͖ͳݴޠ(౳) JavaScript(React/Redux/Redux-Saga,Cordova, ReactNative), Groovy, Elm, Rust,

    Haskell, Dart ॻ੶ ϓϩάϥϛϯάGroovy(ٕධ) Grailsపఈೖ໳(ᠳӭࣾ)
  3. DartͰݟͳΕ͵΋ͷΛௐ΂Δ γϦʔζ 1. DartͷඇಉظετϦʔϜΛͪΐͬͱݟͯΈΔ →Dart Meetup Tokyo #4 (2018/3/29) 2.

    Dartͷม਺ఆٛ࣌ͷम০static/final/constɺͦͯ͠ const constructorʹ͍ͭͯ →Qiitaهࣄ(2018/6/27) 3. DartͷΞΠιϨʔτͱέΠύϏϦςΟɺPortʹ͍ͭ ͯ →Dart Meetup Tokyo #5 (2018/7/18) ←ࠓ͜͜
  4. ΞΠιϨʔτ is Կ? Dartʹ͓͚Δฒߦॲཧͷ୯Ґ ϝϞϦඇڞ༗ܕϚϧνεϨου ErlangͷϓϩηεɺgoͷgoroutineతͳԿ͔? ಠࣗͷΠϕϯτϧʔϓΛ࣋ͭ Isolateͷ࣮ମ Dart VM…εϨου?

    ϒϥ΢β…WebWorker? Flutter…εϨου? Fuchsia…?
  5. ΞΠιϨʔτͷ࣮ମ Dart VM OSεϨου (LinuxܥɺmacosɺFuchsiaͰ͸pthreadɺWin Ͱ͸Thread) [*1] ϒϥ΢β (dart2js,dartdevc) Dart2Ҏ߱ɺdart:isolate͸ഇࢭɻ

    ωΠςΟϒAOT ίϯύΠϥ ? [*1] https://github.com/dart-lang/sdk/blob/master/runtime/vm/os_thread_*.cc
  6. ϒϥ΢βͰͷഇࢭࣄ৘ ݩ͸WebWorkerΛ࢖༻͢ΔΑ͏ʹม׵͞Ε͍ͯͨ 2.0.0-dev.55.0ͰDart2js, dartdevc͔Βػೳ࡟আ[*2] ཧ༝͸ैདྷ͔Βػೳ͕ݶఆ͞Ε͍ͯͯ໰୊͕ଟ͔ͬ ͨͨΊͱͷ͜ͱ ୅ସखஈ: web workerΛdart:htmlܦ༝Ͱ࢖͏ [*2]

    https://github.com/dart-lang/sdk/issues/30538
  7. (༨ஊ)ϒϥ΢β&Dart VMͰฒߦ ॲཧίʔυΛڞ༗͍ͨ͠ͱ͖ a.dart: 
 void test() { print("a"); }

    b.dart:
 void test() { print("b"); } default.dart:
 void test() { print("default"); } main.dart:
 import 'default.dart'
 if (a) 'a.dart'
 if (b) 'b.dart';
 main() {
 test();
 } Configuration Specific Import(Dart2?)Λ࢖͑Δ dart -Da=true main.dart
  8. ΞΠιϨʔτ͸(ࠓͷͱ͜Ζ ͸)ԿͰͳ͍͔ ෼ࢄϝοηʔδج൫Ͱ͸ͳ͍ ϦϞʔτىಈͷ࢓૊Έ͸ͳ͍(ͱࢥ͏) spawnUriͷURIʹ͸file: URLΛࢦఆ͢Δ͕ (ErlangతͳҙຯͰͷ)ʮܰྔεϨουʯͰ͸ͳ͍ OSεϨου(pthred)ʹରԠ͢Δ 100ສ΋200ສ΋ͷଟ਺εϨουΛىಈ͢Δɺͱ ͍͏࿩͸ͳ͍

  9. ΞΠιϨʔτͰ(ࠓͷͱ͜Ζ) Ͱ͖ͳ͍ࣄ" εϨουIDऔಘɺεϨουҰཡऔಘɺεϨουϓʔ ϧɺεϨουάϧʔϐϯάɺεϨου໊ɺεϨουά ϧʔϓ໊ɺ༏ઌ౓੍ޚɺ࣮ߦΛۭ͚Θͨͨ͢Ίͷ Yieldɺ…

  10. ΞΠιϨʔτͷىಈ import ‘dart:isolate'; Isolate isolate = await Isolate.spawn(
 entryPoint,
 arg,


    options); void entryPoint(arg) {
 }
  11. ࣮ߦϞσϧ ϝΠϯεϨου spawn/spawnUri spawn/spawnUri ΞΠιϨʔτ1 ΞΠιϨʔτ2

  12. ΞΠιϨʔτؒ௨৴: Port import ‘dart:isolate'; ReceivePort receivePort = new ReceivePort(); Isolate

    isolate = await Isolate.spawn(
 entryPoint,
 receivePort.sendPort,
 options); void entryPoint(sendPort) {
 sendPort.send(“hello”);
 };
  13. ReceivePort/SendPort ReceivePort: ड͚ଆ ܕ͖ͭͷ೚ҙͷσʔλΛड͚ͱΕΔετϦʔϜ SendPort: ૹΓଆ ReceivePortͷϓϩύςΟ send()ݺͿ͜ͱͰඇಉظϝοηʔδૹ৴ ReceivePort SendPort

    1ରଟ
  14. ΞΠιϨʔτؒ௨৴: ࢠˠ਌ ϝΠϯεϨου ReceivePort SendPort ReceivePort SendPort (1)ReceivePort ͔ΒsendPortΛऔ Γग़ͯ͠spawnͷ

    Ҿ਺Ͱ౉͢ (2)sendPortΛ௨͡ ͯ਌ʹσʔλΛૹ ৴
  15. ReceivePort/SendPort ΞΠιϨʔτؒ௨৴Ͱ౉ͤΔͷ͸ ϓϦϛςΟϒ஋(null, num, bool, double, String) SendPort(extends Capability) ্هΛཁૉͱ͢ΔϦετɺϚοϓ

    + ্هΛϝϯόʔͱ͢ΔΫϥεͷΦϒδΣΫτ ※ ಉ͡Dart VMϓϩηε্Ͱಈ͍͍ͯΔ৔߹ݶఆ ҎԼ͸ૹ৴ෆՄ Ϋϩʔδϟ(ૹ৴࣌Τϥʔ) Future(ΞΠιϨʔτΛ·͕ͨͬͯcomplete͸Ͱ͖ͳ͍)
  16. ΞΠιϨʔτؒ௨৴: ਌ˠࢠ ϝΠϯεϨου ReceivePort SendPort ReceivePort SendPort (1)લड़ͷࢠˠ਌ͷ௨৴ ͰɺࢠͷΞΠιϨʔτͰ ࡞੒ͨ͠sendPortΛ਌ʹ

    ૹΔ (2)ड͚औͬͨ sendPortܦ༝Ͱ਌ˠ ࢠ௨৴
  17. ΞΠιϨʔτؒ௨৴: ਌ˠࢠ import ‘dart:isolate'; ReceivePort receivePort = new ReceivePort(); Isolate

    isolate = await Isolate.spawn(
 entryPoint,
 receivePort.sendPort,
 options); receivePort.listen((toChild){ toChild.send(“hello”)); void entryPoint(sendPort) {
 ReceivePort rp2 = new ReceivePort();
 sendPort.send(rp2.sendPort);
 };
  18. IsolateΫϥε ΞΠιϨʔτ࣮ߦ੍ޚͷͨΊͷϝιου܈ pause(), resume(), kill(), setErrorsFatal() ੍ޚϙʔτ SendPort get controlPort;

    έΠύϏϦςΟϓϩύςΟ Capability get pauseCapability; Capability get terminateCapability;
  19. έΠύϏϦςΟ(Capability)1/2 ͋ΔछͷτʔΫϯ ੜ੒͢Δͨͼʹɺ࠶ݱɺ༧ଌෆՄೳͳҟͳΔ஋Λ อ࣋͢Δ cf. Capability Based Security

  20. έΠύϏϦςΟ(Capability)2/2 DartͰͷ࢖༻໨త pauseͷ࣮ߦΛࣝผ͢Δ(pauseͷฦΓ஋͸CapabilityΦϒδΣΫτ Ͱ͋ΓɺresumeͷҾ਺) ʮ਌͔Β௚઀ͷࢠΞΠιϨʔτʯҎ֎ͷίϯτϩʔϧ → Ͳ͏΍ͬͯ?(εϨουID΋ͳ͍ɺIsolate͸౉ͤͳ͍) controlPortϓϩύςΟΛΞΠιϨʔτؒͰड͚౉͢ Ճ͑ͯɺcapability஋Λड͚౉͢ ੍ޚ͍ͨ͠ଆͰcontrolPortΛݩʹnew

    Isolate()͢Δ ͦͷͱ͖ɺड͚औͬͨcapability஋Λઃఆ͠ݖݶ෇༩ ෮ݩͨ͠Isolateʹରͯ͠pause΍killΛ࣮ߦ
  21. TIPS: ΍͓͍ͬͯͨํ͕ྑ͍͜ͱ: ࢠΞΠιϨʔτଆͰͷΤϥʔදࣔ ReceivePort onErrorPort = new ReceivePort();
 onErrorPort.listen((error) {


    print("error: ${error}.");
 }); Isolate isolate = await Isolate.spawn(
 entryPoint,
 onError: onErrorPort.sendPort);
  22. Streamͱͯ͠ͷReceivePort ReceivePort.close()͠ͳ͍ݶΓແݶετϦʔϜ ͔͠͠IsolateଆͰReceivePort.close()͢Δ͜ͱ͸Ͱ͖ͳ͍ https://github.com/dart-lang/sdk/issues/28731 Isolateͷݺͼग़͠ଆReceivePortΛlistenͯࣗ͠ݾcloseͤ͞Δ? Ͱ΋listenͨ͠ΒStreamΛ࠶౓࢖༻Ͱ͖ͳ͍ ReceivePort͕ແݶετϦʔϜʹͳΔͱ for asyncͰ͖ͳ͍ StreamॲཧͷҰ෦͕ແݶϒϩοΫ͢Δ

    ༗ݶͰ͋Δ͜ͱΛલఏͱͨ͠ετϦʔϜࢼݧ(emitsInOrder)Ͱ͖ͳ͍ ReceivePort͕StreamͰ͋Δ͜ͱ͸ͦ΋ͦ΋ઃܭ্ଥ౰ͳͷ͔? We chose to make ReceivePort look like a Stream. That might have been a mistake, but it's too late to change now. StreamͰ͸ͳ͍RawReceivePort΋͋Δ
  23. ·ͱΊ ΞΠιϨʔτͷ৘ใ͸ɺ(গͳ͘ͱ΋೔ຊޠ৘ใ͸)அยత ͳͷͰ·ͱΊͯΈ·ͨ͠ ײ૝ͱͯ͠͸͔ͳΓૉ๿ ฒߦॲཧ୯Ґ͕ʮετϦʔϜͷઌʹ͋ΔԿ͔ʯ͙Β͍ ͷந৅౓ goͷCSP(Communication Sequential Processes)ͱɺ͋ ͑ͯൺֱ͢Δͱ

    Stream<T>ͱͯ͠ͷPort͕goͷchan?