func (c *Conn) Run(runCtx context.Context, handler Handler) error {
	for {
		// get the next message
		msg, n, err := c.stream.Read(runCtx)
		if err != nil {
			// The stream failed, we cannot continue. If the client disconnected
			// normally, we should get ErrDisconnected here.
			return err
		}
		switch msg := msg.(type) {
		case Request:
			labels := []label.Label{
				tag.Method.Of(msg.Method()),
				tag.RPCDirection.Of(tag.Inbound),
				{}, // reserved for ID if present
			}
			if call, ok := msg.(*Call); ok {
				labels[len(labels)-1] = tag.RPCID.Of(fmt.Sprintf("%q", call.ID()))
			} else {
				labels = labels[:len(labels)-1]
			}
			reqCtx, spanDone := event.Start(runCtx, msg.Method(), labels...)
			event.Metric(reqCtx,
				tag.Started.Of(1),
				tag.ReceivedBytes.Of(n))
			if err := handler(reqCtx, replier(c, msg, spanDone), msg); err != nil {
				// delivery failed, not much we can do
				event.Error(reqCtx, "jsonrpc2 message delivery failed", err)
			}
		case *Response:
			// If method is not set, this should be a response, in which case we must
			// have an id to send the response back to the caller.
			c.pendingMu.Lock()
			rchan, ok := c.pending[msg.id]
			c.pendingMu.Unlock()
			if ok {
				rchan <- msg
			}
		}
	}
}
// Run blocks until the connection is terminated, and returns any error that
// caused the termination.
// It must be called exactly once for each Conn.
// It returns only when the reader is closed or there is an error in the stream.
Run blocks until the connection is terminated, and returns any error that
caused the termination.
It must be called exactly once for each Conn.
It returns only when the reader is closed or there is an error in the stream.
// Run blocks until the connection is terminated and returns any error that // caused the termination . // It must be called exactly once for each Conn . // It returns only when the reader is closed or there is an error in the stream .
func (c *Conn) Run(runCtx context.Context, handler Handler) error {
	for {
		msg, n, err := c.stream.Read(runCtx)
		if err != nil {
			return err
		}
		switch msg := msg.(type) {
		case Request:
			labels := []label.Label{
				tag.Method.Of(msg.Method()),
				tag.RPCDirection.Of(tag.Inbound),
				{}, 
			}
			if call, ok := msg.(*Call); ok {
				labels[len(labels)-1] = tag.RPCID.Of(fmt.Sprintf("%q", call.ID()))
			} else {
				labels = labels[:len(labels)-1]
			}
			reqCtx, spanDone := event.Start(runCtx, msg.Method(), labels...)
			event.Metric(reqCtx,
				tag.Started.Of(1),
				tag.ReceivedBytes.Of(n))
			if err := handler(reqCtx, replier(c, msg, spanDone), msg); err != nil {
				event.Error(reqCtx, "jsonrpc2 message delivery failed", err)
			}
		case *Response:
			c.pendingMu.Lock()
			rchan, ok := c.pending[msg.id]
			c.pendingMu.Unlock()
			if ok {
				rchan <- msg
			}
		}
	}
}
func ( c * Conn ) Run ( runCtx context . Context , handler Handler ) error { for { msg , n , err := c . stream . Read ( runCtx ) if err != nil { return err } switch msg := msg . ( type ) { case Request : labels := [ ] label . Label { tag . Method . Of ( msg . Method ( ) ) , tag . RPCDirection . Of ( tag . Inbound ) , { } , } if call , ok := msg . ( * Call ) ; ok { labels [ len ( labels ) - 1 ] = tag . RPCID . Of ( fmt . Sprintf ( " " , call . ID ( ) ) ) } else { labels = labels [ : len ( labels ) - 1 ] } reqCtx , spanDone := event . Start ( runCtx , msg . Method ( ) , labels ... ) event . Metric ( reqCtx , tag . Started . Of ( 1 ) , tag . ReceivedBytes . Of ( n ) ) if err := handler ( reqCtx , replier ( c , msg , spanDone ) , msg ) ; err != nil { event . Error ( reqCtx , " " , err ) } case * Response : c . pendingMu . Lock ( ) rchan , ok := c . pending [ msg . id ] c . pendingMu . Unlock ( ) if ok { rchan <- msg } } } }
Run blocks until the connection is terminated, and returns any error that
caused the termination.
Run blocks until the connection is terminated and returns any error that caused the termination .
// get the next message // The stream failed, we cannot continue. If the client disconnected // normally, we should get ErrDisconnected here. // reserved for ID if present // delivery failed, not much we can do // If method is not set, this should be a response, in which case we must // have an id to send the response back to the caller.