go-hep.org/x/hep@v0.38.1/groot/internal/httpio/httpio.go (about) 1 // Copyright ©2022 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package httpio provides types for basic I/O interfaces over HTTP. 6 package httpio // import "go-hep.org/x/hep/groot/internal/httpio" 7 8 import ( 9 "context" 10 "errors" 11 "net/http" 12 "time" 13 ) 14 15 var ( 16 defaultClient *http.Client 17 18 errAcceptRange = errors.New("httpio: accept-range not supported") 19 ) 20 21 type config struct { 22 ctx context.Context 23 cli *http.Client 24 auth struct { 25 usr string 26 pwd string 27 } 28 } 29 30 func newConfig() *config { 31 return &config{ 32 ctx: context.Background(), 33 cli: defaultClient, 34 } 35 } 36 37 type Option func(*config) error 38 39 // WithClient sets up Reader to use a user-provided HTTP client. 40 // 41 // By default, Reader uses an httpio-local default client. 42 func WithClient(cli *http.Client) Option { 43 return func(c *config) error { 44 c.cli = cli 45 return nil 46 } 47 } 48 49 // WithBasicAuth sets up a basic authentification scheme. 50 func WithBasicAuth(usr, pwd string) Option { 51 return func(c *config) error { 52 c.auth.usr = usr 53 c.auth.pwd = pwd 54 return nil 55 } 56 } 57 58 // WithContext configures Reader to use a user-provided context. 59 // 60 // By default, Reader uses context.Background. 61 func WithContext(ctx context.Context) Option { 62 return func(c *config) error { 63 c.ctx = ctx 64 return nil 65 } 66 } 67 68 func init() { 69 t := http.DefaultTransport.(*http.Transport).Clone() 70 t.MaxIdleConns = 100 71 t.MaxConnsPerHost = 100 72 t.MaxIdleConnsPerHost = 100 73 74 defaultClient = &http.Client{ 75 Timeout: 10 * time.Second, 76 Transport: t, 77 } 78 }