From 1f3cbe8bde6be09de2d9de523b8481bf287a2768 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sat, 30 Jan 2021 05:54:03 +0000 Subject: [PATCH] Isolated BuildClient from GetRunner Now there is a single place where new clients are built --- instance.go | 31 ++++++++++++++++++------------- retrieve.go | 20 -------------------- stream.go | 14 ++------------ 3 files changed, 20 insertions(+), 45 deletions(-) diff --git a/instance.go b/instance.go index b762fc6..3f2f5dc 100644 --- a/instance.go +++ b/instance.go @@ -26,23 +26,27 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) { return resp, err } +func BuildClient(endpoint string) http.Client { + tr := &http.Transport{ + MaxIdleConns: 10, + IdleConnTimeout: 3600 * time.Second, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, + } + 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{} - - tr := &http.Transport{ - MaxIdleConns: 10, - IdleConnTimeout: 7200 * time.Second, - DialContext: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - DualStack: true, - }).DialContext, - } - - o.client = http.Client{Transport: tr} + 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() } } diff --git a/retrieve.go b/retrieve.go index 07de617..485f958 100644 --- a/retrieve.go +++ b/retrieve.go @@ -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 } diff --git a/stream.go b/stream.go index 241842f..f2c8753 100644 --- a/stream.go +++ b/stream.go @@ -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