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

Resty client global variable #737

Closed
billypap1 opened this issue Oct 21, 2023 · 2 comments
Closed

Resty client global variable #737

billypap1 opened this issue Oct 21, 2023 · 2 comments
Assignees
Labels

Comments

@billypap1
Copy link

I use resty in my Golang app to call external APIs.
Is it better to have it as a global variable or create it each time I need to call an external API?

My code bellow:

var RestyClient *resty.Client

func RestyClientSetup() {

	client := resty.New()
	client.SetTimeout(5 * time.Second)
	RestyClient = client
}

Will there be any issue with concurrent external API calls?

@ahuigo
Copy link

ahuigo commented Oct 24, 2023

Global variables are not a good design and are prone to abuse.
Imagine a scenario :

  1. moduleA wants to set SetTimeout(5 * time. Second)
  2. moduleB wants to SetTimeout(500 * time. Second)
  3. moduleC wants to SkipSSL(true)
  4. moduleD wants to SkipSSL(false)

This can easily create conflicts, and very difficult to manage and troubleshoot bugs.

If you want to use a global variable, you can define this global variable within your app, limited to your own app

@jeevatkm
Copy link
Member

jeevatkm commented Mar 2, 2024

@ahuigo Thanks for responding to a comment.

@billypap1 It is recommended to create a resty client per destination Host with respective settings for the usage.

client1 := resty.New().SetBaseURL("http://localohost1")
....

client2 := resty.New().SetBaseURL("http://localohost2")
...

@jeevatkm jeevatkm closed this as completed Mar 2, 2024
@jeevatkm jeevatkm self-assigned this Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants