Isolated BuildClient from GetRunner

Now there is a single place where new clients are built
This commit is contained in:
farhan 2021-01-30 05:54:03 +00:00
parent 96e47f0373
commit 1f3cbe8bde
3 changed files with 20 additions and 45 deletions

View File

@ -26,23 +26,27 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) {
return resp, err
}
func GetRunner(endpoint string) (RunningInstance, bool) {
ri_mutex.Lock()
o, exists := runninginstances[endpoint]
if exists == false {
o := RunningInstance{}
func BuildClient(endpoint string) http.Client {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 7200 * time.Second,
IdleConnTimeout: 3600 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
}
client := http.Client{Transport: tr}
o.client = http.Client{Transport: tr}
return client
}
func GetRunner(endpoint string) (RunningInstance, bool) {
ri_mutex.Lock()
o, exists := runninginstances[endpoint]
if exists == false {
o := RunningInstance{}
o.client = BuildClient(endpoint)
o.Status = KEEPALIVE
runninginstances[endpoint] = o
}
@ -147,14 +151,15 @@ func CheckInstance(newinstance string, callerEndpoint string) {
if newinstance == callerEndpoint {
return
}
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
m.client = BuildClient(newinstance)
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
}

View File

@ -70,25 +70,6 @@ type PostJson struct {
instance string
}
/*
func GetHTTPSession(endpoint string) RunningInstance {
ri_mutex.Lock()
o, exist := runninginstances[endpoint]
ri_mutex.Unlock()
if exist == false {
o := RunningInstance{}
new_client := http.Client{}
o.client = new_client
o.Status = KEEPALIVE
ri_mutex.Lock()
runninginstances[endpoint] = o
ri_mutex.Unlock()
}
return o
}
*/
func check_post(uri string) (PostJson, error) {
var postjson PostJson
for _, banned := range settings.Banned {
@ -215,7 +196,6 @@ func check_user(uri string) (UserJson, error) {
err = json.NewDecoder(resp.Body).Decode(&userjson)
if err != nil {
// Going forward, this might need to be double-checked, but for now just die
// log.Fatal("Retrieval error 2: ", err)
tries = tries + 1
return userjson, err
}

View File

@ -6,21 +6,11 @@ import (
"net/http"
"strings"
"time"
"net"
// "net"
)
func StreamMastodon(endpoint string, o *RunningInstance) {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 7200 * time.Second,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
}
stream_client := http.Client{Transport: tr}
stream_client := BuildClient(endpoint)
var oauthData OAuth
var retry bool