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

View File

@ -70,25 +70,6 @@ type PostJson struct {
instance string 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) { func check_post(uri string) (PostJson, error) {
var postjson PostJson var postjson PostJson
for _, banned := range settings.Banned { for _, banned := range settings.Banned {
@ -215,7 +196,6 @@ func check_user(uri string) (UserJson, error) {
err = json.NewDecoder(resp.Body).Decode(&userjson) err = json.NewDecoder(resp.Body).Decode(&userjson)
if err != nil { if err != nil {
// Going forward, this might need to be double-checked, but for now just die // Going forward, this might need to be double-checked, but for now just die
// log.Fatal("Retrieval error 2: ", err)
tries = tries + 1 tries = tries + 1
return userjson, err return userjson, err
} }

View File

@ -6,21 +6,11 @@ import (
"net/http" "net/http"
"strings" "strings"
"time" "time"
"net" // "net"
) )
func StreamMastodon(endpoint string, o *RunningInstance) { func StreamMastodon(endpoint string, o *RunningInstance) {
tr := &http.Transport{ stream_client := BuildClient(endpoint)
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}
var oauthData OAuth var oauthData OAuth
var retry bool var retry bool