Skip to content

Commit

Permalink
Use shared http.Client instance with cookie jar in appservices
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 6, 2021
1 parent e8b0bf1 commit 974dd86
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
31 changes: 14 additions & 17 deletions appservice/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
"html/template"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"golang.org/x/net/publicsuffix"
"gopkg.in/yaml.v2"

"maunium.net/go/maulogger/v2"
Expand All @@ -33,10 +36,12 @@ var EventChannelSize = 64

// Create a blank appservice instance.
func Create() *AppService {
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
return &AppService{
LogConfig: CreateLogConfig(),
clients: make(map[id.UserID]*mautrix.Client),
intents: make(map[id.UserID]*IntentAPI),
HTTPClient: &http.Client{Timeout: 180 * time.Second, Jar: jar},
StateStore: NewBasicStateStore(),
Router: mux.NewRouter(),
UserAgent: mautrix.DefaultUserAgent,
Expand Down Expand Up @@ -93,11 +98,12 @@ type AppService struct {
QueryHandler QueryHandler `yaml:"-"`
StateStore StateStore `yaml:"-"`

Router *mux.Router `yaml:"-"`
UserAgent string `yaml:"-"`
server *http.Server
botClient *mautrix.Client
botIntent *IntentAPI
Router *mux.Router `yaml:"-"`
UserAgent string `yaml:"-"`
server *http.Server
HTTPClient *http.Client
botClient *mautrix.Client
botIntent *IntentAPI

clients map[id.UserID]*mautrix.Client
clientsLock sync.RWMutex
Expand Down Expand Up @@ -180,8 +186,7 @@ func (as *AppService) Intent(userID id.UserID) *IntentAPI {

func (as *AppService) BotIntent() *IntentAPI {
if as.botIntent == nil {
as.botIntent = as.NewIntentAPI(as.Registration.SenderLocalpart)
as.botIntent.Logger = as.Log.Sub(string(as.botIntent.UserID))
as.botIntent = as.makeIntent(as.BotMXID())
}
return as.botIntent
}
Expand All @@ -205,6 +210,7 @@ func (as *AppService) makeClient(userID id.UserID) *mautrix.Client {
client.Store = nil
client.AppServiceUserID = userID
client.Logger = as.Log.Sub(string(userID))
client.Client = as.HTTPClient
as.clients[userID] = client
return client
}
Expand All @@ -221,17 +227,8 @@ func (as *AppService) Client(userID id.UserID) *mautrix.Client {

func (as *AppService) BotClient() *mautrix.Client {
if as.botClient == nil {
var err error
as.botClient, err = mautrix.NewClient(as.HomeserverURL, as.BotMXID(), as.Registration.AppToken)
if err != nil {
as.Log.Fatalln("Failed to create gomatrix instance:", err)
return nil
}
as.botClient.UserAgent = as.UserAgent
as.botClient.Syncer = nil
as.botClient.Store = nil
as.botClient = as.makeClient(as.BotMXID())
as.botClient.Logger = as.Log.Sub("Bot")
as.botClient.AppServiceUserID = as.BotMXID()
}
return as.botClient
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/tidwall/gjson v1.6.8
github.com/tidwall/sjson v1.1.5
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
gopkg.in/yaml.v2 v2.3.0
maunium.net/go/maulogger/v2 v2.2.4
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ golang.org/x/net v0.0.0-20201026091529-146b70c837a4 h1:awiuzyrRjJDb+OXi9ceHO3SDx
golang.org/x/net v0.0.0-20201026091529-146b70c837a4/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d h1:1aflnvSoWWLI2k/dMUAl5lvU1YO4Mb4hz0gh+1rjcxU=
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -73,6 +75,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mautrix

const Version = "v0.9.3"
const Version = "v0.9.4"

var DefaultUserAgent = "mautrix-go/" + Version

0 comments on commit 974dd86

Please sign in to comment.