Add comments
This commit is contained in:
parent
0ae40d69bc
commit
c17ba102dd
13
socket.go
13
socket.go
@ -636,42 +636,53 @@ func getMsgSender(ctx context.Context, srv *server.Server) (MessageSender, bool)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The MessageSender interface sends messages to the client
|
||||||
type MessageSender interface {
|
type MessageSender interface {
|
||||||
SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error
|
SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rpcMsgSender sends messages using RPCX, for clients that support it
|
||||||
type rpcMsgSender struct {
|
type rpcMsgSender struct {
|
||||||
srv *server.Server
|
srv *server.Server
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendMessage uses the server to send an RPCX message back to the client
|
||||||
func (r *rpcMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
func (r *rpcMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
||||||
|
|
||||||
return r.srv.SendMessage(r.conn, servicePath, serviceMethod, metadata, data)
|
return r.srv.SendMessage(r.conn, servicePath, serviceMethod, metadata, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// httpMsgSender sends messages to the given return URL, for clients that provide it
|
||||||
type httpMsgSender struct {
|
type httpMsgSender struct {
|
||||||
url string
|
url string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendMessage uses HTTP to send a message back to the client
|
||||||
func (h *httpMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
func (h *httpMsgSender) SendMessage(servicePath, serviceMethod string, metadata map[string]string, data []byte) error {
|
||||||
|
// Create new POST request with provided URL
|
||||||
req, err := http.NewRequest(http.MethodPost, h.url, bytes.NewReader(data))
|
req, err := http.NewRequest(http.MethodPost, h.url, bytes.NewReader(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set service path and method headers
|
||||||
req.Header.Set("X-RPCX-ServicePath", servicePath)
|
req.Header.Set("X-RPCX-ServicePath", servicePath)
|
||||||
req.Header.Set("X-RPCX-ServiceMethod", serviceMethod)
|
req.Header.Set("X-RPCX-ServiceMethod", serviceMethod)
|
||||||
|
|
||||||
|
// Create new URL query values
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
|
// Transfer values from metadata to query
|
||||||
for k, v := range metadata {
|
for k, v := range metadata {
|
||||||
query.Set(k, v)
|
query.Set(k, v)
|
||||||
}
|
}
|
||||||
|
// Set metadata header by encoding query values
|
||||||
req.Header.Set("X-RPCX-Meta", query.Encode())
|
req.Header.Set("X-RPCX-Meta", query.Encode())
|
||||||
|
|
||||||
|
// Perform request
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Close body
|
||||||
return res.Body.Close()
|
return res.Body.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user