Created
June 1, 2015 18:15
-
-
Save barnjamin/563c16280dcd670a43a5 to your computer and use it in GitHub Desktop.
V8Worker test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "github.com/ry/v8worker" | |
| ) | |
| const OID_LOOKUP = ` | |
| var oidmap = { | |
| "moto":"0.1.2.3.4", | |
| "arris":"0.1.2.3.5", | |
| "SA":"0.1.2.3.6", | |
| } | |
| $recv(function(msg){ | |
| $send(LookupOid(msg)); | |
| }) | |
| function LookupOid(msg) { | |
| if (msg in oidmap) { | |
| return oidmap[msg]; | |
| } | |
| return "0.0.0.0" | |
| } | |
| ` | |
| const NUM_WORKERS = 1 | |
| type Promise struct { | |
| Message string | |
| Reply chan string | |
| } | |
| func main() { | |
| work := make(chan Promise) | |
| for x := 0; x < NUM_WORKERS; x++ { | |
| go func() { | |
| var reply string | |
| oidLookup := v8worker.New(func(msg string) { | |
| reply = msg | |
| }) | |
| if err := oidLookup.Load("oidlookup.js", OID_LOOKUP); err != nil { | |
| log.Fatalf("Failed to add oidlookup: %+v", err) | |
| } | |
| for msg := range work { | |
| if err := oidLookup.Send(msg.Message); err != nil { | |
| log.Fatalf("Failed to send: %+v", err) | |
| } | |
| msg.Reply <- reply | |
| } | |
| }() | |
| } | |
| reply := make(chan string) | |
| work <- Promise{"arris", reply} | |
| println(<-reply) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment