Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP(x/http/client): net/http/client #13

Closed
wants to merge 21 commits into from

Conversation

spongehah
Copy link
Contributor

@spongehah spongehah commented Jul 30, 2024

  • http.Get()
  • In order to return the response without stopping the loop, Use channels to pass the response.
  • [tmp] timeout
  • header: Add textproto to process header and support custom header
  • Add the post request
    • http.Post()
    • http.PostForm()
  • redirect
  • cookie-jar
  • maxConnsPerHost
  • http proxy
  • 100-continue
  • http.bodyEOFSignal and http.body struct
  • one loop per transport
  • retry
  • gzip reader
  • Trailer
  • ConnPool
  • Mutiple eventLoop

Implementation logic:

t.RoundTrip() {
    once.Do(func(){
       t.loop = libuv.LoopNew()
       t.async = &libuv.Async{}
       t.exec = hyper.NewExecutor()
       t.loop.Async(t.async, asyncCb)
       checker := &libuv.Check{}
       libuv.InitCheck(t.loop, checker)
       (*libuv.Handle)(c.Pointer(checker)).SetData(c.Pointer(t))
       checker.Start(readWriteLoop)
       go t.loop.Run(libuv.RUN_DEFAULT)
    })
    ...
    pc, err := t.getConn(req) {
       pc = &persistConn{
          t:             t,
          cacheKey:      cm.key(),
          closech:       make(chan struct{}, 1),
          writeLoopDone: make(chan struct{}, 1),
       }
       return pc, nil
    }
    ...
    resp, err = pc.roundTrip(req) {
       resc := make(chan responseAndError)
       handshakeTask := hyper.Handshake(hyperIo, opts)
       taskData := &taskData{
          taskId:     write,
          req:        req,
          pc:         pc,
          addedGzip:  requestedGzip,
          writeErrCh: writeErrCh,
          callerGone: gone,
          resc:       resc,
       }
       handshakeTask.SetUserdata(c.Pointer(taskData))
       pc.t.exec.Push(handshakeTask)
       // Wake up libuv. Loop
       pc.t.async.Send()
       select {
       case re := <-resc: // Waiting for pc.startLoop() to put response into resc
          if re.err != nil {
             return nil, re.err
          }
          return re.res, nil
       case <-pc.timeoutch:
          return nil, fmt.Errorf("request timeout\n")
       }
    }
    ...
    if err == nil {
       return resp, nil
    }
    
    // Retry
}
func readWriteLoop(checker *libuv.Check) {
    for {
       task := t.exec.poll()
       if task == nil {
          return
       }
       taskData := (*taskData)(task.Userdata())
       var taskId taskId
       if taskData != nil {
          taskId = taskData.taskId
       } else {
          taskId = notSet
       }
       switch taskId {
       case write:
          t.exec.push(writeTask)
       case read:
          readResponse()
          t.exec.push(bodyForEachTask)
          taskData.resc <- responseAndError{res: resp, err: err}
       case readDone:
          // respBody has been read completely
       case notset:
       }
    }
}

@spongehah spongehah marked this pull request as draft July 30, 2024 08:57
@spongehah spongehah force-pushed the x/http-get branch 3 times, most recently from 22d0b9f to b47deae Compare August 1, 2024 10:12
@spongehah spongehah changed the title WIP(x/http/get): net/http.Get() method built using hyper and libuv WIP(x/http/get): net/http.Get() Aug 2, 2024
@spongehah spongehah force-pushed the x/http-get branch 2 times, most recently from 5b13720 to c4060b4 Compare August 6, 2024 01:57
x/http/transport.go Outdated Show resolved Hide resolved
@spongehah spongehah force-pushed the x/http-get branch 4 times, most recently from 20d9618 to 36db26b Compare August 12, 2024 03:22
@spongehah spongehah changed the title WIP(x/http/get): net/http.Get() WIP(x/http/client): net/http/client Aug 18, 2024
@spongehah spongehah force-pushed the x/http-get branch 2 times, most recently from 9cf4537 to 16e4177 Compare September 12, 2024 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants