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

Client: Add Read-Write Mutex to struct to make all the client properties thread-safe #812

Closed
jeevatkm opened this issue Aug 4, 2024 · 4 comments · Fixed by #827
Closed
Assignees
Labels
v3 For resty v3

Comments

@jeevatkm
Copy link
Member

jeevatkm commented Aug 4, 2024

All the exported fields will become getter methods,

e.g., client.BaseURL => client.BaseURL()

@jeevatkm jeevatkm added the v3 For resty v3 label Aug 4, 2024
@jeevatkm jeevatkm added this to the v3.0.0 Milestone milestone Aug 4, 2024
@tttturtle-russ
Copy link

In this case, all the setter method should also be protected by the rw mutex right?
I write a simple demo as follows:

// Cookies method gets all cookies in the client instance.
// It uses a Read-Write mutex to make itself thread-safe.
func (c *Client) Cookies() []*http.Cookie {
	c.mutex.RLock()
	defer c.mutex.RUnlock()
	return c.cookies
}

// SetCookies method sets an array of cookies in the client instance.
// These cookies will be added to all the request raised from this client instance.
//
//	cookies := []*http.Cookie{
//		&http.Cookie{
//			Name:"go-resty-1",
//			Value:"This is cookie 1 value",
//		},
//		&http.Cookie{
//			Name:"go-resty-2",
//			Value:"This is cookie 2 value",
//		},
//	}
//
//	// Setting a cookies into resty
//	client.SetCookies(cookies)
func (c *Client) SetCookies(cs []*http.Cookie) *Client {
	c.mutex.Lock()
	c.cookies = append(c.cookies, cs...)
	c.mutex.Unlock()
	return c
}

@jeevatkm
Copy link
Member Author

@tttturtle-russ, your understanding is correct. This is for Resty v3.

@tttturtle-russ
Copy link

tttturtle-russ commented Aug 18, 2024

@jeevatkm I can help with this. Actually, I add the lock to some methods, such as SetQueryParams. However, it ends up failing the retry test. I tested 5 times, and it only succeeded once and passed all the tests. Maybe it's because the lock introduced extra time consumption.

@jeevatkm
Copy link
Member Author

@tttturtle-russ You're welcome to create a PR for v3. With Mutex, some of the tests may need to be rewritten or updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 For resty v3
2 participants