diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-27 11:34:16 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-27 11:34:16 +0200 |
| commit | f9a1a4dd6f154c08f308431b821cc23e5dac8942 (patch) | |
| tree | 2850b7f610b55d84e15d40cad898a355044029af | |
| parent | 063d630bc1acbaaf1b9743a33bef559601b117d3 (diff) | |
fix timeouts
| -rw-r--r-- | cmd/gos/main.go | 2 | ||||
| -rw-r--r-- | internal/entry/entry.go | 7 | ||||
| -rw-r--r-- | internal/platforms/linkedin/linkedin.go | 14 | ||||
| -rw-r--r-- | internal/platforms/mastodon/mastodon.go | 8 | ||||
| -rw-r--r-- | internal/run.go | 1 |
5 files changed, 23 insertions, 9 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go index 81f747a..de41866 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -74,7 +74,7 @@ func main() { return } - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(1*time.Minute)) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() if err := internal.Run(ctx, args); err != nil { diff --git a/internal/entry/entry.go b/internal/entry/entry.go index a103e69..ee73f42 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -20,6 +20,8 @@ const ( Posted ) +var ErrSizeLimitExceeded = errors.New("message size limit exceeded") + func (s State) String() string { switch s { case Unknown: @@ -40,8 +42,7 @@ type Entry struct { } func (e Entry) String() string { - return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", - e.Path, e.Time.Format(timestamp.Format), e.State) + return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", e.Path, e.Time.Format(timestamp.Format), e.State) } var Zero = Entry{} @@ -92,7 +93,7 @@ func (e Entry) ContentWithLimit(sizeLimit int) (string, []string, error) { return "", urls, err } if len(content) > sizeLimit { - err := fmt.Errorf("entry content exceeds size limit: %d > %d: %v", len(content), sizeLimit, e) + err := fmt.Errorf("%w (%d > %d): %v", ErrSizeLimitExceeded, len(content), sizeLimit, e) if err2 := prompt.Acknowledge("You need to shorten the content as "+err.Error(), content); err2 != nil { return "", urls, errors.Join(err, err2) } diff --git a/internal/platforms/linkedin/linkedin.go b/internal/platforms/linkedin/linkedin.go index 54752b9..a6507a3 100644 --- a/internal/platforms/linkedin/linkedin.go +++ b/internal/platforms/linkedin/linkedin.go @@ -9,6 +9,7 @@ import ( "io" "log" "net/http" + "time" "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/entry" @@ -18,7 +19,8 @@ import ( var errUnauthorized = errors.New("unauthorized access, refresh or create token?") -// TODO: Why are no previews of links shown then posted? +const linkedInTimeout = 10 * time.Second + func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { err := post(ctx, args, sizeLimit, ent) if errors.Is(err, errUnauthorized) { @@ -34,7 +36,10 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) log.Println("Not posting", ent, "to LinkedIn as dry-run enabled") return nil } - personID, accessToken, err := oauth2.LinkedInCreds(ctx, args) + + newCtx, cancel := context.WithTimeout(ctx, linkedInTimeout) + defer cancel() + personID, accessToken, err := oauth2.LinkedInCreds(newCtx, args) if err != nil { return err } @@ -53,7 +58,10 @@ func post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } return err } - return callLinkedInAPI(ctx, personID, accessToken, content, urls) + + newCtx, cancel = context.WithTimeout(ctx, linkedInTimeout) + defer cancel() + return callLinkedInAPI(newCtx, personID, accessToken, content, urls) } func callLinkedInAPI(ctx context.Context, personID, accessToken, content string, urls []string) error { diff --git a/internal/platforms/mastodon/mastodon.go b/internal/platforms/mastodon/mastodon.go index 42700e5..f896290 100644 --- a/internal/platforms/mastodon/mastodon.go +++ b/internal/platforms/mastodon/mastodon.go @@ -9,12 +9,15 @@ import ( "io" "log" "net/http" + "time" "codeberg.org/snonux/gos/internal/config" "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/prompt" ) +const mastodonTimeout = 10 * time.Second + func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) error { content, _, err := ent.ContentWithLimit(sizeLimit) if err != nil { @@ -38,7 +41,10 @@ func Post(ctx context.Context, args config.Args, sizeLimit int, ent entry.Entry) } return err } - req, err := http.NewRequestWithContext(ctx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes)) + + newCtx, cancel := context.WithTimeout(ctx, mastodonTimeout) + defer cancel() + req, err := http.NewRequestWithContext(newCtx, "POST", args.Secrets.MastodonURL, bytes.NewBuffer(payloadBytes)) if err != nil { return fmt.Errorf("failed to create request: %w", err) } diff --git a/internal/run.go b/internal/run.go index 513d71e..98fb501 100644 --- a/internal/run.go +++ b/internal/run.go @@ -35,7 +35,6 @@ func Run(ctx context.Context, args config.Args) error { } func runPlatform(ctx context.Context, args config.Args, platform string, sizeLimit int) error { - // TODO: ctx should be extended, when editing a file due to its size being too large ent, err := schedule.Run(args, platform) switch { case errors.Is(err, schedule.ErrNothingToSchedule): |
