Add RemoteActor Caching Feature. (#7)
* Enable ActorCache. * Bump version 0.2.0rc2.
This commit is contained in:
parent
2241d02fb2
commit
e47d56e57b
@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// UaString : Use for User-Agent
|
// UaString : Use for User-Agent
|
||||||
var UaString = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.2.0rc1; " + os.Getenv("RELAY_DOMAIN") + ")"
|
var UaString = os.Getenv("RELAY_SERVICENAME") + " (golang net/http; Activity-Relay v0.2.0rc2; " + os.Getenv("RELAY_DOMAIN") + ")"
|
||||||
|
|
||||||
func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKey *rsa.PrivateKey) error {
|
func appendSignature(request *http.Request, body *[]byte, KeyID string, publicKey *rsa.PrivateKey) error {
|
||||||
hash := sha256.New()
|
hash := sha256.New()
|
||||||
|
@ -7,7 +7,9 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/satori/go.uuid"
|
"github.com/satori/go.uuid"
|
||||||
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
||||||
)
|
)
|
||||||
@ -50,7 +52,17 @@ func (actor *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetrieveRemoteActor : Retrieve Actor from remote instance.
|
// RetrieveRemoteActor : Retrieve Actor from remote instance.
|
||||||
func (actor *Actor) RetrieveRemoteActor(url string) error {
|
func (actor *Actor) RetrieveRemoteActor(url string, cache *cache.Cache) error {
|
||||||
|
var err error
|
||||||
|
cacheData, found := cache.Get(url)
|
||||||
|
if found {
|
||||||
|
err = json.Unmarshal(cacheData.([]byte), &actor)
|
||||||
|
if err != nil {
|
||||||
|
cache.Delete(url)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
req, _ := http.NewRequest("GET", url, nil)
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
req.Header.Set("Accept", "application/activity+json, application/ld+json")
|
req.Header.Set("Accept", "application/activity+json, application/ld+json")
|
||||||
req.Header.Set("User-Agent", UaString)
|
req.Header.Set("User-Agent", UaString)
|
||||||
@ -66,6 +78,7 @@ func (actor *Actor) RetrieveRemoteActor(url string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cache.Set(url, data, 5*time.Minute)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ func main() {
|
|||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "Activity Relay Extarnal CLI"
|
app.Name = "Activity Relay Extarnal CLI"
|
||||||
app.Usage = "Configure Activity-Relay"
|
app.Usage = "Configure Activity-Relay"
|
||||||
app.Version = "0.2.0rc1"
|
app.Version = "0.2.0rc2"
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
{
|
{
|
||||||
Name: "domain",
|
Name: "domain",
|
||||||
|
@ -26,7 +26,7 @@ func decodeActivity(request *http.Request) (*activitypub.Activity, *activitypub.
|
|||||||
}
|
}
|
||||||
KeyID := verifier.KeyId()
|
KeyID := verifier.KeyId()
|
||||||
remoteActor := new(activitypub.Actor)
|
remoteActor := new(activitypub.Actor)
|
||||||
err = remoteActor.RetrieveRemoteActor(KeyID)
|
err = remoteActor.RetrieveRemoteActor(KeyID, actorCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
5
main.go
5
main.go
@ -6,10 +6,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/RichardKnop/machinery/v1"
|
"github.com/RichardKnop/machinery/v1"
|
||||||
"github.com/RichardKnop/machinery/v1/config"
|
"github.com/RichardKnop/machinery/v1/config"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/yukimochi/Activity-Relay/ActivityPub"
|
"github.com/yukimochi/Activity-Relay/ActivityPub"
|
||||||
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
||||||
"github.com/yukimochi/Activity-Relay/State"
|
"github.com/yukimochi/Activity-Relay/State"
|
||||||
@ -24,6 +26,7 @@ var WebfingerResource activitypub.WebfingerResource
|
|||||||
var hostURL *url.URL
|
var hostURL *url.URL
|
||||||
var hostPrivatekey *rsa.PrivateKey
|
var hostPrivatekey *rsa.PrivateKey
|
||||||
var redisClient *redis.Client
|
var redisClient *redis.Client
|
||||||
|
var actorCache *cache.Cache
|
||||||
var machineryServer *machinery.Server
|
var machineryServer *machinery.Server
|
||||||
var relayState state.RelayState
|
var relayState state.RelayState
|
||||||
|
|
||||||
@ -58,6 +61,8 @@ func main() {
|
|||||||
Addr: redisURL,
|
Addr: redisURL,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
actorCache = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
|
|
||||||
var macConfig = &config.Config{
|
var macConfig = &config.Config{
|
||||||
Broker: "redis://" + redisURL,
|
Broker: "redis://" + redisURL,
|
||||||
DefaultQueue: "relay",
|
DefaultQueue: "relay",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user