Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions inflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Client[T]) newFlight(key string) *inFlightCall[T] {
return call
}

func makeCall[T, V any](ctx context.Context, c *Client[T], key string, fn FetchFn[V], call *inFlightCall[T]) {
func makeCall[T any](ctx context.Context, c *Client[T], key string, fn FetchFn[T], call *inFlightCall[T]) {
defer func() {
if err := recover(); err != nil {
call.err = fmt.Errorf("sturdyc: panic recovered: %v", err)
Expand All @@ -44,29 +44,23 @@ func makeCall[T, V any](ctx context.Context, c *Client[T], key string, fn FetchF
return
}

res, ok := any(response).(T)
if !ok {
call.err = ErrInvalidType
return
}

call.err = nil
call.val = res
c.Set(key, res)
call.val = response
c.Set(key, response)
}

func callAndCache[V, T any](ctx context.Context, c *Client[T], key string, fn FetchFn[V]) (V, error) {
func callAndCache[T any](ctx context.Context, c *Client[T], key string, fn FetchFn[T]) (T, error) {
c.inFlightMutex.Lock()
if call, ok := c.inFlightMap[key]; ok {
c.inFlightMutex.Unlock()
call.Wait()
return unwrap[V, T](call.val, call.err)
return call.val, call.err
}

call := c.newFlight(key)
c.inFlightMutex.Unlock()
makeCall(ctx, c, key, fn, call)
return unwrap[V, T](call.val, call.err)
return call.val, call.err
}

// newBatchFlight should be called with a lock.
Expand Down