Skip to content

Commit

Permalink
channel: Remove the Sender and Receiver types.
Browse files Browse the repository at this point in the history
The distinction between sender and receiver is not carrying its weight in the
public API for this package. Move unexported safety-check wrappers into the
top-level package and collapse the methods back into the Channel type.
  • Loading branch information
creachadair committed Aug 25, 2021
1 parent cc4a10e commit 65bf9ad
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
9 changes: 7 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"strings"

"github.com/creachadair/jrpc2/channel"
"github.com/creachadair/jrpc2/code"
)

Expand Down Expand Up @@ -389,8 +388,14 @@ func fixID(id json.RawMessage) json.RawMessage {
return nil
}

// sender is the subset of channel.Channel needed to send messages.
type sender interface{ Send([]byte) error }

// receiver is the subset of channel.Channel needed to receive messages.
type receiver interface{ Recv() ([]byte, error) }

// encode marshals rsps as JSON and forwards it to the channel.
func encode(ch channel.Sender, rsps jmessages) (int, error) {
func encode(ch sender, rsps jmessages) (int, error) {
bits, err := rsps.toJSON()
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newPipe(framing Framing) (client, server Channel) {
return
}

func testSendRecv(t *testing.T, s Sender, r Receiver, msg string) {
func testSendRecv(t *testing.T, s, r Channel, msg string) {
var wg sync.WaitGroup
var sendErr, recvErr error
var data []byte
Expand Down
23 changes: 7 additions & 16 deletions channel/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,21 @@ package channel

import "strings"

// A Sender represents the ability to transmit a message on a channel.
type Sender interface {
// A Channel represents the ability to transmit and receive data records. A
// channel does not interpret the contents of a record, but may add and remove
// framing so that records can be embedded in higher-level protocols.
//
// One sender and one receiver may use a Channel concurrently, but the methods
// of a Channel are not otherwise required to be safe for concurrent use.
type Channel interface {
// Send transmits a record on the channel. Each call to Send transmits one
// complete record.
Send([]byte) error
}

// A Receiver represents the ability to receive a message from a channel.
type Receiver interface {
// Recv returns the next available record from the channel. If no further
// messages are available, it returns nil, io.EOF. Each call to Recv
// fetches a single complete record.
Recv() ([]byte, error)
}

// A Channel represents the ability to transmit and receive data records. A
// channel does not interpret the contents of a record, but may add and remove
// framing so that records can be embedded in higher-level protocols.
//
// One sender and one receiver may use a Channel concurrently, but the methods
// of a Channel are not otherwise required to be safe for concurrent use.
type Channel interface {
Sender
Receiver

// Close shuts down the channel, after which no further records may be
// sent or received.
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewClient(ch channel.Channel, opts *ClientOptions) *Client {
// accept receives the next batch of responses from the server. This may
// either be a list or a single object, the decoder for jmessages knows how to
// handle both. The caller must not hold c.mu.
func (c *Client) accept(ch channel.Receiver) error {
func (c *Client) accept(ch receiver) error {
var in jmessages
bits, err := ch.Recv()
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (s *Server) waitForBarrier(n int) {
// completed, to ensure that notifications are processed in a partial order
// that respects order of receipt. Notifications within a batch are handled
// concurrently.
func (s *Server) dispatch(next jmessages, ch channel.Sender) func() error {
func (s *Server) dispatch(next jmessages, ch sender) func() error {
// Resolve all the task handlers or record errors.
start := time.Now()
tasks := s.checkAndAssign(next)
Expand Down Expand Up @@ -240,7 +240,7 @@ func (s *Server) dispatch(next jmessages, ch channel.Sender) func() error {

// deliver cleans up completed responses and arranges their replies (if any) to
// be sent back to the client.
func (s *Server) deliver(rsps jmessages, ch channel.Sender, elapsed time.Duration) error {
func (s *Server) deliver(rsps jmessages, ch sender, elapsed time.Duration) error {
if len(rsps) == 0 {
return nil
}
Expand Down Expand Up @@ -563,7 +563,7 @@ func (s *Server) stop(err error) {
// them to the queue. Decoding errors and message-format problems are handled
// and reported back to the client directly, so that any message that survives
// into the request queue is structurally valid.
func (s *Server) read(ch channel.Receiver) {
func (s *Server) read(ch receiver) {
for {
// If the message is not sensible, report an error; otherwise enqueue it
// for processing. Errors in individual requests are handled later.
Expand Down

0 comments on commit 65bf9ad

Please sign in to comment.