From e47d56e57bb45240dbcf36c8909b5572f198f33a Mon Sep 17 00:00:00 2001 From: Naoki Kosaka Date: Sun, 16 Dec 2018 23:02:30 +0900 Subject: [PATCH] Add RemoteActor Caching Feature. (#7) * Enable ActorCache. * Bump version 0.2.0rc2. --- ActivityPub/activity.go | 2 +- ActivityPub/models.go | 15 ++++++++++++++- cli/cli.go | 2 +- decode.go | 2 +- main.go | 5 +++++ 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ActivityPub/activity.go b/ActivityPub/activity.go index 84414b3..f2fa4a6 100644 --- a/ActivityPub/activity.go +++ b/ActivityPub/activity.go @@ -16,7 +16,7 @@ import ( ) // 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 { hash := sha256.New() diff --git a/ActivityPub/models.go b/ActivityPub/models.go index 1d0641a..06fdf33 100644 --- a/ActivityPub/models.go +++ b/ActivityPub/models.go @@ -7,7 +7,9 @@ import ( "io/ioutil" "net/http" "net/url" + "time" + "github.com/patrickmn/go-cache" "github.com/satori/go.uuid" "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. -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.Header.Set("Accept", "application/activity+json, application/ld+json") req.Header.Set("User-Agent", UaString) @@ -66,6 +78,7 @@ func (actor *Actor) RetrieveRemoteActor(url string) error { if err != nil { return err } + cache.Set(url, data, 5*time.Minute) return nil } diff --git a/cli/cli.go b/cli/cli.go index 6fad905..6fcaecb 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -63,7 +63,7 @@ func main() { app := cli.NewApp() app.Name = "Activity Relay Extarnal CLI" app.Usage = "Configure Activity-Relay" - app.Version = "0.2.0rc1" + app.Version = "0.2.0rc2" app.Commands = []cli.Command{ { Name: "domain", diff --git a/decode.go b/decode.go index 3ba45d3..fc7a108 100644 --- a/decode.go +++ b/decode.go @@ -26,7 +26,7 @@ func decodeActivity(request *http.Request) (*activitypub.Activity, *activitypub. } KeyID := verifier.KeyId() remoteActor := new(activitypub.Actor) - err = remoteActor.RetrieveRemoteActor(KeyID) + err = remoteActor.RetrieveRemoteActor(KeyID, actorCache) if err != nil { return nil, nil, nil, err } diff --git a/main.go b/main.go index 95a0c9a..1c7cee8 100644 --- a/main.go +++ b/main.go @@ -6,10 +6,12 @@ import ( "net/http" "net/url" "os" + "time" "github.com/RichardKnop/machinery/v1" "github.com/RichardKnop/machinery/v1/config" "github.com/go-redis/redis" + "github.com/patrickmn/go-cache" "github.com/yukimochi/Activity-Relay/ActivityPub" "github.com/yukimochi/Activity-Relay/KeyLoader" "github.com/yukimochi/Activity-Relay/State" @@ -24,6 +26,7 @@ var WebfingerResource activitypub.WebfingerResource var hostURL *url.URL var hostPrivatekey *rsa.PrivateKey var redisClient *redis.Client +var actorCache *cache.Cache var machineryServer *machinery.Server var relayState state.RelayState @@ -58,6 +61,8 @@ func main() { Addr: redisURL, }) + actorCache = cache.New(5*time.Minute, 10*time.Minute) + var macConfig = &config.Config{ Broker: "redis://" + redisURL, DefaultQueue: "relay",