
Use a Go context to limit one Imagild HTTP request while preserving the remote task as a separate durable operation. A context deadline can stop connection setup, request transfer or response reading; it does not establish that an accepted image or video generation was cancelled.
The Go net/http reference documents the outgoing request context's scope. Apply that scope deliberately rather than converting every context.DeadlineExceeded into a failed generation record.
Key Takeaways
- Store operation identity before submission and task ID after acceptance.
- Give each status read a context deadline without changing the provider's task state.
- Resume an existing task or original submission identity after an interrupted request.
Keep the provider record outside a goroutine
A goroutine is useful for concurrency, but its memory is not a durable job record. Save the approved request, stable idempotency key, application owner and eventual task ID in a database.
If a browser disconnects from your Go service, its request context may be cancelled. That is a signal about the local HTTP interaction. The accepted provider task can still require a later authenticated status read.
Use a durable worker with its own bounded lifecycle to reconcile existing operations. Do not solve a lost browser connection by starting an untracked background goroutine that becomes the sole owner of paid work.
Read one existing task with a deadline
The helper below is intended for a server-side package. It assumes the caller has validated a saved owned UUID and supplies a configured HTTP client and secret.
type Task struct {
ID string `json:"id"`
Status string `json:"status"`
Usage struct {
Points int `json:"points"`
State string `json:"state"`
} `json:"usage"`
}
func ReadTask(ctx context.Context, client *http.Client,
taskID, apiKey string) (Task, error) {
var zero Task
endpoint := "https://imagild.com/api/relay/v1/tasks/" +
url.PathEscape(taskID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil { return zero, err }
req.Header.Set("Authorization", "Bearer "+apiKey)
response, err := client.Do(req)
if err != nil { return zero, err }
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return zero, fmt.Errorf("task read HTTP %d", response.StatusCode)
}
var envelope struct { Data Task `json:"data"` }
err = json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&envelope)
if err != nil { return zero, err }
if envelope.Data.ID != taskID || envelope.Data.Status == "" {
return zero, fmt.Errorf("unexpected task response")
}
return envelope.Data, nil
}
The surrounding file needs imports for context, net/http, net/url, encoding/json, io and fmt. Configure your client's redirect policy to reject unexpected destinations before using a private key.
A worker can call this helper with a child context created by context.WithTimeout, then call its cancel function to release resources. The Go context reference documents that pattern. A twenty-second read deadline, for example, is your policy rather than a generation-speed promise.
Interpret remote status only after a usable read
An HTTP 200 means the task record was retrieved, not that generation succeeded. Inspect returned status and usage. For succeeded, perform the separate authenticated content download. For failed, retain its failure evidence. For unknown, seek reconciliation rather than creating another operation.
The Imagild API console defines the full response and access rules. Active membership is required even for reads; points alone do not grant that access.
If the context expires during GET, preserve the last good observation and schedule a later read. If it expires during POST before a task ID is received, recover with the original Idempotency-Key and unchanged parameters.
Test timeout recovery, not just happy-path decoding
Use httptest or a controlled fake service to delay headers, interrupt a body and return a valid failed task inside HTTP 200. Confirm that local errors do not overwrite remote status.
Restart the worker with the saved operation and verify that no fresh generation identity appears. Consult the failed-job guide for recovery wording and the points planning guide when a genuine creative retake is approved. Cancellation of local waiting and authorization of new spending are separate decisions.
Related tasks
- What Should You Check Before Retrying a Failed Image or Video Job?
- Imagild Points, Resolution and Jobs: Plan a Creation Before You Spend
Sources and scope
- Imagild API console: Active-member access, supported media request fields, idempotency, asynchronous task records and authenticated downloads; checked against the 2026-10-28 relay contract.
- Go net/http NewRequestWithContext: Context governs outgoing HTTP request and response lifetime, rather than remote provider task cancellation.
- Go context WithTimeout: Child deadline context and cancellation cleanup pattern.
Product instructions were checked against the current Imagild API contract and the public model catalog. Code, budgets and acceptance checks are illustrative; this article does not claim a new paid generation test, an uptime measurement, a search-volume study or a ranking of AI citations.
Prepared by Imagild Editorial, the company editorial team responsible for these guides. AI assisted research and drafting. This is documentation-based guidance with proposed examples, not a claim of hands-on testing or an independent product review. Report a correction.
Cover: Original Imagild product screenshot captured on 28 October 2026; model availability and prices may change. This image is not an API response or a performance benchmark.