reducing potential keep-alive connections to 1 per hostname (not per IP)

This commit is contained in:
farhan 2021-01-02 07:00:21 +00:00
parent 7973cc9600
commit d02ae808a1
5 changed files with 15 additions and 5 deletions

View File

@ -20,9 +20,11 @@ func GetRunner(endpoint string) RunningInstance {
if exists == false { if exists == false {
o := RunningInstance{} o := RunningInstance{}
//tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second}
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{Transport: tr}
//o.client = http.Client{}
o.Status = KEEPALIVE o.Status = KEEPALIVE
runninginstances[endpoint] = o runninginstances[endpoint] = o
} }

View File

@ -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) { 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", "redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "refresh_token", "grant_type": "refresh_token",
"scope": "read write follow push", "scope": "read write follow push",

View File

@ -7,6 +7,7 @@ import (
"html" "html"
"io/ioutil" "io/ioutil"
"log" "log"
"io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -112,6 +113,8 @@ func check_post(uri string) (PostJson, error) {
if err != nil { if err != nil {
return postjson, errors.New("Read error on " + uri) return postjson, errors.New("Read error on " + uri)
} }
resp.Body.Close()
err = json.Unmarshal(body, &postjson) err = json.Unmarshal(body, &postjson)
if err != nil { if err != nil {
return postjson, err return postjson, err
@ -188,6 +191,10 @@ func check_user(uri string) (UserJson, error) {
return userjson, err 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) _, 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 { if err != nil {
// log.Print("INSERT accounts error: ", err) // log.Print("INSERT accounts error: ", err)

View File

@ -10,10 +10,8 @@ import (
) )
func StreamMastodon(endpoint string, o *RunningInstance) { 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 oauthData OAuth
var retry bool 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 { if err != nil {
log.Print("Unable to stream "+api_timeline+": ", err) log.Print("Unable to stream "+api_timeline+": ", err)
return return

3
web.go
View File

@ -107,6 +107,9 @@ func inboxHandler() http.HandlerFunc {
var findtype RelayBase var findtype RelayBase
err = json.Unmarshal(body, &findtype) err = json.Unmarshal(body, &findtype)
if err != nil {
log.Fatal("Unable to unmarshal here, exiting...")
}
switch findtype.Type { switch findtype.Type {
case "Create": case "Create":