Skip to content

Commit

Permalink
sundry
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 27, 2024
1 parent 6c37d8a commit 5bb6174
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Session) getChannels(ctx context.Context, chanTypes []string, cb func(t
ctx, task := trace.NewTask(ctx, "getChannels")
defer task.End()

limiter := network.NewLimiter(network.Tier2, s.cfg.limits.Tier2.Burst, int(s.cfg.limits.Tier2.Boost))
limiter := s.limiter(network.Tier2)

if chanTypes == nil {
chanTypes = AllChanTypes
Expand Down
66 changes: 46 additions & 20 deletions cmd/slackdump/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"runtime/trace"
"time"

"github.com/charmbracelet/huh"
"github.com/rusq/fsadapter"
Expand Down Expand Up @@ -113,24 +114,13 @@ func list(ctx context.Context, listFn listFunc) error {
return err
}

lg := logger.FromContext(ctx)
teamID := sess.Info().TeamID
users, err := m.LoadUsers(teamID, cfg.UserCacheRetention)
if err != nil {
if !errors.Is(err, cache.ErrExpired) && !errors.Is(err, cache.ErrEmpty) {
return err
}
lg.Println("user cache expired or empty, caching users")

users, err = sess.GetUsers(ctx)
users, ok := data.(types.Users)
if !ok {
users, err = getCachedUsers(ctx, sess, m, teamID)
if err != nil {
return err
}

if err := m.CacheUsers(teamID, users); err != nil {
trace.Logf(ctx, "error", "saving user cache to %q, error: %s", userCacheBase, err)
lg.Printf("error saving user cache to %q: %s, but nevermind, let's continue", userCacheBase, err)
}
}

if !nosave {
Expand All @@ -154,20 +144,56 @@ func list(ctx context.Context, listFn listFunc) error {
// saveData saves the given data to the given filename.
func saveData(ctx context.Context, fs fsadapter.FS, data any, filename string, typ format.Type, users []slack.User) error {
// save to a filesystem.
if err := writeData(ctx, fs, filename, data, typ, users); err != nil {
f, err := fs.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()
if err := fmtPrint(ctx, f, data, typ, users); err != nil {
return err
}
logger.FromContext(ctx).Printf("Data saved to: %q\n", filepath.Join(cfg.BaseLocation, filename))

return nil
}

func writeData(ctx context.Context, fs fsadapter.FS, filename string, data any, typ format.Type, users []slack.User) error {
f, err := fs.Create(filename)
type userGetter interface {
GetUsers(ctx context.Context) (types.Users, error)
}

type userCacher interface {
LoadUsers(teamID string, retention time.Duration) ([]slack.User, error)
CacheUsers(teamID string, users []slack.User) error
}

func getCachedUsers(ctx context.Context, ug userGetter, m userCacher, teamID string) ([]slack.User, error) {
users, err := m.LoadUsers(teamID, cfg.UserCacheRetention)
if err == nil {
return users, nil
}

// failed to load from cache
if !errors.Is(err, cache.ErrExpired) && !errors.Is(err, cache.ErrEmpty) {
// some funky error
return nil, err
}

lg := logger.FromContext(ctx)
lg.Println("user cache expired or empty, caching users")

// getting users from API
users, err = ug.GetUsers(ctx)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
return nil, err
}
defer f.Close()
return fmtPrint(ctx, f, data, typ, users)

// saving users to cache, will ignore any errors, but notify the user.
if err := m.CacheUsers(teamID, users); err != nil {
trace.Logf(ctx, "error", "saving user cache to %q, error: %s", userCacheBase, err)
lg.Printf("warning: failed saving user cache to %q: %s, but nevermind, let's continue", userCacheBase, err)
}

return users, nil
}

// fmtPrint prints the given data to the given writer, using the given format.
Expand Down
6 changes: 3 additions & 3 deletions internal/cache/channelcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// loadUsers tries to load the users from the file. If the file does not exist
// or is older than maxAge, it returns an error.
func loadChannels(cacheDir, filename string, suffix string, maxAge time.Duration) (types.Channels, error) {
func loadChannels(cacheDir, filename string, suffix string, maxAge time.Duration) ([]slack.Channel, error) {
uu, err := load[slack.Channel](cacheDir, filename, suffix, maxAge)
if err != nil {
return nil, err
Expand All @@ -19,6 +19,6 @@ func loadChannels(cacheDir, filename string, suffix string, maxAge time.Duration

// saveUsers saves the users to a file, naming the file based on the filename
// and the suffix. The file will be saved in the cache directory.
func saveChannels(cacheDir, filename string, suffix string, uu types.Channels) error {
return save(cacheDir, filename, suffix, []slack.Channel(uu))
func saveChannels(cacheDir, filename string, suffix string, cc []slack.Channel) error {
return save(cacheDir, filename, suffix, cc)
}
10 changes: 5 additions & 5 deletions internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"time"

"github.com/rusq/encio"
"github.com/slack-go/slack"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/types"
)

// Manager is the workspace manager. It is an abstraction over the directory
Expand Down Expand Up @@ -367,21 +367,21 @@ func (m *Manager) WalkUsers(userFn func(path string, r io.Reader) error) error {
}

// LoadUsers loads user cache file no older than maxAge for teamID.
func (m *Manager) LoadUsers(teamID string, maxAge time.Duration) (types.Users, error) {
func (m *Manager) LoadUsers(teamID string, maxAge time.Duration) ([]slack.User, error) {
return loadUsers(m.dir, m.userFile, teamID, maxAge)
}

// CacheUsers saves users to user cache file for teamID.
func (m *Manager) CacheUsers(teamID string, uu types.Users) error {
func (m *Manager) CacheUsers(teamID string, uu []slack.User) error {
return saveUsers(m.dir, m.userFile, teamID, uu)
}

// LoadChannels loads channel cache no older than maxAge.
func (m *Manager) LoadChannels(teamID string, maxAge time.Duration) (types.Channels, error) {
func (m *Manager) LoadChannels(teamID string, maxAge time.Duration) ([]slack.Channel, error) {
return loadChannels(m.dir, m.channelFile, teamID, maxAge)
}

// CacheChannels saves channels to cache.
func (m *Manager) CacheChannels(teamID string, cc types.Channels) error {
func (m *Manager) CacheChannels(teamID string, cc []slack.Channel) error {
return saveChannels(m.dir, m.channelFile, teamID, cc)
}
2 changes: 1 addition & 1 deletion slackdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (s *Session) limiter(t network.Tier) *rate.Limiter {
default:
tl = s.cfg.limits.Tier3
}
return network.NewLimiter(t, tl.Burst, int(tl.Boost)) // BUG: tier was always 3, should fix in master too.
return network.NewLimiter(t, tl.Burst, int(tl.Boost))
}

// Info returns a workspace information. Slackdump retrieves workspace
Expand Down
6 changes: 2 additions & 4 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ func (s *Session) GetUsers(ctx context.Context) (types.Users, error) {
}
s.uc.set(users)
return users, err

}

// fetchUsers fetches users from the API.
func (s *Session) fetchUsers(ctx context.Context) (types.Users, error) {
var (
users []slack.User
)
l := network.NewLimiter(
network.Tier2, s.cfg.limits.Tier2.Burst, int(s.cfg.limits.Tier2.Boost),
)

l := s.limiter(network.Tier2)
if err := network.WithRetry(ctx, l, s.cfg.limits.Tier2.Retries, func() error {
var err error
users, err = s.client.GetUsersContext(ctx)
Expand Down

0 comments on commit 5bb6174

Please sign in to comment.