@@ -481,13 +481,61 @@ func LineSenderFromConf(ctx context.Context, conf string) (LineSender, error) {
481481// sender corresponds to a single client connection. LineSender should
482482// not be called concurrently by multiple goroutines.
483483func 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+
485508 for _ , opt := range opts {
486509 opt (conf )
487510 }
488511 return newLineSender (ctx , conf )
489512}
490513
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+
491539func newLineSender (ctx context.Context , conf * lineSenderConfig ) (LineSender , error ) {
492540 switch conf .senderType {
493541 case tcpSenderType :
@@ -538,17 +586,6 @@ func sanitizeTcpConf(conf *lineSenderConfig) error {
538586 return errors .New ("tcpKeyId is empty and tcpKey is not. both (or none) must be provided" )
539587 }
540588
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-
552589 return nil
553590}
554591
@@ -563,35 +600,6 @@ func sanitizeHttpConf(conf *lineSenderConfig) error {
563600 return errors .New ("both basic and token authentication cannot be used" )
564601 }
565602
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-
595603 return nil
596604}
597605
0 commit comments