Skip to content

Instantly share code, notes, and snippets.

@ephemeralsnow
Created April 8, 2016 12:54
Show Gist options
  • Save ephemeralsnow/f8cfeaf90b49cebe1bb2ae724f145c37 to your computer and use it in GitHub Desktop.
Save ephemeralsnow/f8cfeaf90b49cebe1bb2ae724f145c37 to your computer and use it in GitHub Desktop.
github.com/gengo/grpc-gateway/runtime/stream.go(仮)
package runtime
import (
"io"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type DummyStream struct {
ctx context.Context
reqHeader metadata.MD
reqTrailer metadata.MD
reqCh chan interface{}
resHeader metadata.MD
resTrailer metadata.MD
resCh chan interface{}
//grpc.Stream
}
func NewDummyStream() *DummyStream {
return &DummyStream{
reqCh: make(chan interface{}),
resCh: make(chan interface{}),
}
}
//func (s *DummyStream) Context() context.Context {
// return s.ctx
//}
//
//func (s *DummyStream) SendMsg(m interface{}) error {
// return nil
//}
//
//func (s *DummyStream) RecvMsg(m interface{}) error {
// return nil
//}
func (s *DummyStream) GetClientStream() *DummyClientStream {
return &DummyClientStream{s: s}
}
func (s *DummyStream) GetServerStream() *DummyServerStream {
return &DummyServerStream{s: s}
}
type DummyClientStream struct {
s *DummyStream
grpc.ClientStream
}
// grpc.Stream
func (s *DummyClientStream) Context() context.Context {
return s.s.ctx
}
func (s *DummyClientStream) SendMsg(m interface{}) error {
select {
case <-s.s.ctx.Done():
return s.s.ctx.Err()
case s.s.reqCh <- m:
return nil
default:
return io.EOF
}
}
func (s *DummyClientStream) RecvMsg(m interface{}) error {
select {
case <-s.s.ctx.Done():
return s.s.ctx.Err()
case s.s.resCh <- m:
return nil
default:
return io.EOF
}
}
// grpc.ClientStream
func (s *DummyClientStream) Header() (metadata.MD, error) {
return s.s.resHeader, nil // TODO resHeaderCh?
}
func (s *DummyClientStream) Trailer() metadata.MD {
return s.s.resTrailer // TODO resTrailerCh?
}
func (s *DummyClientStream) CloseSend() error {
close(s.s.reqCh)
return nil
}
type DummyServerStream struct {
s *DummyStream
grpc.ServerStream
}
func (s *DummyServerStream) SendHeader(md metadata.MD) error {
for k, v := range md {
s.header[k] = v
}
return nil
}
func (s *DummyServerStream) SetTrailer(md metadata.MD) {
s.trailer = md
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment