Skip to content

Commit

Permalink
Docs changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Nov 2, 2023
1 parent b115765 commit 6c7dff7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
11 changes: 5 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ func NoRetryError(err error) error {
return noRetryError{Err: err}
}

func IsNoRetryError(err error) bool {
// isNoRetryError returns whether an error is a NoRetryError
func isNoRetryError(err error) bool {
return errors.Is(err, noRetryError{})
}

// noRetryError represents an error that will not be retried.
type noRetryError struct {
Err error
}
Expand Down Expand Up @@ -49,11 +51,8 @@ func RetryAtError(err error, at time.Time) error {
return retryAtError{Err: err, At: at}
}

func IsRetryAtError(err error) bool {
return errors.Is(err, retryAtError{})
}

func GetRetryAtTime(err error) *time.Time {
// getRetryAtTime returns the time from a retryAtError, or nil.
func getRetryAtTime(err error) *time.Time {
retryAt := &retryAtError{}
if ok := errors.As(err, retryAt); ok {
return &retryAt.At
Expand Down
14 changes: 7 additions & 7 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

func TestIsNoRetryError(t *testing.T) {
err := fmt.Errorf("error")
require.False(t, IsNoRetryError(err))
require.False(t, isNoRetryError(err))

wrapped := NoRetryError(err)
require.True(t, IsNoRetryError(wrapped))
require.True(t, isNoRetryError(wrapped))

cause := fmt.Errorf("error: %w", wrapped)
require.True(t, IsNoRetryError(cause))
require.True(t, isNoRetryError(cause))
}

func TestGetRetryAtTime(t *testing.T) {
Expand All @@ -26,15 +26,15 @@ func TestGetRetryAtTime(t *testing.T) {
at := RetryAtError(err, expected)

t.Run("It returns the time with a RetryAtError", func(t *testing.T) {
require.NotNil(t, GetRetryAtTime(at))
require.EqualValues(t, expected, *GetRetryAtTime(at))
require.NotNil(t, getRetryAtTime(at))
require.EqualValues(t, expected, *getRetryAtTime(at))
})

t.Run("It returns if RetryAtError is wrapped itself", func(t *testing.T) {

wrapped := fmt.Errorf("wrap: %w", at)

require.NotNil(t, GetRetryAtTime(wrapped))
require.EqualValues(t, expected, *GetRetryAtTime(wrapped))
require.NotNil(t, getRetryAtTime(wrapped))
require.EqualValues(t, expected, *getRetryAtTime(wrapped))
})
}
4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (ge GenericEvent[D, U]) Validate() error {
return nil
}

// Now returns a timestamp with millisecond precision used for the Event.Timestamp
// NowMillis returns a timestamp with millisecond precision used for the Event.Timestamp
// field.
func Now() int64 {
func NowMillis() int64 {
return time.Now().UnixMilli()
}

Expand Down
3 changes: 1 addition & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func AccountCreated(ctx context.Context, input inngestgo.Input[AccountCreatedEve
// This returns the fully typed result of the lambda.
result := step.Run(ctx, "on-user-created", func(ctx context.Context) (bool, error) {
// Run any code inside a step.
result, err := emails.Send(emails.Opts{})
return result, err
return false, nil
})
// `result` is fully typed from the lambda
_ = result
Expand Down
2 changes: 2 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (f FunctionOpts) GetRateLimit() *inngest.RateLimit {
return f.RateLimit.Convert()
}

// Debounce represents debounce configuration used when creating a new function within
// FunctionOpts
type Debounce struct {
Key string `json:"key"`
Period time.Duration `json:"period"`
Expand Down
19 changes: 15 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,31 @@ type HandlerOpts struct {
UseStreaming bool
}

// GetSigningKey returns the signing key defined within HandlerOpts, or the default
// defined within INNGEST_SIGNING_KEY.
//
// This is the private key used to register functions and communicate with the private
// API.
func (h HandlerOpts) GetSigningKey() string {
if h.SigningKey == nil {
return os.Getenv("INNGEST_SIGNING_KEY")
}
return *h.SigningKey
}

// GetEnv returns the env defined within HandlerOpts, or the default
// defined within INNGEST_ENV.
//
// This is the environment name used for preview/branch environments within Inngest.
func (h HandlerOpts) GetEnv() string {
if h.Env == nil {
return os.Getenv("INNGEST_ENV")
}
return *h.Env
}

// GetRegisterURL returns the registration URL defined wtihin HandlerOpts,
// defaulting to the production Inngest URL if nil.
func (h HandlerOpts) GetRegisterURL() string {
if h.RegisterURL == nil {
return "https://www.inngest.com/fn/register"
Expand Down Expand Up @@ -468,8 +479,8 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {
return json.NewEncoder(w).Encode(StreamResponse{
StatusCode: 500,
Body: fmt.Sprintf("error calling function: %s", err.Error()),
NoRetry: IsNoRetryError(err),
RetryAt: GetRetryAtTime(err),
NoRetry: isNoRetryError(err),
RetryAt: getRetryAtTime(err),
})
}
if len(ops) > 0 {
Expand All @@ -487,11 +498,11 @@ func (h *handler) invoke(w http.ResponseWriter, r *http.Request) error {
if err != nil {
l.Error("error calling function", "error", err)

if IsNoRetryError(err) {
if isNoRetryError(err) {
w.Header().Add("x-inngest-no-retry", "true")
}

if at := GetRetryAtTime(err); at != nil {
if at := getRetryAtTime(err); at != nil {
w.Header().Add("retry-after", at.Format(time.RFC3339))
}

Expand Down
10 changes: 5 additions & 5 deletions internal/sdkrequest/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ func (r *requestCtxManager) NewOp(op enums.Opcode, id string, opts map[string]an
}

type UnhashedOp struct {
ID string `json:"id"`
Op enums.Opcode `json:"op"`
Opts map[string]any `json:"opts"`
Pos uint `json:"pos"`
Parent *string `json:"parent,omitempty"`
Op enums.Opcode `json:"op"`
ID string `json:"id"`
Name string `json:"name"`
Opts map[string]any `json:"opts"`
Pos uint `json:"-"`
}

func (u UnhashedOp) Hash() (string, error) {
Expand Down
3 changes: 2 additions & 1 deletion signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func Sign(ctx context.Context, at time.Time, key, body []byte) string {
}

// ValidateSignature ensures that the signature for the given body is signed with
// the given key recently.
// the given key within a given time period to prevent invalid requests or
// replay attacks.
func ValidateSignature(ctx context.Context, sig string, key, body []byte) (bool, error) {
key = normalizeKey(key)

Expand Down

0 comments on commit 6c7dff7

Please sign in to comment.