From d02ae808a1e050f5eb126309a94839fbb4e8455d Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sat, 2 Jan 2021 07:00:21 +0000 Subject: [PATCH] reducing potential keep-alive connections to 1 per hostname (not per IP) --- instance.go | 2 ++ oauth.go | 2 +- retrieve.go | 7 +++++++ stream.go | 6 ++---- web.go | 3 +++ 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/instance.go b/instance.go index 97b607e..4f2928e 100644 --- a/instance.go +++ b/instance.go @@ -20,9 +20,11 @@ func GetRunner(endpoint string) RunningInstance { if exists == false { o := RunningInstance{} + //tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second} tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second} o.client = http.Client{Transport: tr} + //o.client = http.Client{} o.Status = KEEPALIVE runninginstances[endpoint] = o } diff --git a/oauth.go b/oauth.go index b80f148..98599a1 100644 --- a/oauth.go +++ b/oauth.go @@ -178,7 +178,7 @@ func oauth_login(endpoint string, o *RunningInstance, username string, password } func oauth_refresh(endpoint string, client_id string, client_secret string, refresh_token string) (OAuth, error) { - authMap, err := json.Marshal(map[string]string{ + authMap, _ := json.Marshal(map[string]string{ "redirect_uri": "urn:ietf:wg:oauth:2.0:oob", "grant_type": "refresh_token", "scope": "read write follow push", diff --git a/retrieve.go b/retrieve.go index e453de6..e4647fc 100644 --- a/retrieve.go +++ b/retrieve.go @@ -7,6 +7,7 @@ import ( "html" "io/ioutil" "log" +"io" "net/http" "strings" "time" @@ -112,6 +113,8 @@ func check_post(uri string) (PostJson, error) { if err != nil { return postjson, errors.New("Read error on " + uri) } + resp.Body.Close() + err = json.Unmarshal(body, &postjson) if err != nil { return postjson, err @@ -188,6 +191,10 @@ func check_user(uri string) (UserJson, error) { return userjson, err } +io.Copy(ioutil.Discard, resp.Body) + + resp.Body.Close() + _, err = pool.Exec(context.Background(), "INSERT INTO accounts (id, actor_type, inbox, outbox, followers, following, url, preferredUsername, name, summary, icon, image, publicKey, instance) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", userjson.ID, userjson.Type, userjson.Inbox, userjson.Outbox, userjson.Followers, userjson.Following, userjson.Url, userjson.PreferredUsername, userjson.Name, userjson.Summary, userjson.Icon.Url, userjson.Image.Url, userjson.PublicKey.PublicKeyPem, userjson.instance) if err != nil { // log.Print("INSERT accounts error: ", err) diff --git a/stream.go b/stream.go index dda840f..a34ce02 100644 --- a/stream.go +++ b/stream.go @@ -10,10 +10,8 @@ import ( ) func StreamMastodon(endpoint string, o *RunningInstance) { - http_client := http.Client{} + stream_client := http.Client{} - //var client_id string - //var client_secret string var oauthData OAuth var retry bool @@ -46,7 +44,7 @@ func StreamMastodon(endpoint string, o *RunningInstance) { } } - resp, err := http_client.Do(req) + resp, err := stream_client.Do(req) if err != nil { log.Print("Unable to stream "+api_timeline+": ", err) return diff --git a/web.go b/web.go index 935624c..cd20041 100644 --- a/web.go +++ b/web.go @@ -107,6 +107,9 @@ func inboxHandler() http.HandlerFunc { var findtype RelayBase err = json.Unmarshal(body, &findtype) + if err != nil { + log.Fatal("Unable to unmarshal here, exiting...") + } switch findtype.Type { case "Create":