protocol.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Package protocol provides bounded, multiplexed JSON RPC over inherited pipes.
  2. package protocol
  3. import (
  4. "bufio"
  5. "context"
  6. "encoding/binary"
  7. "encoding/json"
  8. "errors"
  9. "io"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "clusterflow.dev/cfbrowser/api"
  14. )
  15. // FeatureVersion changes when SDK/worker operations must be deployed together.
  16. const FeatureVersion = 2
  17. const Version = 1
  18. const MaxMessage = 512 * 1024
  19. type Request struct {
  20. Version int `json:"version"`
  21. ID uint64 `json:"id"`
  22. Method string `json:"method"`
  23. Deadline int64 `json:"deadline,omitempty"`
  24. Params json.RawMessage `json:"params,omitempty"`
  25. }
  26. type Response struct {
  27. Version int `json:"version"`
  28. ID uint64 `json:"id"`
  29. Result json.RawMessage `json:"result,omitempty"`
  30. Error *api.Error `json:"error,omitempty"`
  31. }
  32. func Read(r io.Reader, v any) error {
  33. var header [4]byte
  34. if _, e := io.ReadFull(r, header[:]); e != nil {
  35. return e
  36. }
  37. n := binary.BigEndian.Uint32(header[:])
  38. if n == 0 || n > MaxMessage {
  39. return api.Err("BROWSER_PROTOCOL_LIMIT", "invalid message length")
  40. }
  41. b := make([]byte, n)
  42. if _, e := io.ReadFull(r, b); e != nil {
  43. return e
  44. }
  45. return json.Unmarshal(b, v)
  46. }
  47. func Write(w io.Writer, v any) error {
  48. b, err := json.Marshal(v)
  49. if err != nil {
  50. return err
  51. }
  52. if len(b) > MaxMessage {
  53. return api.Err("BROWSER_PROTOCOL_LIMIT", "message exceeds 512 KiB")
  54. }
  55. var header [4]byte
  56. binary.BigEndian.PutUint32(header[:], uint32(len(b)))
  57. if err = writeAll(w, header[:]); err != nil {
  58. return err
  59. }
  60. return writeAll(w, b)
  61. }
  62. func writeAll(w io.Writer, b []byte) error {
  63. for len(b) > 0 {
  64. n, e := w.Write(b)
  65. if e != nil {
  66. return e
  67. }
  68. if n <= 0 {
  69. return io.ErrShortWrite
  70. }
  71. b = b[n:]
  72. }
  73. return nil
  74. }
  75. type Client struct {
  76. r io.Reader
  77. w io.Writer
  78. writeMu sync.Mutex
  79. mu sync.Mutex
  80. pending map[uint64]chan Response
  81. seq atomic.Uint64
  82. done chan struct{}
  83. closeOnce sync.Once
  84. }
  85. func NewClient(r io.Reader, w io.Writer) *Client {
  86. c := &Client{r: r, w: w, pending: map[uint64]chan Response{}, done: make(chan struct{})}
  87. go c.readLoop()
  88. return c
  89. }
  90. func (c *Client) Close() { c.closeOnce.Do(func() { close(c.done) }) }
  91. func (c *Client) readLoop() {
  92. defer c.Close()
  93. r := bufio.NewReader(c.r)
  94. for {
  95. var response Response
  96. if e := Read(r, &response); e != nil || response.Version != Version {
  97. return
  98. }
  99. c.mu.Lock()
  100. ch := c.pending[response.ID]
  101. c.mu.Unlock()
  102. if ch != nil {
  103. select {
  104. case ch <- response:
  105. default:
  106. }
  107. }
  108. }
  109. }
  110. func (c *Client) send(request Request) error {
  111. c.writeMu.Lock()
  112. defer c.writeMu.Unlock()
  113. return Write(c.w, request)
  114. }
  115. func (c *Client) Call(ctx context.Context, method string, args, out any) error {
  116. if err := ctx.Err(); err != nil {
  117. return err
  118. }
  119. params, err := json.Marshal(args)
  120. if err != nil {
  121. return err
  122. }
  123. id := c.seq.Add(1)
  124. ch := make(chan Response, 1)
  125. c.mu.Lock()
  126. if len(c.pending) >= 32 {
  127. c.mu.Unlock()
  128. return api.Err("BROWSER_LIMIT", "too many pending worker requests")
  129. }
  130. c.pending[id] = ch
  131. c.mu.Unlock()
  132. defer func() { c.mu.Lock(); delete(c.pending, id); c.mu.Unlock() }()
  133. request := Request{Version: Version, ID: id, Method: method, Params: params}
  134. if d, ok := ctx.Deadline(); ok {
  135. request.Deadline = d.UnixMilli()
  136. }
  137. select {
  138. case <-c.done:
  139. return api.Err("BROWSER_WORKER_EXITED", "worker connection closed")
  140. default:
  141. }
  142. if err = c.send(request); err != nil {
  143. return err
  144. }
  145. select {
  146. case r := <-ch:
  147. if r.Error != nil {
  148. switch r.Error.Code {
  149. case "BROWSER_CANCELED":
  150. r.Error.Cause = context.Canceled
  151. case "BROWSER_TIMEOUT":
  152. r.Error.Cause = context.DeadlineExceeded
  153. }
  154. return r.Error
  155. }
  156. if out == nil {
  157. return nil
  158. }
  159. return json.Unmarshal(r.Result, out)
  160. case <-c.done:
  161. return api.Err("BROWSER_WORKER_EXITED", "worker connection closed")
  162. case <-ctx.Done():
  163. // Cancellation is advisory; a dispatched action may already have taken effect.
  164. go func() {
  165. _ = c.send(Request{Version: Version, ID: c.seq.Add(1), Method: "request.cancel", Params: json.RawMessage([]byte(`{"id":` + itoa(id) + `}`))})
  166. }()
  167. return ctx.Err()
  168. }
  169. }
  170. func itoa(n uint64) string { b, _ := json.Marshal(n); return string(b) }
  171. type Handler func(context.Context, string, json.RawMessage) (any, error)
  172. // Serve bounds concurrency and cancels all handlers when the input closes.
  173. func Serve(ctx context.Context, r io.Reader, w io.Writer, handler Handler) error {
  174. ctx, cancel := context.WithCancel(ctx)
  175. defer cancel()
  176. var mu, writeMu sync.Mutex
  177. pending := map[uint64]context.CancelFunc{}
  178. var wg sync.WaitGroup
  179. if closer, ok := r.(io.Closer); ok {
  180. stop := context.AfterFunc(ctx, func() { _ = closer.Close() })
  181. defer stop()
  182. }
  183. defer func() {
  184. mu.Lock()
  185. for _, stop := range pending {
  186. stop()
  187. }
  188. mu.Unlock()
  189. wg.Wait()
  190. }()
  191. respond := func(request Request, result any, err error) {
  192. response := Response{Version: Version, ID: request.ID}
  193. if err != nil {
  194. var typed *api.Error
  195. if errors.Is(err, context.Canceled) {
  196. response.Error = &api.Error{Code: "BROWSER_CANCELED", Message: "request canceled"}
  197. } else if errors.Is(err, context.DeadlineExceeded) {
  198. response.Error = &api.Error{Code: "BROWSER_TIMEOUT", Message: "request deadline exceeded"}
  199. } else if errors.As(err, &typed) {
  200. response.Error = typed
  201. } else {
  202. response.Error = &api.Error{Code: "BROWSER_WORKER_ERROR", Message: err.Error()}
  203. }
  204. } else {
  205. response.Result, _ = json.Marshal(result)
  206. }
  207. writeMu.Lock()
  208. defer writeMu.Unlock()
  209. if e := Write(w, response); e != nil {
  210. cancel()
  211. }
  212. }
  213. for {
  214. var req Request
  215. err := Read(r, &req)
  216. if err == io.EOF {
  217. return nil
  218. }
  219. if err != nil {
  220. return err
  221. }
  222. if req.Version != Version || req.ID == 0 {
  223. respond(req, nil, api.Err("BROWSER_PROTOCOL_VERSION", "unsupported protocol envelope"))
  224. continue
  225. }
  226. if req.Method == "request.cancel" {
  227. var a struct {
  228. ID uint64 `json:"id"`
  229. }
  230. _ = json.Unmarshal(req.Params, &a)
  231. mu.Lock()
  232. stop := pending[a.ID]
  233. mu.Unlock()
  234. if stop != nil {
  235. stop()
  236. }
  237. respond(req, true, nil)
  238. continue
  239. }
  240. mu.Lock()
  241. if pending[req.ID] != nil || len(pending) >= 32 {
  242. mu.Unlock()
  243. respond(req, nil, api.Err("BROWSER_LIMIT", "duplicate ID or concurrency limit"))
  244. continue
  245. }
  246. call, stop := context.WithCancel(ctx)
  247. if req.Deadline > 0 {
  248. var deadlineCancel context.CancelFunc
  249. call, deadlineCancel = context.WithDeadline(call, time.UnixMilli(req.Deadline))
  250. old := stop
  251. stop = func() { deadlineCancel(); old() }
  252. }
  253. pending[req.ID] = stop
  254. mu.Unlock()
  255. wg.Add(1)
  256. go func(req Request, call context.Context, stop context.CancelFunc) {
  257. defer wg.Done()
  258. defer stop()
  259. defer func() { mu.Lock(); delete(pending, req.ID); mu.Unlock() }()
  260. result, err := handler(call, req.Method, req.Params)
  261. respond(req, result, err)
  262. }(req, call, stop)
  263. }
  264. }