checkpoint
This commit is contained in:
79
server/rpc_stream.go
Normal file
79
server/rpc_stream.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type rpcStream struct {
|
||||
sync.RWMutex
|
||||
seq uint64
|
||||
closed bool
|
||||
err error
|
||||
request Request
|
||||
codec serverCodec
|
||||
context context.Context
|
||||
}
|
||||
|
||||
func (r *rpcStream) Context() context.Context {
|
||||
return r.context
|
||||
}
|
||||
|
||||
func (r *rpcStream) Request() Request {
|
||||
return r.request
|
||||
}
|
||||
|
||||
func (r *rpcStream) Send(msg interface{}) error {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
seq := r.seq
|
||||
r.seq++
|
||||
|
||||
resp := response{
|
||||
ServiceMethod: r.request.Method(),
|
||||
Seq: seq,
|
||||
}
|
||||
|
||||
err := codec.WriteResponse(&resp, msg, false)
|
||||
if err != nil {
|
||||
log.Println("rpc: writing response:", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *rpcStream) Recv(msg interface{}) error {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
req := request{}
|
||||
|
||||
if err := codec.ReadRequestHeader(&req); err != nil {
|
||||
// discard body
|
||||
codec.ReadRequestBody(nil)
|
||||
return err
|
||||
}
|
||||
|
||||
if err = codec.ReadRequestBody(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *rpcStream) Error() error {
|
||||
r.RLock()
|
||||
defer r.RUnlock()
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r *rpcStream) Close() error {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
r.closed = true
|
||||
return r.codec.Close()
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
|
||||
log "github.com/golang/glog"
|
||||
"github.com/pborman/uuid"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
@@ -61,10 +62,23 @@ type Request interface {
|
||||
Method() string
|
||||
ContentType() string
|
||||
Request() interface{}
|
||||
// indicates whether the response should be streaming
|
||||
// indicates whether the request will be streamed
|
||||
Stream() bool
|
||||
}
|
||||
|
||||
// Streamer represents a stream established with a client.
|
||||
// A stream can be bidirectional which is indicated by the request.
|
||||
// The last error will be left in Error().
|
||||
// EOF indicated end of the stream.
|
||||
type Streamer interface {
|
||||
Context() context.Context
|
||||
Request() Request
|
||||
Send(interface{}) error
|
||||
Recv(interface{}) error
|
||||
Error() error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
|
||||
var (
|
||||
|
||||
@@ -19,3 +19,5 @@ type HandlerWrapper func(HandlerFunc) HandlerFunc
|
||||
|
||||
// SubscriberWrapper wraps the SubscriberFunc and returns the equivalent
|
||||
type SubscriberWrapper func(SubscriberFunc) SubscriberFunc
|
||||
|
||||
type StreamWrapper func(Streamer) Streamer
|
||||
|
||||
Reference in New Issue
Block a user