-
Notifications
You must be signed in to change notification settings - Fork 4
Add client configurations #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbe28d7
Add client configurations
ynguyensegment 67871b0
support Source config
prayansh d215214
Release v0.0.1-webhookfn
ynguyensegment e45274c
Merge pull request #10 from segmentio/source-config
prayansh 8795017
Patch objects-go (remove sensitive logs) (#11)
prayansh e52e2a8
Merge remote-tracking branch 'origin/master' into client_configs
tysonmote 7d5a623
History.md
tysonmote 28ae668
Rename function for clarity
tysonmote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,15 +31,18 @@ var ( | |
| ErrClientClosed = errors.New("Client is closed") | ||
| ) | ||
|
|
||
| type Client struct { | ||
| type Config struct { | ||
| BaseEndpoint string | ||
| Logger *log.Logger | ||
| Client *http.Client | ||
|
|
||
| MaxBatchBytes int | ||
| MaxBatchCount int | ||
| MaxBatchInterval time.Duration | ||
| } | ||
|
|
||
| type Client struct { | ||
| Config | ||
| writeKey string | ||
| wg sync.WaitGroup | ||
| semaphore semaphore.Semaphore | ||
|
|
@@ -48,17 +51,45 @@ type Client struct { | |
| } | ||
|
|
||
| func New(writeKey string) *Client { | ||
| return NewWithConfig(writeKey, Config{}) | ||
| } | ||
|
|
||
| func NewWithConfig(writeKey string, config Config) *Client { | ||
| conf := getFinalConfig(config) | ||
| return &Client{ | ||
| BaseEndpoint: DefaultBaseEndpoint, | ||
| Logger: log.New(os.Stderr, "segment ", log.LstdFlags), | ||
| writeKey: writeKey, | ||
| Client: http.DefaultClient, | ||
| cmap: newConcurrentMap(), | ||
| MaxBatchBytes: 500 << 10, | ||
| MaxBatchCount: 100, | ||
| MaxBatchInterval: 10 * time.Second, | ||
| semaphore: make(semaphore.Semaphore, 10), | ||
| Config: conf, | ||
| writeKey: writeKey, | ||
| cmap: newConcurrentMap(), | ||
| semaphore: make(semaphore.Semaphore, 10), | ||
| } | ||
| } | ||
|
|
||
| func getFinalConfig(c Config) Config { | ||
|
||
| if c.BaseEndpoint == "" { | ||
| c.BaseEndpoint = DefaultBaseEndpoint | ||
| } | ||
|
|
||
| if c.Logger == nil { | ||
| c.Logger = log.New(os.Stderr, "segment ", log.LstdFlags) | ||
| } | ||
|
|
||
| if c.Client == nil { | ||
| c.Client = http.DefaultClient | ||
| } | ||
|
|
||
| if c.MaxBatchBytes <= 0 { | ||
| c.MaxBatchBytes = 500 << 10 | ||
| } | ||
|
|
||
| if c.MaxBatchCount <= 0 { | ||
| c.MaxBatchCount = 100 | ||
| } | ||
|
|
||
| if c.MaxBatchInterval <= 0 { | ||
| c.MaxBatchInterval = 10 * time.Second | ||
| } | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| func (c *Client) fetchFunction(key string) *buffer { | ||
|
|
@@ -167,7 +198,6 @@ func (c *Client) makeRequest(request *batch) { | |
| return | ||
| } | ||
|
|
||
|
|
||
| b := backoff.NewExponentialBackOff() | ||
| b.MaxElapsedTime = 10 * time.Second | ||
| err = backoff.Retry(func() error { | ||
|
|
@@ -184,7 +214,7 @@ func (c *Client) makeRequest(request *batch) { | |
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return fmt.Errorf("HTTP Post Request Failed, Status Code %d. \nResponse: %v \nRequest payload: %v", | ||
| resp.StatusCode, response, string(payload)) | ||
| resp.StatusCode, response, string(payload)) | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think if
Configcontains the writeKey?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Configs tend to imply optionality. The writekey is not optional. I am not sure it belongs in the config