| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/component" |
| 12 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sender" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type TimeoutConfig struct { |
| 17 |
|
| 18 |
|
| 19 |
Timeout time.Duration `mapstructure:"timeout"` |
| 20 |
} |
| 21 |
|
| 22 |
func (ts *TimeoutConfig) Validate() error { |
| 23 |
|
| 24 |
if ts.Timeout < 0 { |
| 25 |
return errors.New("'timeout' must be non-negative") |
| 26 |
} |
| 27 |
return nil |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func NewDefaultTimeoutConfig() TimeoutConfig { |
| 32 |
return TimeoutConfig{ |
| 33 |
Timeout: 5 * time.Second, |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
type timeoutSender[T any] struct { |
| 39 |
component.StartFunc |
| 40 |
component.ShutdownFunc |
| 41 |
cfg TimeoutConfig |
| 42 |
next sender.Sender[T] |
| 43 |
} |
| 44 |
|
| 45 |
func newTimeoutSender[T any](cfg TimeoutConfig, next sender.Sender[T]) sender.Sender[T] { |
| 46 |
return &timeoutSender[T]{cfg: cfg, next: next} |
| 47 |
} |
| 48 |
|
| 49 |
func (ts *timeoutSender[T]) Send(ctx context.Context, req T) error { |
| 50 |
|
| 51 |
|
| 52 |
tCtx, cancelFunc := context.WithTimeout(ctx, ts.cfg.Timeout) |
| 53 |
defer cancelFunc() |
| 54 |
return ts.next.Send(tCtx, req) |
| 55 |
} |