Isolated BuildClient from GetRunner
Now there is a single place where new clients are built
This commit is contained in:
parent
96e47f0373
commit
1f3cbe8bde
31
instance.go
31
instance.go
@ -26,23 +26,27 @@ func DoTries(o *RunningInstance, req *http.Request) (*http.Response, error) {
|
|||||||
return resp, err
|
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) {
|
func GetRunner(endpoint string) (RunningInstance, bool) {
|
||||||
ri_mutex.Lock()
|
ri_mutex.Lock()
|
||||||
o, exists := runninginstances[endpoint]
|
o, exists := runninginstances[endpoint]
|
||||||
if exists == false {
|
if exists == false {
|
||||||
o := RunningInstance{}
|
o := RunningInstance{}
|
||||||
|
o.client = BuildClient(endpoint)
|
||||||
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.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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
20
retrieve.go
20
retrieve.go
@ -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
|
||||||
}
|
}
|
||||||
|
14
stream.go
14
stream.go
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user