2017-07-22 16:49:22 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-08-02 11:39:58 -04:00
|
|
|
"bufio"
|
2017-07-22 16:49:22 -04:00
|
|
|
"encoding/json"
|
2017-09-24 21:28:21 -04:00
|
|
|
"errors"
|
2017-07-22 16:49:22 -04:00
|
|
|
"fmt"
|
2017-08-02 11:39:58 -04:00
|
|
|
"io"
|
2017-07-22 16:49:22 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-08-25 23:45:04 -04:00
|
|
|
"runtime"
|
2017-08-02 11:39:58 -04:00
|
|
|
"strings"
|
2017-09-24 21:28:21 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-07-22 16:49:22 -04:00
|
|
|
|
2017-08-02 11:39:58 -04:00
|
|
|
"github.com/cosmos72/gomacro/base"
|
2017-07-22 16:49:22 -04:00
|
|
|
"github.com/cosmos72/gomacro/classic"
|
|
|
|
zmq "github.com/pebbe/zmq4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExecCounter is incremented each time we run user code in the notebook.
|
|
|
|
var ExecCounter int
|
|
|
|
|
|
|
|
// ConnectionInfo stores the contents of the kernel connection
|
|
|
|
// file created by Jupyter.
|
|
|
|
type ConnectionInfo struct {
|
|
|
|
SignatureScheme string `json:"signature_scheme"`
|
|
|
|
Transport string `json:"transport"`
|
|
|
|
StdinPort int `json:"stdin_port"`
|
|
|
|
ControlPort int `json:"control_port"`
|
|
|
|
IOPubPort int `json:"iopub_port"`
|
|
|
|
HBPort int `json:"hb_port"`
|
|
|
|
ShellPort int `json:"shell_port"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SocketGroup holds the sockets needed to communicate with the kernel,
|
|
|
|
// and the key for message signing.
|
|
|
|
type SocketGroup struct {
|
|
|
|
ShellSocket *zmq.Socket
|
|
|
|
ControlSocket *zmq.Socket
|
|
|
|
StdinSocket *zmq.Socket
|
|
|
|
IOPubSocket *zmq.Socket
|
2017-09-24 17:13:50 -04:00
|
|
|
HBSocket *zmq.Socket
|
2017-07-22 16:49:22 -04:00
|
|
|
Key []byte
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// KernelLanguageInfo holds information about the language that this kernel executes code in.
|
2017-08-25 23:45:04 -04:00
|
|
|
type kernelLanguageInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
MIMEType string `json:"mimetype"`
|
|
|
|
FileExtension string `json:"file_extension"`
|
|
|
|
PygmentsLexer string `json:"pygments_lexer"`
|
|
|
|
CodeMirrorMode string `json:"codemirror_mode"`
|
|
|
|
NBConvertExporter string `json:"nbconvert_exporter"`
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// HelpLink stores data to be displayed in the help menu of the notebook.
|
2017-08-25 23:45:04 -04:00
|
|
|
type helpLink struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// KernelInfo holds information about the igo kernel, for kernel_info_reply messages.
|
|
|
|
type kernelInfo struct {
|
|
|
|
ProtocolVersion string `json:"protocol_version"`
|
|
|
|
Implementation string `json:"implementation"`
|
|
|
|
ImplementationVersion string `json:"implementation_version"`
|
|
|
|
LanguageInfo kernelLanguageInfo `json:"language_info"`
|
|
|
|
Banner string `json:"banner"`
|
|
|
|
HelpLinks []helpLink `json:"help_links"`
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// shutdownReply encodes a boolean indication of stutdown/restart.
|
2017-07-22 16:49:22 -04:00
|
|
|
type shutdownReply struct {
|
|
|
|
Restart bool `json:"restart"`
|
|
|
|
}
|
|
|
|
|
2017-09-12 11:12:59 -04:00
|
|
|
const (
|
|
|
|
kernelStarting = "starting"
|
|
|
|
kernelBusy = "busy"
|
|
|
|
kernelIdle = "idle"
|
|
|
|
)
|
|
|
|
|
2017-07-22 16:49:22 -04:00
|
|
|
// runKernel is the main entry point to start the kernel.
|
|
|
|
func runKernel(connectionFile string) {
|
|
|
|
|
|
|
|
// Set up the "Session" with the replpkg.
|
|
|
|
ir := classic.New()
|
|
|
|
|
|
|
|
// Parse the connection info.
|
|
|
|
var connInfo ConnectionInfo
|
|
|
|
|
|
|
|
connData, err := ioutil.ReadFile(connectionFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(connData, &connInfo); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the ZMQ sockets through which the kernel will communicate.
|
|
|
|
sockets, err := prepareSockets(connInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-24 17:13:50 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
shutdownHeartbeat := runHeartbeat(sockets.HBSocket, &wg)
|
|
|
|
|
2017-07-22 16:49:22 -04:00
|
|
|
poller := zmq.NewPoller()
|
|
|
|
poller.Add(sockets.ShellSocket, zmq.POLLIN)
|
|
|
|
poller.Add(sockets.StdinSocket, zmq.POLLIN)
|
|
|
|
poller.Add(sockets.ControlSocket, zmq.POLLIN)
|
|
|
|
|
|
|
|
// msgParts will store a received multipart message.
|
|
|
|
var msgParts [][]byte
|
|
|
|
|
|
|
|
// Start a message receiving loop.
|
|
|
|
for {
|
2017-08-04 10:13:18 -04:00
|
|
|
|
2017-07-22 16:49:22 -04:00
|
|
|
polled, err := poller.Poll(-1)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range polled {
|
|
|
|
|
|
|
|
// Handle various types of messages.
|
|
|
|
switch socket := item.Socket; socket {
|
|
|
|
|
|
|
|
// Handle shell messages.
|
|
|
|
case sockets.ShellSocket:
|
2017-08-01 17:22:40 -04:00
|
|
|
msgParts, err = sockets.ShellSocket.RecvMessageBytes(0)
|
2017-07-22 16:49:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:22:40 -04:00
|
|
|
msg, ids, err := WireMsgToComposedMsg(msgParts, sockets.Key)
|
2017-07-22 16:49:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handleShellMsg(ir, msgReceipt{msg, ids, sockets})
|
|
|
|
|
2017-08-25 23:45:04 -04:00
|
|
|
// TODO Handle stdin socket.
|
2017-07-22 16:49:22 -04:00
|
|
|
case sockets.StdinSocket:
|
|
|
|
sockets.StdinSocket.RecvMessageBytes(0)
|
|
|
|
|
2017-08-25 23:45:04 -04:00
|
|
|
// Handle control messages.
|
2017-07-22 16:49:22 -04:00
|
|
|
case sockets.ControlSocket:
|
2017-08-01 17:22:40 -04:00
|
|
|
msgParts, err = sockets.ControlSocket.RecvMessageBytes(0)
|
2017-07-22 16:49:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:22:40 -04:00
|
|
|
msg, ids, err := WireMsgToComposedMsg(msgParts, sockets.Key)
|
2017-07-22 16:49:22 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handleShellMsg(ir, msgReceipt{msg, ids, sockets})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 21:28:21 -04:00
|
|
|
|
2017-09-24 17:13:50 -04:00
|
|
|
shutdownHeartbeat()
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-07-22 16:49:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareSockets sets up the ZMQ sockets through which the kernel
|
|
|
|
// will communicate.
|
|
|
|
func prepareSockets(connInfo ConnectionInfo) (SocketGroup, error) {
|
|
|
|
|
|
|
|
// Initialize the context.
|
|
|
|
context, err := zmq.NewContext()
|
|
|
|
if err != nil {
|
|
|
|
return SocketGroup{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the socket group.
|
|
|
|
var sg SocketGroup
|
|
|
|
|
|
|
|
sg.ShellSocket, err = context.NewSocket(zmq.ROUTER)
|
|
|
|
if err != nil {
|
|
|
|
return sg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.ControlSocket, err = context.NewSocket(zmq.ROUTER)
|
|
|
|
if err != nil {
|
|
|
|
return sg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.StdinSocket, err = context.NewSocket(zmq.ROUTER)
|
|
|
|
if err != nil {
|
|
|
|
return sg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sg.IOPubSocket, err = context.NewSocket(zmq.PUB)
|
|
|
|
if err != nil {
|
|
|
|
return sg, err
|
|
|
|
}
|
|
|
|
|
2017-09-24 17:13:50 -04:00
|
|
|
sg.HBSocket, err = context.NewSocket(zmq.REP)
|
|
|
|
if err != nil {
|
|
|
|
return sg, err
|
|
|
|
}
|
|
|
|
|
2017-07-22 16:49:22 -04:00
|
|
|
// Bind the sockets.
|
|
|
|
address := fmt.Sprintf("%v://%v:%%v", connInfo.Transport, connInfo.IP)
|
|
|
|
sg.ShellSocket.Bind(fmt.Sprintf(address, connInfo.ShellPort))
|
|
|
|
sg.ControlSocket.Bind(fmt.Sprintf(address, connInfo.ControlPort))
|
|
|
|
sg.StdinSocket.Bind(fmt.Sprintf(address, connInfo.StdinPort))
|
|
|
|
sg.IOPubSocket.Bind(fmt.Sprintf(address, connInfo.IOPubPort))
|
2017-09-24 17:13:50 -04:00
|
|
|
sg.HBSocket.Bind(fmt.Sprintf(address, connInfo.HBPort))
|
2017-07-22 16:49:22 -04:00
|
|
|
|
|
|
|
// Set the message signing key.
|
|
|
|
sg.Key = []byte(connInfo.Key)
|
|
|
|
|
|
|
|
return sg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleShellMsg responds to a message on the shell ROUTER socket.
|
|
|
|
func handleShellMsg(ir *classic.Interp, receipt msgReceipt) {
|
|
|
|
switch receipt.Msg.Header.MsgType {
|
|
|
|
case "kernel_info_request":
|
|
|
|
if err := sendKernelInfo(receipt); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
case "execute_request":
|
|
|
|
if err := handleExecuteRequest(ir, receipt); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
case "shutdown_request":
|
2017-08-01 17:22:40 -04:00
|
|
|
handleShutdownRequest(receipt)
|
2017-07-22 16:49:22 -04:00
|
|
|
default:
|
|
|
|
log.Println("Unhandled shell message: ", receipt.Msg.Header.MsgType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendKernelInfo sends a kernel_info_reply message.
|
|
|
|
func sendKernelInfo(receipt msgReceipt) error {
|
2017-08-25 23:45:04 -04:00
|
|
|
return receipt.Reply("kernel_info_reply",
|
|
|
|
kernelInfo{
|
2017-09-12 10:18:28 -04:00
|
|
|
ProtocolVersion: ProtocolVersion,
|
2017-08-25 23:45:04 -04:00
|
|
|
Implementation: "gophernotes",
|
|
|
|
ImplementationVersion: Version,
|
|
|
|
Banner: fmt.Sprintf("Go kernel: gophernotes - v%s", Version),
|
|
|
|
LanguageInfo: kernelLanguageInfo{
|
|
|
|
Name: "go",
|
|
|
|
Version: runtime.Version(),
|
|
|
|
FileExtension: ".go",
|
|
|
|
},
|
|
|
|
HelpLinks: []helpLink{
|
|
|
|
{Text: "Go", URL: "https://golang.org/"},
|
|
|
|
{Text: "gophernotes", URL: "https://github.com/gopherdata/gophernotes"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2017-07-22 16:49:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleExecuteRequest runs code from an execute_request method,
|
|
|
|
// and sends the various reply messages.
|
|
|
|
func handleExecuteRequest(ir *classic.Interp, receipt msgReceipt) error {
|
2017-09-24 21:28:21 -04:00
|
|
|
// Extract the data from the request.
|
2017-07-22 16:49:22 -04:00
|
|
|
reqcontent := receipt.Msg.Content.(map[string]interface{})
|
|
|
|
code := reqcontent["code"].(string)
|
|
|
|
silent := reqcontent["silent"].(bool)
|
|
|
|
|
|
|
|
if !silent {
|
|
|
|
ExecCounter++
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// Prepare the map that will hold the reply content.
|
2017-08-25 23:45:04 -04:00
|
|
|
content := make(map[string]interface{})
|
2017-07-22 16:49:22 -04:00
|
|
|
content["execution_count"] = ExecCounter
|
|
|
|
|
2017-08-25 23:45:04 -04:00
|
|
|
// Tell the front-end that the kernel is working and when finished notify the
|
2017-09-07 20:51:43 -04:00
|
|
|
// front-end that the kernel is idle again.
|
2017-09-12 11:12:59 -04:00
|
|
|
if err := receipt.PublishKernelStatus(kernelBusy); err != nil {
|
2017-09-07 20:51:43 -04:00
|
|
|
log.Printf("Error publishing kernel status 'busy': %v\n", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2017-09-12 11:12:59 -04:00
|
|
|
if err := receipt.PublishKernelStatus(kernelIdle); err != nil {
|
2017-09-07 20:51:43 -04:00
|
|
|
log.Printf("Error publishing kernel status 'idle': %v\n", err)
|
|
|
|
}
|
|
|
|
}()
|
2017-08-25 23:45:04 -04:00
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// Tell the front-end what the kernel is about to execute.
|
|
|
|
if err := receipt.PublishExecutionInput(ExecCounter, code); err != nil {
|
|
|
|
log.Printf("Error publishing execution input: %v\n", err)
|
|
|
|
}
|
2017-08-25 23:45:04 -04:00
|
|
|
|
2017-08-04 10:13:18 -04:00
|
|
|
// Redirect the standard out from the REPL.
|
2017-08-04 10:30:31 -04:00
|
|
|
oldStdout := os.Stdout
|
|
|
|
rOut, wOut, err := os.Pipe()
|
2017-08-02 11:39:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-04 10:30:31 -04:00
|
|
|
os.Stdout = wOut
|
2017-08-02 11:39:58 -04:00
|
|
|
|
2017-08-04 10:13:18 -04:00
|
|
|
// Redirect the standard error from the REPL.
|
2017-09-24 21:28:21 -04:00
|
|
|
oldStderr := os.Stderr
|
2017-08-04 10:13:18 -04:00
|
|
|
rErr, wErr, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-24 21:28:21 -04:00
|
|
|
os.Stderr = wErr
|
2017-08-02 11:39:58 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
var writersWG sync.WaitGroup
|
|
|
|
writersWG.Add(2)
|
2017-08-02 11:39:58 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
// Forward all data written to stdout/stderr to the front-end.
|
|
|
|
go func() {
|
|
|
|
defer writersWG.Done()
|
|
|
|
jupyterStdOut := JupyterStreamWriter{StreamStdout, &receipt}
|
|
|
|
io.Copy(&jupyterStdOut, rOut)
|
|
|
|
}()
|
2017-08-02 11:39:58 -04:00
|
|
|
|
|
|
|
go func() {
|
2017-09-24 21:28:21 -04:00
|
|
|
defer writersWG.Done()
|
|
|
|
jupyterStdErr := JupyterStreamWriter{StreamStderr, &receipt}
|
|
|
|
io.Copy(&jupyterStdErr, rErr)
|
2017-08-02 11:39:58 -04:00
|
|
|
}()
|
2017-08-01 17:22:40 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
val, executionErr := doEval(ir, code)
|
|
|
|
|
|
|
|
// Close and restore the streams.
|
2017-08-04 10:30:31 -04:00
|
|
|
wOut.Close()
|
|
|
|
os.Stdout = oldStdout
|
2017-08-04 10:13:18 -04:00
|
|
|
|
|
|
|
wErr.Close()
|
2017-09-24 21:28:21 -04:00
|
|
|
os.Stderr = oldStderr
|
2017-08-04 10:13:18 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
// Wait for the writers to finish forwarding the data.
|
|
|
|
writersWG.Wait()
|
2017-08-25 23:45:04 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
if executionErr == nil {
|
2017-07-22 16:49:22 -04:00
|
|
|
content["status"] = "ok"
|
|
|
|
content["user_expressions"] = make(map[string]string)
|
2017-08-01 17:22:40 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
if !silent && val != nil {
|
2017-09-07 20:51:43 -04:00
|
|
|
// Publish the result of the execution.
|
2017-09-24 21:28:21 -04:00
|
|
|
if err := receipt.PublishExecutionResult(ExecCounter, fmt.Sprint(val)); err != nil {
|
2017-09-07 20:51:43 -04:00
|
|
|
log.Printf("Error publishing execution result: %v\n", err)
|
|
|
|
}
|
2017-07-22 16:49:22 -04:00
|
|
|
}
|
2017-09-24 21:28:21 -04:00
|
|
|
} else {
|
2017-08-04 10:13:18 -04:00
|
|
|
content["status"] = "error"
|
|
|
|
content["ename"] = "ERROR"
|
2017-09-24 21:28:21 -04:00
|
|
|
content["evalue"] = executionErr.Error()
|
2017-08-04 10:13:18 -04:00
|
|
|
content["traceback"] = nil
|
2017-08-01 17:22:40 -04:00
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
if err := receipt.PublishExecutionError(executionErr.Error(), []string{executionErr.Error()}); err != nil {
|
2017-09-07 20:51:43 -04:00
|
|
|
log.Printf("Error publishing execution error: %v\n", err)
|
|
|
|
}
|
2017-08-04 10:13:18 -04:00
|
|
|
}
|
2017-07-22 16:49:22 -04:00
|
|
|
|
|
|
|
// Send the output back to the notebook.
|
2017-08-25 23:45:04 -04:00
|
|
|
return receipt.Reply("execute_reply", content)
|
2017-07-22 16:49:22 -04:00
|
|
|
}
|
|
|
|
|
2017-09-24 21:28:21 -04:00
|
|
|
// doEval evaluates the code in the interpreter. This function captures an uncaught panic
|
|
|
|
// as well as the value of the last statement/expression.
|
|
|
|
func doEval(ir *classic.Interp, code string) (val interface{}, err error) {
|
|
|
|
// Capture a panic from the evaluation if one occurs
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
var ok bool
|
|
|
|
if err, ok = r.(error); !ok {
|
|
|
|
err = errors.New(fmt.Sprint(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
in := bufio.NewReader(strings.NewReader(code))
|
|
|
|
|
|
|
|
// Prepare and perform the multiline evaluation.
|
|
|
|
env := ir.Env
|
|
|
|
env.Options &^= base.OptShowPrompt
|
|
|
|
env.Options &^= base.OptTrapPanic
|
|
|
|
env.Line = 0
|
|
|
|
|
|
|
|
// Perform the first iteration manually, to collect comments.
|
|
|
|
var comments string
|
|
|
|
str, firstToken := env.ReadMultiline(in, base.ReadOptCollectAllComments)
|
|
|
|
if firstToken >= 0 {
|
|
|
|
comments = str[0:firstToken]
|
|
|
|
if firstToken > 0 {
|
|
|
|
str = str[firstToken:]
|
|
|
|
env.IncLine(comments)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO capture the value of the last expression and return it as val
|
|
|
|
|
|
|
|
// Run the code.
|
|
|
|
if ir.ParseEvalPrint(str, in) {
|
|
|
|
ir.Repl(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
// handleShutdownRequest sends a "shutdown" message.
|
2017-08-01 17:22:40 -04:00
|
|
|
func handleShutdownRequest(receipt msgReceipt) {
|
2017-07-22 16:49:22 -04:00
|
|
|
content := receipt.Msg.Content.(map[string]interface{})
|
|
|
|
restart := content["restart"].(bool)
|
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
reply := shutdownReply{
|
|
|
|
Restart: restart,
|
|
|
|
}
|
2017-08-25 23:45:04 -04:00
|
|
|
|
2017-09-07 20:51:43 -04:00
|
|
|
if err := receipt.Reply("shutdown_reply", reply); err != nil {
|
2017-08-01 17:22:40 -04:00
|
|
|
log.Fatal(err)
|
2017-07-22 16:49:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Shutting down in response to shutdown_request")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2017-09-24 17:13:50 -04:00
|
|
|
|
|
|
|
func runHeartbeat(hbSocket *zmq.Socket, wg *sync.WaitGroup) func() {
|
|
|
|
quit := make(chan bool)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
poller := zmq.NewPoller()
|
|
|
|
poller.Add(hbSocket, zmq.POLLIN)
|
|
|
|
for {
|
|
|
|
select {
|
2017-09-24 21:28:21 -04:00
|
|
|
case <-quit:
|
2017-09-24 17:13:50 -04:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
pingEvents, err := poller.Poll(500 * time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error polling heartbeat channel: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pingEvents) > 0 {
|
|
|
|
pingMsg, err := hbSocket.RecvBytes(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading heartbeat ping bytes: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = hbSocket.SendBytes(pingMsg, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error sending heartbeat pong bytes: %b\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
quit <- true
|
|
|
|
}
|
2017-09-24 21:28:21 -04:00
|
|
|
}
|