This repository was archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
[wip] User cache support. #13
Open
SuperSpyTX
wants to merge
6
commits into
riking:master
Choose a base branch
from
SuperSpyTX:patch-user-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
944699c
[wip] User cache support.
dcb4b40
Actually make sure it will continue to fill users if the user cache i…
d5ef49a
Fix sql migration query formatting
b098afa
Fixed performance issues identified.
bb2eecc
Made changes as requested
6d02953
Oops.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package usercache | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
|
|
||
| "github.com/riking/marvin/slack" | ||
| "github.com/riking/marvin/slack/rtm" | ||
| ) | ||
|
|
||
| const ( | ||
| sqlMigrate1 = `CREATE TABLE module_user_cache ( | ||
| user_id varchar(15) PRIMARY KEY NOT NULL, | ||
| data text | ||
|
|
||
| UNIQUE(user_id) | ||
| )` | ||
|
|
||
| sqlGetAllEntries = `SELECT * FROM module_user_cache` | ||
|
|
||
| // $1 = slack.UserID | ||
| sqlGetEntry = `SELECT data FROM module_user_cache WHERE user_id = $1` | ||
|
|
||
| // $1 = slack.UserID | ||
| // $2 = data (json encoded) | ||
| sqlUpsertEntry = `INSERT INTO module_user_cache (user_id,data) VALUES ($1, $2) | ||
| ON CONFLICT (user_id) DO UPDATE SET data = EXCLUDED.data` | ||
| ) | ||
|
|
||
| func (mod *UserCacheModule) GetEntry(userid slack.UserID) (slack.User, error) { | ||
| var entry slack.User | ||
|
|
||
| var data string | ||
| stmt, err := mod.team.DB().Prepare(sqlGetEntry) | ||
| if err != nil { | ||
| return entry, nil | ||
| } | ||
| defer stmt.Close() | ||
| row := stmt.QueryRow(userid) | ||
| err = row.Scan(&data) | ||
| if err != nil { | ||
| return entry, nil | ||
| } | ||
| err = json.Unmarshal([]byte(userid), &entry) | ||
| if err != nil { | ||
| return entry, nil | ||
| } | ||
| return entry, nil | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) LoadEntries() error { | ||
| stmt, err := mod.team.DB().Query(sqlGetAllEntries) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| rtmClient := mod.team.GetRTMClient().(*rtm.Client) | ||
|
|
||
| defer stmt.Close() | ||
| var arr = make([]*slack.User, 200) | ||
| for stmt.Next() { | ||
| var id string | ||
| var data string | ||
| var user *slack.User | ||
|
|
||
| err = stmt.Scan(&id, &data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = json.Unmarshal([]byte(data), &user) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| arr = append(arr, user) | ||
| if len(arr) >= 199 { | ||
| rtmClient.ReplaceManyUserObjects(arr, false) | ||
| arr = arr[:0] | ||
| } | ||
| } | ||
| if len(arr) >= 0 { | ||
| rtmClient.ReplaceManyUserObjects(arr, false) | ||
| arr = nil | ||
| } | ||
|
|
||
| return stmt.Err() | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) UpdateEntry(userobject *slack.User) error { | ||
| return mod.UpdateEntries([]*slack.User{userobject}) | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) UpdateEntries(userobjects []*slack.User) error { | ||
| stmt, err := mod.team.DB().Prepare(sqlUpsertEntry) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| defer stmt.Close() | ||
|
|
||
| for _, obj := range userobjects { | ||
| if obj != nil { | ||
| entrydata, err := json.Marshal(obj) | ||
| if err == nil { | ||
| _, err := stmt.Exec(obj.ID, entrydata) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package usercache | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/riking/marvin" | ||
| "github.com/riking/marvin/slack" | ||
| "github.com/riking/marvin/slack/rtm" | ||
| ) | ||
|
|
||
| // interface duplicated in rtm package | ||
| type API interface { | ||
| marvin.Module | ||
|
|
||
| GetEntry(userid slack.UserID) (slack.User, error) | ||
| LoadEntries() error | ||
| UpdateEntry(userobject *slack.User) error | ||
| UpdateEntries(userobjects []*slack.User) error | ||
| } | ||
|
|
||
| var _ API = &UserCacheModule{} | ||
|
|
||
| // --- | ||
| func init() { | ||
| marvin.RegisterModule(NewUserCacheModule) | ||
| } | ||
|
|
||
| const Identifier = "usercache" | ||
|
|
||
| type UserCacheModule struct { | ||
| team marvin.Team | ||
| } | ||
|
|
||
| func NewUserCacheModule(t marvin.Team) marvin.Module { | ||
| mod := &UserCacheModule{ | ||
| team: t, | ||
| } | ||
| return mod | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) Identifier() marvin.ModuleID { | ||
| return Identifier | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) Load(t marvin.Team) { | ||
| t.DB().MustMigrate(Identifier, 1505192548, sqlMigrate1) | ||
| t.DB().SyntaxCheck(sqlGetAllEntries, sqlGetEntry, sqlUpsertEntry) | ||
| t.ModuleConfig(Identifier).Add("last-timestamp", "0") | ||
| t.ModuleConfig(Identifier).Add("delay", (72 * time.Hour).String()) | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) Enable(team marvin.Team) { | ||
| go func() { | ||
| fmt.Printf("Loading user cache entries....\n") | ||
| err := mod.LoadEntries() | ||
| if err != nil { | ||
| fmt.Printf("Error whilst updating entries: %s\n", err.Error()) | ||
| return | ||
| } | ||
|
|
||
| fmt.Printf("Loaded all entries from the user cache.\n") | ||
| go mod.UpdateTask() | ||
| }() | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) Disable(t marvin.Team) { | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) UpdateTask() { | ||
| rtmClient := mod.team.GetRTMClient().(*rtm.Client) | ||
|
|
||
| for { | ||
| timestr, _, _ := mod.team.ModuleConfig(Identifier).GetIsDefault("last-timestamp") | ||
| delaystr, _, _ := mod.team.ModuleConfig(Identifier).GetIsDefault("delay") | ||
| timeint, _ := strconv.ParseInt(timestr, 10, 64) | ||
| var timeres = time.Unix(timeint, 0) | ||
| delayres, err := time.ParseDuration(delaystr) | ||
|
|
||
| if err != nil || timeres.Before(time.Now().Add(-delayres)) { | ||
| fmt.Printf("Repolling user list....\n") | ||
| go rtmClient.FillUsersList() | ||
| err = mod.team.ModuleConfig(Identifier).Set("last-timestamp", strconv.FormatInt(time.Now().Unix(), 10)) | ||
| } | ||
| time.Sleep(1 * time.Hour) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,13 @@ func (c *Client) onChannelJoin(msg slack.RTMRawMessage) { | |
| } | ||
|
|
||
| func (c *Client) ReplaceUserObject(obj *slack.User) { | ||
| var cacheApi userCacheAPI | ||
| moduleCacheApi := c.team.GetModule("usercache") | ||
| if moduleCacheApi != nil { | ||
| cacheApi = moduleCacheApi.(userCacheAPI) | ||
| cacheApi.UpdateEntry(obj) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| c.MetadataLock.Lock() | ||
| defer c.MetadataLock.Unlock() | ||
|
|
||
|
|
@@ -103,7 +110,14 @@ func (c *Client) ReplaceUserObject(obj *slack.User) { | |
| c.Users = append(c.Users, obj) | ||
| } | ||
|
|
||
| func (c *Client) ReplaceManyUserObjects(objs []*slack.User) { | ||
| func (c *Client) ReplaceManyUserObjects(objs []*slack.User, updateCache bool) { | ||
| var cacheApi userCacheAPI | ||
| moduleCacheApi := c.team.GetModule("usercache") | ||
| if moduleCacheApi != nil && updateCache { | ||
| cacheApi = moduleCacheApi.(userCacheAPI) | ||
| cacheApi.UpdateEntries(objs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| c.MetadataLock.Lock() | ||
| defer c.MetadataLock.Unlock() | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should fall back to default if it fails -
const defaultDelay = 72*time.Hour; if err != nil { delayres = defaultDelay }Fail safe, not dangerous - setting a bad duration would result in reloading the entire thing every hour.