go-vcloud-director icon indicating copy to clipboard operation
go-vcloud-director copied to clipboard

use context.Context for timing out operations

Open jakecoffman opened this issue 4 years ago • 4 comments

Currently there's no way to set a timeout for an operation using this library. Generally this is done in Go using the context package: https://blog.golang.org/context

In a fork I've begun adding context everywhere that needs it. If you are interested in this change upstream let me know and I can put a PR up when it's done, otherwise I'll just use the fork since I need the timeouts.

jakecoffman avatar Mar 19 '21 19:03 jakecoffman

Hello @jakecoffman, Thanks for looking into it. I wonder what is your approach for cancellation? Are you cancelling tasks or something more?

Would be interesting to see your fork/branch

Didainius avatar Mar 22 '21 08:03 Didainius

Here's an in-progress branch: https://github.com/vmware/go-vcloud-director/compare/master...jakecoffman:context

I need to cancel the underlying HTTP request (by using http.NewRequestWithContext), not the command or task itself. That way when my server that makes calls to vcloud needs to go down, if it happened to just launch a long running operation it can cancel and clean things up before it's killed.

Unfortunately by switching to http.NewRequestWithContext we have to pass a Context down from every function that calls it, which is nearly every function in this project.

jakecoffman avatar Mar 22 '21 12:03 jakecoffman

It will be useful for using with tracing.

	vcdclient.Client.Http.Transport = &ochttp.Transport{
		Base:        vcdclient.Client.Http.Transport,
	}

Now, without context, i have separated traces.

lelvisl avatar May 08 '21 19:05 lelvisl

I have a fork here with the context applied if anyone would like to use it before it is available in this repo: https://github.com/wwt/go-vcloud-director

jakecoffman avatar May 13 '21 22:05 jakecoffman