Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

My client wanted their apps synced, and I made it with Go

2,229 views

Published on

デスクトップ・アプリを並列実行したいという相談に、できるんじゃないっすかと適当なこと言ってたら、金払うからやってくれって話になりました。というわけで Go で書きました。ああ、Go でよかった。

Published in: Software
  • Be the first to comment

  • Be the first to like this

My client wanted their apps synced, and I made it with Go

  1. 1. My client wanted their apps synced, and I made it with Go @torufurukawa Toru Furukawa
  2. 2. Can you run apps in parallel? Yes, I guess
  3. 3. I will send purchase order.
  4. 4. Solver simulates only 1 Object Solver (app/DLL) Wrapper app
  5. 5. SimulaMng mulMple objects serially Solver ControlWrapper Wrapper Swaps internal variables
  6. 6. Want to run them in parallel and synced ControlWrapper Wrapper Wrapper Wrapper (concurrent)
  7. 7. Concurrency? Sync? Go! hOps://talks.golang.org/2012/waza.slide#14
  8. 8. •  Concurrency •  Messaging (data exchange) •  SynchronizaMon
  9. 9. Process Lib memory DLL simulates an object's behavior DLL Load
  10. 10. Delegate simulaMon to DLL func dll, err := syscall.LoadDLL(path) proc := dll.MustFindProc("simulate") ... for { ret, _, err := proc.Call(...) ret != 0{ break } }
  11. 11. Make another DLL to call solver DLL wrap_f(double *x) { *x = f(); } Go app Wrapper DLL wrap_f() Solver DLL f()
  12. 12. Process Lib memory Concurrency Process Lib memory DLL file Load Process Lib memory
  13. 13. Add message broker Process Process Process Message Broker
  14. 14. Listen, Accept and Handle func main() { ... ch := make(chan event) go listenAndServe(ch, ...) ... }
  15. 15. Listen, Accept and Handle func listenAndServe(chan event) { ln, err := net.Listen(…) for { conn, err := ln.Accept() go handleConn(conn, ch) } }
  16. 16. Handle Requests func handleConn(...) { for { req := read(conn) respCh := make(chan …) ch <- event{req, respCh} resp := <- respCh write(conn, resp) } } func main() { … for { e := <- ch result = do(e.req) e.respCh <- result } }
  17. 17. Messaging Process Process Process Message Broker SET SET SET GET
  18. 18. Need all processes synced Process A Time Process B Process C
  19. 19. Suspend task if not ready func main() { ... for { event := <- ch if !ready(event.req) { tasks = append(tasks, func() {do(event.req)}) continue } result = do(event.req) ... }
  20. 20. Flush tasks when ready func main() { ... if readyToFlush(...) { for _, t := range tasks { t() } tasks := make([]task, 0) } ... }
  21. 21. Sync Process A Time Process B Process C
  22. 22. Concurrency? Sync? Go!
  23. 23. Just shipped last week @torufurukawa

×