2018-05-21 22:07:40 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/ry/v8worker2"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2018-05-23 17:23:50 -04:00
|
|
|
var resChan = make(chan *BaseMsg, 10)
|
|
|
|
var doneChan = make(chan bool)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2018-05-25 17:14:56 -04:00
|
|
|
var stats struct {
|
|
|
|
v8workerSend int
|
|
|
|
v8workerRespond int
|
|
|
|
v8workerRecv int
|
|
|
|
v8workerBytesSent int
|
|
|
|
v8workerBytesRecv int
|
|
|
|
}
|
|
|
|
|
2018-05-21 22:07:40 -04:00
|
|
|
// There is a single global worker for this process.
|
|
|
|
// This file should be the only part of deno that directly access it, so that
|
|
|
|
// all interaction with V8 can go through a single point.
|
|
|
|
var worker *v8worker2.Worker
|
|
|
|
|
|
|
|
var channels = make(map[string][]Subscriber)
|
|
|
|
|
|
|
|
type Subscriber func(payload []byte) []byte
|
|
|
|
|
|
|
|
func createWorker() {
|
|
|
|
worker = v8worker2.New(recv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func recv(buf []byte) (response []byte) {
|
2018-05-25 17:14:56 -04:00
|
|
|
stats.v8workerRecv++
|
|
|
|
stats.v8workerBytesRecv += len(buf)
|
|
|
|
|
2018-05-21 22:07:40 -04:00
|
|
|
msg := &BaseMsg{}
|
|
|
|
check(proto.Unmarshal(buf, msg))
|
|
|
|
assert(len(msg.Payload) > 0, "BaseMsg has empty payload.")
|
2018-05-25 15:30:09 -04:00
|
|
|
subscribers, ok := channels[msg.Channel]
|
2018-05-21 22:07:40 -04:00
|
|
|
if !ok {
|
2018-05-25 15:30:09 -04:00
|
|
|
panic("No subscribers for channel " + msg.Channel)
|
2018-05-21 22:07:40 -04:00
|
|
|
}
|
|
|
|
for i := 0; i < len(subscribers); i++ {
|
|
|
|
s := subscribers[i]
|
|
|
|
r := s(msg.Payload)
|
|
|
|
if r != nil {
|
|
|
|
response = r
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 17:14:56 -04:00
|
|
|
if response != nil {
|
|
|
|
stats.v8workerRespond++
|
|
|
|
stats.v8workerBytesSent += len(response)
|
|
|
|
}
|
2018-05-21 22:07:40 -04:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func Sub(channel string, cb Subscriber) {
|
|
|
|
subscribers, ok := channels[channel]
|
|
|
|
if !ok {
|
|
|
|
subscribers = make([]Subscriber, 0)
|
|
|
|
}
|
|
|
|
subscribers = append(subscribers, cb)
|
|
|
|
channels[channel] = subscribers
|
|
|
|
}
|
|
|
|
|
|
|
|
func Pub(channel string, payload []byte) {
|
2018-05-27 03:46:18 -04:00
|
|
|
wg.Add(1)
|
2018-05-21 22:07:40 -04:00
|
|
|
resChan <- &BaseMsg{
|
2018-05-25 15:30:09 -04:00
|
|
|
Channel: channel,
|
2018-05-21 22:07:40 -04:00
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 17:23:50 -04:00
|
|
|
func PubMsg(channel string, msg *Msg) {
|
|
|
|
payload, err := proto.Marshal(msg)
|
|
|
|
check(err)
|
|
|
|
Pub(channel, payload)
|
|
|
|
}
|
2018-05-21 22:07:40 -04:00
|
|
|
|
|
|
|
func DispatchLoop() {
|
|
|
|
wg.Add(1)
|
|
|
|
first := true
|
|
|
|
|
|
|
|
// In a goroutine, we wait on for all goroutines to complete (for example
|
|
|
|
// timers). We use this to signal to the main thread to exit.
|
2018-05-27 03:46:18 -04:00
|
|
|
// wg.Add(1) basically translates to uv_ref, if this was Node.
|
|
|
|
// wg.Done() basically translates to uv_unref
|
2018-05-21 22:07:40 -04:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
doneChan <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-resChan:
|
|
|
|
out, err := proto.Marshal(msg)
|
2018-05-27 03:46:18 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-05-21 22:07:40 -04:00
|
|
|
err = worker.SendBytes(out)
|
2018-05-25 17:14:56 -04:00
|
|
|
stats.v8workerSend++
|
|
|
|
stats.v8workerBytesSent += len(out)
|
2018-05-23 11:27:56 -04:00
|
|
|
exitOnError(err)
|
2018-05-28 14:56:13 -04:00
|
|
|
wg.Done() // Corresponds to the wg.Add(1) in Pub().
|
2018-05-21 22:07:40 -04:00
|
|
|
case <-doneChan:
|
|
|
|
// All goroutines have completed. Now we can exit main().
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to exit until we've received at least one message.
|
|
|
|
// This is so the program doesn't exit after sending the "start"
|
|
|
|
// message.
|
|
|
|
if first {
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
}
|