Skip to content

Document how to have a separate idle and read timeout #87

Closed

Description

@nhooyr
func read(ctx context.Context, c *websocket.Conn) ([]byte, error) {
	ctx, cancel := context.WithTimeout(ctx, time.Minute*30)
	defer cancel()

	_, r, err := c.Reader(ctx)
	if err != nil {
		return nil, err
	}

	// One minute max to read the received message.
	time.AfterFunc(time.Minute, cancel)

	return ioutil.ReadAll(r)
}

That code should be good in an example under Reader.

Related #86

Activity

  1. nhooyr commented on Jun 3, 2019

    @nhooyr
    ContributorAuthor

    If enough people need this, I can add a method to the conn c.SetReadTimeout that controls the maximum time spent reading each message. Then the context passed in would effectively act as the idle timeout.

  2. added 3 commits that reference this issue on Jun 5, 2019
    4640f57
    29d6b09
    22638e0
  3. mikestead commented on Sep 13, 2019

    @mikestead

    @nhooyr thanks for all the work on this library. Add me as a vote for this option

    Specifically it currently looks like I'd have to unwrap the logic encapsulated in wsjson.Read if I wanted to have that functionality with this option. Let me know if not.

  4. nhooyr commented on Sep 13, 2019

    @nhooyr
    ContributorAuthor

    @nhooyr thanks for all the work on this library. Add me as a vote for this option

    Thanks :)

    Specifically it currently looks like I'd have to unwrap the logic encapsulated in wsjson.Read if I wanted to have that functionality with this option. Let me know if not.

    Yes, unfortunately you would need to unwrap the logic in wsjson. For now, I would recommend going that route as no one else has requested this option.

    Is there a reason you can't just bound the entire wsjson read to something like a few minutes? As long as you have a read limit, things should be fine.

  5. mikestead commented on Sep 13, 2019

    @mikestead

    No problem.

    It was with the aim to have checks over the two different steps. A general longer term read wait timeout and a specific timeout once we actually have something to read.

  6. nhooyr commented on Nov 26, 2020

    @nhooyr
    ContributorAuthor

    Looks like this was lost somehow, will need to investigate why it isn't in master anymore.

  7. reopened this on Nov 26, 2020
  8. added a commit that references this issue on Jan 9, 2021
    3b20a49
  9. added this to the v1.8.7 milestone on Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions