From f9a1a4dd6f154c08f308431b821cc23e5dac8942 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 27 Oct 2024 11:34:16 +0200 Subject: fix timeouts --- internal/entry/entry.go | 7 ++++--- internal/platforms/linkedin/linkedin.go | 14 +++++++++++--- internal/platforms/mastodon/mastodon.go | 8 +++++++- internal/run.go | 1 - 4 files changed, 22 insertions(+), 8 deletions(-) (limited to 'internal') 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): -- cgit v1.2.3