2018-04-14 10:45:58 +00:00
|
|
|
// Package jstreamrpc implements the mrpc interfaces using the jstream protocol.
|
|
|
|
// This implementation makes a few design decisions which are not specified in
|
|
|
|
// the protocol, but which are good best practices none-the-less.
|
|
|
|
package jstreamrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/jstream"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO Error?
|
|
|
|
// TODO SizeHints
|
2018-05-16 12:56:06 +00:00
|
|
|
// TODO it'd be nice if the types here played nice with mrpc.ReflectClient
|
2018-04-14 10:45:58 +00:00
|
|
|
|
2018-04-27 08:03:39 +00:00
|
|
|
type debug struct {
|
2018-05-16 12:56:06 +00:00
|
|
|
Debug mrpc.Debug `json:"debug,omitempty"`
|
2018-04-14 10:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type reqHead struct {
|
2018-04-27 08:03:39 +00:00
|
|
|
debug
|
2018-04-14 10:45:58 +00:00
|
|
|
Method string `json:"method"`
|
|
|
|
}
|
|
|
|
|
2018-04-27 08:03:39 +00:00
|
|
|
type resTail struct {
|
|
|
|
debug
|
2018-05-16 12:56:06 +00:00
|
|
|
Error error `json:"err,omitempty"`
|
2018-04-27 08:03:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 10:45:58 +00:00
|
|
|
type ctxVal int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxValR ctxVal = iota
|
|
|
|
ctxValW
|
|
|
|
)
|
|
|
|
|
|
|
|
func unmarshalBody(i interface{}, el jstream.Element) error {
|
|
|
|
switch iT := i.(type) {
|
|
|
|
case func(*jstream.StreamReader) error:
|
|
|
|
stream, err := el.Stream()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return iT(stream)
|
|
|
|
case *io.Reader:
|
|
|
|
ioR, err := el.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*iT = ioR
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return el.Value(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalBody(w *jstream.StreamWriter, i interface{}) error {
|
|
|
|
switch iT := i.(type) {
|
|
|
|
case func(*jstream.StreamWriter) error:
|
|
|
|
return w.EncodeStream(0, iT)
|
|
|
|
case io.Reader:
|
|
|
|
return w.EncodeBytes(0, iT)
|
|
|
|
default:
|
|
|
|
return w.EncodeValue(iT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleCall TODO
|
|
|
|
//
|
|
|
|
// If this returns an error then both r and w should be discarded and no longer
|
|
|
|
// used.
|
|
|
|
func HandleCall(
|
|
|
|
ctx context.Context,
|
|
|
|
r *jstream.StreamReader,
|
|
|
|
w *jstream.StreamWriter,
|
|
|
|
h mrpc.Handler,
|
|
|
|
) error {
|
2018-05-16 12:56:06 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-04-14 10:45:58 +00:00
|
|
|
var head reqHead
|
|
|
|
if err := r.Next().Value(&head); err != nil {
|
|
|
|
return err
|
|
|
|
} else if head.Method == "" {
|
|
|
|
return errors.New("request head missing 'method' field")
|
|
|
|
}
|
|
|
|
|
|
|
|
var didReadBody bool
|
|
|
|
ctx = context.WithValue(ctx, ctxValR, r)
|
|
|
|
ctx = context.WithValue(ctx, ctxValW, w)
|
2018-05-16 12:56:06 +00:00
|
|
|
|
|
|
|
rw := new(mrpc.ResponseWriter)
|
|
|
|
h.ServeRPC(mrpc.Request{
|
2018-04-14 10:45:58 +00:00
|
|
|
Context: ctx,
|
|
|
|
Method: head.Method,
|
2018-05-16 12:56:06 +00:00
|
|
|
Unmarshal: func(i interface{}) error {
|
2018-04-14 10:45:58 +00:00
|
|
|
didReadBody = true
|
|
|
|
return unmarshalBody(i, r.Next())
|
|
|
|
},
|
2018-05-16 12:56:06 +00:00
|
|
|
Debug: head.debug.Debug,
|
|
|
|
}, rw)
|
2018-04-14 10:45:58 +00:00
|
|
|
|
2018-05-16 12:56:06 +00:00
|
|
|
// TODO unmarshaling request and marshaling response should be in
|
|
|
|
// their own go-routines, just in case they are streams/bytes which depend
|
|
|
|
// on each other
|
2018-04-14 10:45:58 +00:00
|
|
|
|
2018-05-16 12:56:06 +00:00
|
|
|
resErr, resErrOk := rw.Response.(error)
|
|
|
|
if resErrOk {
|
|
|
|
if err := w.EncodeValue(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := marshalBody(w, rw.Response); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-14 10:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO to reduce chance of user error maybe Discard should discard any
|
|
|
|
// remaining data on the Element, not on Elements which haven't been read
|
|
|
|
// from yet. Then it could always be called on the request body at this
|
|
|
|
// point.
|
|
|
|
|
|
|
|
// Reading the tail (and maybe discarding the body) should only be done once
|
|
|
|
// marshalBody has finished
|
|
|
|
if !didReadBody {
|
2018-05-16 12:56:06 +00:00
|
|
|
if err := r.Next().Discard(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-14 10:45:58 +00:00
|
|
|
}
|
2018-04-27 08:03:39 +00:00
|
|
|
|
2018-05-16 12:56:06 +00:00
|
|
|
if err := w.EncodeValue(resTail{
|
|
|
|
debug: debug{Debug: rw.Debug},
|
|
|
|
Error: resErr,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-14 10:45:58 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-16 12:56:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
func sqr(r mrpc.Request, rw *mrpc.ResponseWriter) {
|
|
|
|
ch := make(chan int)
|
|
|
|
rw.Response = func(w *jstream.StreamWriter) error {
|
|
|
|
for i := range ch {
|
|
|
|
if err := w.EncodeValue(i * i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
err := r.Unmarshal(func(r *jstream.StreamReader) error {
|
|
|
|
for {
|
|
|
|
var i int
|
|
|
|
if err := r.Next().Value(&i); err == jstream.ErrStreamEnded {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch <- i
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
*/
|