[wip] User cache support.#13
Conversation
riking
left a comment
There was a problem hiding this comment.
Looks mostly good! Some perf problems, though.
|
|
||
| // $1 = slack.UserID | ||
| // $2 = data (json encoded) | ||
| sqlAddEntry = `INSERT INTO module_user_cache (user_id,data) VALUES ($1, $2)` |
There was a problem hiding this comment.
ON CONFLICT DO UPDATE SET data = EXCLUDED.data
😃
Avoids the need for a transaction / checking first.
| if err != nil { | ||
| return errors.Wrap(err, "error in user cache: unmarshal user object") | ||
| } | ||
| rtmClient := mod.team.GetRTMClient().(*rtm.Client) |
| for stmt.Next() { | ||
| var id string | ||
| var data string | ||
| var user slack.User |
There was a problem hiding this comment.
pointer to slack.User, remove & on line 74. The object's going to live on the heap anyways.
| type userCacheAPI interface { | ||
| marvin.Module | ||
|
|
||
| GetEntry(userid slack.UserID) (slack.User, error) |
There was a problem hiding this comment.
GetEntry is never used and can be dropped from the interface.
| "github.com/riking/marvin/slack" | ||
| ) | ||
|
|
||
| type API interface { |
There was a problem hiding this comment.
This API type can be dropped, or at least commented with // interface duplicated in rtm package
|
|
||
| import ( | ||
| "sync" | ||
|
|
There was a problem hiding this comment.
Could have sworn goimports was run on this file and this was the result... I'll run it again and see if it produces the same result.
There was a problem hiding this comment.
Oh, remove the newline between the two import blocks.
| return err | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) UpdateEntries(userobjects []*slack.User) error { |
There was a problem hiding this comment.
consider forwarding UpdateEntry to this function instead - you can prepare the statement once and do many inserts.
There was a problem hiding this comment.
I was also considering using transactions (.Begin and .Commit).
There was a problem hiding this comment.
Oh I see what you mean.
| return stmt.Err() | ||
| } | ||
|
|
||
| func (mod *UserCacheModule) UpdateEntry(userobject slack.User) error { |
There was a problem hiding this comment.
Should take a pointer - slack.User is a large object.
| return errors.Wrap(err, "error in user cache: unmarshal user object") | ||
| } | ||
| rtmClient := mod.team.GetRTMClient().(*rtm.Client) | ||
| rtmClient.ReplaceUserObject(&user) |
There was a problem hiding this comment.
This method takes a lock - consider batching by hundreds?
There was a problem hiding this comment.
I was going to do an big array and loading it that way, but I didn't want to consume so much memory. Perhaps maybe I can make this so every 200 entries (like the Slack API) it'll call ReplaceManyUserObjects with an array of 200, then empty and continue.
| c.ReplaceManyUserObjects(response.Members, true) | ||
|
|
||
| for response.PageInfo.NextCursor != "" { | ||
| c.ReplaceManyUserObjects(response.Members) |
There was a problem hiding this comment.
I had to make this change because otherwise the ReplaceManyUserObjects would get called again for the same group of objects retrieved from the last successful query.
There was a problem hiding this comment.
And with the new changes, it was also not retrieving all the users.
| var id string | ||
| var data string | ||
| var user slack.User | ||
| var user *slack.User = &slack.User{} |
There was a problem hiding this comment.
Leave it at nil, json.Unmarshal(&user) will allocate for you.
| err = json.Unmarshal([]byte(data), user) | ||
| if err != nil { | ||
| return errors.Wrap(err, "error in user cache: unmarshal user object") | ||
| return err |
There was a problem hiding this comment.
Probably safer to skip erroring rows - we'll re-fetch the data later.
| arr = append(arr, user) | ||
| if len(arr) >= 199 { | ||
| go rtmClient.ReplaceManyUserObjects(arr, false) | ||
| arr = make([]*slack.User, 200) |
There was a problem hiding this comment.
The function isn't blocking boot, so just call ReplaceMany directly and use arr = arr[:0] instead.
|
|
||
| func (mod *UserCacheModule) Enable(team marvin.Team) { | ||
| go func() { | ||
| fmt.Printf("Loading cache entries....\n") |
There was a problem hiding this comment.
user cache, until we're caching other objects as well
| func (mod *UserCacheModule) UpdateEntry(userobject *slack.User) error { | ||
| var objarray = make([]*slack.User, 1) | ||
| objarray[0] = userobject | ||
| return mod.UpdateEntries(objarray) |
There was a problem hiding this comment.
mod.UpdateEntries([]*slack.User{userobject})
There was a problem hiding this comment.
I was trying to figure out what the easier way was...ugh.
| if err != nil { | ||
| return err | ||
| entrydata, err := json.Marshal(obj) | ||
| if err == nil { |
There was a problem hiding this comment.
if err != nil { continue } on json marshalling
riking
left a comment
There was a problem hiding this comment.
Make sure to change the callers of rtm.Client.fillUsersList() - it doesn't need to be called on boot because we have the cache.
| moduleCacheApi := c.team.GetModule("usercache") | ||
| if moduleCacheApi != nil && updateCache { | ||
| cacheApi = moduleCacheApi.(userCacheAPI) | ||
| cacheApi.UpdateEntries(objs) |
There was a problem hiding this comment.
Move this outside the MetadataLock.
| moduleCacheApi := c.team.GetModule("usercache") | ||
| if moduleCacheApi != nil { | ||
| cacheApi = moduleCacheApi.(userCacheAPI) | ||
| cacheApi.UpdateEntry(obj) |
|
If you would like, I can squash these commits (like the old bukkit days). |
|
And yes, I've been testing my code. |
| delaystr, _, _ := mod.team.ModuleConfig(Identifier).GetIsDefault("delay") | ||
| timeint, _ := strconv.ParseInt(timestr, 10, 64) | ||
| var timeres = time.Unix(timeint, 0) | ||
| delayres, err := time.ParseDuration(delaystr) |
There was a problem hiding this comment.
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.
|
Is this still an problem a year later? |
Added preliminary support for user caching. When the module is loaded and enabled, it loads the cached database entries and inserts them directly into the RTM object
client.users. In addition, any calls toclient.ReplaceManyUserObjectsandclient.ReplaceUserObjectwill have the cache updated.In addition to these changes, I added the delay from the
Retry-Afterhttp header returned if the call was ratelimited.Questions:
Retry-Afterheader a good idea? It does fill the user cache (all 2,600 users) successfully.Overall, do you think this is a good implementation?