| |
| |
| |
|
|
| package rpc |
|
|
| import ( |
| "bufio" |
| "encoding/gob" |
| "errors" |
| "io" |
| "log" |
| "net" |
| "net/http" |
| "sync" |
| ) |
|
|
| |
| |
| type ServerError string |
|
|
| func (e ServerError) Error() string { |
| return string(e) |
| } |
|
|
| var ErrShutdown = errors.New("connection is shut down") |
|
|
| |
| type Call struct { |
| ServiceMethod string |
| Args any |
| Reply any |
| Error error |
| Done chan *Call |
| } |
|
|
| |
| |
| |
| |
| type Client struct { |
| codec ClientCodec |
|
|
| reqMutex sync.Mutex |
| request Request |
|
|
| mutex sync.Mutex |
| seq uint64 |
| pending map[uint64]*Call |
| closing bool |
| shutdown bool |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type ClientCodec interface { |
| WriteRequest(*Request, any) error |
| ReadResponseHeader(*Response) error |
| ReadResponseBody(any) error |
|
|
| Close() error |
| } |
|
|
| func (client *Client) send(call *Call) { |
| client.reqMutex.Lock() |
| defer client.reqMutex.Unlock() |
|
|
| |
| client.mutex.Lock() |
| if client.shutdown || client.closing { |
| client.mutex.Unlock() |
| call.Error = ErrShutdown |
| call.done() |
| return |
| } |
| seq := client.seq |
| client.seq++ |
| client.pending[seq] = call |
| client.mutex.Unlock() |
|
|
| |
| client.request.Seq = seq |
| client.request.ServiceMethod = call.ServiceMethod |
| err := client.codec.WriteRequest(&client.request, call.Args) |
| if err != nil { |
| client.mutex.Lock() |
| call = client.pending[seq] |
| delete(client.pending, seq) |
| client.mutex.Unlock() |
| if call != nil { |
| call.Error = err |
| call.done() |
| } |
| } |
| } |
|
|
| func (client *Client) input() { |
| var err error |
| var response Response |
| for err == nil { |
| response = Response{} |
| err = client.codec.ReadResponseHeader(&response) |
| if err != nil { |
| break |
| } |
| seq := response.Seq |
| client.mutex.Lock() |
| call := client.pending[seq] |
| delete(client.pending, seq) |
| client.mutex.Unlock() |
|
|
| switch { |
| case call == nil: |
| |
| |
| |
| |
| |
| err = client.codec.ReadResponseBody(nil) |
| if err != nil { |
| err = errors.New("reading error body: " + err.Error()) |
| } |
| case response.Error != "": |
| |
| |
| |
| call.Error = ServerError(response.Error) |
| err = client.codec.ReadResponseBody(nil) |
| if err != nil { |
| err = errors.New("reading error body: " + err.Error()) |
| } |
| call.done() |
| default: |
| err = client.codec.ReadResponseBody(call.Reply) |
| if err != nil { |
| call.Error = errors.New("reading body " + err.Error()) |
| } |
| call.done() |
| } |
| } |
| |
| client.reqMutex.Lock() |
| client.mutex.Lock() |
| client.shutdown = true |
| closing := client.closing |
| if err == io.EOF { |
| if closing { |
| err = ErrShutdown |
| } else { |
| err = io.ErrUnexpectedEOF |
| } |
| } |
| for _, call := range client.pending { |
| call.Error = err |
| call.done() |
| } |
| client.mutex.Unlock() |
| client.reqMutex.Unlock() |
| if debugLog && err != io.EOF && !closing { |
| log.Println("rpc: client protocol error:", err) |
| } |
| } |
|
|
| func (call *Call) done() { |
| select { |
| case call.Done <- call: |
| |
| default: |
| |
| |
| if debugLog { |
| log.Println("rpc: discarding Call reply due to insufficient Done chan capacity") |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func NewClient(conn io.ReadWriteCloser) *Client { |
| encBuf := bufio.NewWriter(conn) |
| client := &gobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(encBuf), encBuf} |
| return NewClientWithCodec(client) |
| } |
|
|
| |
| |
| func NewClientWithCodec(codec ClientCodec) *Client { |
| client := &Client{ |
| codec: codec, |
| pending: make(map[uint64]*Call), |
| } |
| go client.input() |
| return client |
| } |
|
|
| type gobClientCodec struct { |
| rwc io.ReadWriteCloser |
| dec *gob.Decoder |
| enc *gob.Encoder |
| encBuf *bufio.Writer |
| } |
|
|
| func (c *gobClientCodec) WriteRequest(r *Request, body any) (err error) { |
| if err = c.enc.Encode(r); err != nil { |
| return |
| } |
| if err = c.enc.Encode(body); err != nil { |
| return |
| } |
| return c.encBuf.Flush() |
| } |
|
|
| func (c *gobClientCodec) ReadResponseHeader(r *Response) error { |
| return c.dec.Decode(r) |
| } |
|
|
| func (c *gobClientCodec) ReadResponseBody(body any) error { |
| return c.dec.Decode(body) |
| } |
|
|
| func (c *gobClientCodec) Close() error { |
| return c.rwc.Close() |
| } |
|
|
| |
| |
| func DialHTTP(network, address string) (*Client, error) { |
| return DialHTTPPath(network, address, DefaultRPCPath) |
| } |
|
|
| |
| |
| func DialHTTPPath(network, address, path string) (*Client, error) { |
| conn, err := net.Dial(network, address) |
| if err != nil { |
| return nil, err |
| } |
| io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n") |
|
|
| |
| |
| resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"}) |
| if err == nil && resp.Status == connected { |
| return NewClient(conn), nil |
| } |
| if err == nil { |
| err = errors.New("unexpected HTTP response: " + resp.Status) |
| } |
| conn.Close() |
| return nil, &net.OpError{ |
| Op: "dial-http", |
| Net: network + " " + address, |
| Addr: nil, |
| Err: err, |
| } |
| } |
|
|
| |
| func Dial(network, address string) (*Client, error) { |
| conn, err := net.Dial(network, address) |
| if err != nil { |
| return nil, err |
| } |
| return NewClient(conn), nil |
| } |
|
|
| |
| |
| func (client *Client) Close() error { |
| client.mutex.Lock() |
| if client.closing { |
| client.mutex.Unlock() |
| return ErrShutdown |
| } |
| client.closing = true |
| client.mutex.Unlock() |
| return client.codec.Close() |
| } |
|
|
| |
| |
| |
| |
| func (client *Client) Go(serviceMethod string, args any, reply any, done chan *Call) *Call { |
| call := new(Call) |
| call.ServiceMethod = serviceMethod |
| call.Args = args |
| call.Reply = reply |
| if done == nil { |
| done = make(chan *Call, 10) |
| } else { |
| |
| |
| |
| |
| if cap(done) == 0 { |
| log.Panic("rpc: done channel is unbuffered") |
| } |
| } |
| call.Done = done |
| client.send(call) |
| return call |
| } |
|
|
| |
| func (client *Client) Call(serviceMethod string, args any, reply any) error { |
| call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done |
| return call.Error |
| } |
|
|