@@ -481,13 +481,61 @@ func LineSenderFromConf(ctx context.Context, conf string) (LineSender, error) {
481
481
// sender corresponds to a single client connection. LineSender should
482
482
// not be called concurrently by multiple goroutines.
483
483
func NewLineSender (ctx context.Context , opts ... LineSenderOption ) (LineSender , error ) {
484
- conf := & lineSenderConfig {}
484
+ var conf * lineSenderConfig
485
+
486
+ // Iterate over all options to determine the sender type
487
+ // This is used to set defaults based on the type of sender (http vs tcp)
488
+ // Worst case performance is 2N for the number of LineSenderOptions
489
+ tmp := newLineSenderConfig (noSenderType )
490
+ for _ , opt := range opts {
491
+ opt (tmp )
492
+ switch tmp .senderType {
493
+ case httpSenderType :
494
+ conf = newLineSenderConfig (httpSenderType )
495
+ case tcpSenderType :
496
+ conf = newLineSenderConfig (tcpSenderType )
497
+ }
498
+
499
+ if conf != nil {
500
+ break
501
+ }
502
+ }
503
+
504
+ if tmp .senderType == noSenderType {
505
+ return nil , errors .New ("sender type is not specified: use WithHttp or WithTcp" )
506
+ }
507
+
485
508
for _ , opt := range opts {
486
509
opt (conf )
487
510
}
488
511
return newLineSender (ctx , conf )
489
512
}
490
513
514
+ func newLineSenderConfig (t senderType ) * lineSenderConfig {
515
+ switch t {
516
+ case tcpSenderType :
517
+ return & lineSenderConfig {
518
+ senderType : t ,
519
+ address : defaultTcpAddress ,
520
+ initBufSize : defaultInitBufferSize ,
521
+ fileNameLimit : defaultFileNameLimit ,
522
+ }
523
+ default :
524
+ return & lineSenderConfig {
525
+ senderType : t ,
526
+ address : defaultHttpAddress ,
527
+ requestTimeout : defaultRequestTimeout ,
528
+ retryTimeout : defaultRetryTimeout ,
529
+ minThroughput : defaultMinThroughput ,
530
+ autoFlushRows : defaultAutoFlushRows ,
531
+ autoFlushInterval : defaultAutoFlushInterval ,
532
+ initBufSize : defaultInitBufferSize ,
533
+ maxBufSize : defaultMaxBufferSize ,
534
+ fileNameLimit : defaultFileNameLimit ,
535
+ }
536
+ }
537
+ }
538
+
491
539
func newLineSender (ctx context.Context , conf * lineSenderConfig ) (LineSender , error ) {
492
540
switch conf .senderType {
493
541
case tcpSenderType :
@@ -538,17 +586,6 @@ func sanitizeTcpConf(conf *lineSenderConfig) error {
538
586
return errors .New ("tcpKeyId is empty and tcpKey is not. both (or none) must be provided" )
539
587
}
540
588
541
- // Set defaults
542
- if conf .address == "" {
543
- conf .address = defaultTcpAddress
544
- }
545
- if conf .initBufSize == 0 {
546
- conf .initBufSize = defaultInitBufferSize
547
- }
548
- if conf .fileNameLimit == 0 {
549
- conf .fileNameLimit = defaultFileNameLimit
550
- }
551
-
552
589
return nil
553
590
}
554
591
@@ -563,35 +600,6 @@ func sanitizeHttpConf(conf *lineSenderConfig) error {
563
600
return errors .New ("both basic and token authentication cannot be used" )
564
601
}
565
602
566
- // Set defaults
567
- if conf .address == "" {
568
- conf .address = defaultHttpAddress
569
- }
570
- if conf .requestTimeout == 0 {
571
- conf .requestTimeout = defaultRequestTimeout
572
- }
573
- if conf .retryTimeout == 0 {
574
- conf .retryTimeout = defaultRetryTimeout
575
- }
576
- if conf .minThroughput == 0 {
577
- conf .minThroughput = defaultMinThroughput
578
- }
579
- if conf .autoFlushRows == 0 {
580
- conf .autoFlushRows = defaultAutoFlushRows
581
- }
582
- if conf .autoFlushInterval == 0 {
583
- conf .autoFlushInterval = defaultAutoFlushInterval
584
- }
585
- if conf .initBufSize == 0 {
586
- conf .initBufSize = defaultInitBufferSize
587
- }
588
- if conf .maxBufSize == 0 {
589
- conf .maxBufSize = defaultMaxBufferSize
590
- }
591
- if conf .fileNameLimit == 0 {
592
- conf .fileNameLimit = defaultFileNameLimit
593
- }
594
-
595
603
return nil
596
604
}
597
605
0 commit comments