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

martian: cancel request context when connection is closed #800

Open
mmatczuk opened this issue May 7, 2024 · 2 comments
Open

martian: cancel request context when connection is closed #800

mmatczuk opened this issue May 7, 2024 · 2 comments
Milestone

Comments

@mmatczuk
Copy link
Contributor

mmatczuk commented May 7, 2024

For Martian TCP listener implementation support request context cancelation when downstream connection dies.

@mmatczuk mmatczuk added this to the v1.3 milestone May 7, 2024
@Choraden
Copy link
Contributor

Determining when a connection dies is not a simple task.

For example http.Server starts background read after it consumed all the request body.
If the read fails the context will be cancelled.

func (cr *connReader) backgroundRead() {
	n, err := cr.conn.rwc.Read(cr.byteBuf[:])
	cr.lock()
	if n == 1 {
		cr.hasByte = true
	}
	if ne, ok := err.(net.Error); ok && cr.aborted && ne.Timeout() {
		// Ignore this error. It's the expected error from
		// another goroutine calling abortPendingRead.
	} else if err != nil {
		cr.handleReadError(err)
	}
	cr.aborted = false
	cr.inRead = false
	cr.unlock()
	cr.cond.Broadcast()
}

// requestBodyRemains reports whether future calls to Read
// on rc might yield more data.
func requestBodyRemains(rc io.ReadCloser) bool {
	if rc == NoBody {
		return false
	}
	switch v := rc.(type) {
	case *expectContinueReader:
		return requestBodyRemains(v.readCloser)
	case *body:
		return v.bodyRemains()
	default:
		panic("unexpected type " + fmt.Sprintf("%T", rc))
	}
}

@Choraden Choraden self-assigned this May 21, 2024
@Choraden
Copy link
Contributor

The way it could be implemented is very similar to the http.Server approach. We need:

  1. http request body wrapper that would notify about the body being consumed in a round tripper
  2. conn wrapper that would have the functionality of a backgroundRead()

@Choraden Choraden removed their assignment May 21, 2024
@Choraden Choraden modified the milestones: v1.3, v1.x May 21, 2024
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

No branches or pull requests

2 participants