diff --git a/Dockerfile b/Dockerfile index a14a0f3..c7db75b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build container -FROM golang:1.17-bullseye AS build +FROM golang:1.18-bullseye AS build # Set build workdir WORKDIR /app diff --git a/go.mod b/go.mod index b1a6b36..0c9093d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module gitea.russia9.dev/Russia9/chatwars-deals -go 1.17 +go 1.18 require ( github.com/confluentinc/confluent-kafka-go v1.8.2 diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/LICENSE b/vendor/github.com/confluentinc/confluent-kafka-go/LICENSE new file mode 100644 index 0000000..e06d208 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/LICENSE @@ -0,0 +1,202 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/.gitignore b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/.gitignore new file mode 100644 index 0000000..b1a2111 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/.gitignore @@ -0,0 +1,2 @@ +testconf.json +go_rdkafka_generr/go_rdkafka_generr diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/00version.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/00version.go new file mode 100644 index 0000000..8c2c7b4 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/00version.go @@ -0,0 +1,58 @@ +package kafka + +/** + * Copyright 2016-2019 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" +) + +/* +#include "select_rdkafka.h" + +//Minimum required librdkafka version. This is checked both during +//build-time and runtime. +//Make sure to keep the MIN_RD_KAFKA_VERSION, MIN_VER_ERRSTR and #error +//defines and strings in sync. +// + +#define MIN_RD_KAFKA_VERSION 0x01060000 + +#ifdef __APPLE__ +#define MIN_VER_ERRSTR "confluent-kafka-go requires librdkafka v1.6.0 or later. Install the latest version of librdkafka from Homebrew by running `brew install librdkafka` or `brew upgrade librdkafka`" +#else +#define MIN_VER_ERRSTR "confluent-kafka-go requires librdkafka v1.6.0 or later. Install the latest version of librdkafka from the Confluent repositories, see http://docs.confluent.io/current/installation.html" +#endif + +#if RD_KAFKA_VERSION < MIN_RD_KAFKA_VERSION +#ifdef __APPLE__ +#error "confluent-kafka-go requires librdkafka v1.6.0 or later. Install the latest version of librdkafka from Homebrew by running `brew install librdkafka` or `brew upgrade librdkafka`" +#else +#error "confluent-kafka-go requires librdkafka v1.6.0 or later. Install the latest version of librdkafka from the Confluent repositories, see http://docs.confluent.io/current/installation.html" +#endif +#endif +*/ +import "C" + +func versionCheck() error { + ver, verstr := LibraryVersion() + if ver < C.MIN_RD_KAFKA_VERSION { + return newErrorFromString(ErrNotImplemented, + fmt.Sprintf("%s: librdkafka version %s (0x%x) detected", + C.MIN_VER_ERRSTR, verstr, ver)) + } + return nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/README.md b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/README.md new file mode 100644 index 0000000..98152e9 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/README.md @@ -0,0 +1,145 @@ +# Information for confluent-kafka-go developers + +Whenever librdkafka error codes are updated make sure to run generate +before building: + +``` + $ make -f mk/Makefile generr + $ go build ./... +``` + + + + +## Testing + +Some of the tests included in this directory, the benchmark and integration tests in particular, +require an existing Kafka cluster and a testconf.json configuration file to +provide tests with bootstrap brokers, topic name, etc. + +The format of testconf.json is a JSON object: +``` +{ + "Brokers": "", + "Topic": "" +} +``` + +See testconf-example.json for an example and full set of available options. + + +To run unit-tests: +``` +$ go test +``` + +To run benchmark tests: +``` +$ go test -bench . +``` + +For the code coverage: +``` +$ go test -coverprofile=coverage.out -bench=. +$ go tool cover -func=coverage.out +``` + + +## Build tags + +Different build types are supported through Go build tags (`-tags ..`), +these tags should be specified on the **application** build/get/install command. + + * By default the bundled platform-specific static build of librdkafka will + be used. This works out of the box on Mac OSX and glibc-based Linux distros, + such as Ubuntu and CentOS. + * `-tags musl` - must be specified when building on/for musl-based Linux + distros, such as Alpine. Will use the bundled static musl build of + librdkafka. + * `-tags dynamic` - link librdkafka dynamically. A shared librdkafka library + must be installed manually through other means (apt-get, yum, build from + source, etc). + + + +## Generating HTML documentation + +To generate one-page HTML documentation run the mk/doc-gen.py script from the +top-level directory. This script requires the beautifulsoup4 Python package. + +``` +$ source .../your/virtualenv/bin/activate +$ pip install beautifulsoup4 +... +$ make -f mk/Makefile docs +``` + + +## Release process + +For each release candidate and final release, perform the following steps: + +### Update bundle to latest librdkafka + +See instructions in [kafka/librdkafka/README.md](kafka/librdkafka/README.md). + + +### Update librdkafka version requirement + +Update the minimum required librdkafka version in `kafka/00version.go` +and `README.md`. + + +### Update error codes + +Error codes can be automatically generated from the current librdkafka version. + + +Update generated error codes: + + $ make -f mk/Makefile generr + # Verify by building + + +### Rebuild everything + + $ go clean -i ./... + $ go build ./... + + +### Run full test suite + +Set up a test cluster using whatever mechanism you typically use +(docker, trivup, ccloud, ..). + +Make sure to update `kafka/testconf.json` as needed (broker list, $BROKERS) + +Run test suite: + + $ go test ./... + + +### Verify examples + +Manually verify that the examples/ applications work. + +Also make sure the examples in README.md work. + +Convert any examples using `github.com/confluentinc/confluent-kafka-go/kafka` to use +`gopkg.in/confluentinc/confluent-kafka-go.v1/kafka` import path. + + $ find examples/ -type f -name *\.go -exec sed -i -e 's|github\.com/confluentinc/confluent-kafka-go/kafka|gopkg\.in/confluentinc/confluent-kafka-go\.v1/kafka|g' {} + + +### Commit any changes + +Make sure to push to github before creating the tag to have CI tests pass. + + +### Create and push tag + + $ git tag v1.3.0 + $ git push --dry-run origin v1.3.0 + # Remove --dry-run and re-execute if it looks ok. + + +### Create release notes page on github diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminapi.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminapi.go new file mode 100644 index 0000000..e128129 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminapi.go @@ -0,0 +1,1025 @@ +/** + * Copyright 2018 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "context" + "fmt" + "strings" + "time" + "unsafe" +) + +/* +#include "select_rdkafka.h" +#include + +static const rd_kafka_topic_result_t * +topic_result_by_idx (const rd_kafka_topic_result_t **topics, size_t cnt, size_t idx) { + if (idx >= cnt) + return NULL; + return topics[idx]; +} + +static const rd_kafka_ConfigResource_t * +ConfigResource_by_idx (const rd_kafka_ConfigResource_t **res, size_t cnt, size_t idx) { + if (idx >= cnt) + return NULL; + return res[idx]; +} + +static const rd_kafka_ConfigEntry_t * +ConfigEntry_by_idx (const rd_kafka_ConfigEntry_t **entries, size_t cnt, size_t idx) { + if (idx >= cnt) + return NULL; + return entries[idx]; +} +*/ +import "C" + +// AdminClient is derived from an existing Producer or Consumer +type AdminClient struct { + handle *handle + isDerived bool // Derived from existing client handle +} + +func durationToMilliseconds(t time.Duration) int { + if t > 0 { + return (int)(t.Seconds() * 1000.0) + } + return (int)(t) +} + +// TopicResult provides per-topic operation result (error) information. +type TopicResult struct { + // Topic name + Topic string + // Error, if any, of result. Check with `Error.Code() != ErrNoError`. + Error Error +} + +// String returns a human-readable representation of a TopicResult. +func (t TopicResult) String() string { + if t.Error.code == 0 { + return t.Topic + } + return fmt.Sprintf("%s (%s)", t.Topic, t.Error.str) +} + +// TopicSpecification holds parameters for creating a new topic. +// TopicSpecification is analogous to NewTopic in the Java Topic Admin API. +type TopicSpecification struct { + // Topic name to create. + Topic string + // Number of partitions in topic. + NumPartitions int + // Default replication factor for the topic's partitions, or zero + // if an explicit ReplicaAssignment is set. + ReplicationFactor int + // (Optional) Explicit replica assignment. The outer array is + // indexed by the partition number, while the inner per-partition array + // contains the replica broker ids. The first broker in each + // broker id list will be the preferred replica. + ReplicaAssignment [][]int32 + // Topic configuration. + Config map[string]string +} + +// PartitionsSpecification holds parameters for creating additional partitions for a topic. +// PartitionsSpecification is analogous to NewPartitions in the Java Topic Admin API. +type PartitionsSpecification struct { + // Topic to create more partitions for. + Topic string + // New partition count for topic, must be higher than current partition count. + IncreaseTo int + // (Optional) Explicit replica assignment. The outer array is + // indexed by the new partition index (i.e., 0 for the first added + // partition), while the inner per-partition array + // contains the replica broker ids. The first broker in each + // broker id list will be the preferred replica. + ReplicaAssignment [][]int32 +} + +// ResourceType represents an Apache Kafka resource type +type ResourceType int + +const ( + // ResourceUnknown - Unknown + ResourceUnknown = ResourceType(C.RD_KAFKA_RESOURCE_UNKNOWN) + // ResourceAny - match any resource type (DescribeConfigs) + ResourceAny = ResourceType(C.RD_KAFKA_RESOURCE_ANY) + // ResourceTopic - Topic + ResourceTopic = ResourceType(C.RD_KAFKA_RESOURCE_TOPIC) + // ResourceGroup - Group + ResourceGroup = ResourceType(C.RD_KAFKA_RESOURCE_GROUP) + // ResourceBroker - Broker + ResourceBroker = ResourceType(C.RD_KAFKA_RESOURCE_BROKER) +) + +// String returns the human-readable representation of a ResourceType +func (t ResourceType) String() string { + return C.GoString(C.rd_kafka_ResourceType_name(C.rd_kafka_ResourceType_t(t))) +} + +// ResourceTypeFromString translates a resource type name/string to +// a ResourceType value. +func ResourceTypeFromString(typeString string) (ResourceType, error) { + switch strings.ToUpper(typeString) { + case "ANY": + return ResourceAny, nil + case "TOPIC": + return ResourceTopic, nil + case "GROUP": + return ResourceGroup, nil + case "BROKER": + return ResourceBroker, nil + default: + return ResourceUnknown, NewError(ErrInvalidArg, "Unknown resource type", false) + } +} + +// ConfigSource represents an Apache Kafka config source +type ConfigSource int + +const ( + // ConfigSourceUnknown is the default value + ConfigSourceUnknown = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_UNKNOWN_CONFIG) + // ConfigSourceDynamicTopic is dynamic topic config that is configured for a specific topic + ConfigSourceDynamicTopic = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_TOPIC_CONFIG) + // ConfigSourceDynamicBroker is dynamic broker config that is configured for a specific broker + ConfigSourceDynamicBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_BROKER_CONFIG) + // ConfigSourceDynamicDefaultBroker is dynamic broker config that is configured as default for all brokers in the cluster + ConfigSourceDynamicDefaultBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_DEFAULT_BROKER_CONFIG) + // ConfigSourceStaticBroker is static broker config provided as broker properties at startup (e.g. from server.properties file) + ConfigSourceStaticBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_STATIC_BROKER_CONFIG) + // ConfigSourceDefault is built-in default configuration for configs that have a default value + ConfigSourceDefault = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DEFAULT_CONFIG) +) + +// String returns the human-readable representation of a ConfigSource type +func (t ConfigSource) String() string { + return C.GoString(C.rd_kafka_ConfigSource_name(C.rd_kafka_ConfigSource_t(t))) +} + +// ConfigResource holds parameters for altering an Apache Kafka configuration resource +type ConfigResource struct { + // Type of resource to set. + Type ResourceType + // Name of resource to set. + Name string + // Config entries to set. + // Configuration updates are atomic, any configuration property not provided + // here will be reverted (by the broker) to its default value. + // Use DescribeConfigs to retrieve the list of current configuration entry values. + Config []ConfigEntry +} + +// String returns a human-readable representation of a ConfigResource +func (c ConfigResource) String() string { + return fmt.Sprintf("Resource(%s, %s)", c.Type, c.Name) +} + +// AlterOperation specifies the operation to perform on the ConfigEntry. +// Currently only AlterOperationSet. +type AlterOperation int + +const ( + // AlterOperationSet sets/overwrites the configuration setting. + AlterOperationSet = iota +) + +// String returns the human-readable representation of an AlterOperation +func (o AlterOperation) String() string { + switch o { + case AlterOperationSet: + return "Set" + default: + return fmt.Sprintf("Unknown%d?", int(o)) + } +} + +// ConfigEntry holds parameters for altering a resource's configuration. +type ConfigEntry struct { + // Name of configuration entry, e.g., topic configuration property name. + Name string + // Value of configuration entry. + Value string + // Operation to perform on the entry. + Operation AlterOperation +} + +// StringMapToConfigEntries creates a new map of ConfigEntry objects from the +// provided string map. The AlterOperation is set on each created entry. +func StringMapToConfigEntries(stringMap map[string]string, operation AlterOperation) []ConfigEntry { + var ceList []ConfigEntry + + for k, v := range stringMap { + ceList = append(ceList, ConfigEntry{Name: k, Value: v, Operation: operation}) + } + + return ceList +} + +// String returns a human-readable representation of a ConfigEntry. +func (c ConfigEntry) String() string { + return fmt.Sprintf("%v %s=\"%s\"", c.Operation, c.Name, c.Value) +} + +// ConfigEntryResult contains the result of a single configuration entry from a +// DescribeConfigs request. +type ConfigEntryResult struct { + // Name of configuration entry, e.g., topic configuration property name. + Name string + // Value of configuration entry. + Value string + // Source indicates the configuration source. + Source ConfigSource + // IsReadOnly indicates whether the configuration entry can be altered. + IsReadOnly bool + // IsSensitive indicates whether the configuration entry contains sensitive information, in which case the value will be unset. + IsSensitive bool + // IsSynonym indicates whether the configuration entry is a synonym for another configuration property. + IsSynonym bool + // Synonyms contains a map of configuration entries that are synonyms to this configuration entry. + Synonyms map[string]ConfigEntryResult +} + +// String returns a human-readable representation of a ConfigEntryResult. +func (c ConfigEntryResult) String() string { + return fmt.Sprintf("%s=\"%s\"", c.Name, c.Value) +} + +// setFromC sets up a ConfigEntryResult from a C ConfigEntry +func configEntryResultFromC(cEntry *C.rd_kafka_ConfigEntry_t) (entry ConfigEntryResult) { + entry.Name = C.GoString(C.rd_kafka_ConfigEntry_name(cEntry)) + cValue := C.rd_kafka_ConfigEntry_value(cEntry) + if cValue != nil { + entry.Value = C.GoString(cValue) + } + entry.Source = ConfigSource(C.rd_kafka_ConfigEntry_source(cEntry)) + entry.IsReadOnly = cint2bool(C.rd_kafka_ConfigEntry_is_read_only(cEntry)) + entry.IsSensitive = cint2bool(C.rd_kafka_ConfigEntry_is_sensitive(cEntry)) + entry.IsSynonym = cint2bool(C.rd_kafka_ConfigEntry_is_synonym(cEntry)) + + var cSynCnt C.size_t + cSyns := C.rd_kafka_ConfigEntry_synonyms(cEntry, &cSynCnt) + if cSynCnt > 0 { + entry.Synonyms = make(map[string]ConfigEntryResult) + } + + for si := 0; si < int(cSynCnt); si++ { + cSyn := C.ConfigEntry_by_idx(cSyns, cSynCnt, C.size_t(si)) + Syn := configEntryResultFromC(cSyn) + entry.Synonyms[Syn.Name] = Syn + } + + return entry +} + +// ConfigResourceResult provides the result for a resource from a AlterConfigs or +// DescribeConfigs request. +type ConfigResourceResult struct { + // Type of returned result resource. + Type ResourceType + // Name of returned result resource. + Name string + // Error, if any, of returned result resource. + Error Error + // Config entries, if any, of returned result resource. + Config map[string]ConfigEntryResult +} + +// String returns a human-readable representation of a ConfigResourceResult. +func (c ConfigResourceResult) String() string { + if c.Error.Code() != 0 { + return fmt.Sprintf("ResourceResult(%s, %s, \"%v\")", c.Type, c.Name, c.Error) + + } + return fmt.Sprintf("ResourceResult(%s, %s, %d config(s))", c.Type, c.Name, len(c.Config)) +} + +// waitResult waits for a result event on cQueue or the ctx to be cancelled, whichever happens +// first. +// The returned result event is checked for errors its error is returned if set. +func (a *AdminClient) waitResult(ctx context.Context, cQueue *C.rd_kafka_queue_t, cEventType C.rd_kafka_event_type_t) (rkev *C.rd_kafka_event_t, err error) { + + resultChan := make(chan *C.rd_kafka_event_t) + closeChan := make(chan bool) // never written to, just closed + + go func() { + for { + select { + case _, ok := <-closeChan: + if !ok { + // Context cancelled/timed out + close(resultChan) + return + } + + default: + // Wait for result event for at most 50ms + // to avoid blocking for too long if + // context is cancelled. + rkev := C.rd_kafka_queue_poll(cQueue, 50) + if rkev != nil { + resultChan <- rkev + close(resultChan) + return + } + } + } + }() + + select { + case rkev = <-resultChan: + // Result type check + if cEventType != C.rd_kafka_event_type(rkev) { + err = newErrorFromString(ErrInvalidType, + fmt.Sprintf("Expected %d result event, not %d", (int)(cEventType), (int)(C.rd_kafka_event_type(rkev)))) + C.rd_kafka_event_destroy(rkev) + return nil, err + } + + // Generic error handling + cErr := C.rd_kafka_event_error(rkev) + if cErr != 0 { + err = newErrorFromCString(cErr, C.rd_kafka_event_error_string(rkev)) + C.rd_kafka_event_destroy(rkev) + return nil, err + } + close(closeChan) + return rkev, nil + case <-ctx.Done(): + // signal close to go-routine + close(closeChan) + // wait for close from go-routine to make sure it is done + // using cQueue before we return. + rkev, ok := <-resultChan + if ok { + // throw away result since context was cancelled + C.rd_kafka_event_destroy(rkev) + } + return nil, ctx.Err() + } +} + +// cToTopicResults converts a C topic_result_t array to Go TopicResult list. +func (a *AdminClient) cToTopicResults(cTopicRes **C.rd_kafka_topic_result_t, cCnt C.size_t) (result []TopicResult, err error) { + + result = make([]TopicResult, int(cCnt)) + + for i := 0; i < int(cCnt); i++ { + cTopic := C.topic_result_by_idx(cTopicRes, cCnt, C.size_t(i)) + result[i].Topic = C.GoString(C.rd_kafka_topic_result_name(cTopic)) + result[i].Error = newErrorFromCString( + C.rd_kafka_topic_result_error(cTopic), + C.rd_kafka_topic_result_error_string(cTopic)) + } + + return result, nil +} + +// cConfigResourceToResult converts a C ConfigResource result array to Go ConfigResourceResult +func (a *AdminClient) cConfigResourceToResult(cRes **C.rd_kafka_ConfigResource_t, cCnt C.size_t) (result []ConfigResourceResult, err error) { + + result = make([]ConfigResourceResult, int(cCnt)) + + for i := 0; i < int(cCnt); i++ { + cRes := C.ConfigResource_by_idx(cRes, cCnt, C.size_t(i)) + result[i].Type = ResourceType(C.rd_kafka_ConfigResource_type(cRes)) + result[i].Name = C.GoString(C.rd_kafka_ConfigResource_name(cRes)) + result[i].Error = newErrorFromCString( + C.rd_kafka_ConfigResource_error(cRes), + C.rd_kafka_ConfigResource_error_string(cRes)) + var cConfigCnt C.size_t + cConfigs := C.rd_kafka_ConfigResource_configs(cRes, &cConfigCnt) + if cConfigCnt > 0 { + result[i].Config = make(map[string]ConfigEntryResult) + } + for ci := 0; ci < int(cConfigCnt); ci++ { + cEntry := C.ConfigEntry_by_idx(cConfigs, cConfigCnt, C.size_t(ci)) + entry := configEntryResultFromC(cEntry) + result[i].Config[entry.Name] = entry + } + } + + return result, nil +} + +// ClusterID returns the cluster ID as reported in broker metadata. +// +// Note on cancellation: Although the underlying C function respects the +// timeout, it currently cannot be manually cancelled. That means manually +// cancelling the context will block until the C function call returns. +// +// Requires broker version >= 0.10.0. +func (a *AdminClient) ClusterID(ctx context.Context) (clusterID string, err error) { + responseChan := make(chan *C.char, 1) + + go func() { + responseChan <- C.rd_kafka_clusterid(a.handle.rk, cTimeoutFromContext(ctx)) + }() + + select { + case <-ctx.Done(): + if cClusterID := <-responseChan; cClusterID != nil { + C.rd_kafka_mem_free(a.handle.rk, unsafe.Pointer(cClusterID)) + } + return "", ctx.Err() + + case cClusterID := <-responseChan: + if cClusterID == nil { // C timeout + <-ctx.Done() + return "", ctx.Err() + } + defer C.rd_kafka_mem_free(a.handle.rk, unsafe.Pointer(cClusterID)) + return C.GoString(cClusterID), nil + } +} + +// ControllerID returns the broker ID of the current controller as reported in +// broker metadata. +// +// Note on cancellation: Although the underlying C function respects the +// timeout, it currently cannot be manually cancelled. That means manually +// cancelling the context will block until the C function call returns. +// +// Requires broker version >= 0.10.0. +func (a *AdminClient) ControllerID(ctx context.Context) (controllerID int32, err error) { + responseChan := make(chan int32, 1) + + go func() { + responseChan <- int32(C.rd_kafka_controllerid(a.handle.rk, cTimeoutFromContext(ctx))) + }() + + select { + case <-ctx.Done(): + <-responseChan + return 0, ctx.Err() + + case controllerID := <-responseChan: + if controllerID < 0 { // C timeout + <-ctx.Done() + return 0, ctx.Err() + } + return controllerID, nil + } +} + +// CreateTopics creates topics in cluster. +// +// The list of TopicSpecification objects define the per-topic partition count, replicas, etc. +// +// Topic creation is non-atomic and may succeed for some topics but fail for others, +// make sure to check the result for topic-specific errors. +// +// Note: TopicSpecification is analogous to NewTopic in the Java Topic Admin API. +func (a *AdminClient) CreateTopics(ctx context.Context, topics []TopicSpecification, options ...CreateTopicsAdminOption) (result []TopicResult, err error) { + cTopics := make([]*C.rd_kafka_NewTopic_t, len(topics)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + // Convert Go TopicSpecifications to C TopicSpecifications + for i, topic := range topics { + + var cReplicationFactor C.int + if topic.ReplicationFactor == 0 { + cReplicationFactor = -1 + } else { + cReplicationFactor = C.int(topic.ReplicationFactor) + } + if topic.ReplicaAssignment != nil { + if cReplicationFactor != -1 { + return nil, newErrorFromString(ErrInvalidArg, + "TopicSpecification.ReplicationFactor and TopicSpecification.ReplicaAssignment are mutually exclusive") + } + + if len(topic.ReplicaAssignment) != topic.NumPartitions { + return nil, newErrorFromString(ErrInvalidArg, + "TopicSpecification.ReplicaAssignment must contain exactly TopicSpecification.NumPartitions partitions") + } + } + + cTopics[i] = C.rd_kafka_NewTopic_new( + C.CString(topic.Topic), + C.int(topic.NumPartitions), + cReplicationFactor, + cErrstr, cErrstrSize) + if cTopics[i] == nil { + return nil, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Topic %s: %s", topic.Topic, C.GoString(cErrstr))) + } + + defer C.rd_kafka_NewTopic_destroy(cTopics[i]) + + for p, replicas := range topic.ReplicaAssignment { + cReplicas := make([]C.int32_t, len(replicas)) + for ri, replica := range replicas { + cReplicas[ri] = C.int32_t(replica) + } + cErr := C.rd_kafka_NewTopic_set_replica_assignment( + cTopics[i], C.int32_t(p), + (*C.int32_t)(&cReplicas[0]), C.size_t(len(cReplicas)), + cErrstr, cErrstrSize) + if cErr != 0 { + return nil, newCErrorFromString(cErr, + fmt.Sprintf("Failed to set replica assignment for topic %s partition %d: %s", topic.Topic, p, C.GoString(cErrstr))) + } + } + + for key, value := range topic.Config { + cErr := C.rd_kafka_NewTopic_set_config( + cTopics[i], + C.CString(key), C.CString(value)) + if cErr != 0 { + return nil, newCErrorFromString(cErr, + fmt.Sprintf("Failed to set config %s=%s for topic %s", key, value, topic.Topic)) + } + } + } + + // Convert Go AdminOptions (if any) to C AdminOptions + genericOptions := make([]AdminOption, len(options)) + for i := range options { + genericOptions[i] = options[i] + } + cOptions, err := adminOptionsSetup(a.handle, C.RD_KAFKA_ADMIN_OP_CREATETOPICS, genericOptions) + if err != nil { + return nil, err + } + defer C.rd_kafka_AdminOptions_destroy(cOptions) + + // Create temporary queue for async operation + cQueue := C.rd_kafka_queue_new(a.handle.rk) + defer C.rd_kafka_queue_destroy(cQueue) + + // Asynchronous call + C.rd_kafka_CreateTopics( + a.handle.rk, + (**C.rd_kafka_NewTopic_t)(&cTopics[0]), + C.size_t(len(cTopics)), + cOptions, + cQueue) + + // Wait for result, error or context timeout + rkev, err := a.waitResult(ctx, cQueue, C.RD_KAFKA_EVENT_CREATETOPICS_RESULT) + if err != nil { + return nil, err + } + defer C.rd_kafka_event_destroy(rkev) + + cRes := C.rd_kafka_event_CreateTopics_result(rkev) + + // Convert result from C to Go + var cCnt C.size_t + cTopicRes := C.rd_kafka_CreateTopics_result_topics(cRes, &cCnt) + + return a.cToTopicResults(cTopicRes, cCnt) +} + +// DeleteTopics deletes a batch of topics. +// +// This operation is not transactional and may succeed for a subset of topics while +// failing others. +// It may take several seconds after the DeleteTopics result returns success for +// all the brokers to become aware that the topics are gone. During this time, +// topic metadata and configuration may continue to return information about deleted topics. +// +// Requires broker version >= 0.10.1.0 +func (a *AdminClient) DeleteTopics(ctx context.Context, topics []string, options ...DeleteTopicsAdminOption) (result []TopicResult, err error) { + cTopics := make([]*C.rd_kafka_DeleteTopic_t, len(topics)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + // Convert Go DeleteTopics to C DeleteTopics + for i, topic := range topics { + cTopics[i] = C.rd_kafka_DeleteTopic_new(C.CString(topic)) + if cTopics[i] == nil { + return nil, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Invalid arguments for topic %s", topic)) + } + + defer C.rd_kafka_DeleteTopic_destroy(cTopics[i]) + } + + // Convert Go AdminOptions (if any) to C AdminOptions + genericOptions := make([]AdminOption, len(options)) + for i := range options { + genericOptions[i] = options[i] + } + cOptions, err := adminOptionsSetup(a.handle, C.RD_KAFKA_ADMIN_OP_DELETETOPICS, genericOptions) + if err != nil { + return nil, err + } + defer C.rd_kafka_AdminOptions_destroy(cOptions) + + // Create temporary queue for async operation + cQueue := C.rd_kafka_queue_new(a.handle.rk) + defer C.rd_kafka_queue_destroy(cQueue) + + // Asynchronous call + C.rd_kafka_DeleteTopics( + a.handle.rk, + (**C.rd_kafka_DeleteTopic_t)(&cTopics[0]), + C.size_t(len(cTopics)), + cOptions, + cQueue) + + // Wait for result, error or context timeout + rkev, err := a.waitResult(ctx, cQueue, C.RD_KAFKA_EVENT_DELETETOPICS_RESULT) + if err != nil { + return nil, err + } + defer C.rd_kafka_event_destroy(rkev) + + cRes := C.rd_kafka_event_DeleteTopics_result(rkev) + + // Convert result from C to Go + var cCnt C.size_t + cTopicRes := C.rd_kafka_DeleteTopics_result_topics(cRes, &cCnt) + + return a.cToTopicResults(cTopicRes, cCnt) +} + +// CreatePartitions creates additional partitions for topics. +func (a *AdminClient) CreatePartitions(ctx context.Context, partitions []PartitionsSpecification, options ...CreatePartitionsAdminOption) (result []TopicResult, err error) { + cParts := make([]*C.rd_kafka_NewPartitions_t, len(partitions)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + // Convert Go PartitionsSpecification to C NewPartitions + for i, part := range partitions { + cParts[i] = C.rd_kafka_NewPartitions_new(C.CString(part.Topic), C.size_t(part.IncreaseTo), cErrstr, cErrstrSize) + if cParts[i] == nil { + return nil, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Topic %s: %s", part.Topic, C.GoString(cErrstr))) + } + + defer C.rd_kafka_NewPartitions_destroy(cParts[i]) + + for pidx, replicas := range part.ReplicaAssignment { + cReplicas := make([]C.int32_t, len(replicas)) + for ri, replica := range replicas { + cReplicas[ri] = C.int32_t(replica) + } + cErr := C.rd_kafka_NewPartitions_set_replica_assignment( + cParts[i], C.int32_t(pidx), + (*C.int32_t)(&cReplicas[0]), C.size_t(len(cReplicas)), + cErrstr, cErrstrSize) + if cErr != 0 { + return nil, newCErrorFromString(cErr, + fmt.Sprintf("Failed to set replica assignment for topic %s new partition index %d: %s", part.Topic, pidx, C.GoString(cErrstr))) + } + } + + } + + // Convert Go AdminOptions (if any) to C AdminOptions + genericOptions := make([]AdminOption, len(options)) + for i := range options { + genericOptions[i] = options[i] + } + cOptions, err := adminOptionsSetup(a.handle, C.RD_KAFKA_ADMIN_OP_CREATEPARTITIONS, genericOptions) + if err != nil { + return nil, err + } + defer C.rd_kafka_AdminOptions_destroy(cOptions) + + // Create temporary queue for async operation + cQueue := C.rd_kafka_queue_new(a.handle.rk) + defer C.rd_kafka_queue_destroy(cQueue) + + // Asynchronous call + C.rd_kafka_CreatePartitions( + a.handle.rk, + (**C.rd_kafka_NewPartitions_t)(&cParts[0]), + C.size_t(len(cParts)), + cOptions, + cQueue) + + // Wait for result, error or context timeout + rkev, err := a.waitResult(ctx, cQueue, C.RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT) + if err != nil { + return nil, err + } + defer C.rd_kafka_event_destroy(rkev) + + cRes := C.rd_kafka_event_CreatePartitions_result(rkev) + + // Convert result from C to Go + var cCnt C.size_t + cTopicRes := C.rd_kafka_CreatePartitions_result_topics(cRes, &cCnt) + + return a.cToTopicResults(cTopicRes, cCnt) +} + +// AlterConfigs alters/updates cluster resource configuration. +// +// Updates are not transactional so they may succeed for a subset +// of the provided resources while others fail. +// The configuration for a particular resource is updated atomically, +// replacing values using the provided ConfigEntrys and reverting +// unspecified ConfigEntrys to their default values. +// +// Requires broker version >=0.11.0.0 +// +// AlterConfigs will replace all existing configuration for +// the provided resources with the new configuration given, +// reverting all other configuration to their default values. +// +// Multiple resources and resource types may be set, but at most one +// resource of type ResourceBroker is allowed per call since these +// resource requests must be sent to the broker specified in the resource. +func (a *AdminClient) AlterConfigs(ctx context.Context, resources []ConfigResource, options ...AlterConfigsAdminOption) (result []ConfigResourceResult, err error) { + cRes := make([]*C.rd_kafka_ConfigResource_t, len(resources)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + // Convert Go ConfigResources to C ConfigResources + for i, res := range resources { + cRes[i] = C.rd_kafka_ConfigResource_new( + C.rd_kafka_ResourceType_t(res.Type), C.CString(res.Name)) + if cRes[i] == nil { + return nil, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Invalid arguments for resource %v", res)) + } + + defer C.rd_kafka_ConfigResource_destroy(cRes[i]) + + for _, entry := range res.Config { + var cErr C.rd_kafka_resp_err_t + switch entry.Operation { + case AlterOperationSet: + cErr = C.rd_kafka_ConfigResource_set_config( + cRes[i], C.CString(entry.Name), C.CString(entry.Value)) + default: + panic(fmt.Sprintf("Invalid ConfigEntry.Operation: %v", entry.Operation)) + } + + if cErr != 0 { + return nil, + newCErrorFromString(cErr, + fmt.Sprintf("Failed to add configuration %s: %s", + entry, C.GoString(C.rd_kafka_err2str(cErr)))) + } + } + } + + // Convert Go AdminOptions (if any) to C AdminOptions + genericOptions := make([]AdminOption, len(options)) + for i := range options { + genericOptions[i] = options[i] + } + cOptions, err := adminOptionsSetup(a.handle, C.RD_KAFKA_ADMIN_OP_ALTERCONFIGS, genericOptions) + if err != nil { + return nil, err + } + defer C.rd_kafka_AdminOptions_destroy(cOptions) + + // Create temporary queue for async operation + cQueue := C.rd_kafka_queue_new(a.handle.rk) + defer C.rd_kafka_queue_destroy(cQueue) + + // Asynchronous call + C.rd_kafka_AlterConfigs( + a.handle.rk, + (**C.rd_kafka_ConfigResource_t)(&cRes[0]), + C.size_t(len(cRes)), + cOptions, + cQueue) + + // Wait for result, error or context timeout + rkev, err := a.waitResult(ctx, cQueue, C.RD_KAFKA_EVENT_ALTERCONFIGS_RESULT) + if err != nil { + return nil, err + } + defer C.rd_kafka_event_destroy(rkev) + + cResult := C.rd_kafka_event_AlterConfigs_result(rkev) + + // Convert results from C to Go + var cCnt C.size_t + cResults := C.rd_kafka_AlterConfigs_result_resources(cResult, &cCnt) + + return a.cConfigResourceToResult(cResults, cCnt) +} + +// DescribeConfigs retrieves configuration for cluster resources. +// +// The returned configuration includes default values, use +// ConfigEntryResult.IsDefault or ConfigEntryResult.Source to distinguish +// default values from manually configured settings. +// +// The value of config entries where .IsSensitive is true +// will always be nil to avoid disclosing sensitive +// information, such as security settings. +// +// Configuration entries where .IsReadOnly is true can't be modified +// (with AlterConfigs). +// +// Synonym configuration entries are returned if the broker supports +// it (broker version >= 1.1.0). See .Synonyms. +// +// Requires broker version >=0.11.0.0 +// +// Multiple resources and resource types may be requested, but at most +// one resource of type ResourceBroker is allowed per call +// since these resource requests must be sent to the broker specified +// in the resource. +func (a *AdminClient) DescribeConfigs(ctx context.Context, resources []ConfigResource, options ...DescribeConfigsAdminOption) (result []ConfigResourceResult, err error) { + cRes := make([]*C.rd_kafka_ConfigResource_t, len(resources)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + // Convert Go ConfigResources to C ConfigResources + for i, res := range resources { + cRes[i] = C.rd_kafka_ConfigResource_new( + C.rd_kafka_ResourceType_t(res.Type), C.CString(res.Name)) + if cRes[i] == nil { + return nil, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Invalid arguments for resource %v", res)) + } + + defer C.rd_kafka_ConfigResource_destroy(cRes[i]) + } + + // Convert Go AdminOptions (if any) to C AdminOptions + genericOptions := make([]AdminOption, len(options)) + for i := range options { + genericOptions[i] = options[i] + } + cOptions, err := adminOptionsSetup(a.handle, C.RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS, genericOptions) + if err != nil { + return nil, err + } + defer C.rd_kafka_AdminOptions_destroy(cOptions) + + // Create temporary queue for async operation + cQueue := C.rd_kafka_queue_new(a.handle.rk) + defer C.rd_kafka_queue_destroy(cQueue) + + // Asynchronous call + C.rd_kafka_DescribeConfigs( + a.handle.rk, + (**C.rd_kafka_ConfigResource_t)(&cRes[0]), + C.size_t(len(cRes)), + cOptions, + cQueue) + + // Wait for result, error or context timeout + rkev, err := a.waitResult(ctx, cQueue, C.RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT) + if err != nil { + return nil, err + } + defer C.rd_kafka_event_destroy(rkev) + + cResult := C.rd_kafka_event_DescribeConfigs_result(rkev) + + // Convert results from C to Go + var cCnt C.size_t + cResults := C.rd_kafka_DescribeConfigs_result_resources(cResult, &cCnt) + + return a.cConfigResourceToResult(cResults, cCnt) +} + +// GetMetadata queries broker for cluster and topic metadata. +// If topic is non-nil only information about that topic is returned, else if +// allTopics is false only information about locally used topics is returned, +// else information about all topics is returned. +// GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +func (a *AdminClient) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) { + return getMetadata(a, topic, allTopics, timeoutMs) +} + +// String returns a human readable name for an AdminClient instance +func (a *AdminClient) String() string { + return fmt.Sprintf("admin-%s", a.handle.String()) +} + +// get_handle implements the Handle interface +func (a *AdminClient) gethandle() *handle { + return a.handle +} + +// SetOAuthBearerToken sets the the data to be transmitted +// to a broker during SASL/OAUTHBEARER authentication. It will return nil +// on success, otherwise an error if: +// 1) the token data is invalid (meaning an expiration time in the past +// or either a token value or an extension key or value that does not meet +// the regular expression requirements as per +// https://tools.ietf.org/html/rfc7628#section-3.1); +// 2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 3) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (a *AdminClient) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error { + return a.handle.setOAuthBearerToken(oauthBearerToken) +} + +// SetOAuthBearerTokenFailure sets the error message describing why token +// retrieval/setting failed; it also schedules a new token refresh event for 10 +// seconds later so the attempt may be retried. It will return nil on +// success, otherwise an error if: +// 1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 2) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (a *AdminClient) SetOAuthBearerTokenFailure(errstr string) error { + return a.handle.setOAuthBearerTokenFailure(errstr) +} + +// Close an AdminClient instance. +func (a *AdminClient) Close() { + if a.isDerived { + // Derived AdminClient needs no cleanup. + a.handle = &handle{} + return + } + + a.handle.cleanup() + + C.rd_kafka_destroy(a.handle.rk) +} + +// NewAdminClient creats a new AdminClient instance with a new underlying client instance +func NewAdminClient(conf *ConfigMap) (*AdminClient, error) { + + err := versionCheck() + if err != nil { + return nil, err + } + + a := &AdminClient{} + a.handle = &handle{} + + // Convert ConfigMap to librdkafka conf_t + cConf, err := conf.convert() + if err != nil { + return nil, err + } + + cErrstr := (*C.char)(C.malloc(C.size_t(256))) + defer C.free(unsafe.Pointer(cErrstr)) + + C.rd_kafka_conf_set_events(cConf, C.RD_KAFKA_EVENT_STATS|C.RD_KAFKA_EVENT_ERROR|C.RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH) + + // Create librdkafka producer instance. The Producer is somewhat cheaper than + // the consumer, but any instance type can be used for Admin APIs. + a.handle.rk = C.rd_kafka_new(C.RD_KAFKA_PRODUCER, cConf, cErrstr, 256) + if a.handle.rk == nil { + return nil, newErrorFromCString(C.RD_KAFKA_RESP_ERR__INVALID_ARG, cErrstr) + } + + a.isDerived = false + a.handle.setup() + + return a, nil +} + +// NewAdminClientFromProducer derives a new AdminClient from an existing Producer instance. +// The AdminClient will use the same configuration and connections as the parent instance. +func NewAdminClientFromProducer(p *Producer) (a *AdminClient, err error) { + if p.handle.rk == nil { + return nil, newErrorFromString(ErrInvalidArg, "Can't derive AdminClient from closed producer") + } + + a = &AdminClient{} + a.handle = &p.handle + a.isDerived = true + return a, nil +} + +// NewAdminClientFromConsumer derives a new AdminClient from an existing Consumer instance. +// The AdminClient will use the same configuration and connections as the parent instance. +func NewAdminClientFromConsumer(c *Consumer) (a *AdminClient, err error) { + if c.handle.rk == nil { + return nil, newErrorFromString(ErrInvalidArg, "Can't derive AdminClient from closed consumer") + } + + a = &AdminClient{} + a.handle = &c.handle + a.isDerived = true + return a, nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminoptions.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminoptions.go new file mode 100644 index 0000000..842631b --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/adminoptions.go @@ -0,0 +1,264 @@ +/** + * Copyright 2018 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "fmt" + "time" + "unsafe" +) + +/* +#include "select_rdkafka.h" +#include +*/ +import "C" + +// AdminOptionOperationTimeout sets the broker's operation timeout, such as the +// timeout for CreateTopics to complete the creation of topics on the controller +// before returning a result to the application. +// +// CreateTopics, DeleteTopics, CreatePartitions: +// a value 0 will return immediately after triggering topic +// creation, while > 0 will wait this long for topic creation to propagate +// in cluster. +// +// Default: 0 (return immediately). +// +// Valid for CreateTopics, DeleteTopics, CreatePartitions. +type AdminOptionOperationTimeout struct { + isSet bool + val time.Duration +} + +func (ao AdminOptionOperationTimeout) supportsCreateTopics() { +} +func (ao AdminOptionOperationTimeout) supportsDeleteTopics() { +} +func (ao AdminOptionOperationTimeout) supportsCreatePartitions() { +} + +func (ao AdminOptionOperationTimeout) apply(cOptions *C.rd_kafka_AdminOptions_t) error { + if !ao.isSet { + return nil + } + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + cErr := C.rd_kafka_AdminOptions_set_operation_timeout( + cOptions, C.int(durationToMilliseconds(ao.val)), + cErrstr, cErrstrSize) + if cErr != 0 { + C.rd_kafka_AdminOptions_destroy(cOptions) + return newCErrorFromString(cErr, + fmt.Sprintf("Failed to set operation timeout: %s", C.GoString(cErrstr))) + + } + + return nil +} + +// SetAdminOperationTimeout sets the broker's operation timeout, such as the +// timeout for CreateTopics to complete the creation of topics on the controller +// before returning a result to the application. +// +// CreateTopics, DeleteTopics, CreatePartitions: +// a value 0 will return immediately after triggering topic +// creation, while > 0 will wait this long for topic creation to propagate +// in cluster. +// +// Default: 0 (return immediately). +// +// Valid for CreateTopics, DeleteTopics, CreatePartitions. +func SetAdminOperationTimeout(t time.Duration) (ao AdminOptionOperationTimeout) { + ao.isSet = true + ao.val = t + return ao +} + +// AdminOptionRequestTimeout sets the overall request timeout, including broker +// lookup, request transmission, operation time on broker, and response. +// +// Default: `socket.timeout.ms`. +// +// Valid for all Admin API methods. +type AdminOptionRequestTimeout struct { + isSet bool + val time.Duration +} + +func (ao AdminOptionRequestTimeout) supportsCreateTopics() { +} +func (ao AdminOptionRequestTimeout) supportsDeleteTopics() { +} +func (ao AdminOptionRequestTimeout) supportsCreatePartitions() { +} +func (ao AdminOptionRequestTimeout) supportsAlterConfigs() { +} +func (ao AdminOptionRequestTimeout) supportsDescribeConfigs() { +} + +func (ao AdminOptionRequestTimeout) apply(cOptions *C.rd_kafka_AdminOptions_t) error { + if !ao.isSet { + return nil + } + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + cErr := C.rd_kafka_AdminOptions_set_request_timeout( + cOptions, C.int(durationToMilliseconds(ao.val)), + cErrstr, cErrstrSize) + if cErr != 0 { + C.rd_kafka_AdminOptions_destroy(cOptions) + return newCErrorFromString(cErr, + fmt.Sprintf("%s", C.GoString(cErrstr))) + + } + + return nil +} + +// SetAdminRequestTimeout sets the overall request timeout, including broker +// lookup, request transmission, operation time on broker, and response. +// +// Default: `socket.timeout.ms`. +// +// Valid for all Admin API methods. +func SetAdminRequestTimeout(t time.Duration) (ao AdminOptionRequestTimeout) { + ao.isSet = true + ao.val = t + return ao +} + +// AdminOptionValidateOnly tells the broker to only validate the request, +// without performing the requested operation (create topics, etc). +// +// Default: false. +// +// Valid for CreateTopics, CreatePartitions, AlterConfigs +type AdminOptionValidateOnly struct { + isSet bool + val bool +} + +func (ao AdminOptionValidateOnly) supportsCreateTopics() { +} +func (ao AdminOptionValidateOnly) supportsCreatePartitions() { +} +func (ao AdminOptionValidateOnly) supportsAlterConfigs() { +} + +func (ao AdminOptionValidateOnly) apply(cOptions *C.rd_kafka_AdminOptions_t) error { + if !ao.isSet { + return nil + } + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + cErr := C.rd_kafka_AdminOptions_set_validate_only( + cOptions, bool2cint(ao.val), + cErrstr, cErrstrSize) + if cErr != 0 { + C.rd_kafka_AdminOptions_destroy(cOptions) + return newCErrorFromString(cErr, + fmt.Sprintf("%s", C.GoString(cErrstr))) + + } + + return nil +} + +// SetAdminValidateOnly tells the broker to only validate the request, +// without performing the requested operation (create topics, etc). +// +// Default: false. +// +// Valid for CreateTopics, DeleteTopics, CreatePartitions, AlterConfigs +func SetAdminValidateOnly(validateOnly bool) (ao AdminOptionValidateOnly) { + ao.isSet = true + ao.val = validateOnly + return ao +} + +// CreateTopicsAdminOption - see setters. +// +// See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. +type CreateTopicsAdminOption interface { + supportsCreateTopics() + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +// DeleteTopicsAdminOption - see setters. +// +// See SetAdminRequestTimeout, SetAdminOperationTimeout. +type DeleteTopicsAdminOption interface { + supportsDeleteTopics() + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +// CreatePartitionsAdminOption - see setters. +// +// See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. +type CreatePartitionsAdminOption interface { + supportsCreatePartitions() + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +// AlterConfigsAdminOption - see setters. +// +// See SetAdminRequestTimeout, SetAdminValidateOnly, SetAdminIncremental. +type AlterConfigsAdminOption interface { + supportsAlterConfigs() + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +// DescribeConfigsAdminOption - see setters. +// +// See SetAdminRequestTimeout. +type DescribeConfigsAdminOption interface { + supportsDescribeConfigs() + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +// AdminOption is a generic type not to be used directly. +// +// See CreateTopicsAdminOption et.al. +type AdminOption interface { + apply(cOptions *C.rd_kafka_AdminOptions_t) error +} + +func adminOptionsSetup(h *handle, opType C.rd_kafka_admin_op_t, options []AdminOption) (*C.rd_kafka_AdminOptions_t, error) { + + cOptions := C.rd_kafka_AdminOptions_new(h.rk, opType) + for _, opt := range options { + if opt == nil { + continue + } + err := opt.apply(cOptions) + if err != nil { + return nil, err + } + } + + return cOptions, nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/api.html b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/api.html new file mode 100644 index 0000000..8c495b3 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/api.html @@ -0,0 +1,4737 @@ + + + + + + + + kafka - Go Documentation Server + + + + + + + + +
+ ... +
+ +
+
+

+ Package kafka + + +

+ + + + +
+
+
+ + import "github.com/confluentinc/confluent-kafka-go/kafka" + +
+
+
+
+ + Overview + +
+
+ + Index + +
+
+
+
+
+ +
+ +
+

+ Overview ▾ +

+

+ Package kafka provides high-level Apache Kafka producer and consumers +using bindings on-top of the librdkafka C library. +

+

+ High-level Consumer +

+

+ * Decide if you want to read messages and events by calling `.Poll()` or +the deprecated option of using the `.Events()` channel. (If you want to use +`.Events()` channel then set `"go.events.channel.enable": true`). +

+

+ * Create a Consumer with `kafka.NewConsumer()` providing at +least the `bootstrap.servers` and `group.id` configuration properties. +

+

+ * Call `.Subscribe()` or (`.SubscribeTopics()` to subscribe to multiple topics) +to join the group with the specified subscription set. +Subscriptions are atomic, calling `.Subscribe*()` again will leave +the group and rejoin with the new set of topics. +

+

+ * Start reading events and messages from either the `.Events` channel +or by calling `.Poll()`. +

+

+ * When the group has rebalanced each client member is assigned a +(sub-)set of topic+partitions. +By default the consumer will start fetching messages for its assigned +partitions at this point, but your application may enable rebalance +events to get an insight into what the assigned partitions where +as well as set the initial offsets. To do this you need to pass +`"go.application.rebalance.enable": true` to the `NewConsumer()` call +mentioned above. You will (eventually) see a `kafka.AssignedPartitions` event +with the assigned partition set. You can optionally modify the initial +offsets (they'll default to stored offsets and if there are no previously stored +offsets it will fall back to `"auto.offset.reset"` +which defaults to the `latest` message) and then call `.Assign(partitions)` +to start consuming. If you don't need to modify the initial offsets you will +not need to call `.Assign()`, the client will do so automatically for you if +you dont, unless you are using the channel-based consumer in which case +you MUST call `.Assign()` when receiving the `AssignedPartitions` and +`RevokedPartitions` events. +

+

+ * As messages are fetched they will be made available on either the +`.Events` channel or by calling `.Poll()`, look for event type `*kafka.Message`. +

+

+ * Handle messages, events and errors to your liking. +

+

+ * When you are done consuming call `.Close()` to commit final offsets +and leave the consumer group. +

+

+ Producer +

+

+ * Create a Producer with `kafka.NewProducer()` providing at least +the `bootstrap.servers` configuration properties. +

+

+ * Messages may now be produced either by sending a `*kafka.Message` +on the `.ProduceChannel` or by calling `.Produce()`. +

+

+ * Producing is an asynchronous operation so the client notifies the application +of per-message produce success or failure through something called delivery reports. +Delivery reports are by default emitted on the `.Events()` channel as `*kafka.Message` +and you should check `msg.TopicPartition.Error` for `nil` to find out if the message +was succesfully delivered or not. +It is also possible to direct delivery reports to alternate channels +by providing a non-nil `chan Event` channel to `.Produce()`. +If no delivery reports are wanted they can be completely disabled by +setting configuration property `"go.delivery.reports": false`. +

+

+ * When you are done producing messages you will need to make sure all messages +are indeed delivered to the broker (or failed), remember that this is +an asynchronous client so some of your messages may be lingering in internal +channels or tranmission queues. +To do this you can either keep track of the messages you've produced +and wait for their corresponding delivery reports, or call the convenience +function `.Flush()` that will block until all message deliveries are done +or the provided timeout elapses. +

+

+ * Finally call `.Close()` to decommission the producer. +

+

+ Transactional producer API +

+

+ The transactional producer operates on top of the idempotent producer, +and provides full exactly-once semantics (EOS) for Apache Kafka when used +with the transaction aware consumer (`isolation.level=read_committed`). +

+

+ A producer instance is configured for transactions by setting the +`transactional.id` to an identifier unique for the application. This +id will be used to fence stale transactions from previous instances of +the application, typically following an outage or crash. +

+

+ After creating the transactional producer instance using `NewProducer()` +the transactional state must be initialized by calling +`InitTransactions()`. This is a blocking call that will +acquire a runtime producer id from the transaction coordinator broker +as well as abort any stale transactions and fence any still running producer +instances with the same `transactional.id`. +

+

+ Once transactions are initialized the application may begin a new +transaction by calling `BeginTransaction()`. +A producer instance may only have one single on-going transaction. +

+

+ Any messages produced after the transaction has been started will +belong to the ongoing transaction and will be committed or aborted +atomically. +It is not permitted to produce messages outside a transaction +boundary, e.g., before `BeginTransaction()` or after `CommitTransaction()`, +`AbortTransaction()` or if the current transaction has failed. +

+

+ If consumed messages are used as input to the transaction, the consumer +instance must be configured with `enable.auto.commit` set to `false`. +To commit the consumed offsets along with the transaction pass the +list of consumed partitions and the last offset processed + 1 to +`SendOffsetsToTransaction()` prior to committing the transaction. +This allows an aborted transaction to be restarted using the previously +committed offsets. +

+

+ To commit the produced messages, and any consumed offsets, to the +current transaction, call `CommitTransaction()`. +This call will block until the transaction has been fully committed or +failed (typically due to fencing by a newer producer instance). +

+

+ Alternatively, if processing fails, or an abortable transaction error is +raised, the transaction needs to be aborted by calling +`AbortTransaction()` which marks any produced messages and +offset commits as aborted. +

+

+ After the current transaction has been committed or aborted a new +transaction may be started by calling `BeginTransaction()` again. +

+

+ Retriable errors: +Some error cases allow the attempted operation to be retried, this is +indicated by the error object having the retriable flag set which can +be detected by calling `err.(kafka.Error).IsRetriable()`. +When this flag is set the application may retry the operation immediately +or preferably after a shorter grace period (to avoid busy-looping). +Retriable errors include timeouts, broker transport failures, etc. +

+

+ Abortable errors: +An ongoing transaction may fail permanently due to various errors, +such as transaction coordinator becoming unavailable, write failures to the +Apache Kafka log, under-replicated partitions, etc. +At this point the producer application must abort the current transaction +using `AbortTransaction()` and optionally start a new transaction +by calling `BeginTransaction()`. +Whether an error is abortable or not is detected by calling +`err.(kafka.Error).TxnRequiresAbort()` on the returned error object. +

+

+ Fatal errors: +While the underlying idempotent producer will typically only raise +fatal errors for unrecoverable cluster errors where the idempotency +guarantees can't be maintained, most of these are treated as abortable by +the transactional producer since transactions may be aborted and retried +in their entirety; +The transactional producer on the other hand introduces a set of additional +fatal errors which the application needs to handle by shutting down the +producer and terminate. There is no way for a producer instance to recover +from fatal errors. +Whether an error is fatal or not is detected by calling +`err.(kafka.Error).IsFatal()` on the returned error object or by checking +the global `GetFatalError()`. +

+

+ Handling of other errors: +For errors that have neither retriable, abortable or the fatal flag set +it is not always obvious how to handle them. While some of these errors +may be indicative of bugs in the application code, such as when +an invalid parameter is passed to a method, other errors might originate +from the broker and be passed thru as-is to the application. +The general recommendation is to treat these errors, that have +neither the retriable or abortable flags set, as fatal. +

+

+ Error handling example: +

+
retry:
+
+   err := producer.CommitTransaction(...)
+   if err == nil {
+       return nil
+   } else if err.(kafka.Error).TxnRequiresAbort() {
+       do_abort_transaction_and_reset_inputs()
+   } else if err.(kafka.Error).IsRetriable() {
+       goto retry
+   } else { // treat all other errors as fatal errors
+       panic(err)
+   }
+
+

+ Events +

+

+ Apart from emitting messages and delivery reports the client also communicates +with the application through a number of different event types. +An application may choose to handle or ignore these events. +

+

+ Consumer events +

+

+ * `*kafka.Message` - a fetched message. +

+

+ * `AssignedPartitions` - The assigned partition set for this client following a rebalance. +Requires `go.application.rebalance.enable` +

+

+ * `RevokedPartitions` - The counter part to `AssignedPartitions` following a rebalance. +`AssignedPartitions` and `RevokedPartitions` are symmetrical. +Requires `go.application.rebalance.enable` +

+

+ * `PartitionEOF` - Consumer has reached the end of a partition. +NOTE: The consumer will keep trying to fetch new messages for the partition. +

+

+ * `OffsetsCommitted` - Offset commit results (when `enable.auto.commit` is enabled). +

+

+ Producer events +

+

+ * `*kafka.Message` - delivery report for produced message. +Check `.TopicPartition.Error` for delivery result. +

+

+ Generic events for both Consumer and Producer +

+

+ * `KafkaError` - client (error codes are prefixed with _) or broker error. +These errors are normally just informational since the +client will try its best to automatically recover (eventually). +

+

+ * `OAuthBearerTokenRefresh` - retrieval of a new SASL/OAUTHBEARER token is required. +This event only occurs with sasl.mechanism=OAUTHBEARER. +Be sure to invoke SetOAuthBearerToken() on the Producer/Consumer/AdminClient +instance when a successful token retrieval is completed, otherwise be sure to +invoke SetOAuthBearerTokenFailure() to indicate that retrieval failed (or +if setting the token failed, which could happen if an extension doesn't meet +the required regular expression); invoking SetOAuthBearerTokenFailure() will +schedule a new event for 10 seconds later so another retrieval can be attempted. +

+

+ Hint: If your application registers a signal notification +(signal.Notify) makes sure the signals channel is buffered to avoid +possible complications with blocking Poll() calls. +

+

+ Note: The Confluent Kafka Go client is safe for concurrent use. +

+
+
+
+ +
+

+ Index ▾ +

+ +
+
+
+ + Constants + +
+
+ + func LibraryVersion() (int, string) + +
+
+ + func WriteErrorCodes(f *os.File) + +
+
+ + type AdminClient + +
+
+ + func NewAdminClient(conf *ConfigMap) (*AdminClient, error) + +
+
+ + func NewAdminClientFromConsumer(c *Consumer) (a *AdminClient, err error) + +
+
+ + func NewAdminClientFromProducer(p *Producer) (a *AdminClient, err error) + +
+
+ + func (a *AdminClient) AlterConfigs(ctx context.Context, resources []ConfigResource, options ...AlterConfigsAdminOption) (result []ConfigResourceResult, err error) + +
+
+ + func (a *AdminClient) Close() + +
+
+ + func (a *AdminClient) ClusterID(ctx context.Context) (clusterID string, err error) + +
+
+ + func (a *AdminClient) ControllerID(ctx context.Context) (controllerID int32, err error) + +
+
+ + func (a *AdminClient) CreatePartitions(ctx context.Context, partitions []PartitionsSpecification, options ...CreatePartitionsAdminOption) (result []TopicResult, err error) + +
+
+ + func (a *AdminClient) CreateTopics(ctx context.Context, topics []TopicSpecification, options ...CreateTopicsAdminOption) (result []TopicResult, err error) + +
+
+ + func (a *AdminClient) DeleteTopics(ctx context.Context, topics []string, options ...DeleteTopicsAdminOption) (result []TopicResult, err error) + +
+
+ + func (a *AdminClient) DescribeConfigs(ctx context.Context, resources []ConfigResource, options ...DescribeConfigsAdminOption) (result []ConfigResourceResult, err error) + +
+
+ + func (a *AdminClient) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) + +
+
+ + func (a *AdminClient) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error + +
+
+ + func (a *AdminClient) SetOAuthBearerTokenFailure(errstr string) error + +
+
+ + func (a *AdminClient) String() string + +
+
+ + type AdminOption + +
+
+ + type AdminOptionOperationTimeout + +
+
+ + func SetAdminOperationTimeout(t time.Duration) (ao AdminOptionOperationTimeout) + +
+
+ + type AdminOptionRequestTimeout + +
+
+ + func SetAdminRequestTimeout(t time.Duration) (ao AdminOptionRequestTimeout) + +
+
+ + type AdminOptionValidateOnly + +
+
+ + func SetAdminValidateOnly(validateOnly bool) (ao AdminOptionValidateOnly) + +
+
+ + type AlterConfigsAdminOption + +
+
+ + type AlterOperation + +
+
+ + func (o AlterOperation) String() string + +
+
+ + type AssignedPartitions + +
+
+ + func (e AssignedPartitions) String() string + +
+
+ + type BrokerMetadata + +
+
+ + type ConfigEntry + +
+
+ + func StringMapToConfigEntries(stringMap map[string]string, operation AlterOperation) []ConfigEntry + +
+
+ + func (c ConfigEntry) String() string + +
+
+ + type ConfigEntryResult + +
+
+ + func (c ConfigEntryResult) String() string + +
+
+ + type ConfigMap + +
+
+ + func (m ConfigMap) Get(key string, defval ConfigValue) (ConfigValue, error) + +
+
+ + func (m ConfigMap) Set(kv string) error + +
+
+ + func (m ConfigMap) SetKey(key string, value ConfigValue) error + +
+
+ + type ConfigResource + +
+
+ + func (c ConfigResource) String() string + +
+
+ + type ConfigResourceResult + +
+
+ + func (c ConfigResourceResult) String() string + +
+
+ + type ConfigSource + +
+
+ + func (t ConfigSource) String() string + +
+
+ + type ConfigValue + +
+
+ + type Consumer + +
+
+ + func NewConsumer(conf *ConfigMap) (*Consumer, error) + +
+
+ + func (c *Consumer) Assign(partitions []TopicPartition) (err error) + +
+
+ + func (c *Consumer) Assignment() (partitions []TopicPartition, err error) + +
+
+ + func (c *Consumer) AssignmentLost() bool + +
+
+ + func (c *Consumer) Close() (err error) + +
+
+ + func (c *Consumer) Commit() ([]TopicPartition, error) + +
+
+ + func (c *Consumer) CommitMessage(m *Message) ([]TopicPartition, error) + +
+
+ + func (c *Consumer) CommitOffsets(offsets []TopicPartition) ([]TopicPartition, error) + +
+
+ + func (c *Consumer) Committed(partitions []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) + +
+
+ + func (c *Consumer) Events() chan Event + +
+
+ + func (c *Consumer) GetConsumerGroupMetadata() (*ConsumerGroupMetadata, error) + +
+
+ + func (c *Consumer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) + +
+
+ + func (c *Consumer) GetRebalanceProtocol() string + +
+
+ + func (c *Consumer) GetWatermarkOffsets(topic string, partition int32) (low, high int64, err error) + +
+
+ + func (c *Consumer) IncrementalAssign(partitions []TopicPartition) (err error) + +
+
+ + func (c *Consumer) IncrementalUnassign(partitions []TopicPartition) (err error) + +
+
+ + func (c *Consumer) Logs() chan LogEvent + +
+
+ + func (c *Consumer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) + +
+
+ + func (c *Consumer) Pause(partitions []TopicPartition) (err error) + +
+
+ + func (c *Consumer) Poll(timeoutMs int) (event Event) + +
+
+ + func (c *Consumer) Position(partitions []TopicPartition) (offsets []TopicPartition, err error) + +
+
+ + func (c *Consumer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error) + +
+
+ + func (c *Consumer) ReadMessage(timeout time.Duration) (*Message, error) + +
+
+ + func (c *Consumer) Resume(partitions []TopicPartition) (err error) + +
+
+ + func (c *Consumer) Seek(partition TopicPartition, timeoutMs int) error + +
+
+ + func (c *Consumer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error + +
+
+ + func (c *Consumer) SetOAuthBearerTokenFailure(errstr string) error + +
+
+ + func (c *Consumer) StoreOffsets(offsets []TopicPartition) (storedOffsets []TopicPartition, err error) + +
+
+ + func (c *Consumer) String() string + +
+
+ + func (c *Consumer) Subscribe(topic string, rebalanceCb RebalanceCb) error + +
+
+ + func (c *Consumer) SubscribeTopics(topics []string, rebalanceCb RebalanceCb) (err error) + +
+
+ + func (c *Consumer) Subscription() (topics []string, err error) + +
+
+ + func (c *Consumer) Unassign() (err error) + +
+
+ + func (c *Consumer) Unsubscribe() (err error) + +
+
+ + type ConsumerGroupMetadata + +
+
+ + func NewTestConsumerGroupMetadata(groupID string) (*ConsumerGroupMetadata, error) + +
+
+ + type CreatePartitionsAdminOption + +
+
+ + type CreateTopicsAdminOption + +
+
+ + type DeleteTopicsAdminOption + +
+
+ + type DescribeConfigsAdminOption + +
+
+ + type Error + +
+
+ + func NewError(code ErrorCode, str string, fatal bool) (err Error) + +
+
+ + func (e Error) Code() ErrorCode + +
+
+ + func (e Error) Error() string + +
+
+ + func (e Error) IsFatal() bool + +
+
+ + func (e Error) IsRetriable() bool + +
+
+ + func (e Error) String() string + +
+
+ + func (e Error) TxnRequiresAbort() bool + +
+
+ + type ErrorCode + +
+
+ + func (c ErrorCode) String() string + +
+
+ + type Event + +
+
+ + type Handle + +
+
+ + type Header + +
+
+ + func (h Header) String() string + +
+
+ + type LogEvent + +
+
+ + func (logEvent LogEvent) String() string + +
+
+ + type Message + +
+
+ + func (m *Message) String() string + +
+
+ + type Metadata + +
+
+ + type OAuthBearerToken + +
+
+ + type OAuthBearerTokenRefresh + +
+
+ + func (o OAuthBearerTokenRefresh) String() string + +
+
+ + type Offset + +
+
+ + func NewOffset(offset interface{}) (Offset, error) + +
+
+ + func OffsetTail(relativeOffset Offset) Offset + +
+
+ + func (o *Offset) Set(offset interface{}) error + +
+
+ + func (o Offset) String() string + +
+
+ + type OffsetsCommitted + +
+
+ + func (o OffsetsCommitted) String() string + +
+
+ + type PartitionEOF + +
+
+ + func (p PartitionEOF) String() string + +
+
+ + type PartitionMetadata + +
+
+ + type PartitionsSpecification + +
+
+ + type Producer + +
+
+ + func NewProducer(conf *ConfigMap) (*Producer, error) + +
+
+ + func (p *Producer) AbortTransaction(ctx context.Context) error + +
+
+ + func (p *Producer) BeginTransaction() error + +
+
+ + func (p *Producer) Close() + +
+
+ + func (p *Producer) CommitTransaction(ctx context.Context) error + +
+
+ + func (p *Producer) Events() chan Event + +
+
+ + func (p *Producer) Flush(timeoutMs int) int + +
+
+ + func (p *Producer) GetFatalError() error + +
+
+ + func (p *Producer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) + +
+
+ + func (p *Producer) InitTransactions(ctx context.Context) error + +
+
+ + func (p *Producer) Len() int + +
+
+ + func (p *Producer) Logs() chan LogEvent + +
+
+ + func (p *Producer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) + +
+
+ + func (p *Producer) Produce(msg *Message, deliveryChan chan Event) error + +
+
+ + func (p *Producer) ProduceChannel() chan *Message + +
+
+ + func (p *Producer) Purge(flags int) error + +
+
+ + func (p *Producer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error) + +
+
+ + func (p *Producer) SendOffsetsToTransaction(ctx context.Context, offsets []TopicPartition, consumerMetadata *ConsumerGroupMetadata) error + +
+
+ + func (p *Producer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error + +
+
+ + func (p *Producer) SetOAuthBearerTokenFailure(errstr string) error + +
+
+ + func (p *Producer) String() string + +
+
+ + func (p *Producer) TestFatalError(code ErrorCode, str string) ErrorCode + +
+
+ + type RebalanceCb + +
+
+ + type ResourceType + +
+
+ + func ResourceTypeFromString(typeString string) (ResourceType, error) + +
+
+ + func (t ResourceType) String() string + +
+
+ + type RevokedPartitions + +
+
+ + func (e RevokedPartitions) String() string + +
+
+ + type Stats + +
+
+ + func (e Stats) String() string + +
+
+ + type TimestampType + +
+
+ + func (t TimestampType) String() string + +
+
+ + type TopicMetadata + +
+
+ + type TopicPartition + +
+
+ + func (p TopicPartition) String() string + +
+
+ + type TopicPartitions + +
+
+ + func (tps TopicPartitions) Len() int + +
+
+ + func (tps TopicPartitions) Less(i, j int) bool + +
+
+ + func (tps TopicPartitions) Swap(i, j int) + +
+
+ + type TopicResult + +
+
+ + func (t TopicResult) String() string + +
+
+ + type TopicSpecification + +
+
+
+ +

+ Package files +

+

+ + + 00version.go + + + adminapi.go + + + adminoptions.go + + + build_glibc_linux.go + + + config.go + + + consumer.go + + + context.go + + + error.go + + + error_gen.go + + + event.go + + + generated_errors.go + + + handle.go + + + header.go + + + kafka.go + + + log.go + + + message.go + + + metadata.go + + + misc.go + + + offset.go + + + producer.go + + + testhelpers.go + + + time.go + + +

+
+ +
+ +

+ Constants +

+
const (
+    // ResourceUnknown - Unknown
+    ResourceUnknown = ResourceType(C.RD_KAFKA_RESOURCE_UNKNOWN)
+    // ResourceAny - match any resource type (DescribeConfigs)
+    ResourceAny = ResourceType(C.RD_KAFKA_RESOURCE_ANY)
+    // ResourceTopic - Topic
+    ResourceTopic = ResourceType(C.RD_KAFKA_RESOURCE_TOPIC)
+    // ResourceGroup - Group
+    ResourceGroup = ResourceType(C.RD_KAFKA_RESOURCE_GROUP)
+    // ResourceBroker - Broker
+    ResourceBroker = ResourceType(C.RD_KAFKA_RESOURCE_BROKER)
+)
+
const (
+    // ConfigSourceUnknown is the default value
+    ConfigSourceUnknown = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_UNKNOWN_CONFIG)
+    // ConfigSourceDynamicTopic is dynamic topic config that is configured for a specific topic
+    ConfigSourceDynamicTopic = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_TOPIC_CONFIG)
+    // ConfigSourceDynamicBroker is dynamic broker config that is configured for a specific broker
+    ConfigSourceDynamicBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_BROKER_CONFIG)
+    // ConfigSourceDynamicDefaultBroker is dynamic broker config that is configured as default for all brokers in the cluster
+    ConfigSourceDynamicDefaultBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DYNAMIC_DEFAULT_BROKER_CONFIG)
+    // ConfigSourceStaticBroker is static broker config provided as broker properties at startup (e.g. from server.properties file)
+    ConfigSourceStaticBroker = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_STATIC_BROKER_CONFIG)
+    // ConfigSourceDefault is built-in default configuration for configs that have a default value
+    ConfigSourceDefault = ConfigSource(C.RD_KAFKA_CONFIG_SOURCE_DEFAULT_CONFIG)
+)
+
const (
+    // TimestampNotAvailable indicates no timestamp was set, or not available due to lacking broker support
+    TimestampNotAvailable = TimestampType(C.RD_KAFKA_TIMESTAMP_NOT_AVAILABLE)
+    // TimestampCreateTime indicates timestamp set by producer (source time)
+    TimestampCreateTime = TimestampType(C.RD_KAFKA_TIMESTAMP_CREATE_TIME)
+    // TimestampLogAppendTime indicates timestamp set set by broker (store time)
+    TimestampLogAppendTime = TimestampType(C.RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME)
+)
+
const (
+    // PurgeInFlight purges messages in-flight to or from the broker.
+    // Purging these messages will void any future acknowledgements from the
+    // broker, making it impossible for the application to know if these
+    // messages were successfully delivered or not.
+    // Retrying these messages may lead to duplicates.
+    PurgeInFlight = int(C.RD_KAFKA_PURGE_F_INFLIGHT)
+
+    // PurgeQueue Purge messages in internal queues.
+    PurgeQueue = int(C.RD_KAFKA_PURGE_F_QUEUE)
+
+    // PurgeNonBlocking Don't wait for background thread queue purging to finish.
+    PurgeNonBlocking = int(C.RD_KAFKA_PURGE_F_NON_BLOCKING)
+)
+
const (
+    // AlterOperationSet sets/overwrites the configuration setting.
+    AlterOperationSet = iota
+)
+

+ LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +

+
const LibrdkafkaLinkInfo = "static glibc_linux from librdkafka-static-bundle-v1.8.2.tgz"
+

+ OffsetBeginning represents the earliest offset (logical) +

+
const OffsetBeginning = Offset(C.RD_KAFKA_OFFSET_BEGINNING)
+

+ OffsetEnd represents the latest offset (logical) +

+
const OffsetEnd = Offset(C.RD_KAFKA_OFFSET_END)
+

+ OffsetInvalid represents an invalid/unspecified offset +

+
const OffsetInvalid = Offset(C.RD_KAFKA_OFFSET_INVALID)
+

+ OffsetStored represents a stored offset +

+
const OffsetStored = Offset(C.RD_KAFKA_OFFSET_STORED)
+

+ PartitionAny represents any partition (for partitioning), +or unspecified value (for all other cases) +

+
const PartitionAny = int32(C.RD_KAFKA_PARTITION_UA)
+

+ func + + LibraryVersion + + +

+
func LibraryVersion() (int, string)
+

+ LibraryVersion returns the underlying librdkafka library version as a +(version_int, version_str) tuple. +

+

+ func + + WriteErrorCodes + + +

+
func WriteErrorCodes(f *os.File)
+

+ WriteErrorCodes writes Go error code constants to file from the +librdkafka error codes. +This function is not intended for public use. +

+

+ type + + AdminClient + + +

+

+ AdminClient is derived from an existing Producer or Consumer +

+
type AdminClient struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + NewAdminClient + + +

+
func NewAdminClient(conf *ConfigMap) (*AdminClient, error)
+

+ NewAdminClient creats a new AdminClient instance with a new underlying client instance +

+

+ func + + NewAdminClientFromConsumer + + +

+
func NewAdminClientFromConsumer(c *Consumer) (a *AdminClient, err error)
+

+ NewAdminClientFromConsumer derives a new AdminClient from an existing Consumer instance. +The AdminClient will use the same configuration and connections as the parent instance. +

+

+ func + + NewAdminClientFromProducer + + +

+
func NewAdminClientFromProducer(p *Producer) (a *AdminClient, err error)
+

+ NewAdminClientFromProducer derives a new AdminClient from an existing Producer instance. +The AdminClient will use the same configuration and connections as the parent instance. +

+

+ func (*AdminClient) + + AlterConfigs + + +

+
func (a *AdminClient) AlterConfigs(ctx context.Context, resources []ConfigResource, options ...AlterConfigsAdminOption) (result []ConfigResourceResult, err error)
+

+ AlterConfigs alters/updates cluster resource configuration. +

+

+ Updates are not transactional so they may succeed for a subset +of the provided resources while others fail. +The configuration for a particular resource is updated atomically, +replacing values using the provided ConfigEntrys and reverting +unspecified ConfigEntrys to their default values. +

+

+ Requires broker version >=0.11.0.0 +

+

+ AlterConfigs will replace all existing configuration for +the provided resources with the new configuration given, +reverting all other configuration to their default values. +

+

+ Multiple resources and resource types may be set, but at most one +resource of type ResourceBroker is allowed per call since these +resource requests must be sent to the broker specified in the resource. +

+

+ func (*AdminClient) + + Close + + +

+
func (a *AdminClient) Close()
+

+ Close an AdminClient instance. +

+

+ func (*AdminClient) + + ClusterID + + +

+
func (a *AdminClient) ClusterID(ctx context.Context) (clusterID string, err error)
+

+ ClusterID returns the cluster ID as reported in broker metadata. +

+

+ Note on cancellation: Although the underlying C function respects the +timeout, it currently cannot be manually cancelled. That means manually +cancelling the context will block until the C function call returns. +

+

+ Requires broker version >= 0.10.0. +

+

+ func (*AdminClient) + + ControllerID + + +

+
func (a *AdminClient) ControllerID(ctx context.Context) (controllerID int32, err error)
+

+ ControllerID returns the broker ID of the current controller as reported in +broker metadata. +

+

+ Note on cancellation: Although the underlying C function respects the +timeout, it currently cannot be manually cancelled. That means manually +cancelling the context will block until the C function call returns. +

+

+ Requires broker version >= 0.10.0. +

+

+ func (*AdminClient) + + CreatePartitions + + +

+
func (a *AdminClient) CreatePartitions(ctx context.Context, partitions []PartitionsSpecification, options ...CreatePartitionsAdminOption) (result []TopicResult, err error)
+

+ CreatePartitions creates additional partitions for topics. +

+

+ func (*AdminClient) + + CreateTopics + + +

+
func (a *AdminClient) CreateTopics(ctx context.Context, topics []TopicSpecification, options ...CreateTopicsAdminOption) (result []TopicResult, err error)
+

+ CreateTopics creates topics in cluster. +

+

+ The list of TopicSpecification objects define the per-topic partition count, replicas, etc. +

+

+ Topic creation is non-atomic and may succeed for some topics but fail for others, +make sure to check the result for topic-specific errors. +

+

+ Note: TopicSpecification is analogous to NewTopic in the Java Topic Admin API. +

+

+ func (*AdminClient) + + DeleteTopics + + +

+
func (a *AdminClient) DeleteTopics(ctx context.Context, topics []string, options ...DeleteTopicsAdminOption) (result []TopicResult, err error)
+

+ DeleteTopics deletes a batch of topics. +

+

+ This operation is not transactional and may succeed for a subset of topics while +failing others. +It may take several seconds after the DeleteTopics result returns success for +all the brokers to become aware that the topics are gone. During this time, +topic metadata and configuration may continue to return information about deleted topics. +

+

+ Requires broker version >= 0.10.1.0 +

+

+ func (*AdminClient) + + DescribeConfigs + + +

+
func (a *AdminClient) DescribeConfigs(ctx context.Context, resources []ConfigResource, options ...DescribeConfigsAdminOption) (result []ConfigResourceResult, err error)
+

+ DescribeConfigs retrieves configuration for cluster resources. +

+

+ The returned configuration includes default values, use +ConfigEntryResult.IsDefault or ConfigEntryResult.Source to distinguish +default values from manually configured settings. +

+

+ The value of config entries where .IsSensitive is true +will always be nil to avoid disclosing sensitive +information, such as security settings. +

+

+ Configuration entries where .IsReadOnly is true can't be modified +(with AlterConfigs). +

+

+ Synonym configuration entries are returned if the broker supports +it (broker version >= 1.1.0). See .Synonyms. +

+

+ Requires broker version >=0.11.0.0 +

+

+ Multiple resources and resource types may be requested, but at most +one resource of type ResourceBroker is allowed per call +since these resource requests must be sent to the broker specified +in the resource. +

+

+ func (*AdminClient) + + GetMetadata + + +

+
func (a *AdminClient) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error)
+

+ GetMetadata queries broker for cluster and topic metadata. +If topic is non-nil only information about that topic is returned, else if +allTopics is false only information about locally used topics is returned, +else information about all topics is returned. +GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +

+

+ func (*AdminClient) + + SetOAuthBearerToken + + +

+
func (a *AdminClient) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error
+

+ SetOAuthBearerToken sets the the data to be transmitted +to a broker during SASL/OAUTHBEARER authentication. It will return nil +on success, otherwise an error if: +1) the token data is invalid (meaning an expiration time in the past +or either a token value or an extension key or value that does not meet +the regular expression requirements as per + + https://tools.ietf.org/html/rfc7628#section-3.1 + + ); +2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +3) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*AdminClient) + + SetOAuthBearerTokenFailure + + +

+
func (a *AdminClient) SetOAuthBearerTokenFailure(errstr string) error
+

+ SetOAuthBearerTokenFailure sets the error message describing why token +retrieval/setting failed; it also schedules a new token refresh event for 10 +seconds later so the attempt may be retried. It will return nil on +success, otherwise an error if: +1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +2) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*AdminClient) + + String + + +

+
func (a *AdminClient) String() string
+

+ String returns a human readable name for an AdminClient instance +

+

+ type + + AdminOption + + +

+

+ AdminOption is a generic type not to be used directly. +

+

+ See CreateTopicsAdminOption et.al. +

+
type AdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + AdminOptionOperationTimeout + + +

+

+ AdminOptionOperationTimeout sets the broker's operation timeout, such as the +timeout for CreateTopics to complete the creation of topics on the controller +before returning a result to the application. +

+

+ CreateTopics, DeleteTopics, CreatePartitions: +a value 0 will return immediately after triggering topic +creation, while > 0 will wait this long for topic creation to propagate +in cluster. +

+

+ Default: 0 (return immediately). +

+

+ Valid for CreateTopics, DeleteTopics, CreatePartitions. +

+
type AdminOptionOperationTimeout struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + SetAdminOperationTimeout + + +

+
func SetAdminOperationTimeout(t time.Duration) (ao AdminOptionOperationTimeout)
+

+ SetAdminOperationTimeout sets the broker's operation timeout, such as the +timeout for CreateTopics to complete the creation of topics on the controller +before returning a result to the application. +

+

+ CreateTopics, DeleteTopics, CreatePartitions: +a value 0 will return immediately after triggering topic +creation, while > 0 will wait this long for topic creation to propagate +in cluster. +

+

+ Default: 0 (return immediately). +

+

+ Valid for CreateTopics, DeleteTopics, CreatePartitions. +

+

+ type + + AdminOptionRequestTimeout + + +

+

+ AdminOptionRequestTimeout sets the overall request timeout, including broker +lookup, request transmission, operation time on broker, and response. +

+

+ Default: `socket.timeout.ms`. +

+

+ Valid for all Admin API methods. +

+
type AdminOptionRequestTimeout struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + SetAdminRequestTimeout + + +

+
func SetAdminRequestTimeout(t time.Duration) (ao AdminOptionRequestTimeout)
+

+ SetAdminRequestTimeout sets the overall request timeout, including broker +lookup, request transmission, operation time on broker, and response. +

+

+ Default: `socket.timeout.ms`. +

+

+ Valid for all Admin API methods. +

+

+ type + + AdminOptionValidateOnly + + +

+

+ AdminOptionValidateOnly tells the broker to only validate the request, +without performing the requested operation (create topics, etc). +

+

+ Default: false. +

+

+ Valid for CreateTopics, CreatePartitions, AlterConfigs +

+
type AdminOptionValidateOnly struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + SetAdminValidateOnly + + +

+
func SetAdminValidateOnly(validateOnly bool) (ao AdminOptionValidateOnly)
+

+ SetAdminValidateOnly tells the broker to only validate the request, +without performing the requested operation (create topics, etc). +

+

+ Default: false. +

+

+ Valid for CreateTopics, DeleteTopics, CreatePartitions, AlterConfigs +

+

+ type + + AlterConfigsAdminOption + + +

+

+ AlterConfigsAdminOption - see setters. +

+

+ See SetAdminRequestTimeout, SetAdminValidateOnly, SetAdminIncremental. +

+
type AlterConfigsAdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + AlterOperation + + +

+

+ AlterOperation specifies the operation to perform on the ConfigEntry. +Currently only AlterOperationSet. +

+
type AlterOperation int
+

+ func (AlterOperation) + + String + + +

+
func (o AlterOperation) String() string
+

+ String returns the human-readable representation of an AlterOperation +

+

+ type + + AssignedPartitions + + +

+

+ AssignedPartitions consumer group rebalance event: assigned partition set +

+
type AssignedPartitions struct {
+    Partitions []TopicPartition
+}
+
+

+ func (AssignedPartitions) + + String + + +

+
func (e AssignedPartitions) String() string
+

+ type + + BrokerMetadata + + +

+

+ BrokerMetadata contains per-broker metadata +

+
type BrokerMetadata struct {
+    ID   int32
+    Host string
+    Port int
+}
+
+

+ type + + ConfigEntry + + +

+

+ ConfigEntry holds parameters for altering a resource's configuration. +

+
type ConfigEntry struct {
+    // Name of configuration entry, e.g., topic configuration property name.
+    Name string
+    // Value of configuration entry.
+    Value string
+    // Operation to perform on the entry.
+    Operation AlterOperation
+}
+
+

+ func + + StringMapToConfigEntries + + +

+
func StringMapToConfigEntries(stringMap map[string]string, operation AlterOperation) []ConfigEntry
+

+ StringMapToConfigEntries creates a new map of ConfigEntry objects from the +provided string map. The AlterOperation is set on each created entry. +

+

+ func (ConfigEntry) + + String + + +

+
func (c ConfigEntry) String() string
+

+ String returns a human-readable representation of a ConfigEntry. +

+

+ type + + ConfigEntryResult + + +

+

+ ConfigEntryResult contains the result of a single configuration entry from a +DescribeConfigs request. +

+
type ConfigEntryResult struct {
+    // Name of configuration entry, e.g., topic configuration property name.
+    Name string
+    // Value of configuration entry.
+    Value string
+    // Source indicates the configuration source.
+    Source ConfigSource
+    // IsReadOnly indicates whether the configuration entry can be altered.
+    IsReadOnly bool
+    // IsSensitive indicates whether the configuration entry contains sensitive information, in which case the value will be unset.
+    IsSensitive bool
+    // IsSynonym indicates whether the configuration entry is a synonym for another configuration property.
+    IsSynonym bool
+    // Synonyms contains a map of configuration entries that are synonyms to this configuration entry.
+    Synonyms map[string]ConfigEntryResult
+}
+
+

+ func (ConfigEntryResult) + + String + + +

+
func (c ConfigEntryResult) String() string
+

+ String returns a human-readable representation of a ConfigEntryResult. +

+

+ type + + ConfigMap + + +

+

+ ConfigMap is a map containing standard librdkafka configuration properties as documented in: + + https://github.com/edenhill/librdkafka/tree/master/CONFIGURATION.md + +

+

+ The special property "default.topic.config" (optional) is a ConfigMap +containing default topic configuration properties. +

+

+ The use of "default.topic.config" is deprecated, +topic configuration properties shall be specified in the standard ConfigMap. +For backwards compatibility, "default.topic.config" (if supplied) +takes precedence. +

+
type ConfigMap map[string]ConfigValue
+

+ func (ConfigMap) + + Get + + +

+
func (m ConfigMap) Get(key string, defval ConfigValue) (ConfigValue, error)
+

+ Get finds the given key in the ConfigMap and returns its value. +If the key is not found `defval` is returned. +If the key is found but the type does not match that of `defval` (unless nil) +an ErrInvalidArg error is returned. +

+

+ func (ConfigMap) + + Set + + +

+
func (m ConfigMap) Set(kv string) error
+

+ Set implements flag.Set (command line argument parser) as a convenience +for `-X key=value` config. +

+

+ func (ConfigMap) + + SetKey + + +

+
func (m ConfigMap) SetKey(key string, value ConfigValue) error
+

+ SetKey sets configuration property key to value. +

+

+ For user convenience a key prefixed with {topic}. will be +set on the "default.topic.config" sub-map, this use is deprecated. +

+

+ type + + ConfigResource + + +

+

+ ConfigResource holds parameters for altering an Apache Kafka configuration resource +

+
type ConfigResource struct {
+    // Type of resource to set.
+    Type ResourceType
+    // Name of resource to set.
+    Name string
+    // Config entries to set.
+    // Configuration updates are atomic, any configuration property not provided
+    // here will be reverted (by the broker) to its default value.
+    // Use DescribeConfigs to retrieve the list of current configuration entry values.
+    Config []ConfigEntry
+}
+
+

+ func (ConfigResource) + + String + + +

+
func (c ConfigResource) String() string
+

+ String returns a human-readable representation of a ConfigResource +

+

+ type + + ConfigResourceResult + + +

+

+ ConfigResourceResult provides the result for a resource from a AlterConfigs or +DescribeConfigs request. +

+
type ConfigResourceResult struct {
+    // Type of returned result resource.
+    Type ResourceType
+    // Name of returned result resource.
+    Name string
+    // Error, if any, of returned result resource.
+    Error Error
+    // Config entries, if any, of returned result resource.
+    Config map[string]ConfigEntryResult
+}
+
+

+ func (ConfigResourceResult) + + String + + +

+
func (c ConfigResourceResult) String() string
+

+ String returns a human-readable representation of a ConfigResourceResult. +

+

+ type + + ConfigSource + + +

+

+ ConfigSource represents an Apache Kafka config source +

+
type ConfigSource int
+

+ func (ConfigSource) + + String + + +

+
func (t ConfigSource) String() string
+

+ String returns the human-readable representation of a ConfigSource type +

+

+ type + + ConfigValue + + +

+

+ ConfigValue supports the following types: +

+
bool, int, string, any type with the standard String() interface
+
+
type ConfigValue interface{}
+

+ type + + Consumer + + +

+

+ Consumer implements a High-level Apache Kafka Consumer instance +

+
type Consumer struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + NewConsumer + + +

+
func NewConsumer(conf *ConfigMap) (*Consumer, error)
+

+ NewConsumer creates a new high-level Consumer instance. +

+

+ conf is a *ConfigMap with standard librdkafka configuration properties. +

+

+ Supported special configuration properties: +

+
go.application.rebalance.enable (bool, false) - Forward rebalancing responsibility to application via the Events() channel.
+                                     If set to true the app must handle the AssignedPartitions and
+                                     RevokedPartitions events and call Assign() and Unassign()
+                                     respectively.
+go.events.channel.enable (bool, false) - [deprecated] Enable the Events() channel. Messages and events will be pushed on the Events() channel and the Poll() interface will be disabled.
+go.events.channel.size (int, 1000) - Events() channel size
+go.logs.channel.enable (bool, false) - Forward log to Logs() channel.
+go.logs.channel (chan kafka.LogEvent, nil) - Forward logs to application-provided channel instead of Logs(). Requires go.logs.channel.enable=true.
+
+

+ WARNING: Due to the buffering nature of channels (and queues in general) the +use of the events channel risks receiving outdated events and +messages. Minimizing go.events.channel.size reduces the risk +and number of outdated events and messages but does not eliminate +the factor completely. With a channel size of 1 at most one +event or message may be outdated. +

+

+ func (*Consumer) + + Assign + + +

+
func (c *Consumer) Assign(partitions []TopicPartition) (err error)
+

+ Assign an atomic set of partitions to consume. +

+

+ The .Offset field of each TopicPartition must either be set to an absolute +starting offset (>= 0), or one of the logical offsets (`kafka.OffsetEnd` etc), +but should typically be set to `kafka.OffsetStored` to have the consumer +use the committed offset as a start position, with a fallback to +`auto.offset.reset` if there is no committed offset. +

+

+ This replaces the current assignment. +

+

+ func (*Consumer) + + Assignment + + +

+
func (c *Consumer) Assignment() (partitions []TopicPartition, err error)
+

+ Assignment returns the current partition assignments +

+

+ func (*Consumer) + + AssignmentLost + + +

+
func (c *Consumer) AssignmentLost() bool
+

+ AssignmentLost returns true if current partition assignment has been lost. +This method is only applicable for use with a subscribing consumer when +handling a rebalance event or callback. +Partitions that have been lost may already be owned by other members in the +group and therefore commiting offsets, for example, may fail. +

+

+ func (*Consumer) + + Close + + +

+
func (c *Consumer) Close() (err error)
+

+ Close Consumer instance. +The object is no longer usable after this call. +

+

+ func (*Consumer) + + Commit + + +

+
func (c *Consumer) Commit() ([]TopicPartition, error)
+

+ Commit offsets for currently assigned partitions +This is a blocking call. +Returns the committed offsets on success. +

+

+ func (*Consumer) + + CommitMessage + + +

+
func (c *Consumer) CommitMessage(m *Message) ([]TopicPartition, error)
+

+ CommitMessage commits offset based on the provided message. +This is a blocking call. +Returns the committed offsets on success. +

+

+ func (*Consumer) + + CommitOffsets + + +

+
func (c *Consumer) CommitOffsets(offsets []TopicPartition) ([]TopicPartition, error)
+

+ CommitOffsets commits the provided list of offsets +This is a blocking call. +Returns the committed offsets on success. +

+

+ func (*Consumer) + + Committed + + +

+
func (c *Consumer) Committed(partitions []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error)
+

+ Committed retrieves committed offsets for the given set of partitions +

+

+ func (*Consumer) + + Events + + +

+
func (c *Consumer) Events() chan Event
+

+ Events returns the Events channel (if enabled) +

+

+ func (*Consumer) + + GetConsumerGroupMetadata + + +

+
func (c *Consumer) GetConsumerGroupMetadata() (*ConsumerGroupMetadata, error)
+

+ GetConsumerGroupMetadata returns the consumer's current group metadata. +This object should be passed to the transactional producer's +SendOffsetsToTransaction() API. +

+

+ func (*Consumer) + + GetMetadata + + +

+
func (c *Consumer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error)
+

+ GetMetadata queries broker for cluster and topic metadata. +If topic is non-nil only information about that topic is returned, else if +allTopics is false only information about locally used topics is returned, +else information about all topics is returned. +GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +

+

+ func (*Consumer) + + GetRebalanceProtocol + + +

+
func (c *Consumer) GetRebalanceProtocol() string
+

+ GetRebalanceProtocol returns the current consumer group rebalance protocol, +which is either "EAGER" or "COOPERATIVE". +If the rebalance protocol is not known in the current state an empty string +is returned. +Should typically only be called during rebalancing. +

+

+ func (*Consumer) + + GetWatermarkOffsets + + +

+
func (c *Consumer) GetWatermarkOffsets(topic string, partition int32) (low, high int64, err error)
+

+ GetWatermarkOffsets returns the cached low and high offsets for the given topic +and partition. The high offset is populated on every fetch response or via calling QueryWatermarkOffsets. +The low offset is populated every statistics.interval.ms if that value is set. +OffsetInvalid will be returned if there is no cached offset for either value. +

+

+ func (*Consumer) + + IncrementalAssign + + +

+
func (c *Consumer) IncrementalAssign(partitions []TopicPartition) (err error)
+

+ IncrementalAssign adds the specified partitions to the current set of +partitions to consume. +

+

+ The .Offset field of each TopicPartition must either be set to an absolute +starting offset (>= 0), or one of the logical offsets (`kafka.OffsetEnd` etc), +but should typically be set to `kafka.OffsetStored` to have the consumer +use the committed offset as a start position, with a fallback to +`auto.offset.reset` if there is no committed offset. +

+

+ The new partitions must not be part of the current assignment. +

+

+ func (*Consumer) + + IncrementalUnassign + + +

+
func (c *Consumer) IncrementalUnassign(partitions []TopicPartition) (err error)
+

+ IncrementalUnassign removes the specified partitions from the current set of +partitions to consume. +

+

+ The .Offset field of the TopicPartition is ignored. +

+

+ The removed partitions must be part of the current assignment. +

+

+ func (*Consumer) + + Logs + + +

+
func (c *Consumer) Logs() chan LogEvent
+

+ Logs returns the log channel if enabled, or nil otherwise. +

+

+ func (*Consumer) + + OffsetsForTimes + + +

+
func (c *Consumer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error)
+

+ OffsetsForTimes looks up offsets by timestamp for the given partitions. +

+

+ The returned offset for each partition is the earliest offset whose +timestamp is greater than or equal to the given timestamp in the +corresponding partition. If the provided timestamp exceeds that of the +last message in the partition, a value of -1 will be returned. +

+

+ The timestamps to query are represented as `.Offset` in the `times` +argument and the looked up offsets are represented as `.Offset` in the returned +`offsets` list. +

+

+ The function will block for at most timeoutMs milliseconds. +

+

+ Duplicate Topic+Partitions are not supported. +Per-partition errors may be returned in the `.Error` field. +

+

+ func (*Consumer) + + Pause + + +

+
func (c *Consumer) Pause(partitions []TopicPartition) (err error)
+

+ Pause consumption for the provided list of partitions +

+

+ Note that messages already enqueued on the consumer's Event channel +(if `go.events.channel.enable` has been set) will NOT be purged by +this call, set `go.events.channel.size` accordingly. +

+

+ func (*Consumer) + + Poll + + +

+
func (c *Consumer) Poll(timeoutMs int) (event Event)
+

+ Poll the consumer for messages or events. +

+

+ Will block for at most timeoutMs milliseconds +

+

+ The following callbacks may be triggered: +

+
Subscribe()'s rebalanceCb
+
+

+ Returns nil on timeout, else an Event +

+

+ func (*Consumer) + + Position + + +

+
func (c *Consumer) Position(partitions []TopicPartition) (offsets []TopicPartition, err error)
+

+ Position returns the current consume position for the given partitions. +Typical use is to call Assignment() to get the partition list +and then pass it to Position() to get the current consume position for +each of the assigned partitions. +The consume position is the next message to read from the partition. +i.e., the offset of the last message seen by the application + 1. +

+

+ func (*Consumer) + + QueryWatermarkOffsets + + +

+
func (c *Consumer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error)
+

+ QueryWatermarkOffsets queries the broker for the low and high offsets for the given topic and partition. +

+

+ func (*Consumer) + + ReadMessage + + +

+
func (c *Consumer) ReadMessage(timeout time.Duration) (*Message, error)
+

+ ReadMessage polls the consumer for a message. +

+

+ This is a convenience API that wraps Poll() and only returns +messages or errors. All other event types are discarded. +

+

+ The call will block for at most `timeout` waiting for +a new message or error. `timeout` may be set to -1 for +indefinite wait. +

+

+ Timeout is returned as (nil, err) where err is `err.(kafka.Error).Code() == kafka.ErrTimedOut`. +

+

+ Messages are returned as (msg, nil), +while general errors are returned as (nil, err), +and partition-specific errors are returned as (msg, err) where +msg.TopicPartition provides partition-specific information (such as topic, partition and offset). +

+

+ All other event types, such as PartitionEOF, AssignedPartitions, etc, are silently discarded. +

+

+ func (*Consumer) + + Resume + + +

+
func (c *Consumer) Resume(partitions []TopicPartition) (err error)
+

+ Resume consumption for the provided list of partitions +

+

+ func (*Consumer) + + Seek + + +

+
func (c *Consumer) Seek(partition TopicPartition, timeoutMs int) error
+

+ Seek seeks the given topic partitions using the offset from the TopicPartition. +

+

+ If timeoutMs is not 0 the call will wait this long for the +seek to be performed. If the timeout is reached the internal state +will be unknown and this function returns ErrTimedOut. +If timeoutMs is 0 it will initiate the seek but return +immediately without any error reporting (e.g., async). +

+

+ Seek() may only be used for partitions already being consumed +(through Assign() or implicitly through a self-rebalanced Subscribe()). +To set the starting offset it is preferred to use Assign() and provide +a starting offset for each partition. +

+

+ Returns an error on failure or nil otherwise. +

+

+ func (*Consumer) + + SetOAuthBearerToken + + +

+
func (c *Consumer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error
+

+ SetOAuthBearerToken sets the the data to be transmitted +to a broker during SASL/OAUTHBEARER authentication. It will return nil +on success, otherwise an error if: +1) the token data is invalid (meaning an expiration time in the past +or either a token value or an extension key or value that does not meet +the regular expression requirements as per + + https://tools.ietf.org/html/rfc7628#section-3.1 + + ); +2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +3) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*Consumer) + + SetOAuthBearerTokenFailure + + +

+
func (c *Consumer) SetOAuthBearerTokenFailure(errstr string) error
+

+ SetOAuthBearerTokenFailure sets the error message describing why token +retrieval/setting failed; it also schedules a new token refresh event for 10 +seconds later so the attempt may be retried. It will return nil on +success, otherwise an error if: +1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +2) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*Consumer) + + StoreOffsets + + +

+
func (c *Consumer) StoreOffsets(offsets []TopicPartition) (storedOffsets []TopicPartition, err error)
+

+ StoreOffsets stores the provided list of offsets that will be committed +to the offset store according to `auto.commit.interval.ms` or manual +offset-less Commit(). +

+

+ Returns the stored offsets on success. If at least one offset couldn't be stored, +an error and a list of offsets is returned. Each offset can be checked for +specific errors via its `.Error` member. +

+

+ func (*Consumer) + + String + + +

+
func (c *Consumer) String() string
+

+ Strings returns a human readable name for a Consumer instance +

+

+ func (*Consumer) + + Subscribe + + +

+
func (c *Consumer) Subscribe(topic string, rebalanceCb RebalanceCb) error
+

+ Subscribe to a single topic +This replaces the current subscription +

+

+ func (*Consumer) + + SubscribeTopics + + +

+
func (c *Consumer) SubscribeTopics(topics []string, rebalanceCb RebalanceCb) (err error)
+

+ SubscribeTopics subscribes to the provided list of topics. +This replaces the current subscription. +

+

+ func (*Consumer) + + Subscription + + +

+
func (c *Consumer) Subscription() (topics []string, err error)
+

+ Subscription returns the current subscription as set by Subscribe() +

+

+ func (*Consumer) + + Unassign + + +

+
func (c *Consumer) Unassign() (err error)
+

+ Unassign the current set of partitions to consume. +

+

+ func (*Consumer) + + Unsubscribe + + +

+
func (c *Consumer) Unsubscribe() (err error)
+

+ Unsubscribe from the current subscription, if any. +

+

+ type + + ConsumerGroupMetadata + + +

+

+ ConsumerGroupMetadata reflects the current consumer group member metadata. +

+
type ConsumerGroupMetadata struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + NewTestConsumerGroupMetadata + + +

+
func NewTestConsumerGroupMetadata(groupID string) (*ConsumerGroupMetadata, error)
+

+ NewTestConsumerGroupMetadata creates a new consumer group metadata instance +mainly for testing use. +Use GetConsumerGroupMetadata() to retrieve the real metadata. +

+

+ type + + CreatePartitionsAdminOption + + +

+

+ CreatePartitionsAdminOption - see setters. +

+

+ See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. +

+
type CreatePartitionsAdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + CreateTopicsAdminOption + + +

+

+ CreateTopicsAdminOption - see setters. +

+

+ See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. +

+
type CreateTopicsAdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + DeleteTopicsAdminOption + + +

+

+ DeleteTopicsAdminOption - see setters. +

+

+ See SetAdminRequestTimeout, SetAdminOperationTimeout. +

+
type DeleteTopicsAdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + DescribeConfigsAdminOption + + +

+

+ DescribeConfigsAdminOption - see setters. +

+

+ See SetAdminRequestTimeout. +

+
type DescribeConfigsAdminOption interface {
+    // contains filtered or unexported methods
+}
+

+ type + + Error + + +

+

+ Error provides a Kafka-specific error container +

+
type Error struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + NewError + + +

+
func NewError(code ErrorCode, str string, fatal bool) (err Error)
+

+ NewError creates a new Error. +

+

+ func (Error) + + Code + + +

+
func (e Error) Code() ErrorCode
+

+ Code returns the ErrorCode of an Error +

+

+ func (Error) + + Error + + +

+
func (e Error) Error() string
+

+ Error returns a human readable representation of an Error +Same as Error.String() +

+

+ func (Error) + + IsFatal + + +

+
func (e Error) IsFatal() bool
+

+ IsFatal returns true if the error is a fatal error. +A fatal error indicates the client instance is no longer operable and +should be terminated. Typical causes include non-recoverable +idempotent producer errors. +

+

+ func (Error) + + IsRetriable + + +

+
func (e Error) IsRetriable() bool
+

+ IsRetriable returns true if the operation that caused this error +may be retried. +This flag is currently only set by the Transactional producer API. +

+

+ func (Error) + + String + + +

+
func (e Error) String() string
+

+ String returns a human readable representation of an Error +

+

+ func (Error) + + TxnRequiresAbort + + +

+
func (e Error) TxnRequiresAbort() bool
+

+ TxnRequiresAbort returns true if the error is an abortable transaction error +that requires the application to abort the current transaction with +AbortTransaction() and start a new transaction with BeginTransaction() +if it wishes to proceed with transactional operations. +This flag is only set by the Transactional producer API. +

+

+ type + + ErrorCode + + +

+

+ ErrorCode is the integer representation of local and broker error codes +

+
type ErrorCode int
+
const (
+    // ErrBadMsg Local: Bad message format
+    ErrBadMsg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__BAD_MSG)
+    // ErrBadCompression Local: Invalid compressed data
+    ErrBadCompression ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__BAD_COMPRESSION)
+    // ErrDestroy Local: Broker handle destroyed
+    ErrDestroy ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__DESTROY)
+    // ErrFail Local: Communication failure with broker
+    ErrFail ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FAIL)
+    // ErrTransport Local: Broker transport failure
+    ErrTransport ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TRANSPORT)
+    // ErrCritSysResource Local: Critical system resource failure
+    ErrCritSysResource ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE)
+    // ErrResolve Local: Host resolution failure
+    ErrResolve ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__RESOLVE)
+    // ErrMsgTimedOut Local: Message timed out
+    ErrMsgTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__MSG_TIMED_OUT)
+    // ErrPartitionEOF Broker: No more messages
+    ErrPartitionEOF ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PARTITION_EOF)
+    // ErrUnknownPartition Local: Unknown partition
+    ErrUnknownPartition ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION)
+    // ErrFs Local: File or filesystem error
+    ErrFs ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FS)
+    // ErrUnknownTopic Local: Unknown topic
+    ErrUnknownTopic ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC)
+    // ErrAllBrokersDown Local: All broker connections are down
+    ErrAllBrokersDown ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN)
+    // ErrInvalidArg Local: Invalid argument or configuration
+    ErrInvalidArg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INVALID_ARG)
+    // ErrTimedOut Local: Timed out
+    ErrTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TIMED_OUT)
+    // ErrQueueFull Local: Queue full
+    ErrQueueFull ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__QUEUE_FULL)
+    // ErrIsrInsuff Local: ISR count insufficient
+    ErrIsrInsuff ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ISR_INSUFF)
+    // ErrNodeUpdate Local: Broker node update
+    ErrNodeUpdate ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NODE_UPDATE)
+    // ErrSsl Local: SSL error
+    ErrSsl ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__SSL)
+    // ErrWaitCoord Local: Waiting for coordinator
+    ErrWaitCoord ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__WAIT_COORD)
+    // ErrUnknownGroup Local: Unknown group
+    ErrUnknownGroup ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_GROUP)
+    // ErrInProgress Local: Operation in progress
+    ErrInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__IN_PROGRESS)
+    // ErrPrevInProgress Local: Previous operation in progress
+    ErrPrevInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS)
+    // ErrExistingSubscription Local: Existing subscription
+    ErrExistingSubscription ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__EXISTING_SUBSCRIPTION)
+    // ErrAssignPartitions Local: Assign partitions
+    ErrAssignPartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS)
+    // ErrRevokePartitions Local: Revoke partitions
+    ErrRevokePartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS)
+    // ErrConflict Local: Conflicting use
+    ErrConflict ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__CONFLICT)
+    // ErrState Local: Erroneous state
+    ErrState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__STATE)
+    // ErrUnknownProtocol Local: Unknown protocol
+    ErrUnknownProtocol ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_PROTOCOL)
+    // ErrNotImplemented Local: Not implemented
+    ErrNotImplemented ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED)
+    // ErrAuthentication Local: Authentication failure
+    ErrAuthentication ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__AUTHENTICATION)
+    // ErrNoOffset Local: No offset stored
+    ErrNoOffset ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NO_OFFSET)
+    // ErrOutdated Local: Outdated
+    ErrOutdated ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__OUTDATED)
+    // ErrTimedOutQueue Local: Timed out in queue
+    ErrTimedOutQueue ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TIMED_OUT_QUEUE)
+    // ErrUnsupportedFeature Local: Required feature not supported by broker
+    ErrUnsupportedFeature ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE)
+    // ErrWaitCache Local: Awaiting cache update
+    ErrWaitCache ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__WAIT_CACHE)
+    // ErrIntr Local: Operation interrupted
+    ErrIntr ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INTR)
+    // ErrKeySerialization Local: Key serialization error
+    ErrKeySerialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__KEY_SERIALIZATION)
+    // ErrValueSerialization Local: Value serialization error
+    ErrValueSerialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__VALUE_SERIALIZATION)
+    // ErrKeyDeserialization Local: Key deserialization error
+    ErrKeyDeserialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__KEY_DESERIALIZATION)
+    // ErrValueDeserialization Local: Value deserialization error
+    ErrValueDeserialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__VALUE_DESERIALIZATION)
+    // ErrPartial Local: Partial response
+    ErrPartial ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PARTIAL)
+    // ErrReadOnly Local: Read-only object
+    ErrReadOnly ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__READ_ONLY)
+    // ErrNoent Local: No such entry
+    ErrNoent ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOENT)
+    // ErrUnderflow Local: Read underflow
+    ErrUnderflow ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNDERFLOW)
+    // ErrInvalidType Local: Invalid type
+    ErrInvalidType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INVALID_TYPE)
+    // ErrRetry Local: Retry operation
+    ErrRetry ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__RETRY)
+    // ErrPurgeQueue Local: Purged in queue
+    ErrPurgeQueue ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PURGE_QUEUE)
+    // ErrPurgeInflight Local: Purged in flight
+    ErrPurgeInflight ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PURGE_INFLIGHT)
+    // ErrFatal Local: Fatal error
+    ErrFatal ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FATAL)
+    // ErrInconsistent Local: Inconsistent state
+    ErrInconsistent ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INCONSISTENT)
+    // ErrGaplessGuarantee Local: Gap-less ordering would not be guaranteed if proceeding
+    ErrGaplessGuarantee ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__GAPLESS_GUARANTEE)
+    // ErrMaxPollExceeded Local: Maximum application poll interval (max.poll.interval.ms) exceeded
+    ErrMaxPollExceeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED)
+    // ErrUnknownBroker Local: Unknown broker
+    ErrUnknownBroker ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_BROKER)
+    // ErrNotConfigured Local: Functionality not configured
+    ErrNotConfigured ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOT_CONFIGURED)
+    // ErrFenced Local: This instance has been fenced by a newer instance
+    ErrFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FENCED)
+    // ErrApplication Local: Application generated error
+    ErrApplication ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__APPLICATION)
+    // ErrAssignmentLost Local: Group partition assignment lost
+    ErrAssignmentLost ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ASSIGNMENT_LOST)
+    // ErrNoop Local: No operation performed
+    ErrNoop ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOOP)
+    // ErrAutoOffsetReset Local: No offset to automatically reset to
+    ErrAutoOffsetReset ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__AUTO_OFFSET_RESET)
+    // ErrUnknown Unknown broker error
+    ErrUnknown ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN)
+    // ErrNoError Success
+    ErrNoError ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NO_ERROR)
+    // ErrOffsetOutOfRange Broker: Offset out of range
+    ErrOffsetOutOfRange ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE)
+    // ErrInvalidMsg Broker: Invalid message
+    ErrInvalidMsg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_MSG)
+    // ErrUnknownTopicOrPart Broker: Unknown topic or partition
+    ErrUnknownTopicOrPart ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART)
+    // ErrInvalidMsgSize Broker: Invalid message size
+    ErrInvalidMsgSize ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_MSG_SIZE)
+    // ErrLeaderNotAvailable Broker: Leader not available
+    ErrLeaderNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE)
+    // ErrNotLeaderForPartition Broker: Not leader for partition
+    ErrNotLeaderForPartition ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_LEADER_FOR_PARTITION)
+    // ErrRequestTimedOut Broker: Request timed out
+    ErrRequestTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REQUEST_TIMED_OUT)
+    // ErrBrokerNotAvailable Broker: Broker not available
+    ErrBrokerNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_BROKER_NOT_AVAILABLE)
+    // ErrReplicaNotAvailable Broker: Replica not available
+    ErrReplicaNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REPLICA_NOT_AVAILABLE)
+    // ErrMsgSizeTooLarge Broker: Message size too large
+    ErrMsgSizeTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE)
+    // ErrStaleCtrlEpoch Broker: StaleControllerEpochCode
+    ErrStaleCtrlEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_STALE_CTRL_EPOCH)
+    // ErrOffsetMetadataTooLarge Broker: Offset metadata string too large
+    ErrOffsetMetadataTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_METADATA_TOO_LARGE)
+    // ErrNetworkException Broker: Broker disconnected before response received
+    ErrNetworkException ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NETWORK_EXCEPTION)
+    // ErrCoordinatorLoadInProgress Broker: Coordinator load in progress
+    ErrCoordinatorLoadInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_COORDINATOR_LOAD_IN_PROGRESS)
+    // ErrCoordinatorNotAvailable Broker: Coordinator not available
+    ErrCoordinatorNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_COORDINATOR_NOT_AVAILABLE)
+    // ErrNotCoordinator Broker: Not coordinator
+    ErrNotCoordinator ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_COORDINATOR)
+    // ErrTopicException Broker: Invalid topic
+    ErrTopicException ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_EXCEPTION)
+    // ErrRecordListTooLarge Broker: Message batch larger than configured server segment size
+    ErrRecordListTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_RECORD_LIST_TOO_LARGE)
+    // ErrNotEnoughReplicas Broker: Not enough in-sync replicas
+    ErrNotEnoughReplicas ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS)
+    // ErrNotEnoughReplicasAfterAppend Broker: Message(s) written to insufficient number of in-sync replicas
+    ErrNotEnoughReplicasAfterAppend ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND)
+    // ErrInvalidRequiredAcks Broker: Invalid required acks value
+    ErrInvalidRequiredAcks ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REQUIRED_ACKS)
+    // ErrIllegalGeneration Broker: Specified group generation id is not valid
+    ErrIllegalGeneration ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ILLEGAL_GENERATION)
+    // ErrInconsistentGroupProtocol Broker: Inconsistent group protocol
+    ErrInconsistentGroupProtocol ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INCONSISTENT_GROUP_PROTOCOL)
+    // ErrInvalidGroupID Broker: Invalid group.id
+    ErrInvalidGroupID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_GROUP_ID)
+    // ErrUnknownMemberID Broker: Unknown member
+    ErrUnknownMemberID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID)
+    // ErrInvalidSessionTimeout Broker: Invalid session timeout
+    ErrInvalidSessionTimeout ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_SESSION_TIMEOUT)
+    // ErrRebalanceInProgress Broker: Group rebalance in progress
+    ErrRebalanceInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS)
+    // ErrInvalidCommitOffsetSize Broker: Commit offset data size is not valid
+    ErrInvalidCommitOffsetSize ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_COMMIT_OFFSET_SIZE)
+    // ErrTopicAuthorizationFailed Broker: Topic authorization failed
+    ErrTopicAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED)
+    // ErrGroupAuthorizationFailed Broker: Group authorization failed
+    ErrGroupAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED)
+    // ErrClusterAuthorizationFailed Broker: Cluster authorization failed
+    ErrClusterAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_CLUSTER_AUTHORIZATION_FAILED)
+    // ErrInvalidTimestamp Broker: Invalid timestamp
+    ErrInvalidTimestamp ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TIMESTAMP)
+    // ErrUnsupportedSaslMechanism Broker: Unsupported SASL mechanism
+    ErrUnsupportedSaslMechanism ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_SASL_MECHANISM)
+    // ErrIllegalSaslState Broker: Request not valid in current SASL state
+    ErrIllegalSaslState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ILLEGAL_SASL_STATE)
+    // ErrUnsupportedVersion Broker: API version not supported
+    ErrUnsupportedVersion ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_VERSION)
+    // ErrTopicAlreadyExists Broker: Topic already exists
+    ErrTopicAlreadyExists ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS)
+    // ErrInvalidPartitions Broker: Invalid number of partitions
+    ErrInvalidPartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PARTITIONS)
+    // ErrInvalidReplicationFactor Broker: Invalid replication factor
+    ErrInvalidReplicationFactor ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REPLICATION_FACTOR)
+    // ErrInvalidReplicaAssignment Broker: Invalid replica assignment
+    ErrInvalidReplicaAssignment ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REPLICA_ASSIGNMENT)
+    // ErrInvalidConfig Broker: Configuration is invalid
+    ErrInvalidConfig ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_CONFIG)
+    // ErrNotController Broker: Not controller for cluster
+    ErrNotController ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_CONTROLLER)
+    // ErrInvalidRequest Broker: Invalid request
+    ErrInvalidRequest ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REQUEST)
+    // ErrUnsupportedForMessageFormat Broker: Message format on broker does not support request
+    ErrUnsupportedForMessageFormat ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT)
+    // ErrPolicyViolation Broker: Policy violation
+    ErrPolicyViolation ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_POLICY_VIOLATION)
+    // ErrOutOfOrderSequenceNumber Broker: Broker received an out of order sequence number
+    ErrOutOfOrderSequenceNumber ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OUT_OF_ORDER_SEQUENCE_NUMBER)
+    // ErrDuplicateSequenceNumber Broker: Broker received a duplicate sequence number
+    ErrDuplicateSequenceNumber ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DUPLICATE_SEQUENCE_NUMBER)
+    // ErrInvalidProducerEpoch Broker: Producer attempted an operation with an old epoch
+    ErrInvalidProducerEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRODUCER_EPOCH)
+    // ErrInvalidTxnState Broker: Producer attempted a transactional operation in an invalid state
+    ErrInvalidTxnState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TXN_STATE)
+    // ErrInvalidProducerIDMapping Broker: Producer attempted to use a producer id which is not currently assigned to its transactional id
+    ErrInvalidProducerIDMapping ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRODUCER_ID_MAPPING)
+    // ErrInvalidTransactionTimeout Broker: Transaction timeout is larger than the maximum value allowed by the broker's max.transaction.timeout.ms
+    ErrInvalidTransactionTimeout ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TRANSACTION_TIMEOUT)
+    // ErrConcurrentTransactions Broker: Producer attempted to update a transaction while another concurrent operation on the same transaction was ongoing
+    ErrConcurrentTransactions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS)
+    // ErrTransactionCoordinatorFenced Broker: Indicates that the transaction coordinator sending a WriteTxnMarker is no longer the current coordinator for a given producer
+    ErrTransactionCoordinatorFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TRANSACTION_COORDINATOR_FENCED)
+    // ErrTransactionalIDAuthorizationFailed Broker: Transactional Id authorization failed
+    ErrTransactionalIDAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED)
+    // ErrSecurityDisabled Broker: Security features are disabled
+    ErrSecurityDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_SECURITY_DISABLED)
+    // ErrOperationNotAttempted Broker: Operation not attempted
+    ErrOperationNotAttempted ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OPERATION_NOT_ATTEMPTED)
+    // ErrKafkaStorageError Broker: Disk error when trying to access log file on disk
+    ErrKafkaStorageError ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_KAFKA_STORAGE_ERROR)
+    // ErrLogDirNotFound Broker: The user-specified log directory is not found in the broker config
+    ErrLogDirNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LOG_DIR_NOT_FOUND)
+    // ErrSaslAuthenticationFailed Broker: SASL Authentication failed
+    ErrSaslAuthenticationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_SASL_AUTHENTICATION_FAILED)
+    // ErrUnknownProducerID Broker: Unknown Producer Id
+    ErrUnknownProducerID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_PRODUCER_ID)
+    // ErrReassignmentInProgress Broker: Partition reassignment is in progress
+    ErrReassignmentInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REASSIGNMENT_IN_PROGRESS)
+    // ErrDelegationTokenAuthDisabled Broker: Delegation Token feature is not enabled
+    ErrDelegationTokenAuthDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTH_DISABLED)
+    // ErrDelegationTokenNotFound Broker: Delegation Token is not found on server
+    ErrDelegationTokenNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_NOT_FOUND)
+    // ErrDelegationTokenOwnerMismatch Broker: Specified Principal is not valid Owner/Renewer
+    ErrDelegationTokenOwnerMismatch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_OWNER_MISMATCH)
+    // ErrDelegationTokenRequestNotAllowed Broker: Delegation Token requests are not allowed on this connection
+    ErrDelegationTokenRequestNotAllowed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED)
+    // ErrDelegationTokenAuthorizationFailed Broker: Delegation Token authorization failed
+    ErrDelegationTokenAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED)
+    // ErrDelegationTokenExpired Broker: Delegation Token is expired
+    ErrDelegationTokenExpired ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_EXPIRED)
+    // ErrInvalidPrincipalType Broker: Supplied principalType is not supported
+    ErrInvalidPrincipalType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRINCIPAL_TYPE)
+    // ErrNonEmptyGroup Broker: The group is not empty
+    ErrNonEmptyGroup ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NON_EMPTY_GROUP)
+    // ErrGroupIDNotFound Broker: The group id does not exist
+    ErrGroupIDNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_ID_NOT_FOUND)
+    // ErrFetchSessionIDNotFound Broker: The fetch session ID was not found
+    ErrFetchSessionIDNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FETCH_SESSION_ID_NOT_FOUND)
+    // ErrInvalidFetchSessionEpoch Broker: The fetch session epoch is invalid
+    ErrInvalidFetchSessionEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_FETCH_SESSION_EPOCH)
+    // ErrListenerNotFound Broker: No matching listener
+    ErrListenerNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LISTENER_NOT_FOUND)
+    // ErrTopicDeletionDisabled Broker: Topic deletion is disabled
+    ErrTopicDeletionDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_DELETION_DISABLED)
+    // ErrFencedLeaderEpoch Broker: Leader epoch is older than broker epoch
+    ErrFencedLeaderEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FENCED_LEADER_EPOCH)
+    // ErrUnknownLeaderEpoch Broker: Leader epoch is newer than broker epoch
+    ErrUnknownLeaderEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_LEADER_EPOCH)
+    // ErrUnsupportedCompressionType Broker: Unsupported compression type
+    ErrUnsupportedCompressionType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_COMPRESSION_TYPE)
+    // ErrStaleBrokerEpoch Broker: Broker epoch has changed
+    ErrStaleBrokerEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_STALE_BROKER_EPOCH)
+    // ErrOffsetNotAvailable Broker: Leader high watermark is not caught up
+    ErrOffsetNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_NOT_AVAILABLE)
+    // ErrMemberIDRequired Broker: Group member needs a valid member ID
+    ErrMemberIDRequired ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_MEMBER_ID_REQUIRED)
+    // ErrPreferredLeaderNotAvailable Broker: Preferred leader was not available
+    ErrPreferredLeaderNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PREFERRED_LEADER_NOT_AVAILABLE)
+    // ErrGroupMaxSizeReached Broker: Consumer group has reached maximum size
+    ErrGroupMaxSizeReached ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_MAX_SIZE_REACHED)
+    // ErrFencedInstanceID Broker: Static consumer fenced by other consumer with same group.instance.id
+    ErrFencedInstanceID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FENCED_INSTANCE_ID)
+    // ErrEligibleLeadersNotAvailable Broker: Eligible partition leaders are not available
+    ErrEligibleLeadersNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ELIGIBLE_LEADERS_NOT_AVAILABLE)
+    // ErrElectionNotNeeded Broker: Leader election not needed for topic partition
+    ErrElectionNotNeeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ELECTION_NOT_NEEDED)
+    // ErrNoReassignmentInProgress Broker: No partition reassignment is in progress
+    ErrNoReassignmentInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NO_REASSIGNMENT_IN_PROGRESS)
+    // ErrGroupSubscribedToTopic Broker: Deleting offsets of a topic while the consumer group is subscribed to it
+    ErrGroupSubscribedToTopic ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_SUBSCRIBED_TO_TOPIC)
+    // ErrInvalidRecord Broker: Broker failed to validate record
+    ErrInvalidRecord ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_RECORD)
+    // ErrUnstableOffsetCommit Broker: There are unstable offsets that need to be cleared
+    ErrUnstableOffsetCommit ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSTABLE_OFFSET_COMMIT)
+    // ErrThrottlingQuotaExceeded Broker: Throttling quota has been exceeded
+    ErrThrottlingQuotaExceeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_THROTTLING_QUOTA_EXCEEDED)
+    // ErrProducerFenced Broker: There is a newer producer with the same transactionalId which fences the current one
+    ErrProducerFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PRODUCER_FENCED)
+    // ErrResourceNotFound Broker: Request illegally referred to resource that does not exist
+    ErrResourceNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_RESOURCE_NOT_FOUND)
+    // ErrDuplicateResource Broker: Request illegally referred to the same resource twice
+    ErrDuplicateResource ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DUPLICATE_RESOURCE)
+    // ErrUnacceptableCredential Broker: Requested credential would not meet criteria for acceptability
+    ErrUnacceptableCredential ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNACCEPTABLE_CREDENTIAL)
+    // ErrInconsistentVoterSet Broker: Indicates that the either the sender or recipient of a voter-only request is not one of the expected voters
+    ErrInconsistentVoterSet ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INCONSISTENT_VOTER_SET)
+    // ErrInvalidUpdateVersion Broker: Invalid update version
+    ErrInvalidUpdateVersion ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_UPDATE_VERSION)
+    // ErrFeatureUpdateFailed Broker: Unable to update finalized features due to server error
+    ErrFeatureUpdateFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FEATURE_UPDATE_FAILED)
+    // ErrPrincipalDeserializationFailure Broker: Request principal deserialization failed during forwarding
+    ErrPrincipalDeserializationFailure ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PRINCIPAL_DESERIALIZATION_FAILURE)
+)
+

+ func (ErrorCode) + + String + + +

+
func (c ErrorCode) String() string
+

+ String returns a human readable representation of an error code +

+

+ type + + Event + + +

+

+ Event generic interface +

+
type Event interface {
+    // String returns a human-readable representation of the event
+    String() string
+}
+

+ type + + Handle + + +

+

+ Handle represents a generic client handle containing common parts for +both Producer and Consumer. +

+
type Handle interface {
+    // SetOAuthBearerToken sets the the data to be transmitted
+    // to a broker during SASL/OAUTHBEARER authentication. It will return nil
+    // on success, otherwise an error if:
+    // 1) the token data is invalid (meaning an expiration time in the past
+    // or either a token value or an extension key or value that does not meet
+    // the regular expression requirements as per
+    // https://tools.ietf.org/html/rfc7628#section-3.1);
+    // 2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build;
+    // 3) SASL/OAUTHBEARER is supported but is not configured as the client's
+    // authentication mechanism.
+    SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error
+
+    // SetOAuthBearerTokenFailure sets the error message describing why token
+    // retrieval/setting failed; it also schedules a new token refresh event for 10
+    // seconds later so the attempt may be retried. It will return nil on
+    // success, otherwise an error if:
+    // 1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build;
+    // 2) SASL/OAUTHBEARER is supported but is not configured as the client's
+    // authentication mechanism.
+    SetOAuthBearerTokenFailure(errstr string) error
+    // contains filtered or unexported methods
+}
+ +

+ Header represents a single Kafka message header. +

+

+ Message headers are made up of a list of Header elements, retaining their original insert +order and allowing for duplicate Keys. +

+

+ Key is a human readable string identifying the header. +Value is the key's binary value, Kafka does not put any restrictions on the format of +of the Value but it should be made relatively compact. +The value may be a byte array, empty, or nil. +

+

+ NOTE: Message headers are not available on producer delivery report messages. +

+
type Header struct {
+    Key   string // Header name (utf-8 string)
+    Value []byte // Header value (nil, empty, or binary)
+}
+
+

+ func (Header) + + String + + +

+
func (h Header) String() string
+

+ String returns the Header Key and data in a human representable possibly truncated form +suitable for displaying to the user. +

+

+ type + + LogEvent + + +

+

+ LogEvent represent the log from librdkafka internal log queue +

+
type LogEvent struct {
+    Name      string    // Name of client instance
+    Tag       string    // Log tag that provides context to the log Message (e.g., "METADATA" or "GRPCOORD")
+    Message   string    // Log message
+    Level     int       // Log syslog level, lower is more critical.
+    Timestamp time.Time // Log timestamp
+}
+
+

+ func (LogEvent) + + String + + +

+
func (logEvent LogEvent) String() string
+

+ type + + Message + + +

+

+ Message represents a Kafka message +

+
type Message struct {
+    TopicPartition TopicPartition
+    Value          []byte
+    Key            []byte
+    Timestamp      time.Time
+    TimestampType  TimestampType
+    Opaque         interface{}
+    Headers        []Header
+}
+
+

+ func (*Message) + + String + + +

+
func (m *Message) String() string
+

+ String returns a human readable representation of a Message. +Key and payload are not represented. +

+

+ type + + Metadata + + +

+

+ Metadata contains broker and topic metadata for all (matching) topics +

+
type Metadata struct {
+    Brokers []BrokerMetadata
+    Topics  map[string]TopicMetadata
+
+    OriginatingBroker BrokerMetadata
+}
+
+

+ type + + OAuthBearerToken + + +

+

+ OAuthBearerToken represents the data to be transmitted +to a broker during SASL/OAUTHBEARER authentication. +

+
type OAuthBearerToken struct {
+    // Token value, often (but not necessarily) a JWS compact serialization
+    // as per https://tools.ietf.org/html/rfc7515#section-3.1; it must meet
+    // the regular expression for a SASL/OAUTHBEARER value defined at
+    // https://tools.ietf.org/html/rfc7628#section-3.1
+    TokenValue string
+    // Metadata about the token indicating when it expires (local time);
+    // it must represent a time in the future
+    Expiration time.Time
+    // Metadata about the token indicating the Kafka principal name
+    // to which it applies (for example, "admin")
+    Principal string
+    // SASL extensions, if any, to be communicated to the broker during
+    // authentication (all keys and values of which must meet the regular
+    // expressions defined at https://tools.ietf.org/html/rfc7628#section-3.1,
+    // and it must not contain the reserved "auth" key)
+    Extensions map[string]string
+}
+
+

+ type + + OAuthBearerTokenRefresh + + +

+

+ OAuthBearerTokenRefresh indicates token refresh is required +

+
type OAuthBearerTokenRefresh struct {
+    // Config is the value of the sasl.oauthbearer.config property
+    Config string
+}
+
+

+ func (OAuthBearerTokenRefresh) + + String + + +

+
func (o OAuthBearerTokenRefresh) String() string
+

+ type + + Offset + + +

+

+ Offset type (int64) with support for canonical names +

+
type Offset int64
+

+ func + + NewOffset + + +

+
func NewOffset(offset interface{}) (Offset, error)
+

+ NewOffset creates a new Offset using the provided logical string, or an +absolute int64 offset value. +Logical offsets: "beginning", "earliest", "end", "latest", "unset", "invalid", "stored" +

+

+ func + + OffsetTail + + +

+
func OffsetTail(relativeOffset Offset) Offset
+

+ OffsetTail returns the logical offset relativeOffset from current end of partition +

+

+ func (*Offset) + + Set + + +

+
func (o *Offset) Set(offset interface{}) error
+

+ Set offset value, see NewOffset() +

+

+ func (Offset) + + String + + +

+
func (o Offset) String() string
+

+ type + + OffsetsCommitted + + +

+

+ OffsetsCommitted reports committed offsets +

+
type OffsetsCommitted struct {
+    Error   error
+    Offsets []TopicPartition
+}
+
+

+ func (OffsetsCommitted) + + String + + +

+
func (o OffsetsCommitted) String() string
+

+ type + + PartitionEOF + + +

+

+ PartitionEOF consumer reached end of partition +Needs to be explicitly enabled by setting the `enable.partition.eof` +configuration property to true. +

+
type PartitionEOF TopicPartition
+

+ func (PartitionEOF) + + String + + +

+
func (p PartitionEOF) String() string
+

+ type + + PartitionMetadata + + +

+

+ PartitionMetadata contains per-partition metadata +

+
type PartitionMetadata struct {
+    ID       int32
+    Error    Error
+    Leader   int32
+    Replicas []int32
+    Isrs     []int32
+}
+
+

+ type + + PartitionsSpecification + + +

+

+ PartitionsSpecification holds parameters for creating additional partitions for a topic. +PartitionsSpecification is analogous to NewPartitions in the Java Topic Admin API. +

+
type PartitionsSpecification struct {
+    // Topic to create more partitions for.
+    Topic string
+    // New partition count for topic, must be higher than current partition count.
+    IncreaseTo int
+    // (Optional) Explicit replica assignment. The outer array is
+    // indexed by the new partition index (i.e., 0 for the first added
+    // partition), while the inner per-partition array
+    // contains the replica broker ids. The first broker in each
+    // broker id list will be the preferred replica.
+    ReplicaAssignment [][]int32
+}
+
+

+ type + + Producer + + +

+

+ Producer implements a High-level Apache Kafka Producer instance +

+
type Producer struct {
+    // contains filtered or unexported fields
+}
+
+

+ func + + NewProducer + + +

+
func NewProducer(conf *ConfigMap) (*Producer, error)
+

+ NewProducer creates a new high-level Producer instance. +

+

+ conf is a *ConfigMap with standard librdkafka configuration properties. +

+

+ Supported special configuration properties (type, default): +

+
go.batch.producer (bool, false) - EXPERIMENTAL: Enable batch producer (for increased performance).
+                                  These batches do not relate to Kafka message batches in any way.
+                                  Note: timestamps and headers are not supported with this interface.
+go.delivery.reports (bool, true) - Forward per-message delivery reports to the
+                                   Events() channel.
+go.delivery.report.fields (string, "key,value") - Comma separated list of fields to enable for delivery reports.
+                                    Allowed values: all, none (or empty string), key, value, headers
+                                    Warning: There is a performance penalty to include headers in the delivery report.
+go.events.channel.size (int, 1000000) - Events().
+go.produce.channel.size (int, 1000000) - ProduceChannel() buffer size (in number of messages)
+go.logs.channel.enable (bool, false) - Forward log to Logs() channel.
+go.logs.channel (chan kafka.LogEvent, nil) - Forward logs to application-provided channel instead of Logs(). Requires go.logs.channel.enable=true.
+
+

+ func (*Producer) + + AbortTransaction + + +

+
func (p *Producer) AbortTransaction(ctx context.Context) error
+

+ AbortTransaction aborts the ongoing transaction. +

+

+ This function should also be used to recover from non-fatal abortable +transaction errors. +

+

+ Any outstanding messages will be purged and fail with +`ErrPurgeInflight` or `ErrPurgeQueue`. +

+

+ Parameters: +

+
* `ctx` - The maximum amount of time to block, or nil for indefinite.
+
+

+ Note: This function will block until all outstanding messages are purged +and the transaction abort request has been successfully +handled by the transaction coordinator, or until the `ctx` expires, +which ever comes first. On timeout the application may +call the function again. +

+

+ Note: Will automatically call `Purge()` and `Flush()` to ensure all queued +and in-flight messages are purged before attempting to abort the transaction. +The application MUST serve the `producer.Events()` channel for delivery +reports in a separate go-routine during this time. +

+

+ Returns nil on success or an error object on failure. +Check whether the returned error object permits retrying +by calling `err.(kafka.Error).IsRetriable()`, or whether a fatal error +has been raised by calling `err.(kafka.Error).IsFatal()`. +

+

+ func (*Producer) + + BeginTransaction + + +

+
func (p *Producer) BeginTransaction() error
+

+ BeginTransaction starts a new transaction. +

+

+ `InitTransactions()` must have been called successfully (once) +before this function is called. +

+

+ Any messages produced, offsets sent (`SendOffsetsToTransaction()`), +etc, after the successful return of this function will be part of +the transaction and committed or aborted atomatically. +

+

+ Finish the transaction by calling `CommitTransaction()` or +abort the transaction by calling `AbortTransaction()`. +

+

+ Returns nil on success or an error object on failure. +Check whether a fatal error has been raised by +calling `err.(kafka.Error).IsFatal()`. +

+

+ Note: With the transactional producer, `Produce()`, et.al, are only +allowed during an on-going transaction, as started with this function. +Any produce call outside an on-going transaction, or for a failed +transaction, will fail. +

+

+ func (*Producer) + + Close + + +

+
func (p *Producer) Close()
+

+ Close a Producer instance. +The Producer object or its channels are no longer usable after this call. +

+

+ func (*Producer) + + CommitTransaction + + +

+
func (p *Producer) CommitTransaction(ctx context.Context) error
+

+ CommitTransaction commits the current transaction. +

+

+ Any outstanding messages will be flushed (delivered) before actually +committing the transaction. +

+

+ If any of the outstanding messages fail permanently the current +transaction will enter the abortable error state and this +function will return an abortable error, in this case the application +must call `AbortTransaction()` before attempting a new +transaction with `BeginTransaction()`. +

+

+ Parameters: +

+
* `ctx` - The maximum amount of time to block, or nil for indefinite.
+
+

+ Note: This function will block until all outstanding messages are +delivered and the transaction commit request has been successfully +handled by the transaction coordinator, or until the `ctx` expires, +which ever comes first. On timeout the application may +call the function again. +

+

+ Note: Will automatically call `Flush()` to ensure all queued +messages are delivered before attempting to commit the transaction. +The application MUST serve the `producer.Events()` channel for delivery +reports in a separate go-routine during this time. +

+

+ Returns nil on success or an error object on failure. +Check whether the returned error object permits retrying +by calling `err.(kafka.Error).IsRetriable()`, or whether an abortable +or fatal error has been raised by calling +`err.(kafka.Error).TxnRequiresAbort()` or `err.(kafka.Error).IsFatal()` +respectively. +

+

+ func (*Producer) + + Events + + +

+
func (p *Producer) Events() chan Event
+

+ Events returns the Events channel (read) +

+

+ func (*Producer) + + Flush + + +

+
func (p *Producer) Flush(timeoutMs int) int
+

+ Flush and wait for outstanding messages and requests to complete delivery. +Includes messages on ProduceChannel. +Runs until value reaches zero or on timeoutMs. +Returns the number of outstanding events still un-flushed. +

+

+ func (*Producer) + + GetFatalError + + +

+
func (p *Producer) GetFatalError() error
+

+ GetFatalError returns an Error object if the client instance has raised a fatal error, else nil. +

+

+ func (*Producer) + + GetMetadata + + +

+
func (p *Producer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error)
+

+ GetMetadata queries broker for cluster and topic metadata. +If topic is non-nil only information about that topic is returned, else if +allTopics is false only information about locally used topics is returned, +else information about all topics is returned. +GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +

+

+ func (*Producer) + + InitTransactions + + +

+
func (p *Producer) InitTransactions(ctx context.Context) error
+

+ InitTransactions Initializes transactions for the producer instance. +

+

+ This function ensures any transactions initiated by previous instances +of the producer with the same `transactional.id` are completed. +If the previous instance failed with a transaction in progress the +previous transaction will be aborted. +This function needs to be called before any other transactional or +produce functions are called when the `transactional.id` is configured. +

+

+ If the last transaction had begun completion (following transaction commit) +but not yet finished, this function will await the previous transaction's +completion. +

+

+ When any previous transactions have been fenced this function +will acquire the internal producer id and epoch, used in all future +transactional messages issued by this producer instance. +

+

+ Upon successful return from this function the application has to perform at +least one of the following operations within `transaction.timeout.ms` to +avoid timing out the transaction on the broker: +

+
* `Produce()` (et.al)
+* `SendOffsetsToTransaction()`
+* `CommitTransaction()`
+* `AbortTransaction()`
+
+

+ Parameters: +

+
* `ctx` - The maximum time to block, or nil for indefinite.
+          On timeout the operation may continue in the background,
+          depending on state, and it is okay to call `InitTransactions()`
+          again.
+
+

+ Returns nil on success or an error on failure. +Check whether the returned error object permits retrying +by calling `err.(kafka.Error).IsRetriable()`, or whether a fatal +error has been raised by calling `err.(kafka.Error).IsFatal()`. +

+

+ func (*Producer) + + Len + + +

+
func (p *Producer) Len() int
+

+ Len returns the number of messages and requests waiting to be transmitted to the broker +as well as delivery reports queued for the application. +Includes messages on ProduceChannel. +

+

+ func (*Producer) + + Logs + + +

+
func (p *Producer) Logs() chan LogEvent
+

+ Logs returns the Log channel (if enabled), else nil +

+

+ func (*Producer) + + OffsetsForTimes + + +

+
func (p *Producer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error)
+

+ OffsetsForTimes looks up offsets by timestamp for the given partitions. +

+

+ The returned offset for each partition is the earliest offset whose +timestamp is greater than or equal to the given timestamp in the +corresponding partition. If the provided timestamp exceeds that of the +last message in the partition, a value of -1 will be returned. +

+

+ The timestamps to query are represented as `.Offset` in the `times` +argument and the looked up offsets are represented as `.Offset` in the returned +`offsets` list. +

+

+ The function will block for at most timeoutMs milliseconds. +

+

+ Duplicate Topic+Partitions are not supported. +Per-partition errors may be returned in the `.Error` field. +

+

+ func (*Producer) + + Produce + + +

+
func (p *Producer) Produce(msg *Message, deliveryChan chan Event) error
+

+ Produce single message. +This is an asynchronous call that enqueues the message on the internal +transmit queue, thus returning immediately. +The delivery report will be sent on the provided deliveryChan if specified, +or on the Producer object's Events() channel if not. +msg.Timestamp requires librdkafka >= 0.9.4 (else returns ErrNotImplemented), +api.version.request=true, and broker >= 0.10.0.0. +msg.Headers requires librdkafka >= 0.11.4 (else returns ErrNotImplemented), +api.version.request=true, and broker >= 0.11.0.0. +Returns an error if message could not be enqueued. +

+

+ func (*Producer) + + ProduceChannel + + +

+
func (p *Producer) ProduceChannel() chan *Message
+

+ ProduceChannel returns the produce *Message channel (write) +

+

+ func (*Producer) + + Purge + + +

+
func (p *Producer) Purge(flags int) error
+

+ Purge messages currently handled by this producer instance. +

+

+ flags is a combination of PurgeQueue, PurgeInFlight and PurgeNonBlocking. +

+

+ The application will need to call Poll(), Flush() or read the Events() channel +after this call to serve delivery reports for the purged messages. +

+

+ Messages purged from internal queues fail with the delivery report +error code set to ErrPurgeQueue, while purged messages that +are in-flight to or from the broker will fail with the error code set to +ErrPurgeInflight. +

+

+ Warning: Purging messages that are in-flight to or from the broker +will ignore any sub-sequent acknowledgement for these messages +received from the broker, effectively making it impossible +for the application to know if the messages were successfully +produced or not. This may result in duplicate messages if the +application retries these messages at a later time. +

+

+ Note: This call may block for a short time while background thread +queues are purged. +

+

+ Returns nil on success, ErrInvalidArg if the purge flags are invalid or unknown. +

+

+ func (*Producer) + + QueryWatermarkOffsets + + +

+
func (p *Producer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error)
+

+ QueryWatermarkOffsets returns the broker's low and high offsets for the given topic +and partition. +

+

+ func (*Producer) + + SendOffsetsToTransaction + + +

+
func (p *Producer) SendOffsetsToTransaction(ctx context.Context, offsets []TopicPartition, consumerMetadata *ConsumerGroupMetadata) error
+

+ SendOffsetsToTransaction sends a list of topic partition offsets to the +consumer group coordinator for `consumerMetadata`, and marks the offsets +as part part of the current transaction. +These offsets will be considered committed only if the transaction is +committed successfully. +

+

+ The offsets should be the next message your application will consume, +i.e., the last processed message's offset + 1 for each partition. +Either track the offsets manually during processing or use +`consumer.Position()` (on the consumer) to get the current offsets for +the partitions assigned to the consumer. +

+

+ Use this method at the end of a consume-transform-produce loop prior +to committing the transaction with `CommitTransaction()`. +

+

+ Parameters: +

+
* `ctx` - The maximum amount of time to block, or nil for indefinite.
+* `offsets` - List of offsets to commit to the consumer group upon
+              successful commit of the transaction. Offsets should be
+              the next message to consume, e.g., last processed message + 1.
+* `consumerMetadata` - The current consumer group metadata as returned by
+              `consumer.GetConsumerGroupMetadata()` on the consumer
+              instance the provided offsets were consumed from.
+
+

+ Note: The consumer must disable auto commits (set `enable.auto.commit` to false on the consumer). +

+

+ Note: Logical and invalid offsets (e.g., OffsetInvalid) in +`offsets` will be ignored. If there are no valid offsets in +`offsets` the function will return nil and no action will be taken. +

+

+ Returns nil on success or an error object on failure. +Check whether the returned error object permits retrying +by calling `err.(kafka.Error).IsRetriable()`, or whether an abortable +or fatal error has been raised by calling +`err.(kafka.Error).TxnRequiresAbort()` or `err.(kafka.Error).IsFatal()` +respectively. +

+

+ func (*Producer) + + SetOAuthBearerToken + + +

+
func (p *Producer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error
+

+ SetOAuthBearerToken sets the the data to be transmitted +to a broker during SASL/OAUTHBEARER authentication. It will return nil +on success, otherwise an error if: +1) the token data is invalid (meaning an expiration time in the past +or either a token value or an extension key or value that does not meet +the regular expression requirements as per + + https://tools.ietf.org/html/rfc7628#section-3.1 + + ); +2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +3) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*Producer) + + SetOAuthBearerTokenFailure + + +

+
func (p *Producer) SetOAuthBearerTokenFailure(errstr string) error
+

+ SetOAuthBearerTokenFailure sets the error message describing why token +retrieval/setting failed; it also schedules a new token refresh event for 10 +seconds later so the attempt may be retried. It will return nil on +success, otherwise an error if: +1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +2) SASL/OAUTHBEARER is supported but is not configured as the client's +authentication mechanism. +

+

+ func (*Producer) + + String + + +

+
func (p *Producer) String() string
+

+ String returns a human readable name for a Producer instance +

+

+ func (*Producer) + + TestFatalError + + +

+
func (p *Producer) TestFatalError(code ErrorCode, str string) ErrorCode
+

+ TestFatalError triggers a fatal error in the underlying client. +This is to be used strictly for testing purposes. +

+

+ type + + RebalanceCb + + +

+

+ RebalanceCb provides a per-Subscribe*() rebalance event callback. +The passed Event will be either AssignedPartitions or RevokedPartitions +

+
type RebalanceCb func(*Consumer, Event) error
+

+ type + + ResourceType + + +

+

+ ResourceType represents an Apache Kafka resource type +

+
type ResourceType int
+

+ func + + ResourceTypeFromString + + +

+
func ResourceTypeFromString(typeString string) (ResourceType, error)
+

+ ResourceTypeFromString translates a resource type name/string to +a ResourceType value. +

+

+ func (ResourceType) + + String + + +

+
func (t ResourceType) String() string
+

+ String returns the human-readable representation of a ResourceType +

+

+ type + + RevokedPartitions + + +

+

+ RevokedPartitions consumer group rebalance event: revoked partition set +

+
type RevokedPartitions struct {
+    Partitions []TopicPartition
+}
+
+

+ func (RevokedPartitions) + + String + + +

+
func (e RevokedPartitions) String() string
+

+ type + + Stats + + +

+

+ Stats statistics event +

+
type Stats struct {
+    // contains filtered or unexported fields
+}
+
+

+ func (Stats) + + String + + +

+
func (e Stats) String() string
+

+ type + + TimestampType + + +

+

+ TimestampType is a the Message timestamp type or source +

+
type TimestampType int
+

+ func (TimestampType) + + String + + +

+
func (t TimestampType) String() string
+

+ type + + TopicMetadata + + +

+

+ TopicMetadata contains per-topic metadata +

+
type TopicMetadata struct {
+    Topic      string
+    Partitions []PartitionMetadata
+    Error      Error
+}
+
+

+ type + + TopicPartition + + +

+

+ TopicPartition is a generic placeholder for a Topic+Partition and optionally Offset. +

+
type TopicPartition struct {
+    Topic     *string
+    Partition int32
+    Offset    Offset
+    Metadata  *string
+    Error     error
+}
+
+

+ func (TopicPartition) + + String + + +

+
func (p TopicPartition) String() string
+

+ type + + TopicPartitions + + +

+

+ TopicPartitions is a slice of TopicPartitions that also implements +the sort interface +

+
type TopicPartitions []TopicPartition
+

+ func (TopicPartitions) + + Len + + +

+
func (tps TopicPartitions) Len() int
+

+ func (TopicPartitions) + + Less + + +

+
func (tps TopicPartitions) Less(i, j int) bool
+

+ func (TopicPartitions) + + Swap + + +

+
func (tps TopicPartitions) Swap(i, j int)
+

+ type + + TopicResult + + +

+

+ TopicResult provides per-topic operation result (error) information. +

+
type TopicResult struct {
+    // Topic name
+    Topic string
+    // Error, if any, of result. Check with `Error.Code() != ErrNoError`.
+    Error Error
+}
+
+

+ func (TopicResult) + + String + + +

+
func (t TopicResult) String() string
+

+ String returns a human-readable representation of a TopicResult. +

+

+ type + + TopicSpecification + + +

+

+ TopicSpecification holds parameters for creating a new topic. +TopicSpecification is analogous to NewTopic in the Java Topic Admin API. +

+
type TopicSpecification struct {
+    // Topic name to create.
+    Topic string
+    // Number of partitions in topic.
+    NumPartitions int
+    // Default replication factor for the topic's partitions, or zero
+    // if an explicit ReplicaAssignment is set.
+    ReplicationFactor int
+    // (Optional) Explicit replica assignment. The outer array is
+    // indexed by the partition number, while the inner per-partition array
+    // contains the replica broker ids. The first broker in each
+    // broker id list will be the preferred replica.
+    ReplicaAssignment [][]int32
+    // Topic configuration.
+    Config map[string]string
+}
+
+ +
+ +
+ + + + diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_darwin.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_darwin.go new file mode 100644 index 0000000..5b6812f --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_darwin.go @@ -0,0 +1,13 @@ +// +build !dynamic + + +// This file was auto-generated by librdkafka_vendor/bundle-import.sh, DO NOT EDIT. + +package kafka + +// #cgo CFLAGS: -DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB +// #cgo LDFLAGS: ${SRCDIR}/librdkafka_vendor/librdkafka_darwin.a -lm -lsasl2 -ldl -lpthread +import "C" + +// LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +const LibrdkafkaLinkInfo = "static darwin from librdkafka-static-bundle-v1.8.2.tgz" diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_dynamic.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_dynamic.go new file mode 100644 index 0000000..bb7da9b --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_dynamic.go @@ -0,0 +1,9 @@ +// +build dynamic + +package kafka + +// #cgo pkg-config: rdkafka +import "C" + +// LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +const LibrdkafkaLinkInfo = "dynamically linked to librdkafka" diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_glibc_linux.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_glibc_linux.go new file mode 100644 index 0000000..a69638d --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_glibc_linux.go @@ -0,0 +1,13 @@ +// +build !dynamic +// +build !musl + +// This file was auto-generated by librdkafka_vendor/bundle-import.sh, DO NOT EDIT. + +package kafka + +// #cgo CFLAGS: -DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB +// #cgo LDFLAGS: ${SRCDIR}/librdkafka_vendor/librdkafka_glibc_linux.a -lm -ldl -lpthread -lrt +import "C" + +// LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +const LibrdkafkaLinkInfo = "static glibc_linux from librdkafka-static-bundle-v1.8.2.tgz" diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_musl_linux.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_musl_linux.go new file mode 100644 index 0000000..ed2bee5 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_musl_linux.go @@ -0,0 +1,13 @@ +// +build !dynamic +// +build musl + +// This file was auto-generated by librdkafka_vendor/bundle-import.sh, DO NOT EDIT. + +package kafka + +// #cgo CFLAGS: -DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB +// #cgo LDFLAGS: ${SRCDIR}/librdkafka_vendor/librdkafka_musl_linux.a -lm -ldl -lpthread -lrt -lpthread -lrt +import "C" + +// LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +const LibrdkafkaLinkInfo = "static musl_linux from librdkafka-static-bundle-v1.8.2.tgz" diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_windows.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_windows.go new file mode 100644 index 0000000..1c15c55 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/build_windows.go @@ -0,0 +1,13 @@ +// +build !dynamic + + +// This file was auto-generated by librdkafka_vendor/bundle-import.sh, DO NOT EDIT. + +package kafka + +// #cgo CFLAGS: -DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB +// #cgo LDFLAGS: ${SRCDIR}/librdkafka_vendor/librdkafka_windows.a -lws2_32 -lsecur32 -lcrypt32 +import "C" + +// LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client +const LibrdkafkaLinkInfo = "static windows from librdkafka-static-bundle-v1.8.2.tgz" diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/config.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/config.go new file mode 100644 index 0000000..f0c8ecf --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/config.go @@ -0,0 +1,280 @@ +package kafka + +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" + "reflect" + "strings" + "unsafe" +) + +/* +#include +#include "select_rdkafka.h" +*/ +import "C" + +// ConfigValue supports the following types: +// bool, int, string, any type with the standard String() interface +type ConfigValue interface{} + +// ConfigMap is a map containing standard librdkafka configuration properties as documented in: +// https://github.com/edenhill/librdkafka/tree/master/CONFIGURATION.md +// +// The special property "default.topic.config" (optional) is a ConfigMap +// containing default topic configuration properties. +// +// The use of "default.topic.config" is deprecated, +// topic configuration properties shall be specified in the standard ConfigMap. +// For backwards compatibility, "default.topic.config" (if supplied) +// takes precedence. +type ConfigMap map[string]ConfigValue + +// SetKey sets configuration property key to value. +// +// For user convenience a key prefixed with {topic}. will be +// set on the "default.topic.config" sub-map, this use is deprecated. +func (m ConfigMap) SetKey(key string, value ConfigValue) error { + if strings.HasPrefix(key, "{topic}.") { + _, found := m["default.topic.config"] + if !found { + m["default.topic.config"] = ConfigMap{} + } + m["default.topic.config"].(ConfigMap)[strings.TrimPrefix(key, "{topic}.")] = value + } else { + m[key] = value + } + + return nil +} + +// Set implements flag.Set (command line argument parser) as a convenience +// for `-X key=value` config. +func (m ConfigMap) Set(kv string) error { + i := strings.Index(kv, "=") + if i == -1 { + return newErrorFromString(ErrInvalidArg, "Expected key=value") + } + + k := kv[:i] + v := kv[i+1:] + + return m.SetKey(k, v) +} + +func value2string(v ConfigValue) (ret string, errstr string) { + + switch x := v.(type) { + case bool: + if x { + ret = "true" + } else { + ret = "false" + } + case int: + ret = fmt.Sprintf("%d", x) + case string: + ret = x + case fmt.Stringer: + ret = x.String() + default: + return "", fmt.Sprintf("Invalid value type %T", v) + } + + return ret, "" +} + +// rdkAnyconf abstracts rd_kafka_conf_t and rd_kafka_topic_conf_t +// into a common interface. +type rdkAnyconf interface { + set(cKey *C.char, cVal *C.char, cErrstr *C.char, errstrSize int) C.rd_kafka_conf_res_t +} + +func anyconfSet(anyconf rdkAnyconf, key string, val ConfigValue) (err error) { + value, errstr := value2string(val) + if errstr != "" { + return newErrorFromString(ErrInvalidArg, fmt.Sprintf("%s for key %s (expected string,bool,int,ConfigMap)", errstr, key)) + } + cKey := C.CString(key) + defer C.free(unsafe.Pointer(cKey)) + cVal := C.CString(value) + defer C.free(unsafe.Pointer(cVal)) + cErrstr := (*C.char)(C.malloc(C.size_t(128))) + defer C.free(unsafe.Pointer(cErrstr)) + + if anyconf.set(cKey, cVal, cErrstr, 128) != C.RD_KAFKA_CONF_OK { + return newErrorFromCString(C.RD_KAFKA_RESP_ERR__INVALID_ARG, cErrstr) + } + + return nil +} + +// we need these typedefs to workaround a crash in golint +// when parsing the set() methods below +type rdkConf C.rd_kafka_conf_t +type rdkTopicConf C.rd_kafka_topic_conf_t + +func (cConf *rdkConf) set(cKey *C.char, cVal *C.char, cErrstr *C.char, errstrSize int) C.rd_kafka_conf_res_t { + return C.rd_kafka_conf_set((*C.rd_kafka_conf_t)(cConf), cKey, cVal, cErrstr, C.size_t(errstrSize)) +} + +func (ctopicConf *rdkTopicConf) set(cKey *C.char, cVal *C.char, cErrstr *C.char, errstrSize int) C.rd_kafka_conf_res_t { + return C.rd_kafka_topic_conf_set((*C.rd_kafka_topic_conf_t)(ctopicConf), cKey, cVal, cErrstr, C.size_t(errstrSize)) +} + +func configConvertAnyconf(m ConfigMap, anyconf rdkAnyconf) (err error) { + // set plugins first, any plugin-specific configuration depends on + // the plugin to have already been set + pluginPaths, ok := m["plugin.library.paths"] + if ok { + err = anyconfSet(anyconf, "plugin.library.paths", pluginPaths) + if err != nil { + return err + } + } + for k, v := range m { + if k == "plugin.library.paths" { + continue + } + switch v.(type) { + case ConfigMap: + /* Special sub-ConfigMap, only used for default.topic.config */ + + if k != "default.topic.config" { + return newErrorFromString(ErrInvalidArg, fmt.Sprintf("Invalid type for key %s", k)) + } + + var cTopicConf = C.rd_kafka_topic_conf_new() + + err = configConvertAnyconf(v.(ConfigMap), + (*rdkTopicConf)(cTopicConf)) + if err != nil { + C.rd_kafka_topic_conf_destroy(cTopicConf) + return err + } + + C.rd_kafka_conf_set_default_topic_conf( + (*C.rd_kafka_conf_t)(anyconf.(*rdkConf)), + (*C.rd_kafka_topic_conf_t)((*rdkTopicConf)(cTopicConf))) + + default: + err = anyconfSet(anyconf, k, v) + if err != nil { + return err + } + } + } + + return nil +} + +// convert ConfigMap to C rd_kafka_conf_t * +func (m ConfigMap) convert() (cConf *C.rd_kafka_conf_t, err error) { + cConf = C.rd_kafka_conf_new() + + // Set the client.software.name and .version (use librdkafka version). + _, librdkafkaVersion := LibraryVersion() + anyconfSet((*rdkConf)(cConf), "client.software.name", "confluent-kafka-go") + anyconfSet((*rdkConf)(cConf), "client.software.version", librdkafkaVersion) + + err = configConvertAnyconf(m, (*rdkConf)(cConf)) + if err != nil { + C.rd_kafka_conf_destroy(cConf) + return nil, err + } + return cConf, nil +} + +// get finds key in the configmap and returns its value. +// If the key is not found defval is returned. +// If the key is found but the type is mismatched an error is returned. +func (m ConfigMap) get(key string, defval ConfigValue) (ConfigValue, error) { + if strings.HasPrefix(key, "{topic}.") { + defconfCv, found := m["default.topic.config"] + if !found { + return defval, nil + } + return defconfCv.(ConfigMap).get(strings.TrimPrefix(key, "{topic}."), defval) + } + + v, ok := m[key] + if !ok { + return defval, nil + } + + if defval != nil && reflect.TypeOf(defval) != reflect.TypeOf(v) { + return nil, newErrorFromString(ErrInvalidArg, fmt.Sprintf("%s expects type %T, not %T", key, defval, v)) + } + + return v, nil +} + +// extract performs a get() and if found deletes the key. +func (m ConfigMap) extract(key string, defval ConfigValue) (ConfigValue, error) { + + v, err := m.get(key, defval) + if err != nil { + return nil, err + } + + delete(m, key) + + return v, nil +} + +// extractLogConfig extracts generic go.logs.* configuration properties. +func (m ConfigMap) extractLogConfig() (logsChanEnable bool, logsChan chan LogEvent, err error) { + v, err := m.extract("go.logs.channel.enable", false) + if err != nil { + return + } + + logsChanEnable = v.(bool) + + v, err = m.extract("go.logs.channel", nil) + if err != nil { + return + } + + if v != nil { + logsChan = v.(chan LogEvent) + } + + if logsChanEnable { + // Tell librdkafka to forward logs to the log queue + m.Set("log.queue=true") + } + + return +} + +func (m ConfigMap) clone() ConfigMap { + m2 := make(ConfigMap) + for k, v := range m { + m2[k] = v + } + return m2 +} + +// Get finds the given key in the ConfigMap and returns its value. +// If the key is not found `defval` is returned. +// If the key is found but the type does not match that of `defval` (unless nil) +// an ErrInvalidArg error is returned. +func (m ConfigMap) Get(key string, defval ConfigValue) (ConfigValue, error) { + return m.get(key, defval) +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/consumer.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/consumer.go new file mode 100644 index 0000000..ef48330 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/consumer.go @@ -0,0 +1,923 @@ +package kafka + +/** + * Copyright 2016-2020 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" + "math" + "time" + "unsafe" +) + +/* +#include +#include "select_rdkafka.h" + + +static rd_kafka_topic_partition_t *_c_rdkafka_topic_partition_list_entry(rd_kafka_topic_partition_list_t *rktparlist, int idx) { + return idx < rktparlist->cnt ? &rktparlist->elems[idx] : NULL; +} +*/ +import "C" + +// RebalanceCb provides a per-Subscribe*() rebalance event callback. +// The passed Event will be either AssignedPartitions or RevokedPartitions +type RebalanceCb func(*Consumer, Event) error + +// Consumer implements a High-level Apache Kafka Consumer instance +type Consumer struct { + events chan Event + handle handle + eventsChanEnable bool + readerTermChan chan bool + rebalanceCb RebalanceCb + appReassigned bool + appRebalanceEnable bool // Config setting +} + +// Strings returns a human readable name for a Consumer instance +func (c *Consumer) String() string { + return c.handle.String() +} + +// getHandle implements the Handle interface +func (c *Consumer) gethandle() *handle { + return &c.handle +} + +// Subscribe to a single topic +// This replaces the current subscription +func (c *Consumer) Subscribe(topic string, rebalanceCb RebalanceCb) error { + return c.SubscribeTopics([]string{topic}, rebalanceCb) +} + +// SubscribeTopics subscribes to the provided list of topics. +// This replaces the current subscription. +func (c *Consumer) SubscribeTopics(topics []string, rebalanceCb RebalanceCb) (err error) { + ctopics := C.rd_kafka_topic_partition_list_new(C.int(len(topics))) + defer C.rd_kafka_topic_partition_list_destroy(ctopics) + + for _, topic := range topics { + ctopic := C.CString(topic) + defer C.free(unsafe.Pointer(ctopic)) + C.rd_kafka_topic_partition_list_add(ctopics, ctopic, C.RD_KAFKA_PARTITION_UA) + } + + e := C.rd_kafka_subscribe(c.handle.rk, ctopics) + if e != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(e) + } + + c.rebalanceCb = rebalanceCb + + return nil +} + +// Unsubscribe from the current subscription, if any. +func (c *Consumer) Unsubscribe() (err error) { + C.rd_kafka_unsubscribe(c.handle.rk) + return nil +} + +// Assign an atomic set of partitions to consume. +// +// The .Offset field of each TopicPartition must either be set to an absolute +// starting offset (>= 0), or one of the logical offsets (`kafka.OffsetEnd` etc), +// but should typically be set to `kafka.OffsetStored` to have the consumer +// use the committed offset as a start position, with a fallback to +// `auto.offset.reset` if there is no committed offset. +// +// This replaces the current assignment. +func (c *Consumer) Assign(partitions []TopicPartition) (err error) { + c.appReassigned = true + + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + + e := C.rd_kafka_assign(c.handle.rk, cparts) + if e != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(e) + } + + return nil +} + +// Unassign the current set of partitions to consume. +func (c *Consumer) Unassign() (err error) { + c.appReassigned = true + + e := C.rd_kafka_assign(c.handle.rk, nil) + if e != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(e) + } + + return nil +} + +// IncrementalAssign adds the specified partitions to the current set of +// partitions to consume. +// +// The .Offset field of each TopicPartition must either be set to an absolute +// starting offset (>= 0), or one of the logical offsets (`kafka.OffsetEnd` etc), +// but should typically be set to `kafka.OffsetStored` to have the consumer +// use the committed offset as a start position, with a fallback to +// `auto.offset.reset` if there is no committed offset. +// +// The new partitions must not be part of the current assignment. +func (c *Consumer) IncrementalAssign(partitions []TopicPartition) (err error) { + c.appReassigned = true + + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + + cError := C.rd_kafka_incremental_assign(c.handle.rk, cparts) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// IncrementalUnassign removes the specified partitions from the current set of +// partitions to consume. +// +// The .Offset field of the TopicPartition is ignored. +// +// The removed partitions must be part of the current assignment. +func (c *Consumer) IncrementalUnassign(partitions []TopicPartition) (err error) { + c.appReassigned = true + + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + + cError := C.rd_kafka_incremental_unassign(c.handle.rk, cparts) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// GetRebalanceProtocol returns the current consumer group rebalance protocol, +// which is either "EAGER" or "COOPERATIVE". +// If the rebalance protocol is not known in the current state an empty string +// is returned. +// Should typically only be called during rebalancing. +func (c *Consumer) GetRebalanceProtocol() string { + cStr := C.rd_kafka_rebalance_protocol(c.handle.rk) + if cStr == nil { + return "" + } + + return C.GoString(cStr) +} + +// AssignmentLost returns true if current partition assignment has been lost. +// This method is only applicable for use with a subscribing consumer when +// handling a rebalance event or callback. +// Partitions that have been lost may already be owned by other members in the +// group and therefore commiting offsets, for example, may fail. +func (c *Consumer) AssignmentLost() bool { + return cint2bool(C.rd_kafka_assignment_lost(c.handle.rk)) +} + +// commit offsets for specified offsets. +// If offsets is nil the currently assigned partitions' offsets are committed. +// This is a blocking call, caller will need to wrap in go-routine to +// get async or throw-away behaviour. +func (c *Consumer) commit(offsets []TopicPartition) (committedOffsets []TopicPartition, err error) { + var rkqu *C.rd_kafka_queue_t + + rkqu = C.rd_kafka_queue_new(c.handle.rk) + defer C.rd_kafka_queue_destroy(rkqu) + + var coffsets *C.rd_kafka_topic_partition_list_t + if offsets != nil { + coffsets = newCPartsFromTopicPartitions(offsets) + defer C.rd_kafka_topic_partition_list_destroy(coffsets) + } + + cErr := C.rd_kafka_commit_queue(c.handle.rk, coffsets, rkqu, nil, nil) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cErr) + } + + rkev := C.rd_kafka_queue_poll(rkqu, C.int(-1)) + if rkev == nil { + // shouldn't happen + return nil, newError(C.RD_KAFKA_RESP_ERR__DESTROY) + } + defer C.rd_kafka_event_destroy(rkev) + + if C.rd_kafka_event_type(rkev) != C.RD_KAFKA_EVENT_OFFSET_COMMIT { + panic(fmt.Sprintf("Expected OFFSET_COMMIT, got %s", + C.GoString(C.rd_kafka_event_name(rkev)))) + } + + cErr = C.rd_kafka_event_error(rkev) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newErrorFromCString(cErr, C.rd_kafka_event_error_string(rkev)) + } + + cRetoffsets := C.rd_kafka_event_topic_partition_list(rkev) + if cRetoffsets == nil { + // no offsets, no error + return nil, nil + } + committedOffsets = newTopicPartitionsFromCparts(cRetoffsets) + + return committedOffsets, nil +} + +// Commit offsets for currently assigned partitions +// This is a blocking call. +// Returns the committed offsets on success. +func (c *Consumer) Commit() ([]TopicPartition, error) { + return c.commit(nil) +} + +// CommitMessage commits offset based on the provided message. +// This is a blocking call. +// Returns the committed offsets on success. +func (c *Consumer) CommitMessage(m *Message) ([]TopicPartition, error) { + if m.TopicPartition.Error != nil { + return nil, newErrorFromString(ErrInvalidArg, "Can't commit errored message") + } + offsets := []TopicPartition{m.TopicPartition} + offsets[0].Offset++ + return c.commit(offsets) +} + +// CommitOffsets commits the provided list of offsets +// This is a blocking call. +// Returns the committed offsets on success. +func (c *Consumer) CommitOffsets(offsets []TopicPartition) ([]TopicPartition, error) { + return c.commit(offsets) +} + +// StoreOffsets stores the provided list of offsets that will be committed +// to the offset store according to `auto.commit.interval.ms` or manual +// offset-less Commit(). +// +// Returns the stored offsets on success. If at least one offset couldn't be stored, +// an error and a list of offsets is returned. Each offset can be checked for +// specific errors via its `.Error` member. +func (c *Consumer) StoreOffsets(offsets []TopicPartition) (storedOffsets []TopicPartition, err error) { + coffsets := newCPartsFromTopicPartitions(offsets) + defer C.rd_kafka_topic_partition_list_destroy(coffsets) + + cErr := C.rd_kafka_offsets_store(c.handle.rk, coffsets) + + // coffsets might be annotated with an error + storedOffsets = newTopicPartitionsFromCparts(coffsets) + + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return storedOffsets, newError(cErr) + } + + return storedOffsets, nil +} + +// StoreMessage stores offset based on the provided message. +// This is a convenience method that uses StoreOffsets to do the actual work. +func (c *Consumer) StoreMessage(m *Message) (storedOffsets []TopicPartition, err error) { + if m.TopicPartition.Error != nil { + return nil, newErrorFromString(ErrInvalidArg, "Can't store errored message") + } + if m.TopicPartition.Offset < 0 { + return nil, newErrorFromString(ErrInvalidArg, "Can't store message with offset less than 0") + } + offsets := []TopicPartition{m.TopicPartition} + offsets[0].Offset++ + return c.StoreOffsets(offsets) +} + +// Seek seeks the given topic partitions using the offset from the TopicPartition. +// +// If timeoutMs is not 0 the call will wait this long for the +// seek to be performed. If the timeout is reached the internal state +// will be unknown and this function returns ErrTimedOut. +// If timeoutMs is 0 it will initiate the seek but return +// immediately without any error reporting (e.g., async). +// +// Seek() may only be used for partitions already being consumed +// (through Assign() or implicitly through a self-rebalanced Subscribe()). +// To set the starting offset it is preferred to use Assign() and provide +// a starting offset for each partition. +// +// Returns an error on failure or nil otherwise. +func (c *Consumer) Seek(partition TopicPartition, timeoutMs int) error { + rkt := c.handle.getRkt(*partition.Topic) + cErr := C.rd_kafka_seek(rkt, + C.int32_t(partition.Partition), + C.int64_t(partition.Offset), + C.int(timeoutMs)) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(cErr) + } + return nil +} + +// Poll the consumer for messages or events. +// +// Will block for at most timeoutMs milliseconds +// +// The following callbacks may be triggered: +// Subscribe()'s rebalanceCb +// +// Returns nil on timeout, else an Event +func (c *Consumer) Poll(timeoutMs int) (event Event) { + ev, _ := c.handle.eventPoll(nil, timeoutMs, 1, nil) + return ev +} + +// Events returns the Events channel (if enabled) +func (c *Consumer) Events() chan Event { + return c.events +} + +// Logs returns the log channel if enabled, or nil otherwise. +func (c *Consumer) Logs() chan LogEvent { + return c.handle.logs +} + +// ReadMessage polls the consumer for a message. +// +// This is a convenience API that wraps Poll() and only returns +// messages or errors. All other event types are discarded. +// +// The call will block for at most `timeout` waiting for +// a new message or error. `timeout` may be set to -1 for +// indefinite wait. +// +// Timeout is returned as (nil, err) where err is `err.(kafka.Error).Code() == kafka.ErrTimedOut`. +// +// Messages are returned as (msg, nil), +// while general errors are returned as (nil, err), +// and partition-specific errors are returned as (msg, err) where +// msg.TopicPartition provides partition-specific information (such as topic, partition and offset). +// +// All other event types, such as PartitionEOF, AssignedPartitions, etc, are silently discarded. +// +func (c *Consumer) ReadMessage(timeout time.Duration) (*Message, error) { + + var absTimeout time.Time + var timeoutMs int + + if timeout > 0 { + absTimeout = time.Now().Add(timeout) + timeoutMs = (int)(timeout.Seconds() * 1000.0) + } else { + timeoutMs = (int)(timeout) + } + + for { + ev := c.Poll(timeoutMs) + + switch e := ev.(type) { + case *Message: + if e.TopicPartition.Error != nil { + return e, e.TopicPartition.Error + } + return e, nil + case Error: + return nil, e + default: + // Ignore other event types + } + + if timeout > 0 { + // Calculate remaining time + timeoutMs = int(math.Max(0.0, absTimeout.Sub(time.Now()).Seconds()*1000.0)) + } + + if timeoutMs == 0 && ev == nil { + return nil, newError(C.RD_KAFKA_RESP_ERR__TIMED_OUT) + } + + } + +} + +// Close Consumer instance. +// The object is no longer usable after this call. +func (c *Consumer) Close() (err error) { + + // Wait for consumerReader() or pollLogEvents to terminate (by closing readerTermChan) + close(c.readerTermChan) + c.handle.waitGroup.Wait() + if c.eventsChanEnable { + close(c.events) + } + + // librdkafka's rd_kafka_consumer_close() will block + // and trigger the rebalance_cb() if one is set, if not, which is the + // case with the Go client since it registers EVENTs rather than callbacks, + // librdkafka will shortcut the rebalance_cb and do a forced unassign. + // But we can't have that since the application might need the final RevokePartitions + // before shutting down. So we trigger an Unsubscribe() first, wait for that to + // propagate (in the Poll loop below), and then close the consumer. + c.Unsubscribe() + + // Poll for rebalance events + for { + c.Poll(10 * 1000) + if int(C.rd_kafka_queue_length(c.handle.rkq)) == 0 { + break + } + } + + // Destroy our queue + C.rd_kafka_queue_destroy(c.handle.rkq) + c.handle.rkq = nil + + // Close the consumer + C.rd_kafka_consumer_close(c.handle.rk) + + c.handle.cleanup() + + C.rd_kafka_destroy(c.handle.rk) + + return nil +} + +// NewConsumer creates a new high-level Consumer instance. +// +// conf is a *ConfigMap with standard librdkafka configuration properties. +// +// Supported special configuration properties: +// go.application.rebalance.enable (bool, false) - Forward rebalancing responsibility to application via the Events() channel. +// If set to true the app must handle the AssignedPartitions and +// RevokedPartitions events and call Assign() and Unassign() +// respectively. +// go.events.channel.enable (bool, false) - [deprecated] Enable the Events() channel. Messages and events will be pushed on the Events() channel and the Poll() interface will be disabled. +// go.events.channel.size (int, 1000) - Events() channel size +// go.logs.channel.enable (bool, false) - Forward log to Logs() channel. +// go.logs.channel (chan kafka.LogEvent, nil) - Forward logs to application-provided channel instead of Logs(). Requires go.logs.channel.enable=true. +// +// WARNING: Due to the buffering nature of channels (and queues in general) the +// use of the events channel risks receiving outdated events and +// messages. Minimizing go.events.channel.size reduces the risk +// and number of outdated events and messages but does not eliminate +// the factor completely. With a channel size of 1 at most one +// event or message may be outdated. +func NewConsumer(conf *ConfigMap) (*Consumer, error) { + + err := versionCheck() + if err != nil { + return nil, err + } + + // before we do anything with the configuration, create a copy such that + // the original is not mutated. + confCopy := conf.clone() + + groupid, _ := confCopy.get("group.id", nil) + if groupid == nil { + // without a group.id the underlying cgrp subsystem in librdkafka wont get started + // and without it there is no way to consume assigned partitions. + // So for now require the group.id, this might change in the future. + return nil, newErrorFromString(ErrInvalidArg, "Required property group.id not set") + } + + c := &Consumer{} + + v, err := confCopy.extract("go.application.rebalance.enable", false) + if err != nil { + return nil, err + } + c.appRebalanceEnable = v.(bool) + + v, err = confCopy.extract("go.events.channel.enable", false) + if err != nil { + return nil, err + } + c.eventsChanEnable = v.(bool) + + v, err = confCopy.extract("go.events.channel.size", 1000) + if err != nil { + return nil, err + } + eventsChanSize := v.(int) + + logsChanEnable, logsChan, err := confCopy.extractLogConfig() + if err != nil { + return nil, err + } + + cConf, err := confCopy.convert() + if err != nil { + return nil, err + } + cErrstr := (*C.char)(C.malloc(C.size_t(256))) + defer C.free(unsafe.Pointer(cErrstr)) + + C.rd_kafka_conf_set_events(cConf, C.RD_KAFKA_EVENT_REBALANCE|C.RD_KAFKA_EVENT_OFFSET_COMMIT|C.RD_KAFKA_EVENT_STATS|C.RD_KAFKA_EVENT_ERROR|C.RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH) + + c.handle.rk = C.rd_kafka_new(C.RD_KAFKA_CONSUMER, cConf, cErrstr, 256) + if c.handle.rk == nil { + return nil, newErrorFromCString(C.RD_KAFKA_RESP_ERR__INVALID_ARG, cErrstr) + } + + C.rd_kafka_poll_set_consumer(c.handle.rk) + + c.handle.c = c + c.handle.setup() + c.readerTermChan = make(chan bool) + c.handle.rkq = C.rd_kafka_queue_get_consumer(c.handle.rk) + if c.handle.rkq == nil { + // no cgrp (no group.id configured), revert to main queue. + c.handle.rkq = C.rd_kafka_queue_get_main(c.handle.rk) + } + + if logsChanEnable { + c.handle.setupLogQueue(logsChan, c.readerTermChan) + } + + if c.eventsChanEnable { + c.events = make(chan Event, eventsChanSize) + /* Start rdkafka consumer queue reader -> events writer goroutine */ + c.handle.waitGroup.Add(1) + go func() { + consumerReader(c, c.readerTermChan) + c.handle.waitGroup.Done() + }() + } + + return c, nil +} + +// consumerReader reads messages and events from the librdkafka consumer queue +// and posts them on the consumer channel. +// Runs until termChan closes +func consumerReader(c *Consumer, termChan chan bool) { + for { + select { + case _ = <-termChan: + return + default: + _, term := c.handle.eventPoll(c.events, 100, 1000, termChan) + if term { + return + } + + } + } +} + +// GetMetadata queries broker for cluster and topic metadata. +// If topic is non-nil only information about that topic is returned, else if +// allTopics is false only information about locally used topics is returned, +// else information about all topics is returned. +// GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +func (c *Consumer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) { + return getMetadata(c, topic, allTopics, timeoutMs) +} + +// QueryWatermarkOffsets queries the broker for the low and high offsets for the given topic and partition. +func (c *Consumer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error) { + return queryWatermarkOffsets(c, topic, partition, timeoutMs) +} + +// GetWatermarkOffsets returns the cached low and high offsets for the given topic +// and partition. The high offset is populated on every fetch response or via calling QueryWatermarkOffsets. +// The low offset is populated every statistics.interval.ms if that value is set. +// OffsetInvalid will be returned if there is no cached offset for either value. +func (c *Consumer) GetWatermarkOffsets(topic string, partition int32) (low, high int64, err error) { + return getWatermarkOffsets(c, topic, partition) +} + +// OffsetsForTimes looks up offsets by timestamp for the given partitions. +// +// The returned offset for each partition is the earliest offset whose +// timestamp is greater than or equal to the given timestamp in the +// corresponding partition. If the provided timestamp exceeds that of the +// last message in the partition, a value of -1 will be returned. +// +// The timestamps to query are represented as `.Offset` in the `times` +// argument and the looked up offsets are represented as `.Offset` in the returned +// `offsets` list. +// +// The function will block for at most timeoutMs milliseconds. +// +// Duplicate Topic+Partitions are not supported. +// Per-partition errors may be returned in the `.Error` field. +func (c *Consumer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) { + return offsetsForTimes(c, times, timeoutMs) +} + +// Subscription returns the current subscription as set by Subscribe() +func (c *Consumer) Subscription() (topics []string, err error) { + var cTopics *C.rd_kafka_topic_partition_list_t + + cErr := C.rd_kafka_subscription(c.handle.rk, &cTopics) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cErr) + } + defer C.rd_kafka_topic_partition_list_destroy(cTopics) + + topicCnt := int(cTopics.cnt) + topics = make([]string, topicCnt) + for i := 0; i < topicCnt; i++ { + crktpar := C._c_rdkafka_topic_partition_list_entry(cTopics, + C.int(i)) + topics[i] = C.GoString(crktpar.topic) + } + + return topics, nil +} + +// Assignment returns the current partition assignments +func (c *Consumer) Assignment() (partitions []TopicPartition, err error) { + var cParts *C.rd_kafka_topic_partition_list_t + + cErr := C.rd_kafka_assignment(c.handle.rk, &cParts) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cErr) + } + defer C.rd_kafka_topic_partition_list_destroy(cParts) + + partitions = newTopicPartitionsFromCparts(cParts) + + return partitions, nil +} + +// Committed retrieves committed offsets for the given set of partitions +func (c *Consumer) Committed(partitions []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) { + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + cerr := C.rd_kafka_committed(c.handle.rk, cparts, C.int(timeoutMs)) + if cerr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cerr) + } + + return newTopicPartitionsFromCparts(cparts), nil +} + +// Position returns the current consume position for the given partitions. +// Typical use is to call Assignment() to get the partition list +// and then pass it to Position() to get the current consume position for +// each of the assigned partitions. +// The consume position is the next message to read from the partition. +// i.e., the offset of the last message seen by the application + 1. +func (c *Consumer) Position(partitions []TopicPartition) (offsets []TopicPartition, err error) { + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + cerr := C.rd_kafka_position(c.handle.rk, cparts) + if cerr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cerr) + } + + return newTopicPartitionsFromCparts(cparts), nil +} + +// Pause consumption for the provided list of partitions +// +// Note that messages already enqueued on the consumer's Event channel +// (if `go.events.channel.enable` has been set) will NOT be purged by +// this call, set `go.events.channel.size` accordingly. +func (c *Consumer) Pause(partitions []TopicPartition) (err error) { + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + cerr := C.rd_kafka_pause_partitions(c.handle.rk, cparts) + if cerr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(cerr) + } + return nil +} + +// Resume consumption for the provided list of partitions +func (c *Consumer) Resume(partitions []TopicPartition) (err error) { + cparts := newCPartsFromTopicPartitions(partitions) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + cerr := C.rd_kafka_resume_partitions(c.handle.rk, cparts) + if cerr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(cerr) + } + return nil +} + +// SetOAuthBearerToken sets the the data to be transmitted +// to a broker during SASL/OAUTHBEARER authentication. It will return nil +// on success, otherwise an error if: +// 1) the token data is invalid (meaning an expiration time in the past +// or either a token value or an extension key or value that does not meet +// the regular expression requirements as per +// https://tools.ietf.org/html/rfc7628#section-3.1); +// 2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 3) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (c *Consumer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error { + return c.handle.setOAuthBearerToken(oauthBearerToken) +} + +// SetOAuthBearerTokenFailure sets the error message describing why token +// retrieval/setting failed; it also schedules a new token refresh event for 10 +// seconds later so the attempt may be retried. It will return nil on +// success, otherwise an error if: +// 1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 2) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (c *Consumer) SetOAuthBearerTokenFailure(errstr string) error { + return c.handle.setOAuthBearerTokenFailure(errstr) +} + +// ConsumerGroupMetadata reflects the current consumer group member metadata. +type ConsumerGroupMetadata struct { + serialized []byte +} + +// serializeConsumerGroupMetadata converts a C metadata object to its +// binary representation so we don't have to hold on to the C object, +// which would require an explicit .Close(). +func serializeConsumerGroupMetadata(cgmd *C.rd_kafka_consumer_group_metadata_t) ([]byte, error) { + var cBuffer *C.void + var cSize C.size_t + cError := C.rd_kafka_consumer_group_metadata_write(cgmd, + (*unsafe.Pointer)(unsafe.Pointer(&cBuffer)), &cSize) + if cError != nil { + return nil, newErrorFromCErrorDestroy(cError) + } + defer C.rd_kafka_mem_free(nil, unsafe.Pointer(cBuffer)) + + return C.GoBytes(unsafe.Pointer(cBuffer), C.int(cSize)), nil +} + +// deserializeConsumerGroupMetadata converts a serialized metadata object +// back to a C object. +func deserializeConsumerGroupMetadata(serialized []byte) (*C.rd_kafka_consumer_group_metadata_t, error) { + var cgmd *C.rd_kafka_consumer_group_metadata_t + + cSerialized := C.CBytes(serialized) + defer C.free(cSerialized) + + cError := C.rd_kafka_consumer_group_metadata_read( + &cgmd, cSerialized, C.size_t(len(serialized))) + if cError != nil { + return nil, newErrorFromCErrorDestroy(cError) + } + + return cgmd, nil +} + +// GetConsumerGroupMetadata returns the consumer's current group metadata. +// This object should be passed to the transactional producer's +// SendOffsetsToTransaction() API. +func (c *Consumer) GetConsumerGroupMetadata() (*ConsumerGroupMetadata, error) { + cgmd := C.rd_kafka_consumer_group_metadata(c.handle.rk) + if cgmd == nil { + return nil, NewError(ErrState, "Consumer group metadata not available", false) + } + defer C.rd_kafka_consumer_group_metadata_destroy(cgmd) + + serialized, err := serializeConsumerGroupMetadata(cgmd) + if err != nil { + return nil, err + } + + return &ConsumerGroupMetadata{serialized}, nil +} + +// NewTestConsumerGroupMetadata creates a new consumer group metadata instance +// mainly for testing use. +// Use GetConsumerGroupMetadata() to retrieve the real metadata. +func NewTestConsumerGroupMetadata(groupID string) (*ConsumerGroupMetadata, error) { + cGroupID := C.CString(groupID) + defer C.free(unsafe.Pointer(cGroupID)) + + cgmd := C.rd_kafka_consumer_group_metadata_new(cGroupID) + if cgmd == nil { + return nil, NewError(ErrInvalidArg, "Failed to create metadata object", false) + } + + defer C.rd_kafka_consumer_group_metadata_destroy(cgmd) + serialized, err := serializeConsumerGroupMetadata(cgmd) + if err != nil { + return nil, err + } + + return &ConsumerGroupMetadata{serialized}, nil +} + +// cEventToRebalanceEvent returns an Event (AssignedPartitions or RevokedPartitions) +// based on the specified rkev. +func cEventToRebalanceEvent(rkev *C.rd_kafka_event_t) Event { + if C.rd_kafka_event_error(rkev) == C.RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS { + var ev AssignedPartitions + ev.Partitions = newTopicPartitionsFromCparts(C.rd_kafka_event_topic_partition_list(rkev)) + return ev + } else if C.rd_kafka_event_error(rkev) == C.RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS { + var ev RevokedPartitions + ev.Partitions = newTopicPartitionsFromCparts(C.rd_kafka_event_topic_partition_list(rkev)) + return ev + } else { + panic(fmt.Sprintf("Unable to create rebalance event from C type %s", + C.GoString(C.rd_kafka_err2name(C.rd_kafka_event_error(rkev))))) + } + +} + +// handleRebalanceEvent handles a assign/rebalance rebalance event. +// +// If the app provided a RebalanceCb to Subscribe*() or +// has go.application.rebalance.enable=true we create an event +// and forward it to the application thru the RebalanceCb or the +// Events channel respectively. +// Since librdkafka requires the rebalance event to be "acked" by +// the application (by calling *assign()) to synchronize state we keep track +// of if the application performed *Assign() or *Unassign(), but this only +// works for the non-channel case. For the channel case we assume the +// application calls *Assign() or *Unassign(). +// Failure to do so will "hang" the consumer, e.g., it wont start consuming +// and it wont close cleanly, so this error case should be visible +// immediately to the application developer. +// +// In the polling case (not channel based consumer) the rebalance event +// is returned in retval, else nil is returned. + +func (c *Consumer) handleRebalanceEvent(channel chan Event, rkev *C.rd_kafka_event_t) (retval Event) { + + var ev Event + + if c.rebalanceCb != nil || c.appRebalanceEnable { + // Application has a rebalance callback or has enabled + // rebalances on the events channel, create the appropriate Event. + ev = cEventToRebalanceEvent(rkev) + + } + + if channel != nil && c.appRebalanceEnable && c.rebalanceCb == nil { + // Channel-based consumer with rebalancing enabled, + // return the rebalance event and rely on the application + // to call *Assign() / *Unassign(). + return ev + } + + // Call the application's rebalance callback, if any. + if c.rebalanceCb != nil { + // Mark .appReassigned as false to keep track of whether the + // application called *Assign() / *Unassign(). + c.appReassigned = false + + c.rebalanceCb(c, ev) + + if c.appReassigned { + // Rebalance event handled by application. + return nil + } + } + + // Either there was no rebalance callback, or the application + // did not call *Assign / *Unassign, so we need to do it. + + isCooperative := c.GetRebalanceProtocol() == "COOPERATIVE" + var cError *C.rd_kafka_error_t + var cErr C.rd_kafka_resp_err_t + + if C.rd_kafka_event_error(rkev) == C.RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS { + // Assign partitions + if isCooperative { + cError = C.rd_kafka_incremental_assign( + c.handle.rk, + C.rd_kafka_event_topic_partition_list(rkev)) + } else { + cErr = C.rd_kafka_assign( + c.handle.rk, + C.rd_kafka_event_topic_partition_list(rkev)) + } + } else { + // Revoke partitions + + if isCooperative { + cError = C.rd_kafka_incremental_unassign( + c.handle.rk, + C.rd_kafka_event_topic_partition_list(rkev)) + } else { + cErr = C.rd_kafka_assign(c.handle.rk, nil) + } + } + + // If the *assign() call returned error, forward it to the + // the consumer's Events() channel for visibility. + if cError != nil { + c.events <- newErrorFromCErrorDestroy(cError) + } else if cErr != 0 { + c.events <- newError(cErr) + } + + return nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/context.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/context.go new file mode 100644 index 0000000..85709be --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/context.go @@ -0,0 +1,31 @@ +/** + * Copyright 2019 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "context" + "time" +) + +// Timeout returns the remaining time after which work done on behalf of this context should be +// canceled, or ok==false if no deadline/timeout is set. +func timeout(ctx context.Context) (timeout time.Duration, ok bool) { + if deadline, ok := ctx.Deadline(); ok { + return deadline.Sub(time.Now()), true + } + return 0, false +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error.go new file mode 100644 index 0000000..1827f43 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error.go @@ -0,0 +1,154 @@ +package kafka + +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Automatically generate error codes from librdkafka +// See README for instructions +//go:generate $GOPATH/bin/go_rdkafka_generr generated_errors.go + +/* +#include +#include "select_rdkafka.h" +*/ +import "C" + +import ( + "fmt" + "unsafe" +) + +// Error provides a Kafka-specific error container +type Error struct { + code ErrorCode + str string + fatal bool + retriable bool + txnRequiresAbort bool +} + +func newError(code C.rd_kafka_resp_err_t) (err Error) { + return Error{code: ErrorCode(code)} +} + +// NewError creates a new Error. +func NewError(code ErrorCode, str string, fatal bool) (err Error) { + return Error{code: code, str: str, fatal: fatal} +} + +func newErrorFromString(code ErrorCode, str string) (err Error) { + return Error{code: code, str: str} +} + +func newErrorFromCString(code C.rd_kafka_resp_err_t, cstr *C.char) (err Error) { + var str string + if cstr != nil { + str = C.GoString(cstr) + } else { + str = "" + } + return Error{code: ErrorCode(code), str: str} +} + +func newCErrorFromString(code C.rd_kafka_resp_err_t, str string) (err Error) { + return newErrorFromString(ErrorCode(code), str) +} + +// newErrorFromCError creates a new Error instance and destroys +// the passed cError. +func newErrorFromCErrorDestroy(cError *C.rd_kafka_error_t) Error { + defer C.rd_kafka_error_destroy(cError) + return Error{ + code: ErrorCode(C.rd_kafka_error_code(cError)), + str: C.GoString(C.rd_kafka_error_string(cError)), + fatal: cint2bool(C.rd_kafka_error_is_fatal(cError)), + retriable: cint2bool(C.rd_kafka_error_is_retriable(cError)), + txnRequiresAbort: cint2bool(C.rd_kafka_error_txn_requires_abort(cError)), + } +} + +// Error returns a human readable representation of an Error +// Same as Error.String() +func (e Error) Error() string { + return e.String() +} + +// String returns a human readable representation of an Error +func (e Error) String() string { + var errstr string + if len(e.str) > 0 { + errstr = e.str + } else { + errstr = e.code.String() + } + + if e.IsFatal() { + return fmt.Sprintf("Fatal error: %s", errstr) + } + + return errstr +} + +// Code returns the ErrorCode of an Error +func (e Error) Code() ErrorCode { + return e.code +} + +// IsFatal returns true if the error is a fatal error. +// A fatal error indicates the client instance is no longer operable and +// should be terminated. Typical causes include non-recoverable +// idempotent producer errors. +func (e Error) IsFatal() bool { + return e.fatal +} + +// IsRetriable returns true if the operation that caused this error +// may be retried. +// This flag is currently only set by the Transactional producer API. +func (e Error) IsRetriable() bool { + return e.retriable +} + +// TxnRequiresAbort returns true if the error is an abortable transaction error +// that requires the application to abort the current transaction with +// AbortTransaction() and start a new transaction with BeginTransaction() +// if it wishes to proceed with transactional operations. +// This flag is only set by the Transactional producer API. +func (e Error) TxnRequiresAbort() bool { + return e.txnRequiresAbort +} + +// getFatalError returns an Error object if the client instance has raised a fatal error, else nil. +func getFatalError(H Handle) error { + cErrstr := (*C.char)(C.malloc(C.size_t(512))) + defer C.free(unsafe.Pointer(cErrstr)) + + cErr := C.rd_kafka_fatal_error(H.gethandle().rk, cErrstr, 512) + if int(cErr) == 0 { + return nil + } + + err := newErrorFromCString(cErr, cErrstr) + err.fatal = true + + return err +} + +// testFatalError triggers a fatal error in the underlying client. +// This is to be used strictly for testing purposes. +func testFatalError(H Handle, code ErrorCode, str string) ErrorCode { + return ErrorCode(C.rd_kafka_test_fatal_error(H.gethandle().rk, C.rd_kafka_resp_err_t(code), C.CString(str))) +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error_gen.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error_gen.go new file mode 100644 index 0000000..4ceecb3 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/error_gen.go @@ -0,0 +1,112 @@ +package kafka + +/** + * Copyright 2020 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Automatically generate error codes from librdkafka +// See README for instructions +//go:generate $GOPATH/bin/go_rdkafka_generr generated_errors.go + +/* +#include +#include "select_rdkafka.h" + +static const char *errdesc_to_string (const struct rd_kafka_err_desc *ed, int idx) { + return ed[idx].name; +} + +static const char *errdesc_to_desc (const struct rd_kafka_err_desc *ed, int idx) { + return ed[idx].desc; +} +*/ +import "C" + +import ( + "fmt" + "os" + "strings" + "time" +) + +// camelCase transforms a snake_case string to camelCase. +func camelCase(s string) string { + ret := "" + for _, v := range strings.Split(s, "_") { + if len(v) == 0 { + continue + } + ret += strings.ToUpper((string)(v[0])) + strings.ToLower(v[1:]) + } + return ret +} + +// WriteErrorCodes writes Go error code constants to file from the +// librdkafka error codes. +// This function is not intended for public use. +func WriteErrorCodes(f *os.File) { + f.WriteString("package kafka\n") + now := time.Now() + f.WriteString(fmt.Sprintf("// Copyright 2016-%d Confluent Inc.\n", now.Year())) + f.WriteString(fmt.Sprintf("// AUTOMATICALLY GENERATED ON %v USING librdkafka %s\n", + now, C.GoString(C.rd_kafka_version_str()))) + + var errdescs *C.struct_rd_kafka_err_desc + var csize C.size_t + C.rd_kafka_get_err_descs(&errdescs, &csize) + + f.WriteString(` +/* +#include "select_rdkafka.h" +*/ +import "C" + +// ErrorCode is the integer representation of local and broker error codes +type ErrorCode int + +// String returns a human readable representation of an error code +func (c ErrorCode) String() string { + return C.GoString(C.rd_kafka_err2str(C.rd_kafka_resp_err_t(c))) +} + +const ( +`) + + for i := 0; i < int(csize); i++ { + orig := C.GoString(C.errdesc_to_string(errdescs, C.int(i))) + if len(orig) == 0 { + continue + } + desc := C.GoString(C.errdesc_to_desc(errdescs, C.int(i))) + if len(desc) == 0 { + continue + } + + errname := "Err" + camelCase(orig) + + // Special handling to please golint + // Eof -> EOF + // Id -> ID + errname = strings.Replace(errname, "Eof", "EOF", -1) + errname = strings.Replace(errname, "Id", "ID", -1) + + f.WriteString(fmt.Sprintf(" // %s %s\n", errname, desc)) + f.WriteString(fmt.Sprintf(" %s ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_%s)\n", + errname, orig)) + } + + f.WriteString(")\n") + +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/event.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/event.go new file mode 100644 index 0000000..6357ad8 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/event.go @@ -0,0 +1,316 @@ +package kafka + +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" + "os" + "unsafe" +) + +/* +#include +#include "select_rdkafka.h" +#include "glue_rdkafka.h" + + +void chdrs_to_tmphdrs (glue_msg_t *gMsg) { + size_t i = 0; + const char *name; + const void *val; + size_t size; + rd_kafka_headers_t *chdrs; + + if (rd_kafka_message_headers(gMsg->msg, &chdrs)) { + gMsg->tmphdrs = NULL; + gMsg->tmphdrsCnt = 0; + return; + } + + gMsg->tmphdrsCnt = rd_kafka_header_cnt(chdrs); + gMsg->tmphdrs = malloc(sizeof(*gMsg->tmphdrs) * gMsg->tmphdrsCnt); + + while (!rd_kafka_header_get_all(chdrs, i, + &gMsg->tmphdrs[i].key, + &gMsg->tmphdrs[i].val, + (size_t *)&gMsg->tmphdrs[i].size)) + i++; +} + +rd_kafka_event_t *_rk_queue_poll (rd_kafka_queue_t *rkq, int timeoutMs, + rd_kafka_event_type_t *evtype, + glue_msg_t *gMsg, + rd_kafka_event_t *prev_rkev) { + rd_kafka_event_t *rkev; + + if (prev_rkev) + rd_kafka_event_destroy(prev_rkev); + + rkev = rd_kafka_queue_poll(rkq, timeoutMs); + *evtype = rd_kafka_event_type(rkev); + + if (*evtype == RD_KAFKA_EVENT_FETCH) { + gMsg->msg = (rd_kafka_message_t *)rd_kafka_event_message_next(rkev); + gMsg->ts = rd_kafka_message_timestamp(gMsg->msg, &gMsg->tstype); + + if (gMsg->want_hdrs) + chdrs_to_tmphdrs(gMsg); + } + + return rkev; +} +*/ +import "C" + +func chdrsToTmphdrs(gMsg *C.glue_msg_t) { + C.chdrs_to_tmphdrs(gMsg) +} + +// Event generic interface +type Event interface { + // String returns a human-readable representation of the event + String() string +} + +// Specific event types + +// Stats statistics event +type Stats struct { + statsJSON string +} + +func (e Stats) String() string { + return e.statsJSON +} + +// AssignedPartitions consumer group rebalance event: assigned partition set +type AssignedPartitions struct { + Partitions []TopicPartition +} + +func (e AssignedPartitions) String() string { + return fmt.Sprintf("AssignedPartitions: %v", e.Partitions) +} + +// RevokedPartitions consumer group rebalance event: revoked partition set +type RevokedPartitions struct { + Partitions []TopicPartition +} + +func (e RevokedPartitions) String() string { + return fmt.Sprintf("RevokedPartitions: %v", e.Partitions) +} + +// PartitionEOF consumer reached end of partition +// Needs to be explicitly enabled by setting the `enable.partition.eof` +// configuration property to true. +type PartitionEOF TopicPartition + +func (p PartitionEOF) String() string { + return fmt.Sprintf("EOF at %s", TopicPartition(p)) +} + +// OffsetsCommitted reports committed offsets +type OffsetsCommitted struct { + Error error + Offsets []TopicPartition +} + +func (o OffsetsCommitted) String() string { + return fmt.Sprintf("OffsetsCommitted (%v, %v)", o.Error, o.Offsets) +} + +// OAuthBearerTokenRefresh indicates token refresh is required +type OAuthBearerTokenRefresh struct { + // Config is the value of the sasl.oauthbearer.config property + Config string +} + +func (o OAuthBearerTokenRefresh) String() string { + return "OAuthBearerTokenRefresh" +} + +// eventPoll polls an event from the handler's C rd_kafka_queue_t, +// translates it into an Event type and then sends on `channel` if non-nil, else returns the Event. +// term_chan is an optional channel to monitor along with producing to channel +// to indicate that `channel` is being terminated. +// returns (event Event, terminate Bool) tuple, where Terminate indicates +// if termChan received a termination event. +func (h *handle) eventPoll(channel chan Event, timeoutMs int, maxEvents int, termChan chan bool) (Event, bool) { + + var prevRkev *C.rd_kafka_event_t + term := false + + var retval Event + + if channel == nil { + maxEvents = 1 + } +out: + for evcnt := 0; evcnt < maxEvents; evcnt++ { + var evtype C.rd_kafka_event_type_t + var gMsg C.glue_msg_t + gMsg.want_hdrs = C.int8_t(bool2cint(h.msgFields.Headers)) + rkev := C._rk_queue_poll(h.rkq, C.int(timeoutMs), &evtype, &gMsg, prevRkev) + prevRkev = rkev + timeoutMs = 0 + + retval = nil + + switch evtype { + case C.RD_KAFKA_EVENT_FETCH: + // Consumer fetch event, new message. + // Extracted into temporary gMsg for optimization + retval = h.newMessageFromGlueMsg(&gMsg) + + case C.RD_KAFKA_EVENT_REBALANCE: + // Consumer rebalance event + retval = h.c.handleRebalanceEvent(channel, rkev) + + case C.RD_KAFKA_EVENT_ERROR: + // Error event + cErr := C.rd_kafka_event_error(rkev) + if cErr == C.RD_KAFKA_RESP_ERR__PARTITION_EOF { + crktpar := C.rd_kafka_event_topic_partition(rkev) + if crktpar == nil { + break + } + + defer C.rd_kafka_topic_partition_destroy(crktpar) + var peof PartitionEOF + setupTopicPartitionFromCrktpar((*TopicPartition)(&peof), crktpar) + + retval = peof + + } else if int(C.rd_kafka_event_error_is_fatal(rkev)) != 0 { + // A fatal error has been raised. + // Extract the actual error from the client + // instance and return a new Error with + // fatal set to true. + cFatalErrstrSize := C.size_t(512) + cFatalErrstr := (*C.char)(C.malloc(cFatalErrstrSize)) + defer C.free(unsafe.Pointer(cFatalErrstr)) + cFatalErr := C.rd_kafka_fatal_error(h.rk, cFatalErrstr, cFatalErrstrSize) + fatalErr := newErrorFromCString(cFatalErr, cFatalErrstr) + fatalErr.fatal = true + retval = fatalErr + + } else { + retval = newErrorFromCString(cErr, C.rd_kafka_event_error_string(rkev)) + } + + case C.RD_KAFKA_EVENT_STATS: + retval = &Stats{C.GoString(C.rd_kafka_event_stats(rkev))} + + case C.RD_KAFKA_EVENT_DR: + // Producer Delivery Report event + // Each such event contains delivery reports for all + // messages in the produced batch. + // Forward delivery reports to per-message's response channel + // or to the global Producer.Events channel, or none. + rkmessages := make([]*C.rd_kafka_message_t, int(C.rd_kafka_event_message_count(rkev))) + + cnt := int(C.rd_kafka_event_message_array(rkev, (**C.rd_kafka_message_t)(unsafe.Pointer(&rkmessages[0])), C.size_t(len(rkmessages)))) + + for _, rkmessage := range rkmessages[:cnt] { + msg := h.newMessageFromC(rkmessage) + var ch *chan Event + + if rkmessage._private != nil { + // Find cgoif by id + cg, found := h.cgoGet((int)((uintptr)(rkmessage._private))) + if found { + cdr := cg.(cgoDr) + + if cdr.deliveryChan != nil { + ch = &cdr.deliveryChan + } + msg.Opaque = cdr.opaque + } + } + + if ch == nil && h.fwdDr { + ch = &channel + } + + if ch != nil { + select { + case *ch <- msg: + case <-termChan: + retval = nil + term = true + break out + } + + } else { + retval = msg + break out + } + } + + case C.RD_KAFKA_EVENT_OFFSET_COMMIT: + // Offsets committed + cErr := C.rd_kafka_event_error(rkev) + coffsets := C.rd_kafka_event_topic_partition_list(rkev) + var offsets []TopicPartition + if coffsets != nil { + offsets = newTopicPartitionsFromCparts(coffsets) + } + + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + retval = OffsetsCommitted{newErrorFromCString(cErr, C.rd_kafka_event_error_string(rkev)), offsets} + } else { + retval = OffsetsCommitted{nil, offsets} + } + + case C.RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH: + ev := OAuthBearerTokenRefresh{C.GoString(C.rd_kafka_event_config_string(rkev))} + retval = ev + + case C.RD_KAFKA_EVENT_NONE: + // poll timed out: no events available + break out + + default: + if rkev != nil { + fmt.Fprintf(os.Stderr, "Ignored event %s\n", + C.GoString(C.rd_kafka_event_name(rkev))) + } + + } + + if retval != nil { + if channel != nil { + select { + case channel <- retval: + case <-termChan: + retval = nil + term = true + break out + } + } else { + break out + } + } + } + + if prevRkev != nil { + C.rd_kafka_event_destroy(prevRkev) + } + + return retval, term +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/generated_errors.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/generated_errors.go new file mode 100644 index 0000000..fb828f9 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/generated_errors.go @@ -0,0 +1,337 @@ +package kafka +// Copyright 2016-2021 Confluent Inc. +// AUTOMATICALLY GENERATED ON 2021-12-08 12:44:39.243338672 +0100 CET m=+0.000248284 USING librdkafka 1.8.2 + +/* +#include "select_rdkafka.h" +*/ +import "C" + +// ErrorCode is the integer representation of local and broker error codes +type ErrorCode int + +// String returns a human readable representation of an error code +func (c ErrorCode) String() string { + return C.GoString(C.rd_kafka_err2str(C.rd_kafka_resp_err_t(c))) +} + +const ( + // ErrBadMsg Local: Bad message format + ErrBadMsg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__BAD_MSG) + // ErrBadCompression Local: Invalid compressed data + ErrBadCompression ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__BAD_COMPRESSION) + // ErrDestroy Local: Broker handle destroyed + ErrDestroy ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__DESTROY) + // ErrFail Local: Communication failure with broker + ErrFail ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FAIL) + // ErrTransport Local: Broker transport failure + ErrTransport ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TRANSPORT) + // ErrCritSysResource Local: Critical system resource failure + ErrCritSysResource ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE) + // ErrResolve Local: Host resolution failure + ErrResolve ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__RESOLVE) + // ErrMsgTimedOut Local: Message timed out + ErrMsgTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__MSG_TIMED_OUT) + // ErrPartitionEOF Broker: No more messages + ErrPartitionEOF ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PARTITION_EOF) + // ErrUnknownPartition Local: Unknown partition + ErrUnknownPartition ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION) + // ErrFs Local: File or filesystem error + ErrFs ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FS) + // ErrUnknownTopic Local: Unknown topic + ErrUnknownTopic ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC) + // ErrAllBrokersDown Local: All broker connections are down + ErrAllBrokersDown ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN) + // ErrInvalidArg Local: Invalid argument or configuration + ErrInvalidArg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INVALID_ARG) + // ErrTimedOut Local: Timed out + ErrTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TIMED_OUT) + // ErrQueueFull Local: Queue full + ErrQueueFull ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__QUEUE_FULL) + // ErrIsrInsuff Local: ISR count insufficient + ErrIsrInsuff ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ISR_INSUFF) + // ErrNodeUpdate Local: Broker node update + ErrNodeUpdate ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NODE_UPDATE) + // ErrSsl Local: SSL error + ErrSsl ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__SSL) + // ErrWaitCoord Local: Waiting for coordinator + ErrWaitCoord ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__WAIT_COORD) + // ErrUnknownGroup Local: Unknown group + ErrUnknownGroup ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_GROUP) + // ErrInProgress Local: Operation in progress + ErrInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__IN_PROGRESS) + // ErrPrevInProgress Local: Previous operation in progress + ErrPrevInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS) + // ErrExistingSubscription Local: Existing subscription + ErrExistingSubscription ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__EXISTING_SUBSCRIPTION) + // ErrAssignPartitions Local: Assign partitions + ErrAssignPartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS) + // ErrRevokePartitions Local: Revoke partitions + ErrRevokePartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS) + // ErrConflict Local: Conflicting use + ErrConflict ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__CONFLICT) + // ErrState Local: Erroneous state + ErrState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__STATE) + // ErrUnknownProtocol Local: Unknown protocol + ErrUnknownProtocol ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_PROTOCOL) + // ErrNotImplemented Local: Not implemented + ErrNotImplemented ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED) + // ErrAuthentication Local: Authentication failure + ErrAuthentication ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__AUTHENTICATION) + // ErrNoOffset Local: No offset stored + ErrNoOffset ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NO_OFFSET) + // ErrOutdated Local: Outdated + ErrOutdated ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__OUTDATED) + // ErrTimedOutQueue Local: Timed out in queue + ErrTimedOutQueue ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__TIMED_OUT_QUEUE) + // ErrUnsupportedFeature Local: Required feature not supported by broker + ErrUnsupportedFeature ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE) + // ErrWaitCache Local: Awaiting cache update + ErrWaitCache ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__WAIT_CACHE) + // ErrIntr Local: Operation interrupted + ErrIntr ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INTR) + // ErrKeySerialization Local: Key serialization error + ErrKeySerialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__KEY_SERIALIZATION) + // ErrValueSerialization Local: Value serialization error + ErrValueSerialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__VALUE_SERIALIZATION) + // ErrKeyDeserialization Local: Key deserialization error + ErrKeyDeserialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__KEY_DESERIALIZATION) + // ErrValueDeserialization Local: Value deserialization error + ErrValueDeserialization ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__VALUE_DESERIALIZATION) + // ErrPartial Local: Partial response + ErrPartial ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PARTIAL) + // ErrReadOnly Local: Read-only object + ErrReadOnly ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__READ_ONLY) + // ErrNoent Local: No such entry + ErrNoent ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOENT) + // ErrUnderflow Local: Read underflow + ErrUnderflow ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNDERFLOW) + // ErrInvalidType Local: Invalid type + ErrInvalidType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INVALID_TYPE) + // ErrRetry Local: Retry operation + ErrRetry ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__RETRY) + // ErrPurgeQueue Local: Purged in queue + ErrPurgeQueue ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PURGE_QUEUE) + // ErrPurgeInflight Local: Purged in flight + ErrPurgeInflight ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__PURGE_INFLIGHT) + // ErrFatal Local: Fatal error + ErrFatal ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FATAL) + // ErrInconsistent Local: Inconsistent state + ErrInconsistent ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__INCONSISTENT) + // ErrGaplessGuarantee Local: Gap-less ordering would not be guaranteed if proceeding + ErrGaplessGuarantee ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__GAPLESS_GUARANTEE) + // ErrMaxPollExceeded Local: Maximum application poll interval (max.poll.interval.ms) exceeded + ErrMaxPollExceeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED) + // ErrUnknownBroker Local: Unknown broker + ErrUnknownBroker ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__UNKNOWN_BROKER) + // ErrNotConfigured Local: Functionality not configured + ErrNotConfigured ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOT_CONFIGURED) + // ErrFenced Local: This instance has been fenced by a newer instance + ErrFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__FENCED) + // ErrApplication Local: Application generated error + ErrApplication ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__APPLICATION) + // ErrAssignmentLost Local: Group partition assignment lost + ErrAssignmentLost ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__ASSIGNMENT_LOST) + // ErrNoop Local: No operation performed + ErrNoop ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__NOOP) + // ErrAutoOffsetReset Local: No offset to automatically reset to + ErrAutoOffsetReset ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR__AUTO_OFFSET_RESET) + // ErrUnknown Unknown broker error + ErrUnknown ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN) + // ErrNoError Success + ErrNoError ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NO_ERROR) + // ErrOffsetOutOfRange Broker: Offset out of range + ErrOffsetOutOfRange ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE) + // ErrInvalidMsg Broker: Invalid message + ErrInvalidMsg ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_MSG) + // ErrUnknownTopicOrPart Broker: Unknown topic or partition + ErrUnknownTopicOrPart ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART) + // ErrInvalidMsgSize Broker: Invalid message size + ErrInvalidMsgSize ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_MSG_SIZE) + // ErrLeaderNotAvailable Broker: Leader not available + ErrLeaderNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE) + // ErrNotLeaderForPartition Broker: Not leader for partition + ErrNotLeaderForPartition ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_LEADER_FOR_PARTITION) + // ErrRequestTimedOut Broker: Request timed out + ErrRequestTimedOut ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REQUEST_TIMED_OUT) + // ErrBrokerNotAvailable Broker: Broker not available + ErrBrokerNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_BROKER_NOT_AVAILABLE) + // ErrReplicaNotAvailable Broker: Replica not available + ErrReplicaNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REPLICA_NOT_AVAILABLE) + // ErrMsgSizeTooLarge Broker: Message size too large + ErrMsgSizeTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE) + // ErrStaleCtrlEpoch Broker: StaleControllerEpochCode + ErrStaleCtrlEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_STALE_CTRL_EPOCH) + // ErrOffsetMetadataTooLarge Broker: Offset metadata string too large + ErrOffsetMetadataTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_METADATA_TOO_LARGE) + // ErrNetworkException Broker: Broker disconnected before response received + ErrNetworkException ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NETWORK_EXCEPTION) + // ErrCoordinatorLoadInProgress Broker: Coordinator load in progress + ErrCoordinatorLoadInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_COORDINATOR_LOAD_IN_PROGRESS) + // ErrCoordinatorNotAvailable Broker: Coordinator not available + ErrCoordinatorNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_COORDINATOR_NOT_AVAILABLE) + // ErrNotCoordinator Broker: Not coordinator + ErrNotCoordinator ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_COORDINATOR) + // ErrTopicException Broker: Invalid topic + ErrTopicException ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_EXCEPTION) + // ErrRecordListTooLarge Broker: Message batch larger than configured server segment size + ErrRecordListTooLarge ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_RECORD_LIST_TOO_LARGE) + // ErrNotEnoughReplicas Broker: Not enough in-sync replicas + ErrNotEnoughReplicas ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS) + // ErrNotEnoughReplicasAfterAppend Broker: Message(s) written to insufficient number of in-sync replicas + ErrNotEnoughReplicasAfterAppend ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND) + // ErrInvalidRequiredAcks Broker: Invalid required acks value + ErrInvalidRequiredAcks ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REQUIRED_ACKS) + // ErrIllegalGeneration Broker: Specified group generation id is not valid + ErrIllegalGeneration ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ILLEGAL_GENERATION) + // ErrInconsistentGroupProtocol Broker: Inconsistent group protocol + ErrInconsistentGroupProtocol ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INCONSISTENT_GROUP_PROTOCOL) + // ErrInvalidGroupID Broker: Invalid group.id + ErrInvalidGroupID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_GROUP_ID) + // ErrUnknownMemberID Broker: Unknown member + ErrUnknownMemberID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID) + // ErrInvalidSessionTimeout Broker: Invalid session timeout + ErrInvalidSessionTimeout ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_SESSION_TIMEOUT) + // ErrRebalanceInProgress Broker: Group rebalance in progress + ErrRebalanceInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS) + // ErrInvalidCommitOffsetSize Broker: Commit offset data size is not valid + ErrInvalidCommitOffsetSize ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_COMMIT_OFFSET_SIZE) + // ErrTopicAuthorizationFailed Broker: Topic authorization failed + ErrTopicAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED) + // ErrGroupAuthorizationFailed Broker: Group authorization failed + ErrGroupAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED) + // ErrClusterAuthorizationFailed Broker: Cluster authorization failed + ErrClusterAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_CLUSTER_AUTHORIZATION_FAILED) + // ErrInvalidTimestamp Broker: Invalid timestamp + ErrInvalidTimestamp ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TIMESTAMP) + // ErrUnsupportedSaslMechanism Broker: Unsupported SASL mechanism + ErrUnsupportedSaslMechanism ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_SASL_MECHANISM) + // ErrIllegalSaslState Broker: Request not valid in current SASL state + ErrIllegalSaslState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ILLEGAL_SASL_STATE) + // ErrUnsupportedVersion Broker: API version not supported + ErrUnsupportedVersion ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_VERSION) + // ErrTopicAlreadyExists Broker: Topic already exists + ErrTopicAlreadyExists ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS) + // ErrInvalidPartitions Broker: Invalid number of partitions + ErrInvalidPartitions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PARTITIONS) + // ErrInvalidReplicationFactor Broker: Invalid replication factor + ErrInvalidReplicationFactor ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REPLICATION_FACTOR) + // ErrInvalidReplicaAssignment Broker: Invalid replica assignment + ErrInvalidReplicaAssignment ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REPLICA_ASSIGNMENT) + // ErrInvalidConfig Broker: Configuration is invalid + ErrInvalidConfig ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_CONFIG) + // ErrNotController Broker: Not controller for cluster + ErrNotController ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NOT_CONTROLLER) + // ErrInvalidRequest Broker: Invalid request + ErrInvalidRequest ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_REQUEST) + // ErrUnsupportedForMessageFormat Broker: Message format on broker does not support request + ErrUnsupportedForMessageFormat ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT) + // ErrPolicyViolation Broker: Policy violation + ErrPolicyViolation ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_POLICY_VIOLATION) + // ErrOutOfOrderSequenceNumber Broker: Broker received an out of order sequence number + ErrOutOfOrderSequenceNumber ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OUT_OF_ORDER_SEQUENCE_NUMBER) + // ErrDuplicateSequenceNumber Broker: Broker received a duplicate sequence number + ErrDuplicateSequenceNumber ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DUPLICATE_SEQUENCE_NUMBER) + // ErrInvalidProducerEpoch Broker: Producer attempted an operation with an old epoch + ErrInvalidProducerEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRODUCER_EPOCH) + // ErrInvalidTxnState Broker: Producer attempted a transactional operation in an invalid state + ErrInvalidTxnState ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TXN_STATE) + // ErrInvalidProducerIDMapping Broker: Producer attempted to use a producer id which is not currently assigned to its transactional id + ErrInvalidProducerIDMapping ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRODUCER_ID_MAPPING) + // ErrInvalidTransactionTimeout Broker: Transaction timeout is larger than the maximum value allowed by the broker's max.transaction.timeout.ms + ErrInvalidTransactionTimeout ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_TRANSACTION_TIMEOUT) + // ErrConcurrentTransactions Broker: Producer attempted to update a transaction while another concurrent operation on the same transaction was ongoing + ErrConcurrentTransactions ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS) + // ErrTransactionCoordinatorFenced Broker: Indicates that the transaction coordinator sending a WriteTxnMarker is no longer the current coordinator for a given producer + ErrTransactionCoordinatorFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TRANSACTION_COORDINATOR_FENCED) + // ErrTransactionalIDAuthorizationFailed Broker: Transactional Id authorization failed + ErrTransactionalIDAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED) + // ErrSecurityDisabled Broker: Security features are disabled + ErrSecurityDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_SECURITY_DISABLED) + // ErrOperationNotAttempted Broker: Operation not attempted + ErrOperationNotAttempted ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OPERATION_NOT_ATTEMPTED) + // ErrKafkaStorageError Broker: Disk error when trying to access log file on disk + ErrKafkaStorageError ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_KAFKA_STORAGE_ERROR) + // ErrLogDirNotFound Broker: The user-specified log directory is not found in the broker config + ErrLogDirNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LOG_DIR_NOT_FOUND) + // ErrSaslAuthenticationFailed Broker: SASL Authentication failed + ErrSaslAuthenticationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_SASL_AUTHENTICATION_FAILED) + // ErrUnknownProducerID Broker: Unknown Producer Id + ErrUnknownProducerID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_PRODUCER_ID) + // ErrReassignmentInProgress Broker: Partition reassignment is in progress + ErrReassignmentInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_REASSIGNMENT_IN_PROGRESS) + // ErrDelegationTokenAuthDisabled Broker: Delegation Token feature is not enabled + ErrDelegationTokenAuthDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTH_DISABLED) + // ErrDelegationTokenNotFound Broker: Delegation Token is not found on server + ErrDelegationTokenNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_NOT_FOUND) + // ErrDelegationTokenOwnerMismatch Broker: Specified Principal is not valid Owner/Renewer + ErrDelegationTokenOwnerMismatch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_OWNER_MISMATCH) + // ErrDelegationTokenRequestNotAllowed Broker: Delegation Token requests are not allowed on this connection + ErrDelegationTokenRequestNotAllowed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED) + // ErrDelegationTokenAuthorizationFailed Broker: Delegation Token authorization failed + ErrDelegationTokenAuthorizationFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED) + // ErrDelegationTokenExpired Broker: Delegation Token is expired + ErrDelegationTokenExpired ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_EXPIRED) + // ErrInvalidPrincipalType Broker: Supplied principalType is not supported + ErrInvalidPrincipalType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_PRINCIPAL_TYPE) + // ErrNonEmptyGroup Broker: The group is not empty + ErrNonEmptyGroup ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NON_EMPTY_GROUP) + // ErrGroupIDNotFound Broker: The group id does not exist + ErrGroupIDNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_ID_NOT_FOUND) + // ErrFetchSessionIDNotFound Broker: The fetch session ID was not found + ErrFetchSessionIDNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FETCH_SESSION_ID_NOT_FOUND) + // ErrInvalidFetchSessionEpoch Broker: The fetch session epoch is invalid + ErrInvalidFetchSessionEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_FETCH_SESSION_EPOCH) + // ErrListenerNotFound Broker: No matching listener + ErrListenerNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_LISTENER_NOT_FOUND) + // ErrTopicDeletionDisabled Broker: Topic deletion is disabled + ErrTopicDeletionDisabled ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_TOPIC_DELETION_DISABLED) + // ErrFencedLeaderEpoch Broker: Leader epoch is older than broker epoch + ErrFencedLeaderEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FENCED_LEADER_EPOCH) + // ErrUnknownLeaderEpoch Broker: Leader epoch is newer than broker epoch + ErrUnknownLeaderEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNKNOWN_LEADER_EPOCH) + // ErrUnsupportedCompressionType Broker: Unsupported compression type + ErrUnsupportedCompressionType ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSUPPORTED_COMPRESSION_TYPE) + // ErrStaleBrokerEpoch Broker: Broker epoch has changed + ErrStaleBrokerEpoch ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_STALE_BROKER_EPOCH) + // ErrOffsetNotAvailable Broker: Leader high watermark is not caught up + ErrOffsetNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_OFFSET_NOT_AVAILABLE) + // ErrMemberIDRequired Broker: Group member needs a valid member ID + ErrMemberIDRequired ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_MEMBER_ID_REQUIRED) + // ErrPreferredLeaderNotAvailable Broker: Preferred leader was not available + ErrPreferredLeaderNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PREFERRED_LEADER_NOT_AVAILABLE) + // ErrGroupMaxSizeReached Broker: Consumer group has reached maximum size + ErrGroupMaxSizeReached ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_MAX_SIZE_REACHED) + // ErrFencedInstanceID Broker: Static consumer fenced by other consumer with same group.instance.id + ErrFencedInstanceID ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FENCED_INSTANCE_ID) + // ErrEligibleLeadersNotAvailable Broker: Eligible partition leaders are not available + ErrEligibleLeadersNotAvailable ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ELIGIBLE_LEADERS_NOT_AVAILABLE) + // ErrElectionNotNeeded Broker: Leader election not needed for topic partition + ErrElectionNotNeeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_ELECTION_NOT_NEEDED) + // ErrNoReassignmentInProgress Broker: No partition reassignment is in progress + ErrNoReassignmentInProgress ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_NO_REASSIGNMENT_IN_PROGRESS) + // ErrGroupSubscribedToTopic Broker: Deleting offsets of a topic while the consumer group is subscribed to it + ErrGroupSubscribedToTopic ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_GROUP_SUBSCRIBED_TO_TOPIC) + // ErrInvalidRecord Broker: Broker failed to validate record + ErrInvalidRecord ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_RECORD) + // ErrUnstableOffsetCommit Broker: There are unstable offsets that need to be cleared + ErrUnstableOffsetCommit ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNSTABLE_OFFSET_COMMIT) + // ErrThrottlingQuotaExceeded Broker: Throttling quota has been exceeded + ErrThrottlingQuotaExceeded ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_THROTTLING_QUOTA_EXCEEDED) + // ErrProducerFenced Broker: There is a newer producer with the same transactionalId which fences the current one + ErrProducerFenced ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PRODUCER_FENCED) + // ErrResourceNotFound Broker: Request illegally referred to resource that does not exist + ErrResourceNotFound ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_RESOURCE_NOT_FOUND) + // ErrDuplicateResource Broker: Request illegally referred to the same resource twice + ErrDuplicateResource ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_DUPLICATE_RESOURCE) + // ErrUnacceptableCredential Broker: Requested credential would not meet criteria for acceptability + ErrUnacceptableCredential ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_UNACCEPTABLE_CREDENTIAL) + // ErrInconsistentVoterSet Broker: Indicates that the either the sender or recipient of a voter-only request is not one of the expected voters + ErrInconsistentVoterSet ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INCONSISTENT_VOTER_SET) + // ErrInvalidUpdateVersion Broker: Invalid update version + ErrInvalidUpdateVersion ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_INVALID_UPDATE_VERSION) + // ErrFeatureUpdateFailed Broker: Unable to update finalized features due to server error + ErrFeatureUpdateFailed ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_FEATURE_UPDATE_FAILED) + // ErrPrincipalDeserializationFailure Broker: Request principal deserialization failed during forwarding + ErrPrincipalDeserializationFailure ErrorCode = ErrorCode(C.RD_KAFKA_RESP_ERR_PRINCIPAL_DESERIALIZATION_FAILURE) +) diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/glue_rdkafka.h b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/glue_rdkafka.h new file mode 100644 index 0000000..bbff507 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/glue_rdkafka.h @@ -0,0 +1,48 @@ +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + + +/** + * Glue between Go, Cgo and librdkafka + */ + + +/** + * Temporary C to Go header representation + */ +typedef struct tmphdr_s { + const char *key; + const void *val; // producer: malloc()ed by Go code if size > 0 + // consumer: owned by librdkafka + ssize_t size; +} tmphdr_t; + + + +/** + * @struct This is a glue struct used by the C code in this client to + * effectively map fields from a librdkafka rd_kafka_message_t + * to something usable in Go with as few CGo calls as possible. + */ +typedef struct glue_msg_s { + rd_kafka_message_t *msg; + rd_kafka_timestamp_type_t tstype; + int64_t ts; + tmphdr_t *tmphdrs; + size_t tmphdrsCnt; + int8_t want_hdrs; /**< If true, copy headers */ +} glue_msg_t; diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/handle.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/handle.go new file mode 100644 index 0000000..161a395 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/handle.go @@ -0,0 +1,379 @@ +package kafka + +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" + "strings" + "sync" + "time" + "unsafe" +) + +/* +#include "select_rdkafka.h" +#include +*/ +import "C" + +// OAuthBearerToken represents the data to be transmitted +// to a broker during SASL/OAUTHBEARER authentication. +type OAuthBearerToken struct { + // Token value, often (but not necessarily) a JWS compact serialization + // as per https://tools.ietf.org/html/rfc7515#section-3.1; it must meet + // the regular expression for a SASL/OAUTHBEARER value defined at + // https://tools.ietf.org/html/rfc7628#section-3.1 + TokenValue string + // Metadata about the token indicating when it expires (local time); + // it must represent a time in the future + Expiration time.Time + // Metadata about the token indicating the Kafka principal name + // to which it applies (for example, "admin") + Principal string + // SASL extensions, if any, to be communicated to the broker during + // authentication (all keys and values of which must meet the regular + // expressions defined at https://tools.ietf.org/html/rfc7628#section-3.1, + // and it must not contain the reserved "auth" key) + Extensions map[string]string +} + +// Handle represents a generic client handle containing common parts for +// both Producer and Consumer. +type Handle interface { + // SetOAuthBearerToken sets the the data to be transmitted + // to a broker during SASL/OAUTHBEARER authentication. It will return nil + // on success, otherwise an error if: + // 1) the token data is invalid (meaning an expiration time in the past + // or either a token value or an extension key or value that does not meet + // the regular expression requirements as per + // https://tools.ietf.org/html/rfc7628#section-3.1); + // 2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; + // 3) SASL/OAUTHBEARER is supported but is not configured as the client's + // authentication mechanism. + SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error + + // SetOAuthBearerTokenFailure sets the error message describing why token + // retrieval/setting failed; it also schedules a new token refresh event for 10 + // seconds later so the attempt may be retried. It will return nil on + // success, otherwise an error if: + // 1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; + // 2) SASL/OAUTHBEARER is supported but is not configured as the client's + // authentication mechanism. + SetOAuthBearerTokenFailure(errstr string) error + + // gethandle() returns the internal handle struct pointer + gethandle() *handle +} + +// Common instance handle for both Producer and Consumer +type handle struct { + rk *C.rd_kafka_t + rkq *C.rd_kafka_queue_t + + // Forward logs from librdkafka log queue to logs channel. + logs chan LogEvent + logq *C.rd_kafka_queue_t + closeLogsChan bool + + // Topic <-> rkt caches + rktCacheLock sync.Mutex + // topic name -> rkt cache + rktCache map[string]*C.rd_kafka_topic_t + // rkt -> topic name cache + rktNameCache map[*C.rd_kafka_topic_t]string + + // Cached instance name to avoid CGo call in String() + name string + + // + // cgo map + // Maps C callbacks based on cgoid back to its Go object + cgoLock sync.Mutex + cgoidNext uintptr + cgomap map[int]cgoif + + // + // producer + // + p *Producer + + // Forward delivery reports on Producer.Events channel + fwdDr bool + + // Enabled message fields for delivery reports and consumed messages. + msgFields *messageFields + + // + // consumer + // + c *Consumer + + // WaitGroup to wait for spawned go-routines to finish. + waitGroup sync.WaitGroup +} + +func (h *handle) String() string { + return h.name +} + +func (h *handle) setup() { + h.rktCache = make(map[string]*C.rd_kafka_topic_t) + h.rktNameCache = make(map[*C.rd_kafka_topic_t]string) + h.cgomap = make(map[int]cgoif) + h.name = C.GoString(C.rd_kafka_name(h.rk)) + if h.msgFields == nil { + h.msgFields = newMessageFields() + } +} + +func (h *handle) cleanup() { + if h.logs != nil { + C.rd_kafka_queue_destroy(h.logq) + if h.closeLogsChan { + close(h.logs) + } + } + + for _, crkt := range h.rktCache { + C.rd_kafka_topic_destroy(crkt) + } + + if h.rkq != nil { + C.rd_kafka_queue_destroy(h.rkq) + } +} + +func (h *handle) setupLogQueue(logsChan chan LogEvent, termChan chan bool) { + if logsChan == nil { + logsChan = make(chan LogEvent, 10000) + h.closeLogsChan = true + } + + h.logs = logsChan + + // Let librdkafka forward logs to our log queue instead of the main queue + h.logq = C.rd_kafka_queue_new(h.rk) + C.rd_kafka_set_log_queue(h.rk, h.logq) + + // Start a polling goroutine to consume the log queue + h.waitGroup.Add(1) + go func() { + h.pollLogEvents(h.logs, 100, termChan) + h.waitGroup.Done() + }() + +} + +// getRkt0 finds or creates and returns a C topic_t object from the local cache. +func (h *handle) getRkt0(topic string, ctopic *C.char, doLock bool) (crkt *C.rd_kafka_topic_t) { + if doLock { + h.rktCacheLock.Lock() + defer h.rktCacheLock.Unlock() + } + crkt, ok := h.rktCache[topic] + if ok { + return crkt + } + + if ctopic == nil { + ctopic = C.CString(topic) + defer C.free(unsafe.Pointer(ctopic)) + } + + crkt = C.rd_kafka_topic_new(h.rk, ctopic, nil) + if crkt == nil { + panic(fmt.Sprintf("Unable to create new C topic \"%s\": %s", + topic, C.GoString(C.rd_kafka_err2str(C.rd_kafka_last_error())))) + } + + h.rktCache[topic] = crkt + h.rktNameCache[crkt] = topic + + return crkt +} + +// getRkt finds or creates and returns a C topic_t object from the local cache. +func (h *handle) getRkt(topic string) (crkt *C.rd_kafka_topic_t) { + return h.getRkt0(topic, nil, true) +} + +// getTopicNameFromRkt returns the topic name for a C topic_t object, preferably +// using the local cache to avoid a cgo call. +func (h *handle) getTopicNameFromRkt(crkt *C.rd_kafka_topic_t) (topic string) { + h.rktCacheLock.Lock() + defer h.rktCacheLock.Unlock() + + topic, ok := h.rktNameCache[crkt] + if ok { + return topic + } + + // we need our own copy/refcount of the crkt + ctopic := C.rd_kafka_topic_name(crkt) + topic = C.GoString(ctopic) + + crkt = h.getRkt0(topic, ctopic, false /* dont lock */) + + return topic +} + +// cgoif is a generic interface for holding Go state passed as opaque +// value to the C code. +// Since pointers to complex Go types cannot be passed to C we instead create +// a cgoif object, generate a unique id that is added to the cgomap, +// and then pass that id to the C code. When the C code callback is called we +// use the id to look up the cgoif object in the cgomap. +type cgoif interface{} + +// delivery report cgoif container +type cgoDr struct { + deliveryChan chan Event + opaque interface{} +} + +// cgoPut adds object cg to the handle's cgo map and returns a +// unique id for the added entry. +// Thread-safe. +// FIXME: the uniquity of the id is questionable over time. +func (h *handle) cgoPut(cg cgoif) (cgoid int) { + h.cgoLock.Lock() + defer h.cgoLock.Unlock() + + h.cgoidNext++ + if h.cgoidNext == 0 { + h.cgoidNext++ + } + cgoid = (int)(h.cgoidNext) + h.cgomap[cgoid] = cg + return cgoid +} + +// cgoGet looks up cgoid in the cgo map, deletes the reference from the map +// and returns the object, if found. Else returns nil, false. +// Thread-safe. +func (h *handle) cgoGet(cgoid int) (cg cgoif, found bool) { + if cgoid == 0 { + return nil, false + } + + h.cgoLock.Lock() + defer h.cgoLock.Unlock() + cg, found = h.cgomap[cgoid] + if found { + delete(h.cgomap, cgoid) + } + + return cg, found +} + +// setOauthBearerToken - see rd_kafka_oauthbearer_set_token() +func (h *handle) setOAuthBearerToken(oauthBearerToken OAuthBearerToken) error { + cTokenValue := C.CString(oauthBearerToken.TokenValue) + defer C.free(unsafe.Pointer(cTokenValue)) + + cPrincipal := C.CString(oauthBearerToken.Principal) + defer C.free(unsafe.Pointer(cPrincipal)) + + cErrstrSize := C.size_t(512) + cErrstr := (*C.char)(C.malloc(cErrstrSize)) + defer C.free(unsafe.Pointer(cErrstr)) + + cExtensions := make([]*C.char, 2*len(oauthBearerToken.Extensions)) + extensionSize := 0 + for key, value := range oauthBearerToken.Extensions { + cExtensions[extensionSize] = C.CString(key) + defer C.free(unsafe.Pointer(cExtensions[extensionSize])) + extensionSize++ + cExtensions[extensionSize] = C.CString(value) + defer C.free(unsafe.Pointer(cExtensions[extensionSize])) + extensionSize++ + } + + var cExtensionsToUse **C.char + if extensionSize > 0 { + cExtensionsToUse = (**C.char)(unsafe.Pointer(&cExtensions[0])) + } + + cErr := C.rd_kafka_oauthbearer_set_token(h.rk, cTokenValue, + C.int64_t(oauthBearerToken.Expiration.UnixNano()/(1000*1000)), cPrincipal, + cExtensionsToUse, C.size_t(extensionSize), cErrstr, cErrstrSize) + if cErr == C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil + } + return newErrorFromCString(cErr, cErrstr) +} + +// setOauthBearerTokenFailure - see rd_kafka_oauthbearer_set_token_failure() +func (h *handle) setOAuthBearerTokenFailure(errstr string) error { + cerrstr := C.CString(errstr) + defer C.free(unsafe.Pointer(cerrstr)) + cErr := C.rd_kafka_oauthbearer_set_token_failure(h.rk, cerrstr) + if cErr == C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil + } + return newError(cErr) +} + +// messageFields controls which fields are made available for producer delivery reports & consumed messages. +// true values indicate that the field should be included +type messageFields struct { + Key bool + Value bool + Headers bool +} + +// disableAll disable all fields +func (mf *messageFields) disableAll() { + mf.Key = false + mf.Value = false + mf.Headers = false +} + +// newMessageFields returns a new messageFields with all fields enabled +func newMessageFields() *messageFields { + return &messageFields{ + Key: true, + Value: true, + Headers: true, + } +} + +// newMessageFieldsFrom constructs a new messageFields from the given configuration value +func newMessageFieldsFrom(v ConfigValue) (*messageFields, error) { + msgFields := newMessageFields() + switch v { + case "all": + // nothing to do + case "", "none": + msgFields.disableAll() + default: + msgFields.disableAll() + for _, value := range strings.Split(v.(string), ",") { + switch value { + case "key": + msgFields.Key = true + case "value": + msgFields.Value = true + case "headers": + msgFields.Headers = true + default: + return nil, fmt.Errorf("unknown message field: %s", value) + } + } + } + return msgFields, nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/header.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/header.go new file mode 100644 index 0000000..f7a73a4 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/header.go @@ -0,0 +1,67 @@ +package kafka + +/** + * Copyright 2018 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "fmt" + "strconv" +) + +/* +#include +#include "select_rdkafka.h" +#include "glue_rdkafka.h" +*/ +import "C" + +// Header represents a single Kafka message header. +// +// Message headers are made up of a list of Header elements, retaining their original insert +// order and allowing for duplicate Keys. +// +// Key is a human readable string identifying the header. +// Value is the key's binary value, Kafka does not put any restrictions on the format of +// of the Value but it should be made relatively compact. +// The value may be a byte array, empty, or nil. +// +// NOTE: Message headers are not available on producer delivery report messages. +type Header struct { + Key string // Header name (utf-8 string) + Value []byte // Header value (nil, empty, or binary) +} + +// String returns the Header Key and data in a human representable possibly truncated form +// suitable for displaying to the user. +func (h Header) String() string { + if h.Value == nil { + return fmt.Sprintf("%s=nil", h.Key) + } + + valueLen := len(h.Value) + if valueLen == 0 { + return fmt.Sprintf("%s=", h.Key) + } + + truncSize := valueLen + trunc := "" + if valueLen > 50+15 { + truncSize = 50 + trunc = fmt.Sprintf("(%d more bytes)", valueLen-truncSize) + } + + return fmt.Sprintf("%s=%s%s", h.Key, strconv.Quote(string(h.Value[:truncSize])), trunc) +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/kafka.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/kafka.go new file mode 100644 index 0000000..20dc30a --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/kafka.go @@ -0,0 +1,375 @@ +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package kafka provides high-level Apache Kafka producer and consumers +// using bindings on-top of the librdkafka C library. +// +// +// High-level Consumer +// +// * Decide if you want to read messages and events by calling `.Poll()` or +// the deprecated option of using the `.Events()` channel. (If you want to use +// `.Events()` channel then set `"go.events.channel.enable": true`). +// +// * Create a Consumer with `kafka.NewConsumer()` providing at +// least the `bootstrap.servers` and `group.id` configuration properties. +// +// * Call `.Subscribe()` or (`.SubscribeTopics()` to subscribe to multiple topics) +// to join the group with the specified subscription set. +// Subscriptions are atomic, calling `.Subscribe*()` again will leave +// the group and rejoin with the new set of topics. +// +// * Start reading events and messages from either the `.Events` channel +// or by calling `.Poll()`. +// +// * When the group has rebalanced each client member is assigned a +// (sub-)set of topic+partitions. +// By default the consumer will start fetching messages for its assigned +// partitions at this point, but your application may enable rebalance +// events to get an insight into what the assigned partitions where +// as well as set the initial offsets. To do this you need to pass +// `"go.application.rebalance.enable": true` to the `NewConsumer()` call +// mentioned above. You will (eventually) see a `kafka.AssignedPartitions` event +// with the assigned partition set. You can optionally modify the initial +// offsets (they'll default to stored offsets and if there are no previously stored +// offsets it will fall back to `"auto.offset.reset"` +// which defaults to the `latest` message) and then call `.Assign(partitions)` +// to start consuming. If you don't need to modify the initial offsets you will +// not need to call `.Assign()`, the client will do so automatically for you if +// you dont, unless you are using the channel-based consumer in which case +// you MUST call `.Assign()` when receiving the `AssignedPartitions` and +// `RevokedPartitions` events. +// +// * As messages are fetched they will be made available on either the +// `.Events` channel or by calling `.Poll()`, look for event type `*kafka.Message`. +// +// * Handle messages, events and errors to your liking. +// +// * When you are done consuming call `.Close()` to commit final offsets +// and leave the consumer group. +// +// +// +// Producer +// +// * Create a Producer with `kafka.NewProducer()` providing at least +// the `bootstrap.servers` configuration properties. +// +// * Messages may now be produced either by sending a `*kafka.Message` +// on the `.ProduceChannel` or by calling `.Produce()`. +// +// * Producing is an asynchronous operation so the client notifies the application +// of per-message produce success or failure through something called delivery reports. +// Delivery reports are by default emitted on the `.Events()` channel as `*kafka.Message` +// and you should check `msg.TopicPartition.Error` for `nil` to find out if the message +// was succesfully delivered or not. +// It is also possible to direct delivery reports to alternate channels +// by providing a non-nil `chan Event` channel to `.Produce()`. +// If no delivery reports are wanted they can be completely disabled by +// setting configuration property `"go.delivery.reports": false`. +// +// * When you are done producing messages you will need to make sure all messages +// are indeed delivered to the broker (or failed), remember that this is +// an asynchronous client so some of your messages may be lingering in internal +// channels or tranmission queues. +// To do this you can either keep track of the messages you've produced +// and wait for their corresponding delivery reports, or call the convenience +// function `.Flush()` that will block until all message deliveries are done +// or the provided timeout elapses. +// +// * Finally call `.Close()` to decommission the producer. +// +// +// Transactional producer API +// +// The transactional producer operates on top of the idempotent producer, +// and provides full exactly-once semantics (EOS) for Apache Kafka when used +// with the transaction aware consumer (`isolation.level=read_committed`). +// +// A producer instance is configured for transactions by setting the +// `transactional.id` to an identifier unique for the application. This +// id will be used to fence stale transactions from previous instances of +// the application, typically following an outage or crash. +// +// After creating the transactional producer instance using `NewProducer()` +// the transactional state must be initialized by calling +// `InitTransactions()`. This is a blocking call that will +// acquire a runtime producer id from the transaction coordinator broker +// as well as abort any stale transactions and fence any still running producer +// instances with the same `transactional.id`. +// +// Once transactions are initialized the application may begin a new +// transaction by calling `BeginTransaction()`. +// A producer instance may only have one single on-going transaction. +// +// Any messages produced after the transaction has been started will +// belong to the ongoing transaction and will be committed or aborted +// atomically. +// It is not permitted to produce messages outside a transaction +// boundary, e.g., before `BeginTransaction()` or after `CommitTransaction()`, +// `AbortTransaction()` or if the current transaction has failed. +// +// If consumed messages are used as input to the transaction, the consumer +// instance must be configured with `enable.auto.commit` set to `false`. +// To commit the consumed offsets along with the transaction pass the +// list of consumed partitions and the last offset processed + 1 to +// `SendOffsetsToTransaction()` prior to committing the transaction. +// This allows an aborted transaction to be restarted using the previously +// committed offsets. +// +// To commit the produced messages, and any consumed offsets, to the +// current transaction, call `CommitTransaction()`. +// This call will block until the transaction has been fully committed or +// failed (typically due to fencing by a newer producer instance). +// +// Alternatively, if processing fails, or an abortable transaction error is +// raised, the transaction needs to be aborted by calling +// `AbortTransaction()` which marks any produced messages and +// offset commits as aborted. +// +// After the current transaction has been committed or aborted a new +// transaction may be started by calling `BeginTransaction()` again. +// +// Retriable errors: +// Some error cases allow the attempted operation to be retried, this is +// indicated by the error object having the retriable flag set which can +// be detected by calling `err.(kafka.Error).IsRetriable()`. +// When this flag is set the application may retry the operation immediately +// or preferably after a shorter grace period (to avoid busy-looping). +// Retriable errors include timeouts, broker transport failures, etc. +// +// Abortable errors: +// An ongoing transaction may fail permanently due to various errors, +// such as transaction coordinator becoming unavailable, write failures to the +// Apache Kafka log, under-replicated partitions, etc. +// At this point the producer application must abort the current transaction +// using `AbortTransaction()` and optionally start a new transaction +// by calling `BeginTransaction()`. +// Whether an error is abortable or not is detected by calling +// `err.(kafka.Error).TxnRequiresAbort()` on the returned error object. +// +// Fatal errors: +// While the underlying idempotent producer will typically only raise +// fatal errors for unrecoverable cluster errors where the idempotency +// guarantees can't be maintained, most of these are treated as abortable by +// the transactional producer since transactions may be aborted and retried +// in their entirety; +// The transactional producer on the other hand introduces a set of additional +// fatal errors which the application needs to handle by shutting down the +// producer and terminate. There is no way for a producer instance to recover +// from fatal errors. +// Whether an error is fatal or not is detected by calling +// `err.(kafka.Error).IsFatal()` on the returned error object or by checking +// the global `GetFatalError()`. +// +// Handling of other errors: +// For errors that have neither retriable, abortable or the fatal flag set +// it is not always obvious how to handle them. While some of these errors +// may be indicative of bugs in the application code, such as when +// an invalid parameter is passed to a method, other errors might originate +// from the broker and be passed thru as-is to the application. +// The general recommendation is to treat these errors, that have +// neither the retriable or abortable flags set, as fatal. +// +// Error handling example: +// retry: +// +// err := producer.CommitTransaction(...) +// if err == nil { +// return nil +// } else if err.(kafka.Error).TxnRequiresAbort() { +// do_abort_transaction_and_reset_inputs() +// } else if err.(kafka.Error).IsRetriable() { +// goto retry +// } else { // treat all other errors as fatal errors +// panic(err) +// } +// +// +// Events +// +// Apart from emitting messages and delivery reports the client also communicates +// with the application through a number of different event types. +// An application may choose to handle or ignore these events. +// +// Consumer events +// +// * `*kafka.Message` - a fetched message. +// +// * `AssignedPartitions` - The assigned partition set for this client following a rebalance. +// Requires `go.application.rebalance.enable` +// +// * `RevokedPartitions` - The counter part to `AssignedPartitions` following a rebalance. +// `AssignedPartitions` and `RevokedPartitions` are symmetrical. +// Requires `go.application.rebalance.enable` +// +// * `PartitionEOF` - Consumer has reached the end of a partition. +// NOTE: The consumer will keep trying to fetch new messages for the partition. +// +// * `OffsetsCommitted` - Offset commit results (when `enable.auto.commit` is enabled). +// +// +// Producer events +// +// * `*kafka.Message` - delivery report for produced message. +// Check `.TopicPartition.Error` for delivery result. +// +// +// Generic events for both Consumer and Producer +// +// * `KafkaError` - client (error codes are prefixed with _) or broker error. +// These errors are normally just informational since the +// client will try its best to automatically recover (eventually). +// +// * `OAuthBearerTokenRefresh` - retrieval of a new SASL/OAUTHBEARER token is required. +// This event only occurs with sasl.mechanism=OAUTHBEARER. +// Be sure to invoke SetOAuthBearerToken() on the Producer/Consumer/AdminClient +// instance when a successful token retrieval is completed, otherwise be sure to +// invoke SetOAuthBearerTokenFailure() to indicate that retrieval failed (or +// if setting the token failed, which could happen if an extension doesn't meet +// the required regular expression); invoking SetOAuthBearerTokenFailure() will +// schedule a new event for 10 seconds later so another retrieval can be attempted. +// +// +// Hint: If your application registers a signal notification +// (signal.Notify) makes sure the signals channel is buffered to avoid +// possible complications with blocking Poll() calls. +// +// Note: The Confluent Kafka Go client is safe for concurrent use. +package kafka + +import ( + "fmt" + // Make sure librdkafka_vendor/ sub-directory is included in vendor pulls. + _ "github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor" + "unsafe" +) + +/* +#include +#include +#include "select_rdkafka.h" + +static rd_kafka_topic_partition_t *_c_rdkafka_topic_partition_list_entry(rd_kafka_topic_partition_list_t *rktparlist, int idx) { + return idx < rktparlist->cnt ? &rktparlist->elems[idx] : NULL; +} +*/ +import "C" + +// PartitionAny represents any partition (for partitioning), +// or unspecified value (for all other cases) +const PartitionAny = int32(C.RD_KAFKA_PARTITION_UA) + +// TopicPartition is a generic placeholder for a Topic+Partition and optionally Offset. +type TopicPartition struct { + Topic *string + Partition int32 + Offset Offset + Metadata *string + Error error +} + +func (p TopicPartition) String() string { + topic := "" + if p.Topic != nil { + topic = *p.Topic + } + if p.Error != nil { + return fmt.Sprintf("%s[%d]@%s(%s)", + topic, p.Partition, p.Offset, p.Error) + } + return fmt.Sprintf("%s[%d]@%s", + topic, p.Partition, p.Offset) +} + +// TopicPartitions is a slice of TopicPartitions that also implements +// the sort interface +type TopicPartitions []TopicPartition + +func (tps TopicPartitions) Len() int { + return len(tps) +} + +func (tps TopicPartitions) Less(i, j int) bool { + if *tps[i].Topic < *tps[j].Topic { + return true + } else if *tps[i].Topic > *tps[j].Topic { + return false + } + return tps[i].Partition < tps[j].Partition +} + +func (tps TopicPartitions) Swap(i, j int) { + tps[i], tps[j] = tps[j], tps[i] +} + +// new_cparts_from_TopicPartitions creates a new C rd_kafka_topic_partition_list_t +// from a TopicPartition array. +func newCPartsFromTopicPartitions(partitions []TopicPartition) (cparts *C.rd_kafka_topic_partition_list_t) { + cparts = C.rd_kafka_topic_partition_list_new(C.int(len(partitions))) + for _, part := range partitions { + ctopic := C.CString(*part.Topic) + defer C.free(unsafe.Pointer(ctopic)) + rktpar := C.rd_kafka_topic_partition_list_add(cparts, ctopic, C.int32_t(part.Partition)) + rktpar.offset = C.int64_t(part.Offset) + + if part.Metadata != nil { + cmetadata := C.CString(*part.Metadata) + rktpar.metadata = unsafe.Pointer(cmetadata) + rktpar.metadata_size = C.size_t(len(*part.Metadata)) + } + } + + return cparts +} + +func setupTopicPartitionFromCrktpar(partition *TopicPartition, crktpar *C.rd_kafka_topic_partition_t) { + + topic := C.GoString(crktpar.topic) + partition.Topic = &topic + partition.Partition = int32(crktpar.partition) + partition.Offset = Offset(crktpar.offset) + if crktpar.metadata_size > 0 { + size := C.int(crktpar.metadata_size) + cstr := (*C.char)(unsafe.Pointer(crktpar.metadata)) + metadata := C.GoStringN(cstr, size) + partition.Metadata = &metadata + } + if crktpar.err != C.RD_KAFKA_RESP_ERR_NO_ERROR { + partition.Error = newError(crktpar.err) + } +} + +func newTopicPartitionsFromCparts(cparts *C.rd_kafka_topic_partition_list_t) (partitions []TopicPartition) { + + partcnt := int(cparts.cnt) + + partitions = make([]TopicPartition, partcnt) + for i := 0; i < partcnt; i++ { + crktpar := C._c_rdkafka_topic_partition_list_entry(cparts, C.int(i)) + setupTopicPartitionFromCrktpar(&partitions[i], crktpar) + } + + return partitions +} + +// LibraryVersion returns the underlying librdkafka library version as a +// (version_int, version_str) tuple. +func LibraryVersion() (int, string) { + ver := (int)(C.rd_kafka_version()) + verstr := C.GoString(C.rd_kafka_version_str()) + return ver, verstr +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/.gitignore b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/.gitignore new file mode 100644 index 0000000..a2f65a7 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/.gitignore @@ -0,0 +1,3 @@ +*.tar.gz +*.tgz +tmp* diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/LICENSES.txt b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/LICENSES.txt new file mode 100644 index 0000000..f2aa57d --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/LICENSES.txt @@ -0,0 +1,366 @@ +LICENSE +-------------------------------------------------------------- +librdkafka - Apache Kafka C driver library + +Copyright (c) 2012-2020, Magnus Edenhill +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + +LICENSE.crc32c +-------------------------------------------------------------- +# For src/crc32c.c copied (with modifications) from +# http://stackoverflow.com/a/17646775/1821055 + +/* crc32c.c -- compute CRC-32C using the Intel crc32 instruction + * Copyright (C) 2013 Mark Adler + * Version 1.1 1 Aug 2013 Mark Adler + */ + +/* + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Mark Adler + madler@alumni.caltech.edu + */ + + +LICENSE.fnv1a +-------------------------------------------------------------- +parts of src/rdfnv1a.c: http://www.isthe.com/chongo/src/fnv/hash_32a.c + + +Please do not copyright this code. This code is in the public domain. + +LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, +INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR +CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF +USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. + +By: + chongo /\oo/\ + http://www.isthe.com/chongo/ + +Share and Enjoy! :-) + + +LICENSE.hdrhistogram +-------------------------------------------------------------- +This license covers src/rdhdrhistogram.c which is a C port of +Coda Hale's Golang HdrHistogram https://github.com/codahale/hdrhistogram +at revision 3a0bb77429bd3a61596f5e8a3172445844342120 + +----------------------------------------------------------------------------- + +The MIT License (MIT) + +Copyright (c) 2014 Coda Hale + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE + + +LICENSE.lz4 +-------------------------------------------------------------- +src/rdxxhash.[ch] src/lz4*.[ch]: git@github.com:lz4/lz4.git e2827775ee80d2ef985858727575df31fc60f1f3 + +LZ4 Library +Copyright (c) 2011-2016, Yann Collet +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +LICENSE.murmur2 +-------------------------------------------------------------- +parts of src/rdmurmur2.c: git@github.com:abrandoned/murmur2.git + + +MurMurHash2 Library +//----------------------------------------------------------------------------- +// MurmurHash2 was written by Austin Appleby, and is placed in the public +// domain. The author hereby disclaims copyright to this source code. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +LICENSE.pycrc +-------------------------------------------------------------- +The following license applies to the files rdcrc32.c and rdcrc32.h which +have been generated by the pycrc tool. +============================================================================ + +Copyright (c) 2006-2012, Thomas Pircher + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +LICENSE.queue +-------------------------------------------------------------- +For sys/queue.h: + + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)queue.h 8.5 (Berkeley) 8/20/94 + * $FreeBSD$ + +LICENSE.regexp +-------------------------------------------------------------- +regexp.c and regexp.h from https://github.com/ccxvii/minilibs sha 875c33568b5a4aa4fb3dd0c52ea98f7f0e5ca684 + +" +These libraries are in the public domain (or the equivalent where that is not possible). You can do anything you want with them. You have no legal obligation to do anything else, although I appreciate attribution. +" + + +LICENSE.snappy +-------------------------------------------------------------- +###################################################################### +# LICENSE.snappy covers files: snappy.c, snappy.h, snappy_compat.h # +# originally retrieved from http://github.com/andikleen/snappy-c # +# git revision 8015f2d28739b9a6076ebaa6c53fe27bc238d219 # +###################################################################### + +The snappy-c code is under the same license as the original snappy source + +Copyright 2011 Intel Corporation All Rights Reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Intel Corporation nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + +LICENSE.tinycthread +-------------------------------------------------------------- +From https://github.com/tinycthread/tinycthread/README.txt c57166cd510ffb5022dd5f127489b131b61441b9 + +License +------- + +Copyright (c) 2012 Marcus Geelnard + 2013-2014 Evan Nemerson + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + +LICENSE.wingetopt +-------------------------------------------------------------- +For the files wingetopt.c wingetopt.h downloaded from https://github.com/alex85k/wingetopt + +/* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/README.md b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/README.md new file mode 100644 index 0000000..b13dfd2 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/README.md @@ -0,0 +1,24 @@ +# Bundling prebuilt librdkafka + +confluent-kafka-go bundles prebuilt statically linked +versions of librdkafka for the following platforms: + + * MacOSX x64 (aka Darwin) + * Linux glibc x64 (Ubuntu, CentOS, etc) + * Linux musl x64 (Alpine) + +## Import static librdkafka bundle + +First create the static librdkafka bundle following the instructions in +librdkafka's packaging/nuget/README.md. + +Then import the new version by using the import.sh script here, this script +will create a branch, import the bundle, create a commit and push the +branch to Github for PR review. This PR must be manually opened, reviewed +and then finally merged (make sure to merge it, DO NOT squash or rebase). + + $ ./import.sh ~/path/to/librdkafka-static-bundle-v1.4.0.tgz + +This will copy the static library and the rdkafka.h header file +to this directory, as well as generate a new ../build_..go file +for this platform + variant. diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/bundle-import.sh b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/bundle-import.sh new file mode 100644 index 0000000..2f7aeda --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/bundle-import.sh @@ -0,0 +1,113 @@ +#!/bin/bash +# +# Updates the bundled prebuilt librdkafka libraries to specified version. +# + +set -e + + +usage() { + echo "Usage: $0 librdkafka-static-bundle-.tgz" + echo "" + echo "This tool must be run from the TOPDIR/kafka/librdkafka_vendor directory" + exit 1 +} + + +parse_dynlibs() { + # Parse dynamic libraries from pkg-config file, + # both the ones specified with Libs: but also through Requires: + local pc=$1 + local libs= + local req= + local n= + for req in $(grep ^Requires: $pc | sed -e 's/^Requires://'); do + n=$(pkg-config --libs $req) + if [[ $n == -l* ]]; then + libs="${libs} $n" + fi + done + for n in $(grep ^Libs: $pc); do + if [[ $n == -l* ]]; then + libs="${libs} $n" + fi + done + + echo "$libs" +} + +setup_build() { + # Copies static library from the temp directory into final location, + # extracts dynamic lib list from the pkg-config file, + # and generates the build_..go file + local btype=$1 + local apath=$2 + local pc=$3 + local srcinfo=$4 + local build_tag= + local gpath="../build_${btype}.go" + local dpath="librdkafka_${btype}.a" + + if [[ $btype == glibc_linux ]]; then + build_tag="// +build !musl" + elif [[ $btype == musl_linux ]]; then + build_tag="// +build musl" + fi + + local dynlibs=$(parse_dynlibs $pc) + + echo "Copying $apath to $dpath" + cp "$apath" "$dpath" + + echo "Generating $gpath (extra build tag: $build_tag)" + + cat >$gpath <.tgz" + echo "" + echo "This tool must be run from the TOPDIR/kafka/librdkafka directory" + echo "" + echo "Options:" + echo " --devel - Development use: No branch checks and does not push to github" + exit 1 +} + +error_cleanup() { + echo "Error occurred, cleaning up" + git checkout $currbranch + git branch -D $import_branch + exit 1 +} + +devel=0 +if [[ $1 == --devel ]]; then + devel=1 + shift +fi + +bundle="$1" +[[ -f $bundle ]] || usage + +# Parse the librdkafka version from the bundle +bundlename=$(basename $bundle) +version=${bundlename#librdkafka-static-bundle-} +version=${version%.tgz} + +if [[ -z $version ]]; then + echo "Error: Could not parse version from bundle $bundle" + exit 1 +fi + +# Verify branch state +curr_branch=$(git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-) +uncommitted=$(git status --untracked-files=no --porcelain) + +if [[ $devel != 1 ]] && ( [[ $curr_branch != master ]] || [[ ! -z $uncommitted ]] ); then + echo "Error: This script must be run on an up-to-date, clean, master branch" + if [[ ! -z $uncommitted ]]; then + echo "Uncommitted files:" + echo "$uncommitted" + fi + exit 1 +fi + + +# Create import branch, import bundle, commit. +import_branch="import_$version" + +exists=$(git branch -rlq | grep "/$import_branch\$" || true) +if [[ ! -z $exists ]]; then + echo "Error: This version branch already seems to exist: $exists: already imorted?" + [[ $devel != 1 ]] && exit 1 +fi + +echo "Checking for existing commits that match this version (should be none)" +git log --oneline | grep "^librdkafka static bundle $version\$" && exit 1 + + +echo "Creating import branch $import_branch" +git checkout -b $import_branch + +echo "Importing bundle $bundle" +./bundle-import.sh "$bundle" || error_cleanup + +echo "Committing $version" +git commit -a -m "librdkafka static bundle $version" || error_cleanup + +echo "Updating error codes and docs" +pushd ../../ +make -f mk/Makefile docs || error_cleanup +git commit -a -m "Documentation and error code update for librdkafka $version" \ + || error_cleanup +popd + +if [[ $devel != 1 ]]; then + echo "Pushing branch" + git push origin $import_branch || error_cleanup +fi + +git checkout $curr_branch + +if [[ $devel != 1 ]]; then + git branch -D $import_branch +fi + +echo "" +echo "############## IMPORT OF $version COMPLETE ##############" +if [[ $devel != 1 ]]; then + echo "Branch $import_branch has been pushed." + echo "Create a PR, have it reviewed and then merge it (do NOT squash or rebase)." +fi + diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka.go new file mode 100644 index 0000000..52e1ad7 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka.go @@ -0,0 +1,21 @@ +/** + * Copyright 2020 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package librdkafka + +// LibrdkafkaGoSubdir is a dummy variable needed to export something so the +// file is not empty. +var LibrdkafkaGoSubdir = true diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_darwin.a b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_darwin.a new file mode 100644 index 0000000..6294180 Binary files /dev/null and b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_darwin.a differ diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a new file mode 100644 index 0000000..c736b84 Binary files /dev/null and b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_glibc_linux.a differ diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_musl_linux.a b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_musl_linux.a new file mode 100644 index 0000000..f876505 Binary files /dev/null and b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_musl_linux.a differ diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_windows.a b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_windows.a new file mode 100644 index 0000000..7c5909f Binary files /dev/null and b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/librdkafka_windows.a differ diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/rdkafka.h b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/rdkafka.h new file mode 100644 index 0000000..b85ba90 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor/rdkafka.h @@ -0,0 +1,7781 @@ +/* + * librdkafka - Apache Kafka C library + * + * Copyright (c) 2012-2020 Magnus Edenhill + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file rdkafka.h + * @brief Apache Kafka C/C++ consumer and producer client library. + * + * rdkafka.h contains the public API for librdkafka. + * The API is documented in this file as comments prefixing the function, type, + * enum, define, etc. + * + * @sa For the C++ interface see rdkafkacpp.h + * + * @tableofcontents + */ + + +/* @cond NO_DOC */ +#ifndef _RDKAFKA_H_ +#define _RDKAFKA_H_ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#if 0 +} /* Restore indent */ +#endif +#endif + +#ifdef _WIN32 +#include +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#endif +#include /* for sockaddr, .. */ +#ifndef _SSIZE_T_DEFINED +#define _SSIZE_T_DEFINED +typedef SSIZE_T ssize_t; +#endif +#define RD_UNUSED +#define RD_INLINE __inline +#define RD_DEPRECATED __declspec(deprecated) +#define RD_FORMAT(...) +#undef RD_EXPORT +#ifdef LIBRDKAFKA_STATICLIB +#define RD_EXPORT +#else +#ifdef LIBRDKAFKA_EXPORTS +#define RD_EXPORT __declspec(dllexport) +#else +#define RD_EXPORT __declspec(dllimport) +#endif +#ifndef LIBRDKAFKA_TYPECHECKS +#define LIBRDKAFKA_TYPECHECKS 0 +#endif +#endif + +#else +#include /* for sockaddr, .. */ + +#define RD_UNUSED __attribute__((unused)) +#define RD_INLINE inline +#define RD_EXPORT +#define RD_DEPRECATED __attribute__((deprecated)) + +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) +#define RD_FORMAT(...) __attribute__((format (__VA_ARGS__))) +#else +#define RD_FORMAT(...) +#endif + +#ifndef LIBRDKAFKA_TYPECHECKS +#define LIBRDKAFKA_TYPECHECKS 1 +#endif +#endif + + +/** + * @brief Type-checking macros + * Compile-time checking that \p ARG is of type \p TYPE. + * @returns \p RET + */ +#if LIBRDKAFKA_TYPECHECKS +#define _LRK_TYPECHECK(RET,TYPE,ARG) \ + ({ if (0) { TYPE __t RD_UNUSED = (ARG); } RET; }) + +#define _LRK_TYPECHECK2(RET,TYPE,ARG,TYPE2,ARG2) \ + ({ \ + if (0) { \ + TYPE __t RD_UNUSED = (ARG); \ + TYPE2 __t2 RD_UNUSED = (ARG2); \ + } \ + RET; }) + +#define _LRK_TYPECHECK3(RET,TYPE,ARG,TYPE2,ARG2,TYPE3,ARG3) \ + ({ \ + if (0) { \ + TYPE __t RD_UNUSED = (ARG); \ + TYPE2 __t2 RD_UNUSED = (ARG2); \ + TYPE3 __t3 RD_UNUSED = (ARG3); \ + } \ + RET; }) +#else +#define _LRK_TYPECHECK(RET,TYPE,ARG) (RET) +#define _LRK_TYPECHECK2(RET,TYPE,ARG,TYPE2,ARG2) (RET) +#define _LRK_TYPECHECK3(RET,TYPE,ARG,TYPE2,ARG2,TYPE3,ARG3) (RET) +#endif + +/* @endcond */ + + +/** + * @name librdkafka version + * @{ + * + * + */ + +/** + * @brief librdkafka version + * + * Interpreted as hex \c MM.mm.rr.xx: + * - MM = Major + * - mm = minor + * - rr = revision + * - xx = pre-release id (0xff is the final release) + * + * E.g.: \c 0x000801ff = 0.8.1 + * + * @remark This value should only be used during compile time, + * for runtime checks of version use rd_kafka_version() + */ +#define RD_KAFKA_VERSION 0x010802ff + +/** + * @brief Returns the librdkafka version as integer. + * + * @returns Version integer. + * + * @sa See RD_KAFKA_VERSION for how to parse the integer format. + * @sa Use rd_kafka_version_str() to retreive the version as a string. + */ +RD_EXPORT +int rd_kafka_version(void); + +/** + * @brief Returns the librdkafka version as string. + * + * @returns Version string + */ +RD_EXPORT +const char *rd_kafka_version_str (void); + +/**@}*/ + + +/** + * @name Constants, errors, types + * @{ + * + * + */ + + +/** + * @enum rd_kafka_type_t + * + * @brief rd_kafka_t handle type. + * + * @sa rd_kafka_new() + */ +typedef enum rd_kafka_type_t { + RD_KAFKA_PRODUCER, /**< Producer client */ + RD_KAFKA_CONSUMER /**< Consumer client */ +} rd_kafka_type_t; + + +/*! + * Timestamp types + * + * @sa rd_kafka_message_timestamp() + */ +typedef enum rd_kafka_timestamp_type_t { + RD_KAFKA_TIMESTAMP_NOT_AVAILABLE, /**< Timestamp not available */ + RD_KAFKA_TIMESTAMP_CREATE_TIME, /**< Message creation time */ + RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME /**< Log append time */ +} rd_kafka_timestamp_type_t; + + + +/** + * @brief Retrieve supported debug contexts for use with the \c \"debug\" + * configuration property. (runtime) + * + * @returns Comma-separated list of available debugging contexts. + */ +RD_EXPORT +const char *rd_kafka_get_debug_contexts(void); + +/** + * @brief Supported debug contexts. (compile time) + * + * @deprecated This compile time value may be outdated at runtime due to + * linking another version of the library. + * Use rd_kafka_get_debug_contexts() instead. + */ +#define RD_KAFKA_DEBUG_CONTEXTS \ + "all,generic,broker,topic,metadata,feature,queue,msg,protocol,cgrp,security,fetch,interceptor,plugin,consumer,admin,eos,mock,assignor,conf" + + +/* @cond NO_DOC */ +/* Private types to provide ABI compatibility */ +typedef struct rd_kafka_s rd_kafka_t; +typedef struct rd_kafka_topic_s rd_kafka_topic_t; +typedef struct rd_kafka_conf_s rd_kafka_conf_t; +typedef struct rd_kafka_topic_conf_s rd_kafka_topic_conf_t; +typedef struct rd_kafka_queue_s rd_kafka_queue_t; +typedef struct rd_kafka_op_s rd_kafka_event_t; +typedef struct rd_kafka_topic_result_s rd_kafka_topic_result_t; +typedef struct rd_kafka_consumer_group_metadata_s +rd_kafka_consumer_group_metadata_t; +typedef struct rd_kafka_error_s rd_kafka_error_t; +typedef struct rd_kafka_headers_s rd_kafka_headers_t; +typedef struct rd_kafka_group_result_s rd_kafka_group_result_t; +/* @endcond */ + + +/** + * @enum rd_kafka_resp_err_t + * @brief Error codes. + * + * The negative error codes delimited by two underscores + * (\c RD_KAFKA_RESP_ERR__..) denotes errors internal to librdkafka and are + * displayed as \c \"Local: \\", while the error codes + * delimited by a single underscore (\c RD_KAFKA_RESP_ERR_..) denote broker + * errors and are displayed as \c \"Broker: \\". + * + * @sa Use rd_kafka_err2str() to translate an error code a human readable string + */ +typedef enum { + /* Internal errors to rdkafka: */ + /** Begin internal error codes */ + RD_KAFKA_RESP_ERR__BEGIN = -200, + /** Received message is incorrect */ + RD_KAFKA_RESP_ERR__BAD_MSG = -199, + /** Bad/unknown compression */ + RD_KAFKA_RESP_ERR__BAD_COMPRESSION = -198, + /** Broker is going away */ + RD_KAFKA_RESP_ERR__DESTROY = -197, + /** Generic failure */ + RD_KAFKA_RESP_ERR__FAIL = -196, + /** Broker transport failure */ + RD_KAFKA_RESP_ERR__TRANSPORT = -195, + /** Critical system resource */ + RD_KAFKA_RESP_ERR__CRIT_SYS_RESOURCE = -194, + /** Failed to resolve broker */ + RD_KAFKA_RESP_ERR__RESOLVE = -193, + /** Produced message timed out*/ + RD_KAFKA_RESP_ERR__MSG_TIMED_OUT = -192, + /** Reached the end of the topic+partition queue on + * the broker. Not really an error. + * This event is disabled by default, + * see the `enable.partition.eof` configuration property. */ + RD_KAFKA_RESP_ERR__PARTITION_EOF = -191, + /** Permanent: Partition does not exist in cluster. */ + RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION = -190, + /** File or filesystem error */ + RD_KAFKA_RESP_ERR__FS = -189, + /** Permanent: Topic does not exist in cluster. */ + RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC = -188, + /** All broker connections are down. */ + RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN = -187, + /** Invalid argument, or invalid configuration */ + RD_KAFKA_RESP_ERR__INVALID_ARG = -186, + /** Operation timed out */ + RD_KAFKA_RESP_ERR__TIMED_OUT = -185, + /** Queue is full */ + RD_KAFKA_RESP_ERR__QUEUE_FULL = -184, + /** ISR count < required.acks */ + RD_KAFKA_RESP_ERR__ISR_INSUFF = -183, + /** Broker node update */ + RD_KAFKA_RESP_ERR__NODE_UPDATE = -182, + /** SSL error */ + RD_KAFKA_RESP_ERR__SSL = -181, + /** Waiting for coordinator to become available. */ + RD_KAFKA_RESP_ERR__WAIT_COORD = -180, + /** Unknown client group */ + RD_KAFKA_RESP_ERR__UNKNOWN_GROUP = -179, + /** Operation in progress */ + RD_KAFKA_RESP_ERR__IN_PROGRESS = -178, + /** Previous operation in progress, wait for it to finish. */ + RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS = -177, + /** This operation would interfere with an existing subscription */ + RD_KAFKA_RESP_ERR__EXISTING_SUBSCRIPTION = -176, + /** Assigned partitions (rebalance_cb) */ + RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS = -175, + /** Revoked partitions (rebalance_cb) */ + RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS = -174, + /** Conflicting use */ + RD_KAFKA_RESP_ERR__CONFLICT = -173, + /** Wrong state */ + RD_KAFKA_RESP_ERR__STATE = -172, + /** Unknown protocol */ + RD_KAFKA_RESP_ERR__UNKNOWN_PROTOCOL = -171, + /** Not implemented */ + RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED = -170, + /** Authentication failure*/ + RD_KAFKA_RESP_ERR__AUTHENTICATION = -169, + /** No stored offset */ + RD_KAFKA_RESP_ERR__NO_OFFSET = -168, + /** Outdated */ + RD_KAFKA_RESP_ERR__OUTDATED = -167, + /** Timed out in queue */ + RD_KAFKA_RESP_ERR__TIMED_OUT_QUEUE = -166, + /** Feature not supported by broker */ + RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE = -165, + /** Awaiting cache update */ + RD_KAFKA_RESP_ERR__WAIT_CACHE = -164, + /** Operation interrupted (e.g., due to yield)) */ + RD_KAFKA_RESP_ERR__INTR = -163, + /** Key serialization error */ + RD_KAFKA_RESP_ERR__KEY_SERIALIZATION = -162, + /** Value serialization error */ + RD_KAFKA_RESP_ERR__VALUE_SERIALIZATION = -161, + /** Key deserialization error */ + RD_KAFKA_RESP_ERR__KEY_DESERIALIZATION = -160, + /** Value deserialization error */ + RD_KAFKA_RESP_ERR__VALUE_DESERIALIZATION = -159, + /** Partial response */ + RD_KAFKA_RESP_ERR__PARTIAL = -158, + /** Modification attempted on read-only object */ + RD_KAFKA_RESP_ERR__READ_ONLY = -157, + /** No such entry / item not found */ + RD_KAFKA_RESP_ERR__NOENT = -156, + /** Read underflow */ + RD_KAFKA_RESP_ERR__UNDERFLOW = -155, + /** Invalid type */ + RD_KAFKA_RESP_ERR__INVALID_TYPE = -154, + /** Retry operation */ + RD_KAFKA_RESP_ERR__RETRY = -153, + /** Purged in queue */ + RD_KAFKA_RESP_ERR__PURGE_QUEUE = -152, + /** Purged in flight */ + RD_KAFKA_RESP_ERR__PURGE_INFLIGHT = -151, + /** Fatal error: see rd_kafka_fatal_error() */ + RD_KAFKA_RESP_ERR__FATAL = -150, + /** Inconsistent state */ + RD_KAFKA_RESP_ERR__INCONSISTENT = -149, + /** Gap-less ordering would not be guaranteed if proceeding */ + RD_KAFKA_RESP_ERR__GAPLESS_GUARANTEE = -148, + /** Maximum poll interval exceeded */ + RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED = -147, + /** Unknown broker */ + RD_KAFKA_RESP_ERR__UNKNOWN_BROKER = -146, + /** Functionality not configured */ + RD_KAFKA_RESP_ERR__NOT_CONFIGURED = -145, + /** Instance has been fenced */ + RD_KAFKA_RESP_ERR__FENCED = -144, + /** Application generated error */ + RD_KAFKA_RESP_ERR__APPLICATION = -143, + /** Assignment lost */ + RD_KAFKA_RESP_ERR__ASSIGNMENT_LOST = -142, + /** No operation performed */ + RD_KAFKA_RESP_ERR__NOOP = -141, + /** No offset to automatically reset to */ + RD_KAFKA_RESP_ERR__AUTO_OFFSET_RESET = -140, + + /** End internal error codes */ + RD_KAFKA_RESP_ERR__END = -100, + + /* Kafka broker errors: */ + /** Unknown broker error */ + RD_KAFKA_RESP_ERR_UNKNOWN = -1, + /** Success */ + RD_KAFKA_RESP_ERR_NO_ERROR = 0, + /** Offset out of range */ + RD_KAFKA_RESP_ERR_OFFSET_OUT_OF_RANGE = 1, + /** Invalid message */ + RD_KAFKA_RESP_ERR_INVALID_MSG = 2, + /** Unknown topic or partition */ + RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART = 3, + /** Invalid message size */ + RD_KAFKA_RESP_ERR_INVALID_MSG_SIZE = 4, + /** Leader not available */ + RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE = 5, + /** Not leader for partition */ + RD_KAFKA_RESP_ERR_NOT_LEADER_FOR_PARTITION = 6, + /** Request timed out */ + RD_KAFKA_RESP_ERR_REQUEST_TIMED_OUT = 7, + /** Broker not available */ + RD_KAFKA_RESP_ERR_BROKER_NOT_AVAILABLE = 8, + /** Replica not available */ + RD_KAFKA_RESP_ERR_REPLICA_NOT_AVAILABLE = 9, + /** Message size too large */ + RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE = 10, + /** StaleControllerEpochCode */ + RD_KAFKA_RESP_ERR_STALE_CTRL_EPOCH = 11, + /** Offset metadata string too large */ + RD_KAFKA_RESP_ERR_OFFSET_METADATA_TOO_LARGE = 12, + /** Broker disconnected before response received */ + RD_KAFKA_RESP_ERR_NETWORK_EXCEPTION = 13, + /** Coordinator load in progress */ + RD_KAFKA_RESP_ERR_COORDINATOR_LOAD_IN_PROGRESS = 14, + /** Group coordinator load in progress */ +#define RD_KAFKA_RESP_ERR_GROUP_LOAD_IN_PROGRESS \ + RD_KAFKA_RESP_ERR_COORDINATOR_LOAD_IN_PROGRESS + /** Coordinator not available */ + RD_KAFKA_RESP_ERR_COORDINATOR_NOT_AVAILABLE = 15, + /** Group coordinator not available */ +#define RD_KAFKA_RESP_ERR_GROUP_COORDINATOR_NOT_AVAILABLE \ + RD_KAFKA_RESP_ERR_COORDINATOR_NOT_AVAILABLE + /** Not coordinator */ + RD_KAFKA_RESP_ERR_NOT_COORDINATOR = 16, + /** Not coordinator for group */ +#define RD_KAFKA_RESP_ERR_NOT_COORDINATOR_FOR_GROUP \ + RD_KAFKA_RESP_ERR_NOT_COORDINATOR + /** Invalid topic */ + RD_KAFKA_RESP_ERR_TOPIC_EXCEPTION = 17, + /** Message batch larger than configured server segment size */ + RD_KAFKA_RESP_ERR_RECORD_LIST_TOO_LARGE = 18, + /** Not enough in-sync replicas */ + RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS = 19, + /** Message(s) written to insufficient number of in-sync replicas */ + RD_KAFKA_RESP_ERR_NOT_ENOUGH_REPLICAS_AFTER_APPEND = 20, + /** Invalid required acks value */ + RD_KAFKA_RESP_ERR_INVALID_REQUIRED_ACKS = 21, + /** Specified group generation id is not valid */ + RD_KAFKA_RESP_ERR_ILLEGAL_GENERATION = 22, + /** Inconsistent group protocol */ + RD_KAFKA_RESP_ERR_INCONSISTENT_GROUP_PROTOCOL = 23, + /** Invalid group.id */ + RD_KAFKA_RESP_ERR_INVALID_GROUP_ID = 24, + /** Unknown member */ + RD_KAFKA_RESP_ERR_UNKNOWN_MEMBER_ID = 25, + /** Invalid session timeout */ + RD_KAFKA_RESP_ERR_INVALID_SESSION_TIMEOUT = 26, + /** Group rebalance in progress */ + RD_KAFKA_RESP_ERR_REBALANCE_IN_PROGRESS = 27, + /** Commit offset data size is not valid */ + RD_KAFKA_RESP_ERR_INVALID_COMMIT_OFFSET_SIZE = 28, + /** Topic authorization failed */ + RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED = 29, + /** Group authorization failed */ + RD_KAFKA_RESP_ERR_GROUP_AUTHORIZATION_FAILED = 30, + /** Cluster authorization failed */ + RD_KAFKA_RESP_ERR_CLUSTER_AUTHORIZATION_FAILED = 31, + /** Invalid timestamp */ + RD_KAFKA_RESP_ERR_INVALID_TIMESTAMP = 32, + /** Unsupported SASL mechanism */ + RD_KAFKA_RESP_ERR_UNSUPPORTED_SASL_MECHANISM = 33, + /** Illegal SASL state */ + RD_KAFKA_RESP_ERR_ILLEGAL_SASL_STATE = 34, + /** Unuspported version */ + RD_KAFKA_RESP_ERR_UNSUPPORTED_VERSION = 35, + /** Topic already exists */ + RD_KAFKA_RESP_ERR_TOPIC_ALREADY_EXISTS = 36, + /** Invalid number of partitions */ + RD_KAFKA_RESP_ERR_INVALID_PARTITIONS = 37, + /** Invalid replication factor */ + RD_KAFKA_RESP_ERR_INVALID_REPLICATION_FACTOR = 38, + /** Invalid replica assignment */ + RD_KAFKA_RESP_ERR_INVALID_REPLICA_ASSIGNMENT = 39, + /** Invalid config */ + RD_KAFKA_RESP_ERR_INVALID_CONFIG = 40, + /** Not controller for cluster */ + RD_KAFKA_RESP_ERR_NOT_CONTROLLER = 41, + /** Invalid request */ + RD_KAFKA_RESP_ERR_INVALID_REQUEST = 42, + /** Message format on broker does not support request */ + RD_KAFKA_RESP_ERR_UNSUPPORTED_FOR_MESSAGE_FORMAT = 43, + /** Policy violation */ + RD_KAFKA_RESP_ERR_POLICY_VIOLATION = 44, + /** Broker received an out of order sequence number */ + RD_KAFKA_RESP_ERR_OUT_OF_ORDER_SEQUENCE_NUMBER = 45, + /** Broker received a duplicate sequence number */ + RD_KAFKA_RESP_ERR_DUPLICATE_SEQUENCE_NUMBER = 46, + /** Producer attempted an operation with an old epoch */ + RD_KAFKA_RESP_ERR_INVALID_PRODUCER_EPOCH = 47, + /** Producer attempted a transactional operation in an invalid state */ + RD_KAFKA_RESP_ERR_INVALID_TXN_STATE = 48, + /** Producer attempted to use a producer id which is not + * currently assigned to its transactional id */ + RD_KAFKA_RESP_ERR_INVALID_PRODUCER_ID_MAPPING = 49, + /** Transaction timeout is larger than the maximum + * value allowed by the broker's max.transaction.timeout.ms */ + RD_KAFKA_RESP_ERR_INVALID_TRANSACTION_TIMEOUT = 50, + /** Producer attempted to update a transaction while another + * concurrent operation on the same transaction was ongoing */ + RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS = 51, + /** Indicates that the transaction coordinator sending a + * WriteTxnMarker is no longer the current coordinator for a + * given producer */ + RD_KAFKA_RESP_ERR_TRANSACTION_COORDINATOR_FENCED = 52, + /** Transactional Id authorization failed */ + RD_KAFKA_RESP_ERR_TRANSACTIONAL_ID_AUTHORIZATION_FAILED = 53, + /** Security features are disabled */ + RD_KAFKA_RESP_ERR_SECURITY_DISABLED = 54, + /** Operation not attempted */ + RD_KAFKA_RESP_ERR_OPERATION_NOT_ATTEMPTED = 55, + /** Disk error when trying to access log file on the disk */ + RD_KAFKA_RESP_ERR_KAFKA_STORAGE_ERROR = 56, + /** The user-specified log directory is not found in the broker config */ + RD_KAFKA_RESP_ERR_LOG_DIR_NOT_FOUND = 57, + /** SASL Authentication failed */ + RD_KAFKA_RESP_ERR_SASL_AUTHENTICATION_FAILED = 58, + /** Unknown Producer Id */ + RD_KAFKA_RESP_ERR_UNKNOWN_PRODUCER_ID = 59, + /** Partition reassignment is in progress */ + RD_KAFKA_RESP_ERR_REASSIGNMENT_IN_PROGRESS = 60, + /** Delegation Token feature is not enabled */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTH_DISABLED = 61, + /** Delegation Token is not found on server */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_NOT_FOUND = 62, + /** Specified Principal is not valid Owner/Renewer */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_OWNER_MISMATCH = 63, + /** Delegation Token requests are not allowed on this connection */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_REQUEST_NOT_ALLOWED = 64, + /** Delegation Token authorization failed */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_AUTHORIZATION_FAILED = 65, + /** Delegation Token is expired */ + RD_KAFKA_RESP_ERR_DELEGATION_TOKEN_EXPIRED = 66, + /** Supplied principalType is not supported */ + RD_KAFKA_RESP_ERR_INVALID_PRINCIPAL_TYPE = 67, + /** The group is not empty */ + RD_KAFKA_RESP_ERR_NON_EMPTY_GROUP = 68, + /** The group id does not exist */ + RD_KAFKA_RESP_ERR_GROUP_ID_NOT_FOUND = 69, + /** The fetch session ID was not found */ + RD_KAFKA_RESP_ERR_FETCH_SESSION_ID_NOT_FOUND = 70, + /** The fetch session epoch is invalid */ + RD_KAFKA_RESP_ERR_INVALID_FETCH_SESSION_EPOCH = 71, + /** No matching listener */ + RD_KAFKA_RESP_ERR_LISTENER_NOT_FOUND = 72, + /** Topic deletion is disabled */ + RD_KAFKA_RESP_ERR_TOPIC_DELETION_DISABLED = 73, + /** Leader epoch is older than broker epoch */ + RD_KAFKA_RESP_ERR_FENCED_LEADER_EPOCH = 74, + /** Leader epoch is newer than broker epoch */ + RD_KAFKA_RESP_ERR_UNKNOWN_LEADER_EPOCH = 75, + /** Unsupported compression type */ + RD_KAFKA_RESP_ERR_UNSUPPORTED_COMPRESSION_TYPE = 76, + /** Broker epoch has changed */ + RD_KAFKA_RESP_ERR_STALE_BROKER_EPOCH = 77, + /** Leader high watermark is not caught up */ + RD_KAFKA_RESP_ERR_OFFSET_NOT_AVAILABLE = 78, + /** Group member needs a valid member ID */ + RD_KAFKA_RESP_ERR_MEMBER_ID_REQUIRED = 79, + /** Preferred leader was not available */ + RD_KAFKA_RESP_ERR_PREFERRED_LEADER_NOT_AVAILABLE = 80, + /** Consumer group has reached maximum size */ + RD_KAFKA_RESP_ERR_GROUP_MAX_SIZE_REACHED = 81, + /** Static consumer fenced by other consumer with same + * group.instance.id. */ + RD_KAFKA_RESP_ERR_FENCED_INSTANCE_ID = 82, + /** Eligible partition leaders are not available */ + RD_KAFKA_RESP_ERR_ELIGIBLE_LEADERS_NOT_AVAILABLE = 83, + /** Leader election not needed for topic partition */ + RD_KAFKA_RESP_ERR_ELECTION_NOT_NEEDED = 84, + /** No partition reassignment is in progress */ + RD_KAFKA_RESP_ERR_NO_REASSIGNMENT_IN_PROGRESS = 85, + /** Deleting offsets of a topic while the consumer group is + * subscribed to it */ + RD_KAFKA_RESP_ERR_GROUP_SUBSCRIBED_TO_TOPIC = 86, + /** Broker failed to validate record */ + RD_KAFKA_RESP_ERR_INVALID_RECORD = 87, + /** There are unstable offsets that need to be cleared */ + RD_KAFKA_RESP_ERR_UNSTABLE_OFFSET_COMMIT = 88, + /** Throttling quota has been exceeded */ + RD_KAFKA_RESP_ERR_THROTTLING_QUOTA_EXCEEDED = 89, + /** There is a newer producer with the same transactionalId + * which fences the current one */ + RD_KAFKA_RESP_ERR_PRODUCER_FENCED = 90, + /** Request illegally referred to resource that does not exist */ + RD_KAFKA_RESP_ERR_RESOURCE_NOT_FOUND = 91, + /** Request illegally referred to the same resource twice */ + RD_KAFKA_RESP_ERR_DUPLICATE_RESOURCE = 92, + /** Requested credential would not meet criteria for acceptability */ + RD_KAFKA_RESP_ERR_UNACCEPTABLE_CREDENTIAL = 93, + /** Indicates that the either the sender or recipient of a + * voter-only request is not one of the expected voters */ + RD_KAFKA_RESP_ERR_INCONSISTENT_VOTER_SET = 94, + /** Invalid update version */ + RD_KAFKA_RESP_ERR_INVALID_UPDATE_VERSION = 95, + /** Unable to update finalized features due to server error */ + RD_KAFKA_RESP_ERR_FEATURE_UPDATE_FAILED = 96, + /** Request principal deserialization failed during forwarding */ + RD_KAFKA_RESP_ERR_PRINCIPAL_DESERIALIZATION_FAILURE = 97, + + RD_KAFKA_RESP_ERR_END_ALL, +} rd_kafka_resp_err_t; + + +/** + * @brief Error code value, name and description. + * Typically for use with language bindings to automatically expose + * the full set of librdkafka error codes. + */ +struct rd_kafka_err_desc { + rd_kafka_resp_err_t code;/**< Error code */ + const char *name; /**< Error name, same as code enum sans prefix */ + const char *desc; /**< Human readable error description. */ +}; + + +/** + * @brief Returns the full list of error codes. + */ +RD_EXPORT +void rd_kafka_get_err_descs (const struct rd_kafka_err_desc **errdescs, + size_t *cntp); + + + + +/** + * @brief Returns a human readable representation of a kafka error. + * + * @param err Error code to translate + */ +RD_EXPORT +const char *rd_kafka_err2str (rd_kafka_resp_err_t err); + + + +/** + * @brief Returns the error code name (enum name). + * + * @param err Error code to translate + */ +RD_EXPORT +const char *rd_kafka_err2name (rd_kafka_resp_err_t err); + + +/** + * @brief Returns the last error code generated by a legacy API call + * in the current thread. + * + * The legacy APIs are the ones using errno to propagate error value, namely: + * - rd_kafka_topic_new() + * - rd_kafka_consume_start() + * - rd_kafka_consume_stop() + * - rd_kafka_consume() + * - rd_kafka_consume_batch() + * - rd_kafka_consume_callback() + * - rd_kafka_consume_queue() + * - rd_kafka_produce() + * + * The main use for this function is to avoid converting system \p errno + * values to rd_kafka_resp_err_t codes for legacy APIs. + * + * @remark The last error is stored per-thread, if multiple rd_kafka_t handles + * are used in the same application thread the developer needs to + * make sure rd_kafka_last_error() is called immediately after + * a failed API call. + * + * @remark errno propagation from librdkafka is not safe on Windows + * and should not be used, use rd_kafka_last_error() instead. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_last_error (void); + + +/** + * @brief Converts the system errno value \p errnox to a rd_kafka_resp_err_t + * error code upon failure from the following functions: + * - rd_kafka_topic_new() + * - rd_kafka_consume_start() + * - rd_kafka_consume_stop() + * - rd_kafka_consume() + * - rd_kafka_consume_batch() + * - rd_kafka_consume_callback() + * - rd_kafka_consume_queue() + * - rd_kafka_produce() + * + * @param errnox System errno value to convert + * + * @returns Appropriate error code for \p errnox + * + * @remark A better alternative is to call rd_kafka_last_error() immediately + * after any of the above functions return -1 or NULL. + * + * @deprecated Use rd_kafka_last_error() to retrieve the last error code + * set by the legacy librdkafka APIs. + * + * @sa rd_kafka_last_error() + */ +RD_EXPORT RD_DEPRECATED +rd_kafka_resp_err_t rd_kafka_errno2err(int errnox); + + +/** + * @brief Returns the thread-local system errno + * + * On most platforms this is the same as \p errno but in case of different + * runtimes between library and application (e.g., Windows static DLLs) + * this provides a means for exposing the errno librdkafka uses. + * + * @remark The value is local to the current calling thread. + * + * @deprecated Use rd_kafka_last_error() to retrieve the last error code + * set by the legacy librdkafka APIs. + */ +RD_EXPORT RD_DEPRECATED +int rd_kafka_errno (void); + + + + +/** + * @brief Returns the first fatal error set on this client instance, + * or RD_KAFKA_RESP_ERR_NO_ERROR if no fatal error has occurred. + * + * This function is to be used with the Idempotent Producer and \c error_cb + * to detect fatal errors. + * + * Generally all errors raised by \c error_cb are to be considered + * informational and temporary, the client will try to recover from all + * errors in a graceful fashion (by retrying, etc). + * + * However, some errors should logically be considered fatal to retain + * consistency; in particular a set of errors that may occur when using the + * Idempotent Producer and the in-order or exactly-once producer guarantees + * can't be satisfied. + * + * @param rk Client instance. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written to if there is a fatal error. + * @param errstr_size Writable size in \p errstr. + * + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if no fatal error has been raised, else + * any other error code. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_fatal_error (rd_kafka_t *rk, + char *errstr, size_t errstr_size); + + +/** + * @brief Trigger a fatal error for testing purposes. + * + * Since there is no practical way to trigger real fatal errors in the + * idempotent producer, this method allows an application to trigger + * fabricated fatal errors in tests to check its error handling code. + * + * @param rk Client instance. + * @param err The underlying error code. + * @param reason A human readable error reason. + * Will be prefixed with "test_fatal_error: " to differentiate + * from real fatal errors. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if a fatal error was triggered, or + * RD_KAFKA_RESP_ERR__PREV_IN_PROGRESS if a previous fatal error + * has already been triggered. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_test_fatal_error (rd_kafka_t *rk, rd_kafka_resp_err_t err, + const char *reason); + + +/** + * @returns the error code for \p error or RD_KAFKA_RESP_ERR_NO_ERROR if + * \p error is NULL. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_error_code (const rd_kafka_error_t *error); + +/** + * @returns the error code name for \p error, e.g, "ERR_UNKNOWN_MEMBER_ID", + * or an empty string if \p error is NULL. + * + * @remark The lifetime of the returned pointer is the same as the error object. + * + * @sa rd_kafka_err2name() + */ +RD_EXPORT +const char *rd_kafka_error_name (const rd_kafka_error_t *error); + +/** + * @returns a human readable error string for \p error, + * or an empty string if \p error is NULL. + * + * @remark The lifetime of the returned pointer is the same as the error object. + */ +RD_EXPORT +const char *rd_kafka_error_string (const rd_kafka_error_t *error); + + +/** + * @returns 1 if the error is a fatal error, indicating that the client + * instance is no longer usable, else 0 (also if \p error is NULL). + */ +RD_EXPORT +int rd_kafka_error_is_fatal (const rd_kafka_error_t *error); + + +/** + * @returns 1 if the operation may be retried, + * else 0 (also if \p error is NULL). + */ +RD_EXPORT +int rd_kafka_error_is_retriable (const rd_kafka_error_t *error); + + +/** + * @returns 1 if the error is an abortable transaction error in which case + * the application must call rd_kafka_abort_transaction() and + * start a new transaction with rd_kafka_begin_transaction() if it + * wishes to proceed with transactions. + * Else returns 0 (also if \p error is NULL). + * + * @remark The return value of this method is only valid for errors returned + * by the transactional API. + */ +RD_EXPORT +int rd_kafka_error_txn_requires_abort (const rd_kafka_error_t *error); + +/** + * @brief Free and destroy an error object. + * + * @remark As a conveniance it is permitted to pass a NULL \p error. + */ +RD_EXPORT +void rd_kafka_error_destroy (rd_kafka_error_t *error); + + +/** + * @brief Create a new error object with error \p code and optional + * human readable error string in \p fmt. + * + * This method is mainly to be used for mocking errors in application test code. + * + * The returned object must be destroyed with rd_kafka_error_destroy(). + */ +RD_EXPORT +rd_kafka_error_t *rd_kafka_error_new (rd_kafka_resp_err_t code, + const char *fmt, ...) + RD_FORMAT(printf, 2, 3); + + +/** + * @brief Topic+Partition place holder + * + * Generic place holder for a Topic+Partition and its related information + * used for multiple purposes: + * - consumer offset (see rd_kafka_commit(), et.al.) + * - group rebalancing callback (rd_kafka_conf_set_rebalance_cb()) + * - offset commit result callback (rd_kafka_conf_set_offset_commit_cb()) + */ + +/** + * @brief Generic place holder for a specific Topic+Partition. + * + * @sa rd_kafka_topic_partition_list_new() + */ +typedef struct rd_kafka_topic_partition_s { + char *topic; /**< Topic name */ + int32_t partition; /**< Partition */ + int64_t offset; /**< Offset */ + void *metadata; /**< Metadata */ + size_t metadata_size; /**< Metadata size */ + void *opaque; /**< Opaque value for application use */ + rd_kafka_resp_err_t err; /**< Error code, depending on use. */ + void *_private; /**< INTERNAL USE ONLY, + * INITIALIZE TO ZERO, DO NOT TOUCH */ +} rd_kafka_topic_partition_t; + + +/** + * @brief Destroy a rd_kafka_topic_partition_t. + * @remark This must not be called for elements in a topic partition list. + */ +RD_EXPORT +void rd_kafka_topic_partition_destroy (rd_kafka_topic_partition_t *rktpar); + + +/** + * @brief A growable list of Topic+Partitions. + * + */ +typedef struct rd_kafka_topic_partition_list_s { + int cnt; /**< Current number of elements */ + int size; /**< Current allocated size */ + rd_kafka_topic_partition_t *elems; /**< Element array[] */ +} rd_kafka_topic_partition_list_t; + + +/** + * @brief Create a new list/vector Topic+Partition container. + * + * @param size Initial allocated size used when the expected number of + * elements is known or can be estimated. + * Avoids reallocation and possibly relocation of the + * elems array. + * + * @returns A newly allocated Topic+Partition list. + * + * @remark Use rd_kafka_topic_partition_list_destroy() to free all resources + * in use by a list and the list itself. + * @sa rd_kafka_topic_partition_list_add() + */ +RD_EXPORT +rd_kafka_topic_partition_list_t *rd_kafka_topic_partition_list_new (int size); + + +/** + * @brief Free all resources used by the list and the list itself. + */ +RD_EXPORT +void +rd_kafka_topic_partition_list_destroy (rd_kafka_topic_partition_list_t *rkparlist); + +/** + * @brief Add topic+partition to list + * + * @param rktparlist List to extend + * @param topic Topic name (copied) + * @param partition Partition id + * + * @returns The object which can be used to fill in additionals fields. + */ +RD_EXPORT +rd_kafka_topic_partition_t * +rd_kafka_topic_partition_list_add (rd_kafka_topic_partition_list_t *rktparlist, + const char *topic, int32_t partition); + + +/** + * @brief Add range of partitions from \p start to \p stop inclusive. + * + * @param rktparlist List to extend + * @param topic Topic name (copied) + * @param start Start partition of range + * @param stop Last partition of range (inclusive) + */ +RD_EXPORT +void +rd_kafka_topic_partition_list_add_range (rd_kafka_topic_partition_list_t + *rktparlist, + const char *topic, + int32_t start, int32_t stop); + + + +/** + * @brief Delete partition from list. + * + * @param rktparlist List to modify + * @param topic Topic name to match + * @param partition Partition to match + * + * @returns 1 if partition was found (and removed), else 0. + * + * @remark Any held indices to elems[] are unusable after this call returns 1. + */ +RD_EXPORT +int +rd_kafka_topic_partition_list_del (rd_kafka_topic_partition_list_t *rktparlist, + const char *topic, int32_t partition); + + +/** + * @brief Delete partition from list by elems[] index. + * + * @returns 1 if partition was found (and removed), else 0. + * + * @sa rd_kafka_topic_partition_list_del() + */ +RD_EXPORT +int +rd_kafka_topic_partition_list_del_by_idx ( + rd_kafka_topic_partition_list_t *rktparlist, + int idx); + + +/** + * @brief Make a copy of an existing list. + * + * @param src The existing list to copy. + * + * @returns A new list fully populated to be identical to \p src + */ +RD_EXPORT +rd_kafka_topic_partition_list_t * +rd_kafka_topic_partition_list_copy (const rd_kafka_topic_partition_list_t *src); + + + + +/** + * @brief Set offset to \p offset for \p topic and \p partition + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or + * RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION if \p partition was not found + * in the list. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_topic_partition_list_set_offset ( + rd_kafka_topic_partition_list_t *rktparlist, + const char *topic, int32_t partition, int64_t offset); + + + +/** + * @brief Find element by \p topic and \p partition. + * + * @returns a pointer to the first matching element, or NULL if not found. + */ +RD_EXPORT +rd_kafka_topic_partition_t * +rd_kafka_topic_partition_list_find ( + const rd_kafka_topic_partition_list_t *rktparlist, + const char *topic, int32_t partition); + + +/** + * @brief Sort list using comparator \p cmp. + * + * If \p cmp is NULL the default comparator will be used that + * sorts by ascending topic name and partition. + * + * \p cmp_opaque is provided as the \p cmp_opaque argument to \p cmp. + * + */ +RD_EXPORT void +rd_kafka_topic_partition_list_sort (rd_kafka_topic_partition_list_t *rktparlist, + int (*cmp) (const void *a, const void *b, + void *cmp_opaque), + void *cmp_opaque); + + +/**@}*/ + + + +/** + * @name Var-arg tag types + * @{ + * + */ + +/** + * @enum rd_kafka_vtype_t + * + * @brief Var-arg tag types + * + * @sa rd_kafka_producev() + */ +typedef enum rd_kafka_vtype_t { + RD_KAFKA_VTYPE_END, /**< va-arg sentinel */ + RD_KAFKA_VTYPE_TOPIC, /**< (const char *) Topic name */ + RD_KAFKA_VTYPE_RKT, /**< (rd_kafka_topic_t *) Topic handle */ + RD_KAFKA_VTYPE_PARTITION, /**< (int32_t) Partition */ + RD_KAFKA_VTYPE_VALUE, /**< (void *, size_t) Message value (payload)*/ + RD_KAFKA_VTYPE_KEY, /**< (void *, size_t) Message key */ + RD_KAFKA_VTYPE_OPAQUE, /**< (void *) Per-message application opaque + * value. This is the same as + * the _private field in + * rd_kafka_message_t, also known + * as the msg_opaque. */ + RD_KAFKA_VTYPE_MSGFLAGS, /**< (int) RD_KAFKA_MSG_F_.. flags */ + RD_KAFKA_VTYPE_TIMESTAMP, /**< (int64_t) Milliseconds since epoch UTC */ + RD_KAFKA_VTYPE_HEADER, /**< (const char *, const void *, ssize_t) + * Message Header */ + RD_KAFKA_VTYPE_HEADERS, /**< (rd_kafka_headers_t *) Headers list */ +} rd_kafka_vtype_t; + + +/** + * @brief VTYPE + argument container for use with rd_kafka_produce_va() + * + * See RD_KAFKA_V_..() macros below for which union field corresponds + * to which RD_KAFKA_VTYPE_... + */ +typedef struct rd_kafka_vu_s { + rd_kafka_vtype_t vtype; /**< RD_KAFKA_VTYPE_.. */ + /** Value union, see RD_KAFKA_V_.. macros for which field to use. */ + union { + const char *cstr; + rd_kafka_topic_t *rkt; + int i; + int32_t i32; + int64_t i64; + struct { + void *ptr; + size_t size; + } mem; + struct { + const char *name; + const void *val; + ssize_t size; + } header; + rd_kafka_headers_t *headers; + void *ptr; + char _pad[64]; /**< Padding size for future-proofness */ + } u; +} rd_kafka_vu_t; + +/** + * @brief Convenience macros for rd_kafka_vtype_t that takes the + * correct arguments for each vtype. + */ + +/*! + * va-arg end sentinel used to terminate the variable argument list + */ +#define RD_KAFKA_V_END RD_KAFKA_VTYPE_END + +/*! + * Topic name (const char *) + * + * rd_kafka_vu_t field: u.cstr + */ +#define RD_KAFKA_V_TOPIC(topic) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_TOPIC, const char *, topic), \ + (const char *)topic +/*! + * Topic object (rd_kafka_topic_t *) + * + * rd_kafka_vu_t field: u.rkt + */ +#define RD_KAFKA_V_RKT(rkt) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_RKT, rd_kafka_topic_t *, rkt), \ + (rd_kafka_topic_t *)rkt +/*! + * Partition (int32_t) + * + * rd_kafka_vu_t field: u.i32 + */ +#define RD_KAFKA_V_PARTITION(partition) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_PARTITION, int32_t, partition), \ + (int32_t)partition +/*! + * Message value/payload pointer and length (void *, size_t) + * + * rd_kafka_vu_t fields: u.mem.ptr, u.mem.size + */ +#define RD_KAFKA_V_VALUE(VALUE,LEN) \ + _LRK_TYPECHECK2(RD_KAFKA_VTYPE_VALUE, void *, VALUE, size_t, LEN), \ + (void *)VALUE, (size_t)LEN +/*! + * Message key pointer and length (const void *, size_t) + * + * rd_kafka_vu_t field: u.mem.ptr, rd_kafka_vu.t.u.mem.size + */ +#define RD_KAFKA_V_KEY(KEY,LEN) \ + _LRK_TYPECHECK2(RD_KAFKA_VTYPE_KEY, const void *, KEY, size_t, LEN), \ + (void *)KEY, (size_t)LEN +/*! + * Message opaque pointer (void *) + * Same as \c msg_opaque, \c produce(.., msg_opaque), + * and \c rkmessage->_private . + * + * rd_kafka_vu_t field: u.ptr + */ +#define RD_KAFKA_V_OPAQUE(msg_opaque) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_OPAQUE, void *, msg_opaque), \ + (void *)msg_opaque +/*! + * Message flags (int) + * @sa RD_KAFKA_MSG_F_COPY, et.al. + * + * rd_kafka_vu_t field: u.i + */ +#define RD_KAFKA_V_MSGFLAGS(msgflags) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_MSGFLAGS, int, msgflags), \ + (int)msgflags +/*! + * Timestamp in milliseconds since epoch UTC (int64_t). + * A value of 0 will use the current wall-clock time. + * + * rd_kafka_vu_t field: u.i64 + */ +#define RD_KAFKA_V_TIMESTAMP(timestamp) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_TIMESTAMP, int64_t, timestamp), \ + (int64_t)timestamp +/*! + * Add Message Header (const char *NAME, const void *VALUE, ssize_t LEN). + * @sa rd_kafka_header_add() + * @remark RD_KAFKA_V_HEADER() and RD_KAFKA_V_HEADERS() MUST NOT be mixed + * in the same call to producev(). + * + * rd_kafka_vu_t fields: u.header.name, u.header.val, u.header.size + */ +#define RD_KAFKA_V_HEADER(NAME,VALUE,LEN) \ + _LRK_TYPECHECK3(RD_KAFKA_VTYPE_HEADER, const char *, NAME, \ + const void *, VALUE, ssize_t, LEN), \ + (const char *)NAME, (const void *)VALUE, (ssize_t)LEN + +/*! + * Message Headers list (rd_kafka_headers_t *). + * The message object will assume ownership of the headers (unless producev() + * fails). + * Any existing headers will be replaced. + * @sa rd_kafka_message_set_headers() + * @remark RD_KAFKA_V_HEADER() and RD_KAFKA_V_HEADERS() MUST NOT be mixed + * in the same call to producev(). + * + * rd_kafka_vu_t fields: u.headers + */ +#define RD_KAFKA_V_HEADERS(HDRS) \ + _LRK_TYPECHECK(RD_KAFKA_VTYPE_HEADERS, rd_kafka_headers_t *, HDRS), \ + (rd_kafka_headers_t *)HDRS + + +/**@}*/ + + +/** + * @name Message headers + * @{ + * + * @brief Message headers consist of a list of (string key, binary value) pairs. + * Duplicate keys are supported and the order in which keys were + * added are retained. + * + * Header values are considered binary and may have three types of + * value: + * - proper value with size > 0 and a valid pointer + * - empty value with size = 0 and any non-NULL pointer + * - null value with size = 0 and a NULL pointer + * + * Headers require Apache Kafka broker version v0.11.0.0 or later. + * + * Header operations are O(n). + */ + + +/** + * @brief Create a new headers list. + * + * @param initial_count Preallocate space for this number of headers. + * Any number of headers may be added, updated and + * removed regardless of the initial count. + */ +RD_EXPORT rd_kafka_headers_t *rd_kafka_headers_new (size_t initial_count); + +/** + * @brief Destroy the headers list. The object and any returned value pointers + * are not usable after this call. + */ +RD_EXPORT void rd_kafka_headers_destroy (rd_kafka_headers_t *hdrs); + +/** + * @brief Make a copy of headers list \p src. + */ +RD_EXPORT rd_kafka_headers_t * +rd_kafka_headers_copy (const rd_kafka_headers_t *src); + +/** + * @brief Add header with name \p name and value \p val (copied) of size + * \p size (not including null-terminator). + * + * @param hdrs Headers list. + * @param name Header name. + * @param name_size Header name size (not including the null-terminator). + * If -1 the \p name length is automatically acquired using + * strlen(). + * @param value Pointer to header value, or NULL (set size to 0 or -1). + * @param value_size Size of header value. If -1 the \p value is assumed to be a + * null-terminated string and the length is automatically + * acquired using strlen(). + * + * @returns RD_KAFKA_RESP_ERR__READ_ONLY if the headers are read-only, + * else RD_KAFKA_RESP_ERR_NO_ERROR. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_header_add (rd_kafka_headers_t *hdrs, + const char *name, ssize_t name_size, + const void *value, ssize_t value_size); + +/** + * @brief Remove all headers for the given key (if any). + * + * @returns RD_KAFKA_RESP_ERR__READ_ONLY if the headers are read-only, + * RD_KAFKA_RESP_ERR__NOENT if no matching headers were found, + * else RD_KAFKA_RESP_ERR_NO_ERROR if headers were removed. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_header_remove (rd_kafka_headers_t *hdrs, const char *name); + + +/** + * @brief Find last header in list \p hdrs matching \p name. + * + * @param hdrs Headers list. + * @param name Header to find (last match). + * @param valuep (out) Set to a (null-terminated) const pointer to the value + * (may be NULL). + * @param sizep (out) Set to the value's size (not including null-terminator). + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if an entry was found, else + * RD_KAFKA_RESP_ERR__NOENT. + * + * @remark The returned pointer in \p valuep includes a trailing null-terminator + * that is not accounted for in \p sizep. + * @remark The returned pointer is only valid as long as the headers list and + * the header item is valid. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_header_get_last (const rd_kafka_headers_t *hdrs, + const char *name, const void **valuep, size_t *sizep); + +/** + * @brief Iterator for headers matching \p name. + * + * Same semantics as rd_kafka_header_get_last() + * + * @param hdrs Headers to iterate. + * @param idx Iterator index, start at 0 and increment by one for each call + * as long as RD_KAFKA_RESP_ERR_NO_ERROR is returned. + * @param name Header name to match. + * @param valuep (out) Set to a (null-terminated) const pointer to the value + * (may be NULL). + * @param sizep (out) Set to the value's size (not including null-terminator). + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_header_get (const rd_kafka_headers_t *hdrs, size_t idx, + const char *name, const void **valuep, size_t *sizep); + + +/** + * @brief Iterator for all headers. + * + * Same semantics as rd_kafka_header_get() + * + * @sa rd_kafka_header_get() + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_header_get_all (const rd_kafka_headers_t *hdrs, size_t idx, + const char **namep, + const void **valuep, size_t *sizep); + + + +/**@}*/ + + + +/** + * @name Kafka messages + * @{ + * + */ + + + +// FIXME: This doesn't show up in docs for some reason +// "Compound rd_kafka_message_t is not documented." + +/** + * @brief A Kafka message as returned by the \c rd_kafka_consume*() family + * of functions as well as provided to the Producer \c dr_msg_cb(). + * + * For the consumer this object has two purposes: + * - provide the application with a consumed message. (\c err == 0) + * - report per-topic+partition consumer errors (\c err != 0) + * + * The application must check \c err to decide what action to take. + * + * When the application is finished with a message it must call + * rd_kafka_message_destroy() unless otherwise noted. + */ +typedef struct rd_kafka_message_s { + rd_kafka_resp_err_t err; /**< Non-zero for error signaling. */ + rd_kafka_topic_t *rkt; /**< Topic */ + int32_t partition; /**< Partition */ + void *payload; /**< Producer: original message payload. + * Consumer: Depends on the value of \c err : + * - \c err==0: Message payload. + * - \c err!=0: Error string */ + size_t len; /**< Depends on the value of \c err : + * - \c err==0: Message payload length + * - \c err!=0: Error string length */ + void *key; /**< Depends on the value of \c err : + * - \c err==0: Optional message key */ + size_t key_len; /**< Depends on the value of \c err : + * - \c err==0: Optional message key length*/ + int64_t offset; /**< Consumer: + * - Message offset (or offset for error + * if \c err!=0 if applicable). + * Producer, dr_msg_cb: + * Message offset assigned by broker. + * May be RD_KAFKA_OFFSET_INVALID + * for retried messages when + * idempotence is enabled. */ + void *_private; /**< Consumer: + * - rdkafka private pointer: DO NOT MODIFY + * Producer: + * - dr_msg_cb: + * msg_opaque from produce() call or + * RD_KAFKA_V_OPAQUE from producev(). */ +} rd_kafka_message_t; + + +/** + * @brief Frees resources for \p rkmessage and hands ownership back to rdkafka. + */ +RD_EXPORT +void rd_kafka_message_destroy(rd_kafka_message_t *rkmessage); + + + + +/** + * @brief Returns the error string for an errored rd_kafka_message_t or NULL if + * there was no error. + * + * @remark This function MUST NOT be used with the producer. + */ +RD_EXPORT +const char *rd_kafka_message_errstr (const rd_kafka_message_t *rkmessage); + + +/** + * @brief Returns the message timestamp for a consumed message. + * + * The timestamp is the number of milliseconds since the epoch (UTC). + * + * \p tstype (if not NULL) is updated to indicate the type of timestamp. + * + * @returns message timestamp, or -1 if not available. + * + * @remark Message timestamps require broker version 0.10.0 or later. + */ +RD_EXPORT +int64_t rd_kafka_message_timestamp (const rd_kafka_message_t *rkmessage, + rd_kafka_timestamp_type_t *tstype); + + + +/** + * @brief Returns the latency for a produced message measured from + * the produce() call. + * + * @returns the latency in microseconds, or -1 if not available. + */ +RD_EXPORT +int64_t rd_kafka_message_latency (const rd_kafka_message_t *rkmessage); + + +/** + * @brief Returns the broker id of the broker the message was produced to + * or fetched from. + * + * @returns a broker id if known, else -1. + */ +RD_EXPORT +int32_t rd_kafka_message_broker_id (const rd_kafka_message_t *rkmessage); + + +/** + * @brief Get the message header list. + * + * The returned pointer in \p *hdrsp is associated with the \p rkmessage and + * must not be used after destruction of the message object or the header + * list is replaced with rd_kafka_message_set_headers(). + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if headers were returned, + * RD_KAFKA_RESP_ERR__NOENT if the message has no headers, + * or another error code if the headers could not be parsed. + * + * @remark Headers require broker version 0.11.0.0 or later. + * + * @remark As an optimization the raw protocol headers are parsed on + * the first call to this function. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_message_headers (const rd_kafka_message_t *rkmessage, + rd_kafka_headers_t **hdrsp); + +/** + * @brief Get the message header list and detach the list from the message + * making the application the owner of the headers. + * The application must eventually destroy the headers using + * rd_kafka_headers_destroy(). + * The message's headers will be set to NULL. + * + * Otherwise same semantics as rd_kafka_message_headers() + * + * @sa rd_kafka_message_headers + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_message_detach_headers (rd_kafka_message_t *rkmessage, + rd_kafka_headers_t **hdrsp); + + +/** + * @brief Replace the message's current headers with a new list. + * + * @param rkmessage The message to set headers. + * @param hdrs New header list. The message object assumes ownership of + * the list, the list will be destroyed automatically with + * the message object. + * The new headers list may be updated until the message object + * is passed or returned to librdkafka. + * + * @remark The existing headers object, if any, will be destroyed. + */ +RD_EXPORT +void rd_kafka_message_set_headers (rd_kafka_message_t *rkmessage, + rd_kafka_headers_t *hdrs); + + +/** + * @brief Returns the number of header key/value pairs + * + * @param hdrs Headers to count + */ +RD_EXPORT size_t rd_kafka_header_cnt (const rd_kafka_headers_t *hdrs); + + +/** + * @enum rd_kafka_msg_status_t + * @brief Message persistence status can be used by the application to + * find out if a produced message was persisted in the topic log. + */ +typedef enum { + /** Message was never transmitted to the broker, or failed with + * an error indicating it was not written to the log. + * Application retry risks ordering, but not duplication. */ + RD_KAFKA_MSG_STATUS_NOT_PERSISTED = 0, + + /** Message was transmitted to broker, but no acknowledgement was + * received. + * Application retry risks ordering and duplication. */ + RD_KAFKA_MSG_STATUS_POSSIBLY_PERSISTED = 1, + + /** Message was written to the log and acknowledged by the broker. + * No reason for application to retry. + * Note: this value should only be trusted with \c acks=all. */ + RD_KAFKA_MSG_STATUS_PERSISTED = 2 +} rd_kafka_msg_status_t; + + +/** + * @brief Returns the message's persistence status in the topic log. + * + * @remark The message status is not available in on_acknowledgement + * interceptors. + */ +RD_EXPORT rd_kafka_msg_status_t +rd_kafka_message_status (const rd_kafka_message_t *rkmessage); + +/**@}*/ + + +/** + * @name Configuration interface + * @{ + * + * @brief Main/global configuration property interface + * + */ + +/** + * @enum rd_kafka_conf_res_t + * @brief Configuration result type + */ +typedef enum { + RD_KAFKA_CONF_UNKNOWN = -2, /**< Unknown configuration name. */ + RD_KAFKA_CONF_INVALID = -1, /**< Invalid configuration value or + * property or value not supported in + * this build. */ + RD_KAFKA_CONF_OK = 0 /**< Configuration okay */ +} rd_kafka_conf_res_t; + + +/** + * @brief Create configuration object. + * + * When providing your own configuration to the \c rd_kafka_*_new_*() calls + * the rd_kafka_conf_t objects needs to be created with this function + * which will set up the defaults. + * I.e.: + * @code + * rd_kafka_conf_t *myconf; + * rd_kafka_conf_res_t res; + * + * myconf = rd_kafka_conf_new(); + * res = rd_kafka_conf_set(myconf, "socket.timeout.ms", "600", + * errstr, sizeof(errstr)); + * if (res != RD_KAFKA_CONF_OK) + * die("%s\n", errstr); + * + * rk = rd_kafka_new(..., myconf); + * @endcode + * + * Please see CONFIGURATION.md for the default settings or use + * rd_kafka_conf_properties_show() to provide the information at runtime. + * + * The properties are identical to the Apache Kafka configuration properties + * whenever possible. + * + * @remark A successful call to rd_kafka_new() will assume ownership of + * the conf object and rd_kafka_conf_destroy() must not be called. + * + * @returns A new rd_kafka_conf_t object with defaults set. + * + * @sa rd_kafka_new(), rd_kafka_conf_set(), rd_kafka_conf_destroy() + */ +RD_EXPORT +rd_kafka_conf_t *rd_kafka_conf_new(void); + + +/** + * @brief Destroys a conf object. + */ +RD_EXPORT +void rd_kafka_conf_destroy(rd_kafka_conf_t *conf); + + +/** + * @brief Creates a copy/duplicate of configuration object \p conf + * + * @remark Interceptors are NOT copied to the new configuration object. + * @sa rd_kafka_interceptor_f_on_conf_dup + */ +RD_EXPORT +rd_kafka_conf_t *rd_kafka_conf_dup(const rd_kafka_conf_t *conf); + + +/** + * @brief Same as rd_kafka_conf_dup() but with an array of property name + * prefixes to filter out (ignore) when copying. + */ +RD_EXPORT +rd_kafka_conf_t *rd_kafka_conf_dup_filter (const rd_kafka_conf_t *conf, + size_t filter_cnt, + const char **filter); + + + +/** + * @returns the configuration object used by an rd_kafka_t instance. + * For use with rd_kafka_conf_get(), et.al., to extract configuration + * properties from a running client. + * + * @remark the returned object is read-only and its lifetime is the same + * as the rd_kafka_t object. + */ +RD_EXPORT +const rd_kafka_conf_t *rd_kafka_conf (rd_kafka_t *rk); + + +/** + * @brief Sets a configuration property. + * + * \p conf must have been previously created with rd_kafka_conf_new(). + * + * Fallthrough: + * Topic-level configuration properties may be set using this interface + * in which case they are applied on the \c default_topic_conf. + * If no \c default_topic_conf has been set one will be created. + * Any sub-sequent rd_kafka_conf_set_default_topic_conf() calls will + * replace the current default topic configuration. + * + * @returns \c rd_kafka_conf_res_t to indicate success or failure. + * In case of failure \p errstr is updated to contain a human readable + * error string. + * + * @remark Setting properties or values that were disabled at build time due to + * missing dependencies will return RD_KAFKA_CONF_INVALID. + */ +RD_EXPORT +rd_kafka_conf_res_t rd_kafka_conf_set(rd_kafka_conf_t *conf, + const char *name, + const char *value, + char *errstr, size_t errstr_size); + + +/** + * @brief Enable event sourcing. + * \p events is a bitmask of \c RD_KAFKA_EVENT_* of events to enable + * for consumption by `rd_kafka_queue_poll()`. + */ +RD_EXPORT +void rd_kafka_conf_set_events(rd_kafka_conf_t *conf, int events); + + +/** + * @brief Generic event callback to be used with the event API to trigger + * callbacks for \c rd_kafka_event_t objects from a background + * thread serving the background queue. + * + * How to use: + * 1. First set the event callback on the configuration object with this + * function, followed by creating an rd_kafka_t instance + * with rd_kafka_new(). + * 2. Get the instance's background queue with rd_kafka_queue_get_background() + * and pass it as the reply/response queue to an API that takes an + * event queue, such as rd_kafka_CreateTopics(). + * 3. As the response event is ready and enqueued on the background queue the + * event callback will be triggered from the background thread. + * 4. Prior to destroying the client instance, loose your reference to the + * background queue by calling rd_kafka_queue_destroy(). + * + * The application must destroy the \c rkev passed to \p event cb using + * rd_kafka_event_destroy(). + * + * The \p event_cb \c opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark This callback is a specialized alternative to the poll-based + * event API described in the Event interface section. + * + * @remark The \p event_cb will be called spontaneously from a background + * thread completely managed by librdkafka. + * Take care to perform proper locking of application objects. + * + * @warning The application MUST NOT call rd_kafka_destroy() from the + * event callback. + * + * @sa rd_kafka_queue_get_background + */ +RD_EXPORT void +rd_kafka_conf_set_background_event_cb (rd_kafka_conf_t *conf, + void (*event_cb) (rd_kafka_t *rk, + rd_kafka_event_t *rkev, + void *opaque)); + + +/** + * @deprecated See rd_kafka_conf_set_dr_msg_cb() + */ +RD_EXPORT +void rd_kafka_conf_set_dr_cb(rd_kafka_conf_t *conf, + void (*dr_cb) (rd_kafka_t *rk, + void *payload, size_t len, + rd_kafka_resp_err_t err, + void *opaque, void *msg_opaque)); + +/** + * @brief \b Producer: Set delivery report callback in provided \p conf object. + * + * The delivery report callback will be called once for each message + * accepted by rd_kafka_produce() (et.al) with \p err set to indicate + * the result of the produce request. + * + * The callback is called when a message is succesfully produced or + * if librdkafka encountered a permanent failure. + * Delivery errors occur when the retry count is exceeded, when the + * message.timeout.ms timeout is exceeded or there is a permanent error + * like RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART. + * + * An application must call rd_kafka_poll() at regular intervals to + * serve queued delivery report callbacks. + * + * The broker-assigned offset can be retrieved with \c rkmessage->offset + * and the timestamp can be retrieved using rd_kafka_message_timestamp(). + * + * The \p dr_msg_cb \c opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * The per-message msg_opaque value is available in + * \c rd_kafka_message_t._private. + * + * @remark The Idempotent Producer may return invalid timestamp + * (RD_KAFKA_TIMESTAMP_NOT_AVAILABLE), and + * and offset (RD_KAFKA_OFFSET_INVALID) for retried messages + * that were previously successfully delivered but not properly + * acknowledged. + */ +RD_EXPORT +void rd_kafka_conf_set_dr_msg_cb(rd_kafka_conf_t *conf, + void (*dr_msg_cb) (rd_kafka_t *rk, + const rd_kafka_message_t * + rkmessage, + void *opaque)); + + +/** + * @brief \b Consumer: Set consume callback for use with + * rd_kafka_consumer_poll() + * + * The \p consume_cb \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + */ +RD_EXPORT +void rd_kafka_conf_set_consume_cb (rd_kafka_conf_t *conf, + void (*consume_cb) (rd_kafka_message_t * + rkmessage, + void *opaque)); + +/** + * @brief \b Consumer: Set rebalance callback for use with + * coordinated consumer group balancing. + * + * The \p err field is set to either RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS + * or RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS and 'partitions' + * contains the full partition set that was either assigned or revoked. + * + * Registering a \p rebalance_cb turns off librdkafka's automatic + * partition assignment/revocation and instead delegates that responsibility + * to the application's \p rebalance_cb. + * + * The rebalance callback is responsible for updating librdkafka's + * assignment set based on the two events: RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS + * and RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS but should also be able to handle + * arbitrary rebalancing failures where \p err is neither of those. + * @remark In this latter case (arbitrary error), the application must + * call rd_kafka_assign(rk, NULL) to synchronize state. + * + * For eager/non-cooperative `partition.assignment.strategy` assignors, + * such as `range` and `roundrobin`, the application must use + * rd_kafka_assign() to set or clear the entire assignment. + * For the cooperative assignors, such as `cooperative-sticky`, the application + * must use rd_kafka_incremental_assign() for + * RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS and rd_kafka_incremental_unassign() + * for RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS. + * + * Without a rebalance callback this is done automatically by librdkafka + * but registering a rebalance callback gives the application flexibility + * in performing other operations along with the assigning/revocation, + * such as fetching offsets from an alternate location (on assign) + * or manually committing offsets (on revoke). + * + * rebalance_cb is always triggered exactly once when a rebalance completes + * with a new assignment, even if that assignment is empty. If an + * eager/non-cooperative assignor is configured, there will eventually be + * exactly one corresponding call to rebalance_cb to revoke these partitions + * (even if empty), whether this is due to a group rebalance or lost + * partitions. In the cooperative case, rebalance_cb will never be called if + * the set of partitions being revoked is empty (whether or not lost). + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark The \p partitions list is destroyed by librdkafka on return + * return from the rebalance_cb and must not be freed or + * saved by the application. + * + * @remark Be careful when modifying the \p partitions list. + * Changing this list should only be done to change the initial + * offsets for each partition. + * But a function like `rd_kafka_position()` might have unexpected + * effects for instance when a consumer gets assigned a partition + * it used to consume at an earlier rebalance. In this case, the + * list of partitions will be updated with the old offset for that + * partition. In this case, it is generally better to pass a copy + * of the list (see `rd_kafka_topic_partition_list_copy()`). + * The result of `rd_kafka_position()` is typically outdated in + * RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS. + * + * @sa rd_kafka_assign() + * @sa rd_kafka_incremental_assign() + * @sa rd_kafka_incremental_unassign() + * @sa rd_kafka_assignment_lost() + * @sa rd_kafka_rebalance_protocol() + * + * The following example shows the application's responsibilities: + * @code + * static void rebalance_cb (rd_kafka_t *rk, rd_kafka_resp_err_t err, + * rd_kafka_topic_partition_list_t *partitions, + * void *opaque) { + * + * switch (err) + * { + * case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS: + * // application may load offets from arbitrary external + * // storage here and update \p partitions + * if (!strcmp(rd_kafka_rebalance_protocol(rk), "COOPERATIVE")) + * rd_kafka_incremental_assign(rk, partitions); + * else // EAGER + * rd_kafka_assign(rk, partitions); + * break; + * + * case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS: + * if (manual_commits) // Optional explicit manual commit + * rd_kafka_commit(rk, partitions, 0); // sync commit + * + * if (!strcmp(rd_kafka_rebalance_protocol(rk), "COOPERATIVE")) + * rd_kafka_incremental_unassign(rk, partitions); + * else // EAGER + * rd_kafka_assign(rk, NULL); + * break; + * + * default: + * handle_unlikely_error(err); + * rd_kafka_assign(rk, NULL); // sync state + * break; + * } + * } + * @endcode + * + * @remark The above example lacks error handling for assign calls, see + * the examples/ directory. + */ +RD_EXPORT +void rd_kafka_conf_set_rebalance_cb ( + rd_kafka_conf_t *conf, + void (*rebalance_cb) (rd_kafka_t *rk, + rd_kafka_resp_err_t err, + rd_kafka_topic_partition_list_t *partitions, + void *opaque)); + + + +/** + * @brief \b Consumer: Set offset commit callback for use with consumer groups. + * + * The results of automatic or manual offset commits will be scheduled + * for this callback and is served by rd_kafka_consumer_poll(). + * + * If no partitions had valid offsets to commit this callback will be called + * with \p err == RD_KAFKA_RESP_ERR__NO_OFFSET which is not to be considered + * an error. + * + * The \p offsets list contains per-partition information: + * - \c offset: committed offset (attempted) + * - \c err: commit error + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + */ +RD_EXPORT +void rd_kafka_conf_set_offset_commit_cb ( + rd_kafka_conf_t *conf, + void (*offset_commit_cb) (rd_kafka_t *rk, + rd_kafka_resp_err_t err, + rd_kafka_topic_partition_list_t *offsets, + void *opaque)); + + +/** + * @brief Set error callback in provided conf object. + * + * The error callback is used by librdkafka to signal warnings and errors + * back to the application. + * + * These errors should generally be considered informational and non-permanent, + * the client will try to recover automatically from all type of errors. + * Given that the client and cluster configuration is correct the + * application should treat these as temporary errors. + * + * \p error_cb will be triggered with \c err set to RD_KAFKA_RESP_ERR__FATAL + * if a fatal error has been raised; in this case use rd_kafka_fatal_error() to + * retrieve the fatal error code and error string, and then begin terminating + * the client instance. + * + * If no \p error_cb is registered, or RD_KAFKA_EVENT_ERROR has not been set + * with rd_kafka_conf_set_events, then the errors will be logged instead. + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + */ +RD_EXPORT +void rd_kafka_conf_set_error_cb(rd_kafka_conf_t *conf, + void (*error_cb) (rd_kafka_t *rk, int err, + const char *reason, + void *opaque)); + +/** + * @brief Set throttle callback. + * + * The throttle callback is used to forward broker throttle times to the + * application for Produce and Fetch (consume) requests. + * + * Callbacks are triggered whenever a non-zero throttle time is returned by + * the broker, or when the throttle time drops back to zero. + * + * An application must call rd_kafka_poll() or rd_kafka_consumer_poll() at + * regular intervals to serve queued callbacks. + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark Requires broker version 0.9.0 or later. + */ +RD_EXPORT +void rd_kafka_conf_set_throttle_cb (rd_kafka_conf_t *conf, + void (*throttle_cb) ( + rd_kafka_t *rk, + const char *broker_name, + int32_t broker_id, + int throttle_time_ms, + void *opaque)); + + +/** + * @brief Set logger callback. + * + * The default is to print to stderr, but a syslog logger is also available, + * see rd_kafka_log_print and rd_kafka_log_syslog for the builtin alternatives. + * Alternatively the application may provide its own logger callback. + * Or pass \p func as NULL to disable logging. + * + * This is the configuration alternative to the deprecated rd_kafka_set_logger() + * + * @remark The log_cb will be called spontaneously from librdkafka's internal + * threads unless logs have been forwarded to a poll queue through + * \c rd_kafka_set_log_queue(). + * An application MUST NOT call any librdkafka APIs or do any prolonged + * work in a non-forwarded \c log_cb. + */ +RD_EXPORT +void rd_kafka_conf_set_log_cb(rd_kafka_conf_t *conf, + void (*log_cb) (const rd_kafka_t *rk, int level, + const char *fac, const char *buf)); + + +/** + * @brief Set statistics callback in provided conf object. + * + * The statistics callback is triggered from rd_kafka_poll() every + * \c statistics.interval.ms (needs to be configured separately). + * Function arguments: + * - \p rk - Kafka handle + * - \p json - String containing the statistics data in JSON format + * - \p json_len - Length of \p json string. + * - \p opaque - Application-provided opaque as set by + * rd_kafka_conf_set_opaque(). + * + * For more information on the format of \p json, see + * https://github.com/edenhill/librdkafka/wiki/Statistics + * + * If the application wishes to hold on to the \p json pointer and free + * it at a later time it must return 1 from the \p stats_cb. + * If the application returns 0 from the \p stats_cb then librdkafka + * will immediately free the \p json pointer. + * + * See STATISTICS.md for a full definition of the JSON object. + */ +RD_EXPORT +void rd_kafka_conf_set_stats_cb(rd_kafka_conf_t *conf, + int (*stats_cb) (rd_kafka_t *rk, + char *json, + size_t json_len, + void *opaque)); + +/** + * @brief Set SASL/OAUTHBEARER token refresh callback in provided conf object. + * + * @param conf the configuration to mutate. + * @param oauthbearer_token_refresh_cb the callback to set; callback function + * arguments:
+ * \p rk - Kafka handle
+ * \p oauthbearer_config - Value of configuration property + * sasl.oauthbearer.config. + * \p opaque - Application-provided opaque set via + * rd_kafka_conf_set_opaque() + * + * The SASL/OAUTHBEARER token refresh callback is triggered via rd_kafka_poll() + * whenever OAUTHBEARER is the SASL mechanism and a token needs to be retrieved, + * typically based on the configuration defined in \c sasl.oauthbearer.config. + * + * The callback should invoke rd_kafka_oauthbearer_set_token() + * or rd_kafka_oauthbearer_set_token_failure() to indicate success + * or failure, respectively. + * + * The refresh operation is eventable and may be received via + * rd_kafka_queue_poll() with an event type of + * \c RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH. + * + * Note that before any SASL/OAUTHBEARER broker connection can succeed the + * application must call rd_kafka_oauthbearer_set_token() once -- either + * directly or, more typically, by invoking either rd_kafka_poll() or + * rd_kafka_queue_poll() -- in order to cause retrieval of an initial token to + * occur. + * + * An unsecured JWT refresh handler is provided by librdkafka for development + * and testing purposes, it is enabled by setting + * the \c enable.sasl.oauthbearer.unsecure.jwt property to true and is + * mutually exclusive to using a refresh callback. + */ +RD_EXPORT +void rd_kafka_conf_set_oauthbearer_token_refresh_cb ( + rd_kafka_conf_t *conf, + void (*oauthbearer_token_refresh_cb) (rd_kafka_t *rk, + const char *oauthbearer_config, + void *opaque)); + +/** + * @brief Set socket callback. + * + * The socket callback is responsible for opening a socket + * according to the supplied \p domain, \p type and \p protocol. + * The socket shall be created with \c CLOEXEC set in a racefree fashion, if + * possible. + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * Default: + * - on linux: racefree CLOEXEC + * - others : non-racefree CLOEXEC + * + * @remark The callback will be called from an internal librdkafka thread. + */ +RD_EXPORT +void rd_kafka_conf_set_socket_cb(rd_kafka_conf_t *conf, + int (*socket_cb) (int domain, int type, + int protocol, + void *opaque)); + + + +/** + * @brief Set connect callback. + * + * The connect callback is responsible for connecting socket \p sockfd + * to peer address \p addr. + * The \p id field contains the broker identifier. + * + * \p connect_cb shall return 0 on success (socket connected) or an error + * number (errno) on error. + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark The callback will be called from an internal librdkafka thread. + */ +RD_EXPORT void +rd_kafka_conf_set_connect_cb (rd_kafka_conf_t *conf, + int (*connect_cb) (int sockfd, + const struct sockaddr *addr, + int addrlen, + const char *id, + void *opaque)); + +/** + * @brief Set close socket callback. + * + * Close a socket (optionally opened with socket_cb()). + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark The callback will be called from an internal librdkafka thread. + */ +RD_EXPORT void +rd_kafka_conf_set_closesocket_cb (rd_kafka_conf_t *conf, + int (*closesocket_cb) (int sockfd, + void *opaque)); + + + +#ifndef _WIN32 +/** + * @brief Set open callback. + * + * The open callback is responsible for opening the file specified by + * pathname, flags and mode. + * The file shall be opened with \c CLOEXEC set in a racefree fashion, if + * possible. + * + * Default: + * - on linux: racefree CLOEXEC + * - others : non-racefree CLOEXEC + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @remark The callback will be called from an internal librdkafka thread. + */ +RD_EXPORT +void rd_kafka_conf_set_open_cb (rd_kafka_conf_t *conf, + int (*open_cb) (const char *pathname, + int flags, mode_t mode, + void *opaque)); +#endif + + +/** + * @brief Sets the verification callback of the broker certificate + * + * The verification callback is triggered from internal librdkafka threads + * upon connecting to a broker. On each connection attempt the callback + * will be called for each certificate in the broker's certificate chain, + * starting at the root certification, as long as the application callback + * returns 1 (valid certificate). + * \c broker_name and \c broker_id correspond to the broker the connection + * is being made to. + * The \c x509_error argument indicates if OpenSSL's verification of + * the certificate succeed (0) or failed (an OpenSSL error code). + * The application may set the SSL context error code by returning 0 + * from the verify callback and providing a non-zero SSL context error code + * in \c x509_error. + * If the verify callback sets \c x509_error to 0, returns 1, and the + * original \c x509_error was non-zero, the error on the SSL context will + * be cleared. + * \c x509_error is always a valid pointer to an int. + * + * \c depth is the depth of the current certificate in the chain, starting + * at the root certificate. + * + * The certificate itself is passed in binary DER format in \c buf of + * size \c size. + * + * The callback must return 1 if verification succeeds, or + * 0 if verification fails and then write a human-readable error message + * to \c errstr (limited to \c errstr_size bytes, including nul-term). + * + * The callback's \p opaque argument is the opaque set with + * rd_kafka_conf_set_opaque(). + * + * @returns RD_KAFKA_CONF_OK if SSL is supported in this build, else + * RD_KAFKA_CONF_INVALID. + * + * @warning This callback will be called from internal librdkafka threads. + * + * @remark See in the OpenSSL source distribution + * for a list of \p x509_error codes. + */ +RD_EXPORT +rd_kafka_conf_res_t rd_kafka_conf_set_ssl_cert_verify_cb ( + rd_kafka_conf_t *conf, + int (*ssl_cert_verify_cb) (rd_kafka_t *rk, + const char *broker_name, + int32_t broker_id, + int *x509_error, + int depth, + const char *buf, size_t size, + char *errstr, size_t errstr_size, + void *opaque)); + + +/** + * @enum rd_kafka_cert_type_t + * + * @brief SSL certificate type + * + * @sa rd_kafka_conf_set_ssl_cert + */ +typedef enum rd_kafka_cert_type_t { + RD_KAFKA_CERT_PUBLIC_KEY, /**< Client's public key */ + RD_KAFKA_CERT_PRIVATE_KEY, /**< Client's private key */ + RD_KAFKA_CERT_CA, /**< CA certificate */ + RD_KAFKA_CERT__CNT, +} rd_kafka_cert_type_t; + +/** + * @enum rd_kafka_cert_enc_t + * + * @brief SSL certificate encoding + * + * @sa rd_kafka_conf_set_ssl_cert + */ +typedef enum rd_kafka_cert_enc_t { + RD_KAFKA_CERT_ENC_PKCS12, /**< PKCS#12 */ + RD_KAFKA_CERT_ENC_DER, /**< DER / binary X.509 ASN1 */ + RD_KAFKA_CERT_ENC_PEM, /**< PEM */ + RD_KAFKA_CERT_ENC__CNT, +} rd_kafka_cert_enc_t; + + +/** + * @brief Set certificate/key \p cert_type from the \p cert_enc encoded + * memory at \p buffer of \p size bytes. + * + * @param conf Configuration object. + * @param cert_type Certificate or key type to configure. + * @param cert_enc Buffer \p encoding type. + * @param buffer Memory pointer to encoded certificate or key. + * The memory is not referenced after this function returns. + * @param size Size of memory at \p buffer. + * @param errstr Memory were a human-readable error string will be written + * on failure. + * @param errstr_size Size of \p errstr, including space for nul-terminator. + * + * @returns RD_KAFKA_CONF_OK on success or RD_KAFKA_CONF_INVALID if the + * memory in \p buffer is of incorrect encoding, or if librdkafka + * was not built with SSL support. + * + * @remark Calling this method multiple times with the same \p cert_type + * will replace the previous value. + * + * @remark Calling this method with \p buffer set to NULL will clear the + * configuration for \p cert_type. + * + * @remark The private key may require a password, which must be specified + * with the `ssl.key.password` configuration property prior to + * calling this function. + * + * @remark Private and public keys in PEM format may also be set with the + * `ssl.key.pem` and `ssl.certificate.pem` configuration properties. + * + * @remark CA certificate in PEM format may also be set with the + * `ssl.ca.pem` configuration property. + */ +RD_EXPORT rd_kafka_conf_res_t +rd_kafka_conf_set_ssl_cert (rd_kafka_conf_t *conf, + rd_kafka_cert_type_t cert_type, + rd_kafka_cert_enc_t cert_enc, + const void *buffer, size_t size, + char *errstr, size_t errstr_size); + + +/** + * @brief Set callback_data for OpenSSL engine. + * + * @param conf Configuration object. + * @param callback_data passed to engine callbacks, + * e.g. \c ENGINE_load_ssl_client_cert. + * + * @remark The \c ssl.engine.location configuration must be set for this + * to have affect. + * + * @remark The memory pointed to by \p value must remain valid for the + * lifetime of the configuration object and any Kafka clients that + * use it. + */ +RD_EXPORT +void rd_kafka_conf_set_engine_callback_data (rd_kafka_conf_t *conf, + void *callback_data); + + +/** + * @brief Sets the application's opaque pointer that will be passed to callbacks + * + * @sa rd_kafka_opaque() + */ +RD_EXPORT +void rd_kafka_conf_set_opaque(rd_kafka_conf_t *conf, void *opaque); + +/** + * @brief Retrieves the opaque pointer previously set + * with rd_kafka_conf_set_opaque() + */ +RD_EXPORT +void *rd_kafka_opaque(const rd_kafka_t *rk); + + + +/** + * @brief Sets the default topic configuration to use for automatically + * subscribed topics (e.g., through pattern-matched topics). + * The topic config object is not usable after this call. + * + * @warning Any topic configuration settings that have been set on the + * global rd_kafka_conf_t object will be overwritten by this call + * since the implicitly created default topic config object is + * replaced by the user-supplied one. + * + * @deprecated Set default topic level configuration on the + * global rd_kafka_conf_t object instead. + */ +RD_EXPORT +void rd_kafka_conf_set_default_topic_conf (rd_kafka_conf_t *conf, + rd_kafka_topic_conf_t *tconf); + +/** + * @brief Gets the default topic configuration as previously set with + * rd_kafka_conf_set_default_topic_conf() or that was implicitly created + * by configuring a topic-level property on the global \p conf object. + * + * @returns the \p conf's default topic configuration (if any), or NULL. + * + * @warning The returned topic configuration object is owned by the \p conf + * object. It may be modified but not destroyed and its lifetime is + * the same as the \p conf object or the next call to + * rd_kafka_conf_set_default_topic_conf(). + */ +RD_EXPORT rd_kafka_topic_conf_t * +rd_kafka_conf_get_default_topic_conf (rd_kafka_conf_t *conf); + + +/** + * @brief Retrieve configuration value for property \p name. + * + * If \p dest is non-NULL the value will be written to \p dest with at + * most \p dest_size. + * + * \p *dest_size is updated to the full length of the value, thus if + * \p *dest_size initially is smaller than the full length the application + * may reallocate \p dest to fit the returned \p *dest_size and try again. + * + * If \p dest is NULL only the full length of the value is returned. + * + * Fallthrough: + * Topic-level configuration properties from the \c default_topic_conf + * may be retrieved using this interface. + * + * @returns \p RD_KAFKA_CONF_OK if the property name matched, else + * \p RD_KAFKA_CONF_UNKNOWN. + */ +RD_EXPORT +rd_kafka_conf_res_t rd_kafka_conf_get (const rd_kafka_conf_t *conf, + const char *name, + char *dest, size_t *dest_size); + + +/** + * @brief Retrieve topic configuration value for property \p name. + * + * @sa rd_kafka_conf_get() + */ +RD_EXPORT +rd_kafka_conf_res_t rd_kafka_topic_conf_get (const rd_kafka_topic_conf_t *conf, + const char *name, + char *dest, size_t *dest_size); + + +/** + * @brief Dump the configuration properties and values of \p conf to an array + * with \"key\", \"value\" pairs. + * + * The number of entries in the array is returned in \p *cntp. + * + * The dump must be freed with `rd_kafka_conf_dump_free()`. + */ +RD_EXPORT +const char **rd_kafka_conf_dump(rd_kafka_conf_t *conf, size_t *cntp); + + +/** + * @brief Dump the topic configuration properties and values of \p conf + * to an array with \"key\", \"value\" pairs. + * + * The number of entries in the array is returned in \p *cntp. + * + * The dump must be freed with `rd_kafka_conf_dump_free()`. + */ +RD_EXPORT +const char **rd_kafka_topic_conf_dump(rd_kafka_topic_conf_t *conf, + size_t *cntp); + +/** + * @brief Frees a configuration dump returned from `rd_kafka_conf_dump()` or + * `rd_kafka_topic_conf_dump(). + */ +RD_EXPORT +void rd_kafka_conf_dump_free(const char **arr, size_t cnt); + +/** + * @brief Prints a table to \p fp of all supported configuration properties, + * their default values as well as a description. + * + * @remark All properties and properties and values are shown, even those + * that have been disabled at build time due to missing dependencies. + */ +RD_EXPORT +void rd_kafka_conf_properties_show(FILE *fp); + +/**@}*/ + + +/** + * @name Topic configuration + * @{ + * + * @brief Topic configuration property interface + * + */ + + +/** + * @brief Create topic configuration object + * + * @sa Same semantics as for rd_kafka_conf_new(). + */ +RD_EXPORT +rd_kafka_topic_conf_t *rd_kafka_topic_conf_new(void); + + +/** + * @brief Creates a copy/duplicate of topic configuration object \p conf. + */ +RD_EXPORT +rd_kafka_topic_conf_t *rd_kafka_topic_conf_dup(const rd_kafka_topic_conf_t + *conf); + +/** + * @brief Creates a copy/duplicate of \p rk 's default topic configuration + * object. + */ +RD_EXPORT +rd_kafka_topic_conf_t *rd_kafka_default_topic_conf_dup (rd_kafka_t *rk); + + +/** + * @brief Destroys a topic conf object. + */ +RD_EXPORT +void rd_kafka_topic_conf_destroy(rd_kafka_topic_conf_t *topic_conf); + + +/** + * @brief Sets a single rd_kafka_topic_conf_t value by property name. + * + * \p topic_conf should have been previously set up + * with `rd_kafka_topic_conf_new()`. + * + * @returns rd_kafka_conf_res_t to indicate success or failure. + */ +RD_EXPORT +rd_kafka_conf_res_t rd_kafka_topic_conf_set(rd_kafka_topic_conf_t *conf, + const char *name, + const char *value, + char *errstr, size_t errstr_size); + +/** + * @brief Sets the application's opaque pointer that will be passed to all topic + * callbacks as the \c rkt_opaque argument. + * + * @sa rd_kafka_topic_opaque() + */ +RD_EXPORT +void rd_kafka_topic_conf_set_opaque(rd_kafka_topic_conf_t *conf, + void *rkt_opaque); + + +/** + * @brief \b Producer: Set partitioner callback in provided topic conf object. + * + * The partitioner may be called in any thread at any time, + * it may be called multiple times for the same message/key. + * + * The callback's \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The callback's \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * Partitioner function constraints: + * - MUST NOT call any rd_kafka_*() functions except: + * rd_kafka_topic_partition_available() + * - MUST NOT block or execute for prolonged periods of time. + * - MUST return a value between 0 and partition_cnt-1, or the + * special \c RD_KAFKA_PARTITION_UA value if partitioning + * could not be performed. + */ +RD_EXPORT +void +rd_kafka_topic_conf_set_partitioner_cb (rd_kafka_topic_conf_t *topic_conf, + int32_t (*partitioner) ( + const rd_kafka_topic_t *rkt, + const void *keydata, + size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, + void *msg_opaque)); + + +/** + * @brief \b Producer: Set message queueing order comparator callback. + * + * The callback may be called in any thread at any time, + * it may be called multiple times for the same message. + * + * Ordering comparator function constraints: + * - MUST be stable sort (same input gives same output). + * - MUST NOT call any rd_kafka_*() functions. + * - MUST NOT block or execute for prolonged periods of time. + * + * The comparator shall compare the two messages and return: + * - < 0 if message \p a should be inserted before message \p b. + * - >=0 if message \p a should be inserted after message \p b. + * + * @remark Insert sorting will be used to enqueue the message in the + * correct queue position, this comes at a cost of O(n). + * + * @remark If `queuing.strategy=fifo` new messages are enqueued to the + * tail of the queue regardless of msg_order_cmp, but retried messages + * are still affected by msg_order_cmp. + * + * @warning THIS IS AN EXPERIMENTAL API, SUBJECT TO CHANGE OR REMOVAL, + * DO NOT USE IN PRODUCTION. + */ +RD_EXPORT void +rd_kafka_topic_conf_set_msg_order_cmp (rd_kafka_topic_conf_t *topic_conf, + int (*msg_order_cmp) ( + const rd_kafka_message_t *a, + const rd_kafka_message_t *b)); + + +/** + * @brief Check if partition is available (has a leader broker). + * + * @returns 1 if the partition is available, else 0. + * + * @warning This function must only be called from inside a partitioner function + */ +RD_EXPORT +int rd_kafka_topic_partition_available(const rd_kafka_topic_t *rkt, + int32_t partition); + + +/******************************************************************* + * * + * Partitioners provided by rdkafka * + * * + *******************************************************************/ + +/** + * @brief Random partitioner. + * + * Will try not to return unavailable partitions. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a random partition between 0 and \p partition_cnt - 1. + * + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_random(const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, void *msg_opaque); + +/** + * @brief Consistent partitioner. + * + * Uses consistent hashing to map identical keys onto identical partitions. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a \"random\" partition between 0 and \p partition_cnt - 1 based on + * the CRC value of the key + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_consistent (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, void *msg_opaque); + +/** + * @brief Consistent-Random partitioner. + * + * This is the default partitioner. + * Uses consistent hashing to map identical keys onto identical partitions, and + * messages without keys will be assigned via the random partitioner. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a \"random\" partition between 0 and \p partition_cnt - 1 based on + * the CRC value of the key (if provided) + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_consistent_random (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, void *msg_opaque); + + +/** + * @brief Murmur2 partitioner (Java compatible). + * + * Uses consistent hashing to map identical keys onto identical partitions + * using Java-compatible Murmur2 hashing. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a partition between 0 and \p partition_cnt - 1. + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_murmur2 (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, + void *msg_opaque); + +/** + * @brief Consistent-Random Murmur2 partitioner (Java compatible). + * + * Uses consistent hashing to map identical keys onto identical partitions + * using Java-compatible Murmur2 hashing. + * Messages without keys will be assigned via the random partitioner. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a partition between 0 and \p partition_cnt - 1. + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_murmur2_random (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, + void *msg_opaque); + + +/** + * @brief FNV-1a partitioner. + * + * Uses consistent hashing to map identical keys onto identical partitions + * using FNV-1a hashing. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a partition between 0 and \p partition_cnt - 1. + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_fnv1a (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, + void *msg_opaque); + + +/** + * @brief Consistent-Random FNV-1a partitioner. + * + * Uses consistent hashing to map identical keys onto identical partitions + * using FNV-1a hashing. + * Messages without keys will be assigned via the random partitioner. + * + * The \p rkt_opaque argument is the opaque set by + * rd_kafka_topic_conf_set_opaque(). + * The \p msg_opaque argument is the per-message opaque + * passed to produce(). + * + * @returns a partition between 0 and \p partition_cnt - 1. + */ +RD_EXPORT +int32_t rd_kafka_msg_partitioner_fnv1a_random (const rd_kafka_topic_t *rkt, + const void *key, size_t keylen, + int32_t partition_cnt, + void *rkt_opaque, + void *msg_opaque); + + +/**@}*/ + + + +/** + * @name Main Kafka and Topic object handles + * @{ + * + * + */ + + + + +/** + * @brief Creates a new Kafka handle and starts its operation according to the + * specified \p type (\p RD_KAFKA_CONSUMER or \p RD_KAFKA_PRODUCER). + * + * \p conf is an optional struct created with `rd_kafka_conf_new()` that will + * be used instead of the default configuration. + * The \p conf object is freed by this function on success and must not be used + * or destroyed by the application sub-sequently. + * See `rd_kafka_conf_set()` et.al for more information. + * + * \p errstr must be a pointer to memory of at least size \p errstr_size where + * `rd_kafka_new()` may write a human readable error message in case the + * creation of a new handle fails. In which case the function returns NULL. + * + * @remark \b RD_KAFKA_CONSUMER: When a new \p RD_KAFKA_CONSUMER + * rd_kafka_t handle is created it may either operate in the + * legacy simple consumer mode using the rd_kafka_consume_start() + * interface, or the High-level KafkaConsumer API. + * @remark An application must only use one of these groups of APIs on a given + * rd_kafka_t RD_KAFKA_CONSUMER handle. + + * + * @returns The Kafka handle on success or NULL on error (see \p errstr) + * + * @sa To destroy the Kafka handle, use rd_kafka_destroy(). + */ +RD_EXPORT +rd_kafka_t *rd_kafka_new(rd_kafka_type_t type, rd_kafka_conf_t *conf, + char *errstr, size_t errstr_size); + + +/** + * @brief Destroy Kafka handle. + * + * @remark This is a blocking operation. + * @remark rd_kafka_consumer_close() will be called from this function + * if the instance type is RD_KAFKA_CONSUMER, a \c group.id was + * configured, and the rd_kafka_consumer_close() was not + * explicitly called by the application. This in turn may + * trigger consumer callbacks, such as rebalance_cb. + * Use rd_kafka_destroy_flags() with + * RD_KAFKA_DESTROY_F_NO_CONSUMER_CLOSE to avoid this behaviour. + * + * @sa rd_kafka_destroy_flags() + */ +RD_EXPORT +void rd_kafka_destroy(rd_kafka_t *rk); + + +/** + * @brief Destroy Kafka handle according to specified destroy flags + * + */ +RD_EXPORT +void rd_kafka_destroy_flags (rd_kafka_t *rk, int flags); + +/** + * @brief Flags for rd_kafka_destroy_flags() + */ + +/*! + * Don't call consumer_close() to leave group and commit final offsets. + * + * This also disables consumer callbacks to be called from rd_kafka_destroy*(), + * such as rebalance_cb. + * + * The consumer group handler is still closed internally, but from an + * application perspective none of the functionality from consumer_close() + * is performed. + */ +#define RD_KAFKA_DESTROY_F_NO_CONSUMER_CLOSE 0x8 + + + +/** + * @brief Returns Kafka handle name. + */ +RD_EXPORT +const char *rd_kafka_name(const rd_kafka_t *rk); + + +/** + * @brief Returns Kafka handle type. + */ +RD_EXPORT +rd_kafka_type_t rd_kafka_type(const rd_kafka_t *rk); + + +/** + * @brief Returns this client's broker-assigned group member id. + * + * @remark This currently requires the high-level KafkaConsumer + * + * @returns An allocated string containing the current broker-assigned group + * member id, or NULL if not available. + * The application must free the string with \p free() or + * rd_kafka_mem_free() + */ +RD_EXPORT +char *rd_kafka_memberid (const rd_kafka_t *rk); + + + +/** + * @brief Returns the ClusterId as reported in broker metadata. + * + * @param rk Client instance. + * @param timeout_ms If there is no cached value from metadata retrieval + * then this specifies the maximum amount of time + * (in milliseconds) the call will block waiting + * for metadata to be retrieved. + * Use 0 for non-blocking calls. + + * @remark Requires broker version >=0.10.0 and api.version.request=true. + * + * @remark The application must free the returned pointer + * using rd_kafka_mem_free(). + * + * @returns a newly allocated string containing the ClusterId, or NULL + * if no ClusterId could be retrieved in the allotted timespan. + */ +RD_EXPORT +char *rd_kafka_clusterid (rd_kafka_t *rk, int timeout_ms); + + +/** + * @brief Returns the current ControllerId as reported in broker metadata. + * + * @param rk Client instance. + * @param timeout_ms If there is no cached value from metadata retrieval + * then this specifies the maximum amount of time + * (in milliseconds) the call will block waiting + * for metadata to be retrieved. + * Use 0 for non-blocking calls. + + * @remark Requires broker version >=0.10.0 and api.version.request=true. + * + * @returns the controller broker id (>= 0), or -1 if no ControllerId could be + * retrieved in the allotted timespan. + */ +RD_EXPORT +int32_t rd_kafka_controllerid (rd_kafka_t *rk, int timeout_ms); + + +/** + * @brief Creates a new topic handle for topic named \p topic. + * + * \p conf is an optional configuration for the topic created with + * `rd_kafka_topic_conf_new()` that will be used instead of the default + * topic configuration. + * The \p conf object is freed by this function and must not be used or + * destroyed by the application sub-sequently. + * See `rd_kafka_topic_conf_set()` et.al for more information. + * + * Topic handles are refcounted internally and calling rd_kafka_topic_new() + * again with the same topic name will return the previous topic handle + * without updating the original handle's configuration. + * Applications must eventually call rd_kafka_topic_destroy() for each + * succesfull call to rd_kafka_topic_new() to clear up resources. + * + * @returns the new topic handle or NULL on error (use rd_kafka_errno2err() + * to convert system \p errno to an rd_kafka_resp_err_t error code. + * + * @sa rd_kafka_topic_destroy() + */ +RD_EXPORT +rd_kafka_topic_t *rd_kafka_topic_new(rd_kafka_t *rk, const char *topic, + rd_kafka_topic_conf_t *conf); + + + +/** + * @brief Loose application's topic handle refcount as previously created + * with `rd_kafka_topic_new()`. + * + * @remark Since topic objects are refcounted (both internally and for the app) + * the topic object might not actually be destroyed by this call, + * but the application must consider the object destroyed. + */ +RD_EXPORT +void rd_kafka_topic_destroy(rd_kafka_topic_t *rkt); + + +/** + * @brief Returns the topic name. + */ +RD_EXPORT +const char *rd_kafka_topic_name(const rd_kafka_topic_t *rkt); + + +/** + * @brief Get the \p rkt_opaque pointer that was set in the topic configuration + * with rd_kafka_topic_conf_set_opaque(). + */ +RD_EXPORT +void *rd_kafka_topic_opaque (const rd_kafka_topic_t *rkt); + + +/** + * @brief Unassigned partition. + * + * The unassigned partition is used by the producer API for messages + * that should be partitioned using the configured or default partitioner. + */ +#define RD_KAFKA_PARTITION_UA ((int32_t)-1) + + +/** + * @brief Polls the provided kafka handle for events. + * + * Events will cause application provided callbacks to be called. + * + * The \p timeout_ms argument specifies the maximum amount of time + * (in milliseconds) that the call will block waiting for events. + * For non-blocking calls, provide 0 as \p timeout_ms. + * To wait indefinately for an event, provide -1. + * + * @remark An application should make sure to call poll() at regular + * intervals to serve any queued callbacks waiting to be called. + * @remark If your producer doesn't have any callback set (in particular + * via rd_kafka_conf_set_dr_msg_cb or rd_kafka_conf_set_error_cb) + * you might chose not to call poll(), though this is not + * recommended. + * + * Events: + * - delivery report callbacks (if dr_cb/dr_msg_cb is configured) [producer] + * - error callbacks (rd_kafka_conf_set_error_cb()) [all] + * - stats callbacks (rd_kafka_conf_set_stats_cb()) [all] + * - throttle callbacks (rd_kafka_conf_set_throttle_cb()) [all] + * - OAUTHBEARER token refresh callbacks (rd_kafka_conf_set_oauthbearer_token_refresh_cb()) [all] + * + * @returns the number of events served. + */ +RD_EXPORT +int rd_kafka_poll(rd_kafka_t *rk, int timeout_ms); + + +/** + * @brief Cancels the current callback dispatcher (rd_kafka_poll(), + * rd_kafka_consume_callback(), etc). + * + * A callback may use this to force an immediate return to the calling + * code (caller of e.g. rd_kafka_poll()) without processing any further + * events. + * + * @remark This function MUST ONLY be called from within a librdkafka callback. + */ +RD_EXPORT +void rd_kafka_yield (rd_kafka_t *rk); + + + + +/** + * @brief Pause producing or consumption for the provided list of partitions. + * + * Success or error is returned per-partition \p err in the \p partitions list. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_pause_partitions (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *partitions); + + + +/** + * @brief Resume producing consumption for the provided list of partitions. + * + * Success or error is returned per-partition \p err in the \p partitions list. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_resume_partitions (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *partitions); + + + + +/** + * @brief Query broker for low (oldest/beginning) and high (newest/end) offsets + * for partition. + * + * Offsets are returned in \p *low and \p *high respectively. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code on failure. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_query_watermark_offsets (rd_kafka_t *rk, + const char *topic, int32_t partition, + int64_t *low, int64_t *high, int timeout_ms); + + +/** + * @brief Get last known low (oldest/beginning) and high (newest/end) offsets + * for partition. + * + * The low offset is updated periodically (if statistics.interval.ms is set) + * while the high offset is updated on each fetched message set from the broker. + * + * If there is no cached offset (either low or high, or both) then + * RD_KAFKA_OFFSET_INVALID will be returned for the respective offset. + * + * Offsets are returned in \p *low and \p *high respectively. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code on failure. + * + * @remark Shall only be used with an active consumer instance. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_get_watermark_offsets (rd_kafka_t *rk, + const char *topic, int32_t partition, + int64_t *low, int64_t *high); + + + +/** + * @brief Look up the offsets for the given partitions by timestamp. + * + * The returned offset for each partition is the earliest offset whose + * timestamp is greater than or equal to the given timestamp in the + * corresponding partition. + * + * The timestamps to query are represented as \c offset in \p offsets + * on input, and \c offset will contain the offset on output. + * + * The function will block for at most \p timeout_ms milliseconds. + * + * @remark Duplicate Topic+Partitions are not supported. + * @remark Per-partition errors may be returned in \c rd_kafka_topic_partition_t.err + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if offsets were be queried (do note + * that per-partition errors might be set), + * RD_KAFKA_RESP_ERR__TIMED_OUT if not all offsets could be fetched + * within \p timeout_ms, + * RD_KAFKA_RESP_ERR__INVALID_ARG if the \p offsets list is empty, + * RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION if all partitions are unknown, + * RD_KAFKA_RESP_ERR_LEADER_NOT_AVAILABLE if unable to query leaders + * for the given partitions. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_offsets_for_times (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *offsets, + int timeout_ms); + + + +/** + * @brief Allocate and zero memory using the same allocator librdkafka uses. + * + * This is typically an abstraction for the calloc(3) call and makes sure + * the application can use the same memory allocator as librdkafka for + * allocating pointers that are used by librdkafka. + * + * \p rk can be set to return memory allocated by a specific \c rk instance + * otherwise pass NULL for \p rk. + * + * @remark Memory allocated by rd_kafka_mem_calloc() must be freed using + * rd_kafka_mem_free() + */ +RD_EXPORT +void *rd_kafka_mem_calloc (rd_kafka_t *rk, size_t num, size_t size); + + + +/** + * @brief Allocate memory using the same allocator librdkafka uses. + * + * This is typically an abstraction for the malloc(3) call and makes sure + * the application can use the same memory allocator as librdkafka for + * allocating pointers that are used by librdkafka. + * + * \p rk can be set to return memory allocated by a specific \c rk instance + * otherwise pass NULL for \p rk. + * + * @remark Memory allocated by rd_kafka_mem_malloc() must be freed using + * rd_kafka_mem_free() + */ +RD_EXPORT +void *rd_kafka_mem_malloc (rd_kafka_t *rk, size_t size); + + + +/** + * @brief Free pointer returned by librdkafka + * + * This is typically an abstraction for the free(3) call and makes sure + * the application can use the same memory allocator as librdkafka for + * freeing pointers returned by librdkafka. + * + * In standard setups it is usually not necessary to use this interface + * rather than the free(3) functione. + * + * \p rk must be set for memory returned by APIs that take an \c rk argument, + * for other APIs pass NULL for \p rk. + * + * @remark rd_kafka_mem_free() must only be used for pointers returned by APIs + * that explicitly mention using this function for freeing. + */ +RD_EXPORT +void rd_kafka_mem_free (rd_kafka_t *rk, void *ptr); + + +/**@}*/ + + + + + +/** + * @name Queue API + * @{ + * + * Message queues allows the application to re-route consumed messages + * from multiple topic+partitions into one single queue point. + * This queue point containing messages from a number of topic+partitions + * may then be served by a single rd_kafka_consume*_queue() call, + * rather than one call per topic+partition combination. + */ + + +/** + * @brief Create a new message queue. + * + * See rd_kafka_consume_start_queue(), rd_kafka_consume_queue(), et.al. + */ +RD_EXPORT +rd_kafka_queue_t *rd_kafka_queue_new(rd_kafka_t *rk); + +/** + * Destroy a queue, purging all of its enqueued messages. + */ +RD_EXPORT +void rd_kafka_queue_destroy(rd_kafka_queue_t *rkqu); + + +/** + * @returns a reference to the main librdkafka event queue. + * This is the queue served by rd_kafka_poll(). + * + * Use rd_kafka_queue_destroy() to loose the reference. + */ +RD_EXPORT +rd_kafka_queue_t *rd_kafka_queue_get_main (rd_kafka_t *rk); + + +/** + * @returns a reference to the librdkafka consumer queue. + * This is the queue served by rd_kafka_consumer_poll(). + * + * Use rd_kafka_queue_destroy() to loose the reference. + * + * @remark rd_kafka_queue_destroy() MUST be called on this queue + * prior to calling rd_kafka_consumer_close(). + */ +RD_EXPORT +rd_kafka_queue_t *rd_kafka_queue_get_consumer (rd_kafka_t *rk); + +/** + * @returns a reference to the partition's queue, or NULL if + * partition is invalid. + * + * Use rd_kafka_queue_destroy() to loose the reference. + * + * @remark rd_kafka_queue_destroy() MUST be called on this queue + * + * @remark This function only works on consumers. + */ +RD_EXPORT +rd_kafka_queue_t *rd_kafka_queue_get_partition (rd_kafka_t *rk, + const char *topic, + int32_t partition); + +/** + * @returns a reference to the background thread queue, or NULL if the + * background queue is not enabled. + * + * To enable the background thread queue set a generic event handler callback + * with rd_kafka_conf_set_background_event_cb() on the client instance + * configuration object (rd_kafka_conf_t). + * + * The background queue is polled and served by librdkafka and MUST NOT be + * polled, forwarded, or otherwise managed by the application, it may only + * be used as the destination queue passed to queue-enabled APIs, such as + * the Admin API. + * + * The background thread queue provides the application with an automatically + * polled queue that triggers the event callback in a background thread, + * this background thread is completely managed by librdkafka. + * + * Use rd_kafka_queue_destroy() to loose the reference. + * + * @warning The background queue MUST NOT be read from (polled, consumed, etc), + * or forwarded from. + */ +RD_EXPORT +rd_kafka_queue_t *rd_kafka_queue_get_background (rd_kafka_t *rk); + + +/** + * @brief Forward/re-route queue \p src to \p dst. + * If \p dst is \c NULL the forwarding is removed. + * + * The internal refcounts for both queues are increased. + * + * @remark Regardless of whether \p dst is NULL or not, after calling this + * function, \p src will not forward it's fetch queue to the consumer + * queue. + */ +RD_EXPORT +void rd_kafka_queue_forward (rd_kafka_queue_t *src, rd_kafka_queue_t *dst); + +/** + * @brief Forward librdkafka logs (and debug) to the specified queue + * for serving with one of the ..poll() calls. + * + * This allows an application to serve log callbacks (\c log_cb) + * in its thread of choice. + * + * @param rk Client instance. + * @param rkqu Queue to forward logs to. If the value is NULL the logs + * are forwarded to the main queue. + * + * @remark The configuration property \c log.queue MUST also be set to true. + * + * @remark librdkafka maintains its own reference to the provided queue. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code on error. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_set_log_queue (rd_kafka_t *rk, + rd_kafka_queue_t *rkqu); + + +/** + * @returns the current number of elements in queue. + */ +RD_EXPORT +size_t rd_kafka_queue_length (rd_kafka_queue_t *rkqu); + + +/** + * @brief Enable IO event triggering for queue. + * + * To ease integration with IO based polling loops this API + * allows an application to create a separate file-descriptor + * that librdkafka will write \p payload (of size \p size) to + * whenever a new element is enqueued on a previously empty queue. + * + * To remove event triggering call with \p fd = -1. + * + * librdkafka will maintain a copy of the \p payload. + * + * @remark IO and callback event triggering are mutually exclusive. + * @remark When using forwarded queues the IO event must only be enabled + * on the final forwarded-to (destination) queue. + * @remark The file-descriptor/socket must be set to non-blocking. + */ +RD_EXPORT +void rd_kafka_queue_io_event_enable (rd_kafka_queue_t *rkqu, int fd, + const void *payload, size_t size); + +/** + * @brief Enable callback event triggering for queue. + * + * The callback will be called from an internal librdkafka thread + * when a new element is enqueued on a previously empty queue. + * + * To remove event triggering call with \p event_cb = NULL. + * + * The \p qev_opaque is passed to the callback's \p qev_opaque argument. + * + * @remark IO and callback event triggering are mutually exclusive. + * @remark Since the callback may be triggered from internal librdkafka + * threads, the application must not perform any pro-longed work in + * the callback, or call any librdkafka APIs (for the same rd_kafka_t + * handle). + */ +RD_EXPORT +void rd_kafka_queue_cb_event_enable (rd_kafka_queue_t *rkqu, + void (*event_cb) (rd_kafka_t *rk, + void *qev_opaque), + void *qev_opaque); + + +/** + * @brief Cancels the current rd_kafka_queue_poll() on \p rkqu. + * + * An application may use this from another thread to force + * an immediate return to the calling code (caller of rd_kafka_queue_poll()). + * Must not be used from signal handlers since that may cause deadlocks. + */ +RD_EXPORT +void rd_kafka_queue_yield (rd_kafka_queue_t *rkqu); + + +/**@}*/ + +/** + * + * @name Simple Consumer API (legacy) + * @{ + * + */ + + +#define RD_KAFKA_OFFSET_BEGINNING -2 /**< Start consuming from beginning of + * kafka partition queue: oldest msg */ +#define RD_KAFKA_OFFSET_END -1 /**< Start consuming from end of kafka + * partition queue: next msg */ +#define RD_KAFKA_OFFSET_STORED -1000 /**< Start consuming from offset retrieved + * from offset store */ +#define RD_KAFKA_OFFSET_INVALID -1001 /**< Invalid offset */ + + +/** @cond NO_DOC */ +#define RD_KAFKA_OFFSET_TAIL_BASE -2000 /* internal: do not use */ +/** @endcond */ + +/** + * @brief Start consuming \p CNT messages from topic's current end offset. + * + * That is, if current end offset is 12345 and \p CNT is 200, it will start + * consuming from offset \c 12345-200 = \c 12145. */ +#define RD_KAFKA_OFFSET_TAIL(CNT) (RD_KAFKA_OFFSET_TAIL_BASE - (CNT)) + +/** + * @brief Start consuming messages for topic \p rkt and \p partition + * at offset \p offset which may either be an absolute \c (0..N) + * or one of the logical offsets: + * - RD_KAFKA_OFFSET_BEGINNING + * - RD_KAFKA_OFFSET_END + * - RD_KAFKA_OFFSET_STORED + * - RD_KAFKA_OFFSET_TAIL + * + * rdkafka will attempt to keep \c queued.min.messages (config property) + * messages in the local queue by repeatedly fetching batches of messages + * from the broker until the threshold is reached. + * + * The application shall use one of the `rd_kafka_consume*()` functions + * to consume messages from the local queue, each kafka message being + * represented as a `rd_kafka_message_t *` object. + * + * `rd_kafka_consume_start()` must not be called multiple times for the same + * topic and partition without stopping consumption first with + * `rd_kafka_consume_stop()`. + * + * @returns 0 on success or -1 on error in which case errno is set accordingly: + * - EBUSY - Conflicts with an existing or previous subscription + * (RD_KAFKA_RESP_ERR__CONFLICT) + * - EINVAL - Invalid offset, or incomplete configuration (lacking group.id) + * (RD_KAFKA_RESP_ERR__INVALID_ARG) + * - ESRCH - requested \p partition is invalid. + * (RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION) + * - ENOENT - topic is unknown in the Kafka cluster. + * (RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC) + * + * Use `rd_kafka_errno2err()` to convert sytem \c errno to `rd_kafka_resp_err_t` + */ +RD_EXPORT +int rd_kafka_consume_start(rd_kafka_topic_t *rkt, int32_t partition, + int64_t offset); + +/** + * @brief Same as rd_kafka_consume_start() but re-routes incoming messages to + * the provided queue \p rkqu (which must have been previously allocated + * with `rd_kafka_queue_new()`. + * + * The application must use one of the `rd_kafka_consume_*_queue()` functions + * to receive fetched messages. + * + * `rd_kafka_consume_start_queue()` must not be called multiple times for the + * same topic and partition without stopping consumption first with + * `rd_kafka_consume_stop()`. + * `rd_kafka_consume_start()` and `rd_kafka_consume_start_queue()` must not + * be combined for the same topic and partition. + */ +RD_EXPORT +int rd_kafka_consume_start_queue(rd_kafka_topic_t *rkt, int32_t partition, + int64_t offset, rd_kafka_queue_t *rkqu); + +/** + * @brief Stop consuming messages for topic \p rkt and \p partition, purging + * all messages currently in the local queue. + * + * NOTE: To enforce synchronisation this call will block until the internal + * fetcher has terminated and offsets are committed to configured + * storage method. + * + * The application needs to be stop all consumers before calling + * `rd_kafka_destroy()` on the main object handle. + * + * @returns 0 on success or -1 on error (see `errno`). + */ +RD_EXPORT +int rd_kafka_consume_stop(rd_kafka_topic_t *rkt, int32_t partition); + + + +/** + * @brief Seek consumer for topic+partition to \p offset which is either an + * absolute or logical offset. + * + * If \p timeout_ms is not 0 the call will wait this long for the + * seek to be performed. If the timeout is reached the internal state + * will be unknown and this function returns `RD_KAFKA_RESP_ERR__TIMED_OUT`. + * If \p timeout_ms is 0 it will initiate the seek but return + * immediately without any error reporting (e.g., async). + * + * This call will purge all pre-fetched messages for the given partition, which + * may be up to \c queued.max.message.kbytes in size. Repeated use of seek + * may thus lead to increased network usage as messages are re-fetched from + * the broker. + * + * @remark Seek must only be performed for already assigned/consumed partitions, + * use rd_kafka_assign() (et.al) to set the initial starting offset + * for a new assignmenmt. + * + * @returns `RD_KAFKA_RESP_ERR__NO_ERROR` on success else an error code. + * + * @deprecated Use rd_kafka_seek_partitions(). + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_seek (rd_kafka_topic_t *rkt, + int32_t partition, + int64_t offset, + int timeout_ms); + + + +/** + * @brief Seek consumer for partitions in \p partitions to the per-partition + * offset in the \c .offset field of \p partitions. + * + * The offset may be either absolute (>= 0) or a logical offset. + * + * If \p timeout_ms is not 0 the call will wait this long for the + * seeks to be performed. If the timeout is reached the internal state + * will be unknown for the remaining partitions to seek and this function + * will return an error with the error code set to + * `RD_KAFKA_RESP_ERR__TIMED_OUT`. + * + * If \p timeout_ms is 0 it will initiate the seek but return + * immediately without any error reporting (e.g., async). + * + * This call will purge all pre-fetched messages for the given partition, which + * may be up to \c queued.max.message.kbytes in size. Repeated use of seek + * may thus lead to increased network usage as messages are re-fetched from + * the broker. + * + * Individual partition errors are reported in the per-partition \c .err field + * of \p partitions. + * + * @remark Seek must only be performed for already assigned/consumed partitions, + * use rd_kafka_assign() (et.al) to set the initial starting offset + * for a new assignmenmt. + * + * @returns NULL on success or an error object on failure. + */ +RD_EXPORT rd_kafka_error_t * +rd_kafka_seek_partitions (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *partitions, + int timeout_ms); + + +/** + * @brief Consume a single message from topic \p rkt and \p partition + * + * \p timeout_ms is maximum amount of time to wait for a message to be received. + * Consumer must have been previously started with `rd_kafka_consume_start()`. + * + * @returns a message object on success or \c NULL on error. + * The message object must be destroyed with `rd_kafka_message_destroy()` + * when the application is done with it. + * + * Errors (when returning NULL): + * - ETIMEDOUT - \p timeout_ms was reached with no new messages fetched. + * - ENOENT - \p rkt + \p partition is unknown. + * (no prior `rd_kafka_consume_start()` call) + * + * NOTE: The returned message's \c ..->err must be checked for errors. + * NOTE: \c ..->err \c == \c RD_KAFKA_RESP_ERR__PARTITION_EOF signals that the + * end of the partition has been reached, which should typically not be + * considered an error. The application should handle this case + * (e.g., ignore). + * + * @remark on_consume() interceptors may be called from this function prior to + * passing message to application. + */ +RD_EXPORT +rd_kafka_message_t *rd_kafka_consume(rd_kafka_topic_t *rkt, int32_t partition, + int timeout_ms); + + + +/** + * @brief Consume up to \p rkmessages_size from topic \p rkt and \p partition + * putting a pointer to each message in the application provided + * array \p rkmessages (of size \p rkmessages_size entries). + * + * `rd_kafka_consume_batch()` provides higher throughput performance + * than `rd_kafka_consume()`. + * + * \p timeout_ms is the maximum amount of time to wait for all of + * \p rkmessages_size messages to be put into \p rkmessages. + * If no messages were available within the timeout period this function + * returns 0 and \p rkmessages remains untouched. + * This differs somewhat from `rd_kafka_consume()`. + * + * The message objects must be destroyed with `rd_kafka_message_destroy()` + * when the application is done with it. + * + * @returns the number of rkmessages added in \p rkmessages, + * or -1 on error (same error codes as for `rd_kafka_consume()`. + * + * @sa rd_kafka_consume() + * + * @remark on_consume() interceptors may be called from this function prior to + * passing message to application. + */ +RD_EXPORT +ssize_t rd_kafka_consume_batch(rd_kafka_topic_t *rkt, int32_t partition, + int timeout_ms, + rd_kafka_message_t **rkmessages, + size_t rkmessages_size); + + + +/** + * @brief Consumes messages from topic \p rkt and \p partition, calling + * the provided callback for each consumed messsage. + * + * `rd_kafka_consume_callback()` provides higher throughput performance + * than both `rd_kafka_consume()` and `rd_kafka_consume_batch()`. + * + * \p timeout_ms is the maximum amount of time to wait for one or more messages + * to arrive. + * + * The provided \p consume_cb function is called for each message, + * the application \b MUST \b NOT call `rd_kafka_message_destroy()` on the + * provided \p rkmessage. + * + * The \p commit_opaque argument is passed to the \p consume_cb + * as \p commit_opaque. + * + * @returns the number of messages processed or -1 on error. + * + * @sa rd_kafka_consume() + * + * @remark on_consume() interceptors may be called from this function prior to + * passing message to application. + * + * @remark This function will return early if a transaction control message is + * received, these messages are not exposed to the application but + * still enqueued on the consumer queue to make sure their + * offsets are stored. + * + * @deprecated This API is deprecated and subject for future removal. + * There is no new callback-based consume interface, use the + * poll/queue based alternatives. + */ +RD_EXPORT +int rd_kafka_consume_callback(rd_kafka_topic_t *rkt, int32_t partition, + int timeout_ms, + void (*consume_cb) (rd_kafka_message_t + *rkmessage, + void *commit_opaque), + void *commit_opaque); + + +/** + * @name Simple Consumer API (legacy): Queue consumers + * @{ + * + * The following `..._queue()` functions are analogue to the functions above + * but reads messages from the provided queue \p rkqu instead. + * \p rkqu must have been previously created with `rd_kafka_queue_new()` + * and the topic consumer must have been started with + * `rd_kafka_consume_start_queue()` utilising the the same queue. + */ + +/** + * @brief Consume from queue + * + * @sa rd_kafka_consume() + */ +RD_EXPORT +rd_kafka_message_t *rd_kafka_consume_queue(rd_kafka_queue_t *rkqu, + int timeout_ms); + +/** + * @brief Consume batch of messages from queue + * + * @sa rd_kafka_consume_batch() + */ +RD_EXPORT +ssize_t rd_kafka_consume_batch_queue(rd_kafka_queue_t *rkqu, + int timeout_ms, + rd_kafka_message_t **rkmessages, + size_t rkmessages_size); + +/** + * @brief Consume multiple messages from queue with callback + * + * @sa rd_kafka_consume_callback() + * + * @deprecated This API is deprecated and subject for future removal. + * There is no new callback-based consume interface, use the + * poll/queue based alternatives. + */ +RD_EXPORT +int rd_kafka_consume_callback_queue (rd_kafka_queue_t *rkqu, + int timeout_ms, + void (*consume_cb) (rd_kafka_message_t + *rkmessage, + void *commit_opaque), + void *commit_opaque); + + +/**@}*/ + + + + +/** + * @name Simple Consumer API (legacy): Topic+partition offset store. + * @{ + * + * If \c auto.commit.enable is true the offset is stored automatically prior to + * returning of the message(s) in each of the rd_kafka_consume*() functions + * above. + */ + + +/** + * @brief Store offset \p offset + 1 for topic \p rkt partition \p partition. + * + * The \c offset + 1 will be committed (written) to broker (or file) according + * to \c `auto.commit.interval.ms` or manual offset-less commit() + * + * @remark \c `enable.auto.offset.store` must be set to "false" when using + * this API. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code on error. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_offset_store (rd_kafka_topic_t *rkt, + int32_t partition, int64_t offset); + + +/** + * @brief Store offsets for next auto-commit for one or more partitions. + * + * The offset will be committed (written) to the offset store according + * to \c `auto.commit.interval.ms` or manual offset-less commit(). + * + * Per-partition success/error status propagated through each partition's + * \c .err field. + * + * @remark The \c .offset field is stored as is, it will NOT be + 1. + * + * @remark \c `enable.auto.offset.store` must be set to "false" when using + * this API. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or + * RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION if none of the + * offsets could be stored, or + * RD_KAFKA_RESP_ERR__INVALID_ARG if \c enable.auto.offset.store + * is true. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_offsets_store (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *offsets); +/**@}*/ + + + + +/** + * @name KafkaConsumer (C) + * @{ + * @brief High-level KafkaConsumer C API + * + * + * + */ + +/** + * @brief Subscribe to topic set using balanced consumer groups. + * + * Wildcard (regex) topics are supported: + * any topic name in the \p topics list that is prefixed with \c \"^\" will + * be regex-matched to the full list of topics in the cluster and matching + * topics will be added to the subscription list. + * + * The full topic list is retrieved every \c topic.metadata.refresh.interval.ms + * to pick up new or delete topics that match the subscription. + * If there is any change to the matched topics the consumer will + * immediately rejoin the group with the updated set of subscribed topics. + * + * Regex and full topic names can be mixed in \p topics. + * + * @remark Only the \c .topic field is used in the supplied \p topics list, + * all other fields are ignored. + * + * @remark subscribe() is an asynchronous method which returns immediately: + * background threads will (re)join the group, wait for group rebalance, + * issue any registered rebalance_cb, assign() the assigned partitions, + * and then start fetching messages. This cycle may take up to + * \c session.timeout.ms * 2 or more to complete. + * + * @remark A consumer error will be raised for each unavailable topic in the + * \p topics. The error will be RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART + * for non-existent topics, and + * RD_KAFKA_RESP_ERR_TOPIC_AUTHORIZATION_FAILED for unauthorized topics. + * The consumer error will be raised through rd_kafka_consumer_poll() + * (et.al.) with the \c rd_kafka_message_t.err field set to one of the + * error codes mentioned above. + * The subscribe function itself is asynchronous and will not return + * an error on unavailable topics. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or + * RD_KAFKA_RESP_ERR__INVALID_ARG if list is empty, contains invalid + * topics or regexes or duplicate entries, + * RD_KAFKA_RESP_ERR__FATAL if the consumer has raised a fatal error. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_subscribe (rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t *topics); + + +/** + * @brief Unsubscribe from the current subscription set. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_unsubscribe (rd_kafka_t *rk); + + +/** + * @brief Returns the current topic subscription + * + * @returns An error code on failure, otherwise \p topic is updated + * to point to a newly allocated topic list (possibly empty). + * + * @remark The application is responsible for calling + * rd_kafka_topic_partition_list_destroy on the returned list. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_subscription (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t **topics); + + + +/** + * @brief Poll the consumer for messages or events. + * + * Will block for at most \p timeout_ms milliseconds. + * + * @remark An application should make sure to call consumer_poll() at regular + * intervals, even if no messages are expected, to serve any + * queued callbacks waiting to be called. This is especially + * important when a rebalance_cb has been registered as it needs + * to be called and handled properly to synchronize internal + * consumer state. + * + * @returns A message object which is a proper message if \p ->err is + * RD_KAFKA_RESP_ERR_NO_ERROR, or an event or error for any other + * value. + * + * @remark on_consume() interceptors may be called from this function prior to + * passing message to application. + * + * @remark When subscribing to topics the application must call poll at + * least every \c max.poll.interval.ms to remain a member of the + * consumer group. + * + * Noteworthy errors returned in \c ->err: + * - RD_KAFKA_RESP_ERR__MAX_POLL_EXCEEDED - application failed to call + * poll within `max.poll.interval.ms`. + * + * @sa rd_kafka_message_t + */ +RD_EXPORT +rd_kafka_message_t *rd_kafka_consumer_poll (rd_kafka_t *rk, int timeout_ms); + +/** + * @brief Close down the KafkaConsumer. + * + * @remark This call will block until the consumer has revoked its assignment, + * calling the \c rebalance_cb if it is configured, committed offsets + * to broker, and left the consumer group. + * The maximum blocking time is roughly limited to session.timeout.ms. + * + * @returns An error code indicating if the consumer close was succesful + * or not. + * RD_KAFKA_RESP_ERR__FATAL is returned if the consumer has raised + * a fatal error. + * + * @remark The application still needs to call rd_kafka_destroy() after + * this call finishes to clean up the underlying handle resources. + * + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_consumer_close (rd_kafka_t *rk); + + +/** + * @brief Incrementally add \p partitions to the current assignment. + * + * If a COOPERATIVE assignor (i.e. incremental rebalancing) is being used, + * this method should be used in a rebalance callback to adjust the current + * assignment appropriately in the case where the rebalance type is + * RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS. The application must pass the + * partition list passed to the callback (or a copy of it), even if the + * list is empty. \p partitions must not be NULL. This method may also be + * used outside the context of a rebalance callback. + * + * @returns NULL on success, or an error object if the operation was + * unsuccessful. + * + * @remark The returned error object (if not NULL) must be destroyed with + * rd_kafka_error_destroy(). + */ +RD_EXPORT rd_kafka_error_t * +rd_kafka_incremental_assign (rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t + *partitions); + + +/** + * @brief Incrementally remove \p partitions from the current assignment. + * + * If a COOPERATIVE assignor (i.e. incremental rebalancing) is being used, + * this method should be used in a rebalance callback to adjust the current + * assignment appropriately in the case where the rebalance type is + * RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS. The application must pass the + * partition list passed to the callback (or a copy of it), even if the + * list is empty. \p partitions must not be NULL. This method may also be + * used outside the context of a rebalance callback. + * + * @returns NULL on success, or an error object if the operation was + * unsuccessful. + * + * @remark The returned error object (if not NULL) must be destroyed with + * rd_kafka_error_destroy(). + */ +RD_EXPORT rd_kafka_error_t * +rd_kafka_incremental_unassign (rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t + *partitions); + + +/** + * @brief The rebalance protocol currently in use. This will be + * "NONE" if the consumer has not (yet) joined a group, else it will + * match the rebalance protocol ("EAGER", "COOPERATIVE") of the + * configured and selected assignor(s). All configured + * assignors must have the same protocol type, meaning + * online migration of a consumer group from using one + * protocol to another (in particular upgading from EAGER + * to COOPERATIVE) without a restart is not currently + * supported. + * + * @returns NULL on error, or one of "NONE", "EAGER", "COOPERATIVE" on success. + */ +RD_EXPORT +const char *rd_kafka_rebalance_protocol (rd_kafka_t *rk); + + +/** + * @brief Atomic assignment of partitions to consume. + * + * The new \p partitions will replace the existing assignment. + * + * A zero-length \p partitions will treat the partitions as a valid, + * albeit empty assignment, and maintain internal state, while a \c NULL + * value for \p partitions will reset and clear the internal state. + * + * When used from a rebalance callback, the application should pass the + * partition list passed to the callback (or a copy of it) even if the list + * is empty (i.e. should not pass NULL in this case) so as to maintain + * internal join state. This is not strictly required - the application + * may adjust the assignment provided by the group. However, this is rarely + * useful in practice. + * + * @returns An error code indicating if the new assignment was applied or not. + * RD_KAFKA_RESP_ERR__FATAL is returned if the consumer has raised + * a fatal error. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_assign (rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t *partitions); + +/** + * @brief Returns the current partition assignment as set by rd_kafka_assign() + * or rd_kafka_incremental_assign(). + * + * @returns An error code on failure, otherwise \p partitions is updated + * to point to a newly allocated partition list (possibly empty). + * + * @remark The application is responsible for calling + * rd_kafka_topic_partition_list_destroy on the returned list. + * + * @remark This assignment represents the partitions assigned through the + * assign functions and not the partitions assigned to this consumer + * instance by the consumer group leader. + * They are usually the same following a rebalance but not necessarily + * since an application is free to assign any partitions. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_assignment (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t **partitions); + + +/** + * @brief Check whether the consumer considers the current assignment to + * have been lost involuntarily. This method is only applicable for + * use with a high level subscribing consumer. Assignments are revoked + * immediately when determined to have been lost, so this method + * is only useful when reacting to a RD_KAFKA_EVENT_REBALANCE event + * or from within a rebalance_cb. Partitions that have been lost may + * already be owned by other members in the group and therefore + * commiting offsets, for example, may fail. + * + * @remark Calling rd_kafka_assign(), rd_kafka_incremental_assign() or + * rd_kafka_incremental_unassign() resets this flag. + * + * @returns Returns 1 if the current partition assignment is considered + * lost, 0 otherwise. + */ +RD_EXPORT int +rd_kafka_assignment_lost (rd_kafka_t *rk); + + +/** + * @brief Commit offsets on broker for the provided list of partitions. + * + * \p offsets should contain \c topic, \c partition, \c offset and possibly + * \c metadata. The \c offset should be the offset where consumption will + * resume, i.e., the last processed offset + 1. + * If \p offsets is NULL the current partition assignment will be used instead. + * + * If \p async is false this operation will block until the broker offset commit + * is done, returning the resulting success or error code. + * + * If a rd_kafka_conf_set_offset_commit_cb() offset commit callback has been + * configured the callback will be enqueued for a future call to + * rd_kafka_poll(), rd_kafka_consumer_poll() or similar. + * + * @returns An error code indiciating if the commit was successful, + * or successfully scheduled if asynchronous, or failed. + * RD_KAFKA_RESP_ERR__FATAL is returned if the consumer has raised + * a fatal error. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_commit (rd_kafka_t *rk, const rd_kafka_topic_partition_list_t *offsets, + int async); + + +/** + * @brief Commit message's offset on broker for the message's partition. + * The committed offset is the message's offset + 1. + * + * @sa rd_kafka_commit + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_commit_message (rd_kafka_t *rk, const rd_kafka_message_t *rkmessage, + int async); + + +/** + * @brief Commit offsets on broker for the provided list of partitions. + * + * See rd_kafka_commit for \p offsets semantics. + * + * The result of the offset commit will be posted on the provided \p rkqu queue. + * + * If the application uses one of the poll APIs (rd_kafka_poll(), + * rd_kafka_consumer_poll(), rd_kafka_queue_poll(), ..) to serve the queue + * the \p cb callback is required. + * + * The \p commit_opaque argument is passed to the callback as \p commit_opaque, + * or if using the event API the callback is ignored and the offset commit + * result will be returned as an RD_KAFKA_EVENT_COMMIT event and the + * \p commit_opaque value will be available with rd_kafka_event_opaque(). + * + * If \p rkqu is NULL a temporary queue will be created and the callback will + * be served by this call. + * + * @sa rd_kafka_commit() + * @sa rd_kafka_conf_set_offset_commit_cb() + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_commit_queue (rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t *offsets, + rd_kafka_queue_t *rkqu, + void (*cb) (rd_kafka_t *rk, + rd_kafka_resp_err_t err, + rd_kafka_topic_partition_list_t *offsets, + void *commit_opaque), + void *commit_opaque); + + +/** + * @brief Retrieve committed offsets for topics+partitions. + * + * The \p offset field of each requested partition will either be set to + * stored offset or to RD_KAFKA_OFFSET_INVALID in case there was no stored + * offset for that partition. + * + * Committed offsets will be returned according to the `isolation.level` + * configuration property, if set to `read_committed` (default) then only + * stable offsets for fully committed transactions will be returned, while + * `read_uncommitted` may return offsets for not yet committed transactions. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success in which case the + * \p offset or \p err field of each \p partitions' element is filled + * in with the stored offset, or a partition specific error. + * Else returns an error code. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_committed (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *partitions, + int timeout_ms); + + + +/** + * @brief Retrieve current positions (offsets) for topics+partitions. + * + * The \p offset field of each requested partition will be set to the offset + * of the last consumed message + 1, or RD_KAFKA_OFFSET_INVALID in case there was + * no previous message. + * + * @remark In this context the last consumed message is the offset consumed + * by the current librdkafka instance and, in case of rebalancing, not + * necessarily the last message fetched from the partition. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success in which case the + * \p offset or \p err field of each \p partitions' element is filled + * in with the stored offset, or a partition specific error. + * Else returns an error code. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_position (rd_kafka_t *rk, + rd_kafka_topic_partition_list_t *partitions); + + + + +/** + * @returns the current consumer group metadata associated with this consumer, + * or NULL if \p rk is not a consumer configured with a \c group.id. + * This metadata object should be passed to the transactional + * producer's rd_kafka_send_offsets_to_transaction() API. + * + * @remark The returned pointer must be freed by the application using + * rd_kafka_consumer_group_metadata_destroy(). + * + * @sa rd_kafka_send_offsets_to_transaction() + */ +RD_EXPORT rd_kafka_consumer_group_metadata_t * +rd_kafka_consumer_group_metadata (rd_kafka_t *rk); + + +/** + * @brief Create a new consumer group metadata object. + * This is typically only used for writing tests. + * + * @param group_id The group id. + * + * @remark The returned pointer must be freed by the application using + * rd_kafka_consumer_group_metadata_destroy(). + */ +RD_EXPORT rd_kafka_consumer_group_metadata_t * +rd_kafka_consumer_group_metadata_new (const char *group_id); + + +/** + * @brief Create a new consumer group metadata object. + * This is typically only used for writing tests. + * + * @param group_id The group id. + * @param generation_id The group generation id. + * @param member_id The group member id. + * @param group_instance_id The group instance id (may be NULL). + * + * @remark The returned pointer must be freed by the application using + * rd_kafka_consumer_group_metadata_destroy(). + */ +RD_EXPORT rd_kafka_consumer_group_metadata_t * +rd_kafka_consumer_group_metadata_new_with_genid (const char *group_id, + int32_t generation_id, + const char *member_id, + const char + *group_instance_id); + + +/** + * @brief Frees the consumer group metadata object as returned by + * rd_kafka_consumer_group_metadata(). + */ +RD_EXPORT void +rd_kafka_consumer_group_metadata_destroy (rd_kafka_consumer_group_metadata_t *); + + +/** + * @brief Serialize the consumer group metadata to a binary format. + * This is mainly for client binding use and not for application use. + * + * @remark The serialized metadata format is private and is not compatible + * across different versions or even builds of librdkafka. + * It should only be used in the same process runtime and must only + * be passed to rd_kafka_consumer_group_metadata_read(). + * + * @param cgmd Metadata to be serialized. + * @param bufferp On success this pointer will be updated to point to na + * allocated buffer containing the serialized metadata. + * The buffer must be freed with rd_kafka_mem_free(). + * @param sizep The pointed to size will be updated with the size of + * the serialized buffer. + * + * @returns NULL on success or an error object on failure. + * + * @sa rd_kafka_consumer_group_metadata_read() + */ +RD_EXPORT rd_kafka_error_t * +rd_kafka_consumer_group_metadata_write ( + const rd_kafka_consumer_group_metadata_t *cgmd, + void **bufferp, size_t *sizep); + +/** + * @brief Reads serialized consumer group metadata and returns a + * consumer group metadata object. + * This is mainly for client binding use and not for application use. + * + * @remark The serialized metadata format is private and is not compatible + * across different versions or even builds of librdkafka. + * It should only be used in the same process runtime and must only + * be passed to rd_kafka_consumer_group_metadata_read(). + * + * @param cgmdp On success this pointer will be updated to point to a new + * consumer group metadata object which must be freed with + * rd_kafka_consumer_group_metadata_destroy(). + * @param buffer Pointer to the serialized data. + * @param size Size of the serialized data. + * + * @returns NULL on success or an error object on failure. + * + * @sa rd_kafka_consumer_group_metadata_write() + */ +RD_EXPORT rd_kafka_error_t * +rd_kafka_consumer_group_metadata_read ( + rd_kafka_consumer_group_metadata_t **cgmdp, + const void *buffer, size_t size); + +/**@}*/ + + + +/** + * @name Producer API + * @{ + * + * + */ + + +/** + * @brief Producer message flags + */ +#define RD_KAFKA_MSG_F_FREE 0x1 /**< Delegate freeing of payload to rdkafka. */ +#define RD_KAFKA_MSG_F_COPY 0x2 /**< rdkafka will make a copy of the payload. */ +#define RD_KAFKA_MSG_F_BLOCK 0x4 /**< Block produce*() on message queue full. + * WARNING: If a delivery report callback + * is used the application MUST + * call rd_kafka_poll() (or equiv.) + * to make sure delivered messages + * are drained from the internal + * delivery report queue. + * Failure to do so will result + * in indefinately blocking on + * the produce() call when the + * message queue is full. */ +#define RD_KAFKA_MSG_F_PARTITION 0x8 /**< produce_batch() will honor + * per-message partition. */ + + + +/** + * @brief Produce and send a single message to broker. + * + * \p rkt is the target topic which must have been previously created with + * `rd_kafka_topic_new()`. + * + * `rd_kafka_produce()` is an asynch non-blocking API. + * See `rd_kafka_conf_set_dr_msg_cb` on how to setup a callback to be called + * once the delivery status (success or failure) is known. The delivery report + * is trigged by the application calling `rd_kafka_poll()` (at regular + * intervals) or `rd_kafka_flush()` (at termination). + * + * Since producing is asynchronous, you should call `rd_kafka_flush()` before + * you destroy the producer. Otherwise, any outstanding messages will be + * silently discarded. + * + * When temporary errors occur, librdkafka automatically retries to produce the + * messages. Retries are triggered after retry.backoff.ms and when the + * leader broker for the given partition is available. Otherwise, librdkafka + * falls back to polling the topic metadata to monitor when a new leader is + * elected (see the topic.metadata.refresh.fast.interval.ms and + * topic.metadata.refresh.interval.ms configurations) and then performs a + * retry. A delivery error will occur if the message could not be produced + * within message.timeout.ms. + * + * See the "Message reliability" chapter in INTRODUCTION.md for more + * information. + * + * \p partition is the target partition, either: + * - RD_KAFKA_PARTITION_UA (unassigned) for + * automatic partitioning using the topic's partitioner function, or + * - a fixed partition (0..N) + * + * \p msgflags is zero or more of the following flags OR:ed together: + * RD_KAFKA_MSG_F_BLOCK - block \p produce*() call if + * \p queue.buffering.max.messages or + * \p queue.buffering.max.kbytes are exceeded. + * Messages are considered in-queue from the point they + * are accepted by produce() until their corresponding + * delivery report callback/event returns. + * It is thus a requirement to call + * rd_kafka_poll() (or equiv.) from a separate + * thread when F_BLOCK is used. + * See WARNING on \c RD_KAFKA_MSG_F_BLOCK above. + * + * RD_KAFKA_MSG_F_FREE - rdkafka will free(3) \p payload when it is done + * with it. + * RD_KAFKA_MSG_F_COPY - the \p payload data will be copied and the + * \p payload pointer will not be used by rdkafka + * after the call returns. + * RD_KAFKA_MSG_F_PARTITION - produce_batch() will honour per-message + * partition, either set manually or by the + * configured partitioner. + * + * .._F_FREE and .._F_COPY are mutually exclusive. If neither of these are + * set, the caller must ensure that the memory backing \p payload remains + * valid and is not modified or reused until the delivery callback is + * invoked. Other buffers passed to `rd_kafka_produce()` don't have this + * restriction on reuse, i.e. the memory backing the key or the topic name + * may be reused as soon as `rd_kafka_produce()` returns. + * + * If the function returns -1 and RD_KAFKA_MSG_F_FREE was specified, then + * the memory associated with the payload is still the caller's + * responsibility. + * + * \p payload is the message payload of size \p len bytes. + * + * \p key is an optional message key of size \p keylen bytes, if non-NULL it + * will be passed to the topic partitioner as well as be sent with the + * message to the broker and passed on to the consumer. + * + * \p msg_opaque is an optional application-provided per-message opaque + * pointer that will provided in the message's delivery report callback + * (\c dr_msg_cb or \c dr_cb) and the \c rd_kafka_message_t \c _private field. + * + * @remark on_send() and on_acknowledgement() interceptors may be called + * from this function. on_acknowledgement() will only be called if the + * message fails partitioning. + * + * @remark If the producer is transactional (\c transactional.id is configured) + * producing is only allowed during an on-going transaction, namely + * after rd_kafka_begin_transaction() has been called. + * + * @returns 0 on success or -1 on error in which case errno is set accordingly: + * - ENOBUFS - maximum number of outstanding messages has been reached: + * "queue.buffering.max.messages" + * (RD_KAFKA_RESP_ERR__QUEUE_FULL) + * - EMSGSIZE - message is larger than configured max size: + * "messages.max.bytes". + * (RD_KAFKA_RESP_ERR_MSG_SIZE_TOO_LARGE) + * - ESRCH - requested \p partition is unknown in the Kafka cluster. + * (RD_KAFKA_RESP_ERR__UNKNOWN_PARTITION) + * - ENOENT - topic is unknown in the Kafka cluster. + * (RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC) + * - ECANCELED - fatal error has been raised on producer, see + * rd_kafka_fatal_error(), + * (RD_KAFKA_RESP_ERR__FATAL). + * - ENOEXEC - transactional state forbids producing + * (RD_KAFKA_RESP_ERR__STATE) + * + * @sa Use rd_kafka_errno2err() to convert `errno` to rdkafka error code. + */ +RD_EXPORT +int rd_kafka_produce(rd_kafka_topic_t *rkt, int32_t partition, + int msgflags, + void *payload, size_t len, + const void *key, size_t keylen, + void *msg_opaque); + + +/** + * @brief Produce and send a single message to broker. + * + * The message is defined by a va-arg list using \c rd_kafka_vtype_t + * tag tuples which must be terminated with a single \c RD_KAFKA_V_END. + * + * @returns \c RD_KAFKA_RESP_ERR_NO_ERROR on success, else an error code as + * described in rd_kafka_produce(). + * \c RD_KAFKA_RESP_ERR__CONFLICT is returned if _V_HEADER and + * _V_HEADERS are mixed. + * + * @sa rd_kafka_produce, rd_kafka_produceva, RD_KAFKA_V_END + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_producev (rd_kafka_t *rk, ...); + + +/** + * @brief Produce and send a single message to broker. + * + * The message is defined by an array of \c rd_kafka_vu_t of + * count \p cnt. + * + * @returns an error object on failure or NULL on success. + * See rd_kafka_producev() for specific error codes. + * + * @sa rd_kafka_produce, rd_kafka_producev, RD_KAFKA_V_END + */ +RD_EXPORT +rd_kafka_error_t *rd_kafka_produceva (rd_kafka_t *rk, + const rd_kafka_vu_t *vus, + size_t cnt); + + +/** + * @brief Produce multiple messages. + * + * If partition is RD_KAFKA_PARTITION_UA the configured partitioner will + * be run for each message (slower), otherwise the messages will be enqueued + * to the specified partition directly (faster). + * + * The messages are provided in the array \p rkmessages of count \p message_cnt + * elements. + * The \p partition and \p msgflags are used for all provided messages. + * + * Honoured \p rkmessages[] fields are: + * - payload,len Message payload and length + * - key,key_len Optional message key + * - _private Message opaque pointer (msg_opaque) + * - err Will be set according to success or failure, see + * rd_kafka_produce() for possible error codes. + * Application only needs to check for errors if + * return value != \p message_cnt. + * + * @remark If \c RD_KAFKA_MSG_F_PARTITION is set in \p msgflags, the + * \c .partition field of the \p rkmessages is used instead of + * \p partition. + * + * @returns the number of messages succesfully enqueued for producing. + * + * @remark This interface does NOT support setting message headers on + * the provided \p rkmessages. + */ +RD_EXPORT +int rd_kafka_produce_batch(rd_kafka_topic_t *rkt, int32_t partition, + int msgflags, + rd_kafka_message_t *rkmessages, int message_cnt); + + + + +/** + * @brief Wait until all outstanding produce requests, et.al, are completed. + * This should typically be done prior to destroying a producer instance + * to make sure all queued and in-flight produce requests are completed + * before terminating. + * + * @remark This function will call rd_kafka_poll() and thus trigger callbacks. + * + * @remark The \c linger.ms time will be ignored for the duration of the call, + * queued messages will be sent to the broker as soon as possible. + * + * @remark If RD_KAFKA_EVENT_DR has been enabled + * (through rd_kafka_conf_set_events()) this function will not call + * rd_kafka_poll() but instead wait for the librdkafka-handled + * message count to reach zero. This requires the application to + * serve the event queue in a separate thread. + * In this mode only messages are counted, not other types of + * queued events. + * + * @returns RD_KAFKA_RESP_ERR__TIMED_OUT if \p timeout_ms was reached before all + * outstanding requests were completed, else RD_KAFKA_RESP_ERR_NO_ERROR + * + * @sa rd_kafka_outq_len() + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_flush (rd_kafka_t *rk, int timeout_ms); + + + +/** + * @brief Purge messages currently handled by the producer instance. + * + * @param rk Client instance. + * @param purge_flags Tells which messages to purge and how. + * + * The application will need to call rd_kafka_poll() or rd_kafka_flush() + * afterwards to serve the delivery report callbacks of the purged messages. + * + * Messages purged from internal queues fail with the delivery report + * error code set to RD_KAFKA_RESP_ERR__PURGE_QUEUE, while purged messages that + * are in-flight to or from the broker will fail with the error code set to + * RD_KAFKA_RESP_ERR__PURGE_INFLIGHT. + * + * @warning Purging messages that are in-flight to or from the broker + * will ignore any sub-sequent acknowledgement for these messages + * received from the broker, effectively making it impossible + * for the application to know if the messages were successfully + * produced or not. This may result in duplicate messages if the + * application retries these messages at a later time. + * + * @remark This call may block for a short time while background thread + * queues are purged. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, + * RD_KAFKA_RESP_ERR__INVALID_ARG if the \p purge flags are invalid + * or unknown, + * RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED if called on a non-producer + * client instance. + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_purge (rd_kafka_t *rk, int purge_flags); + + +/** + * @brief Flags for rd_kafka_purge() + */ + +/*! + * Purge messages in internal queues. + */ +#define RD_KAFKA_PURGE_F_QUEUE 0x1 + +/*! + * Purge messages in-flight to or from the broker. + * Purging these messages will void any future acknowledgements from the + * broker, making it impossible for the application to know if these + * messages were successfully delivered or not. + * Retrying these messages may lead to duplicates. + */ +#define RD_KAFKA_PURGE_F_INFLIGHT 0x2 + + +/*! + * Don't wait for background thread queue purging to finish. + */ +#define RD_KAFKA_PURGE_F_NON_BLOCKING 0x4 + + +/**@}*/ + + +/** +* @name Metadata API +* @{ +* +* +*/ + + +/** + * @brief Broker information + */ +typedef struct rd_kafka_metadata_broker { + int32_t id; /**< Broker Id */ + char *host; /**< Broker hostname */ + int port; /**< Broker listening port */ +} rd_kafka_metadata_broker_t; + +/** + * @brief Partition information + */ +typedef struct rd_kafka_metadata_partition { + int32_t id; /**< Partition Id */ + rd_kafka_resp_err_t err; /**< Partition error reported by broker */ + int32_t leader; /**< Leader broker */ + int replica_cnt; /**< Number of brokers in \p replicas */ + int32_t *replicas; /**< Replica brokers */ + int isr_cnt; /**< Number of ISR brokers in \p isrs */ + int32_t *isrs; /**< In-Sync-Replica brokers */ +} rd_kafka_metadata_partition_t; + +/** + * @brief Topic information + */ +typedef struct rd_kafka_metadata_topic { + char *topic; /**< Topic name */ + int partition_cnt; /**< Number of partitions in \p partitions*/ + struct rd_kafka_metadata_partition *partitions; /**< Partitions */ + rd_kafka_resp_err_t err; /**< Topic error reported by broker */ +} rd_kafka_metadata_topic_t; + + +/** + * @brief Metadata container + */ +typedef struct rd_kafka_metadata { + int broker_cnt; /**< Number of brokers in \p brokers */ + struct rd_kafka_metadata_broker *brokers; /**< Brokers */ + + int topic_cnt; /**< Number of topics in \p topics */ + struct rd_kafka_metadata_topic *topics; /**< Topics */ + + int32_t orig_broker_id; /**< Broker originating this metadata */ + char *orig_broker_name; /**< Name of originating broker */ +} rd_kafka_metadata_t; + + +/** + * @brief Request Metadata from broker. + * + * Parameters: + * - \p all_topics if non-zero: request info about all topics in cluster, + * if zero: only request info about locally known topics. + * - \p only_rkt only request info about this topic + * - \p metadatap pointer to hold metadata result. + * The \p *metadatap pointer must be released + * with rd_kafka_metadata_destroy(). + * - \p timeout_ms maximum response time before failing. + * + * @remark Consumer: If \p all_topics is non-zero the Metadata response + * information may trigger a re-join if any subscribed topics + * have changed partition count or existence state. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success (in which case *metadatap) + * will be set, else RD_KAFKA_RESP_ERR__TIMED_OUT on timeout or + * other error code on error. + */ +RD_EXPORT +rd_kafka_resp_err_t +rd_kafka_metadata (rd_kafka_t *rk, int all_topics, + rd_kafka_topic_t *only_rkt, + const struct rd_kafka_metadata **metadatap, + int timeout_ms); + +/** + * @brief Release metadata memory. + */ +RD_EXPORT +void rd_kafka_metadata_destroy(const struct rd_kafka_metadata *metadata); + + +/**@}*/ + + + +/** +* @name Client group information +* @{ +* +* +*/ + + +/** + * @brief Group member information + * + * For more information on \p member_metadata format, see + * https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-GroupMembershipAPI + * + */ +struct rd_kafka_group_member_info { + char *member_id; /**< Member id (generated by broker) */ + char *client_id; /**< Client's \p client.id */ + char *client_host; /**< Client's hostname */ + void *member_metadata; /**< Member metadata (binary), + * format depends on \p protocol_type. */ + int member_metadata_size; /**< Member metadata size in bytes */ + void *member_assignment; /**< Member assignment (binary), + * format depends on \p protocol_type. */ + int member_assignment_size; /**< Member assignment size in bytes */ +}; + +/** + * @brief Group information + */ +struct rd_kafka_group_info { + struct rd_kafka_metadata_broker broker; /**< Originating broker info */ + char *group; /**< Group name */ + rd_kafka_resp_err_t err; /**< Broker-originated error */ + char *state; /**< Group state */ + char *protocol_type; /**< Group protocol type */ + char *protocol; /**< Group protocol */ + struct rd_kafka_group_member_info *members; /**< Group members */ + int member_cnt; /**< Group member count */ +}; + +/** + * @brief List of groups + * + * @sa rd_kafka_group_list_destroy() to release list memory. + */ +struct rd_kafka_group_list { + struct rd_kafka_group_info *groups; /**< Groups */ + int group_cnt; /**< Group count */ +}; + + +/** + * @brief List and describe client groups in cluster. + * + * \p group is an optional group name to describe, otherwise (\p NULL) all + * groups are returned. + * + * \p timeout_ms is the (approximate) maximum time to wait for response + * from brokers and must be a positive value. + * + * @returns \c RD_KAFKA_RESP_ERR__NO_ERROR on success and \p grplistp is + * updated to point to a newly allocated list of groups. + * \c RD_KAFKA_RESP_ERR__PARTIAL if not all brokers responded + * in time but at least one group is returned in \p grplistlp. + * \c RD_KAFKA_RESP_ERR__TIMED_OUT if no groups were returned in the + * given timeframe but not all brokers have yet responded, or + * if the list of brokers in the cluster could not be obtained within + * the given timeframe. + * \c RD_KAFKA_RESP_ERR__TRANSPORT if no brokers were found. + * Other error codes may also be returned from the request layer. + * + * The \p grplistp remains untouched if any error code is returned, + * with the exception of RD_KAFKA_RESP_ERR__PARTIAL which behaves + * as RD_KAFKA_RESP_ERR__NO_ERROR (success) but with an incomplete + * group list. + * + * @sa Use rd_kafka_group_list_destroy() to release list memory. + */ +RD_EXPORT +rd_kafka_resp_err_t +rd_kafka_list_groups (rd_kafka_t *rk, const char *group, + const struct rd_kafka_group_list **grplistp, + int timeout_ms); + +/** + * @brief Release list memory + */ +RD_EXPORT +void rd_kafka_group_list_destroy (const struct rd_kafka_group_list *grplist); + + +/**@}*/ + + + +/** + * @name Miscellaneous APIs + * @{ + * + */ + + +/** + * @brief Adds one or more brokers to the kafka handle's list of initial + * bootstrap brokers. + * + * Additional brokers will be discovered automatically as soon as rdkafka + * connects to a broker by querying the broker metadata. + * + * If a broker name resolves to multiple addresses (and possibly + * address families) all will be used for connection attempts in + * round-robin fashion. + * + * \p brokerlist is a ,-separated list of brokers in the format: + * \c \,\,.. + * Where each broker is in either the host or URL based format: + * \c \[:\] + * \c \://\[:port] + * \c \ is either \c PLAINTEXT, \c SSL, \c SASL, \c SASL_PLAINTEXT + * The two formats can be mixed but ultimately the value of the + * `security.protocol` config property decides what brokers are allowed. + * + * Example: + * brokerlist = "broker1:10000,broker2" + * brokerlist = "SSL://broker3:9000,ssl://broker2" + * + * @returns the number of brokers successfully added. + * + * @remark Brokers may also be defined with the \c metadata.broker.list or + * \c bootstrap.servers configuration property (preferred method). + * + * @deprecated Set bootstrap servers with the \c bootstrap.servers + * configuration property. + */ +RD_EXPORT +int rd_kafka_brokers_add(rd_kafka_t *rk, const char *brokerlist); + + + + +/** + * @brief Set logger function. + * + * The default is to print to stderr, but a syslog logger is also available, + * see rd_kafka_log_(print|syslog) for the builtin alternatives. + * Alternatively the application may provide its own logger callback. + * Or pass 'func' as NULL to disable logging. + * + * @deprecated Use rd_kafka_conf_set_log_cb() + * + * @remark \p rk may be passed as NULL in the callback. + */ +RD_EXPORT RD_DEPRECATED +void rd_kafka_set_logger(rd_kafka_t *rk, + void (*func) (const rd_kafka_t *rk, int level, + const char *fac, const char *buf)); + + +/** + * @brief Specifies the maximum logging level emitted by + * internal kafka logging and debugging. + * + * @deprecated Set the \c "log_level" configuration property instead. + * + * @remark If the \p \"debug\" configuration property is set the log level is + * automatically adjusted to \c LOG_DEBUG (7). + */ +RD_EXPORT +void rd_kafka_set_log_level(rd_kafka_t *rk, int level); + + +/** + * @brief Builtin (default) log sink: print to stderr + */ +RD_EXPORT +void rd_kafka_log_print(const rd_kafka_t *rk, int level, + const char *fac, const char *buf); + + +/** + * @brief Builtin log sink: print to syslog. + * @remark This logger is only available if librdkafka was built + * with syslog support. + */ +RD_EXPORT +void rd_kafka_log_syslog(const rd_kafka_t *rk, int level, + const char *fac, const char *buf); + + +/** + * @brief Returns the current out queue length. + * + * The out queue length is the sum of: + * - number of messages waiting to be sent to, or acknowledged by, + * the broker. + * - number of delivery reports (e.g., dr_msg_cb) waiting to be served + * by rd_kafka_poll() or rd_kafka_flush(). + * - number of callbacks (e.g., error_cb, stats_cb, etc) waiting to be + * served by rd_kafka_poll(), rd_kafka_consumer_poll() or rd_kafka_flush(). + * - number of events waiting to be served by background_event_cb() in + * the background queue (see rd_kafka_conf_set_background_event_cb). + * + * An application should wait for the return value of this function to reach + * zero before terminating to make sure outstanding messages, + * requests (such as offset commits), callbacks and events are fully processed. + * See rd_kafka_flush(). + * + * @returns number of messages and events waiting in queues. + * + * @sa rd_kafka_flush() + */ +RD_EXPORT +int rd_kafka_outq_len(rd_kafka_t *rk); + + + +/** + * @brief Dumps rdkafka's internal state for handle \p rk to stream \p fp + * + * This is only useful for debugging rdkafka, showing state and statistics + * for brokers, topics, partitions, etc. + */ +RD_EXPORT +void rd_kafka_dump(FILE *fp, rd_kafka_t *rk); + + + +/** + * @brief Retrieve the current number of threads in use by librdkafka. + * + * Used by regression tests. + */ +RD_EXPORT +int rd_kafka_thread_cnt(void); + + +/** + * @enum rd_kafka_thread_type_t + * + * @brief librdkafka internal thread type. + * + * @sa rd_kafka_interceptor_add_on_thread_start() + */ +typedef enum rd_kafka_thread_type_t { + RD_KAFKA_THREAD_MAIN, /**< librdkafka's internal main thread */ + RD_KAFKA_THREAD_BACKGROUND, /**< Background thread (if enabled) */ + RD_KAFKA_THREAD_BROKER /**< Per-broker thread */ +} rd_kafka_thread_type_t; + + +/** + * @brief Wait for all rd_kafka_t objects to be destroyed. + * + * Returns 0 if all kafka objects are now destroyed, or -1 if the + * timeout was reached. + * + * @remark This function is deprecated. + */ +RD_EXPORT +int rd_kafka_wait_destroyed(int timeout_ms); + + +/** + * @brief Run librdkafka's built-in unit-tests. + * + * @returns the number of failures, or 0 if all tests passed. + */ +RD_EXPORT +int rd_kafka_unittest (void); + + +/**@}*/ + + + + +/** + * @name Experimental APIs + * @{ + */ + +/** + * @brief Redirect the main (rd_kafka_poll()) queue to the KafkaConsumer's + * queue (rd_kafka_consumer_poll()). + * + * @warning It is not permitted to call rd_kafka_poll() after directing the + * main queue with rd_kafka_poll_set_consumer(). + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_poll_set_consumer (rd_kafka_t *rk); + + +/**@}*/ + +/** + * @name Event interface + * + * @brief The event API provides an alternative pollable non-callback interface + * to librdkafka's message and event queues. + * + * @{ + */ + + +/** + * @brief Event types + */ +typedef int rd_kafka_event_type_t; +#define RD_KAFKA_EVENT_NONE 0x0 /**< Unset value */ +#define RD_KAFKA_EVENT_DR 0x1 /**< Producer Delivery report batch */ +#define RD_KAFKA_EVENT_FETCH 0x2 /**< Fetched message (consumer) */ +#define RD_KAFKA_EVENT_LOG 0x4 /**< Log message */ +#define RD_KAFKA_EVENT_ERROR 0x8 /**< Error */ +#define RD_KAFKA_EVENT_REBALANCE 0x10 /**< Group rebalance (consumer) */ +#define RD_KAFKA_EVENT_OFFSET_COMMIT 0x20 /**< Offset commit result */ +#define RD_KAFKA_EVENT_STATS 0x40 /**< Stats */ +#define RD_KAFKA_EVENT_CREATETOPICS_RESULT 100 /**< CreateTopics_result_t */ +#define RD_KAFKA_EVENT_DELETETOPICS_RESULT 101 /**< DeleteTopics_result_t */ +#define RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT 102 /**< CreatePartitions_result_t */ +#define RD_KAFKA_EVENT_ALTERCONFIGS_RESULT 103 /**< AlterConfigs_result_t */ +#define RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT 104 /**< DescribeConfigs_result_t */ +#define RD_KAFKA_EVENT_DELETERECORDS_RESULT 105 /**< DeleteRecords_result_t */ +#define RD_KAFKA_EVENT_DELETEGROUPS_RESULT 106 /**< DeleteGroups_result_t */ +/** DeleteConsumerGroupOffsets_result_t */ +#define RD_KAFKA_EVENT_DELETECONSUMERGROUPOFFSETS_RESULT 107 +#define RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH 0x100 /**< SASL/OAUTHBEARER + token needs to be + refreshed */ + + +/** + * @returns the event type for the given event. + * + * @remark As a convenience it is okay to pass \p rkev as NULL in which case + * RD_KAFKA_EVENT_NONE is returned. + */ +RD_EXPORT +rd_kafka_event_type_t rd_kafka_event_type (const rd_kafka_event_t *rkev); + +/** + * @returns the event type's name for the given event. + * + * @remark As a convenience it is okay to pass \p rkev as NULL in which case + * the name for RD_KAFKA_EVENT_NONE is returned. + */ +RD_EXPORT +const char *rd_kafka_event_name (const rd_kafka_event_t *rkev); + + +/** + * @brief Destroy an event. + * + * @remark Any references to this event, such as extracted messages, + * will not be usable after this call. + * + * @remark As a convenience it is okay to pass \p rkev as NULL in which case + * no action is performed. + */ +RD_EXPORT +void rd_kafka_event_destroy (rd_kafka_event_t *rkev); + + +/** + * @returns the next message from an event. + * + * Call repeatedly until it returns NULL. + * + * Event types: + * - RD_KAFKA_EVENT_FETCH (1 message) + * - RD_KAFKA_EVENT_DR (>=1 message(s)) + * + * @remark The returned message(s) MUST NOT be + * freed with rd_kafka_message_destroy(). + * + * @remark on_consume() interceptor may be called + * from this function prior to passing message to application. + */ +RD_EXPORT +const rd_kafka_message_t *rd_kafka_event_message_next (rd_kafka_event_t *rkev); + + +/** + * @brief Extacts \p size message(s) from the event into the + * pre-allocated array \p rkmessages. + * + * Event types: + * - RD_KAFKA_EVENT_FETCH (1 message) + * - RD_KAFKA_EVENT_DR (>=1 message(s)) + * + * @returns the number of messages extracted. + * + * @remark on_consume() interceptor may be called + * from this function prior to passing message to application. + */ +RD_EXPORT +size_t rd_kafka_event_message_array (rd_kafka_event_t *rkev, + const rd_kafka_message_t **rkmessages, + size_t size); + + +/** + * @returns the number of remaining messages in the event. + * + * Event types: + * - RD_KAFKA_EVENT_FETCH (1 message) + * - RD_KAFKA_EVENT_DR (>=1 message(s)) + */ +RD_EXPORT +size_t rd_kafka_event_message_count (rd_kafka_event_t *rkev); + + +/** + * @returns the associated configuration string for the event, or NULL + * if the configuration property is not set or if + * not applicable for the given event type. + * + * The returned memory is read-only and its lifetime is the same as the + * event object. + * + * Event types: + * - RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH: value of sasl.oauthbearer.config + */ +RD_EXPORT +const char *rd_kafka_event_config_string (rd_kafka_event_t *rkev); + + +/** + * @returns the error code for the event. + * + * Use rd_kafka_event_error_is_fatal() to detect if this is a fatal error. + * + * Event types: + * - all + */ +RD_EXPORT +rd_kafka_resp_err_t rd_kafka_event_error (rd_kafka_event_t *rkev); + + +/** + * @returns the error string (if any). + * An application should check that rd_kafka_event_error() returns + * non-zero before calling this function. + * + * Event types: + * - all + */ +RD_EXPORT +const char *rd_kafka_event_error_string (rd_kafka_event_t *rkev); + + +/** + * @returns 1 if the error is a fatal error, else 0. + * + * Event types: + * - RD_KAFKA_EVENT_ERROR + * + * @sa rd_kafka_fatal_error() + */ +RD_EXPORT +int rd_kafka_event_error_is_fatal (rd_kafka_event_t *rkev); + + +/** + * @returns the event opaque (if any) as passed to rd_kafka_commit() (et.al) or + * rd_kafka_AdminOptions_set_opaque(), depending on event type. + * + * Event types: + * - RD_KAFKA_EVENT_OFFSET_COMMIT + * - RD_KAFKA_EVENT_CREATETOPICS_RESULT + * - RD_KAFKA_EVENT_DELETETOPICS_RESULT + * - RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT + * - RD_KAFKA_EVENT_ALTERCONFIGS_RESULT + * - RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT + * - RD_KAFKA_EVENT_DELETEGROUPS_RESULT + * - RD_KAFKA_EVENT_DELETECONSUMERGROUPOFFSETS_RESULT + * - RD_KAFKA_EVENT_DELETERECORDS_RESULT + */ +RD_EXPORT +void *rd_kafka_event_opaque (rd_kafka_event_t *rkev); + + +/** + * @brief Extract log message from the event. + * + * Event types: + * - RD_KAFKA_EVENT_LOG + * + * @returns 0 on success or -1 if unsupported event type. + */ +RD_EXPORT +int rd_kafka_event_log (rd_kafka_event_t *rkev, + const char **fac, const char **str, int *level); + + +/** + * @brief Extract log debug context from event. + * + * Event types: + * - RD_KAFKA_EVENT_LOG + * + * @param rkev the event to extract data from. + * @param dst destination string for comma separated list. + * @param dstsize size of provided dst buffer. + * @returns 0 on success or -1 if unsupported event type. + */ +RD_EXPORT +int rd_kafka_event_debug_contexts (rd_kafka_event_t *rkev, + char *dst, size_t dstsize); + + +/** + * @brief Extract stats from the event. + * + * Event types: + * - RD_KAFKA_EVENT_STATS + * + * @returns stats json string. + * + * @remark the returned string will be freed automatically along with the event object + * + */ +RD_EXPORT +const char *rd_kafka_event_stats (rd_kafka_event_t *rkev); + + +/** + * @returns the topic partition list from the event. + * + * @remark The list MUST NOT be freed with rd_kafka_topic_partition_list_destroy() + * + * Event types: + * - RD_KAFKA_EVENT_REBALANCE + * - RD_KAFKA_EVENT_OFFSET_COMMIT + */ +RD_EXPORT rd_kafka_topic_partition_list_t * +rd_kafka_event_topic_partition_list (rd_kafka_event_t *rkev); + + +/** + * @returns a newly allocated topic_partition container, if applicable for the event type, + * else NULL. + * + * @remark The returned pointer MUST be freed with rd_kafka_topic_partition_destroy(). + * + * Event types: + * RD_KAFKA_EVENT_ERROR (for partition level errors) + */ +RD_EXPORT rd_kafka_topic_partition_t * +rd_kafka_event_topic_partition (rd_kafka_event_t *rkev); + + +/*! CreateTopics result type */ +typedef rd_kafka_event_t rd_kafka_CreateTopics_result_t; +/*! DeleteTopics result type */ +typedef rd_kafka_event_t rd_kafka_DeleteTopics_result_t; +/*! CreatePartitions result type */ +typedef rd_kafka_event_t rd_kafka_CreatePartitions_result_t; +/*! AlterConfigs result type */ +typedef rd_kafka_event_t rd_kafka_AlterConfigs_result_t; +/*! CreateTopics result type */ +typedef rd_kafka_event_t rd_kafka_DescribeConfigs_result_t; +/*! DeleteRecords result type */ +typedef rd_kafka_event_t rd_kafka_DeleteRecords_result_t; +/*! DeleteGroups result type */ +typedef rd_kafka_event_t rd_kafka_DeleteGroups_result_t; +/*! DeleteConsumerGroupOffsets result type */ +typedef rd_kafka_event_t rd_kafka_DeleteConsumerGroupOffsets_result_t; + +/** + * @brief Get CreateTopics result. + * + * @returns the result of a CreateTopics request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_CREATETOPICS_RESULT + */ +RD_EXPORT const rd_kafka_CreateTopics_result_t * +rd_kafka_event_CreateTopics_result (rd_kafka_event_t *rkev); + +/** + * @brief Get DeleteTopics result. + * + * @returns the result of a DeleteTopics request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_DELETETOPICS_RESULT + */ +RD_EXPORT const rd_kafka_DeleteTopics_result_t * +rd_kafka_event_DeleteTopics_result (rd_kafka_event_t *rkev); + +/** + * @brief Get CreatePartitions result. + * + * @returns the result of a CreatePartitions request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT + */ +RD_EXPORT const rd_kafka_CreatePartitions_result_t * +rd_kafka_event_CreatePartitions_result (rd_kafka_event_t *rkev); + +/** + * @brief Get AlterConfigs result. + * + * @returns the result of a AlterConfigs request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_ALTERCONFIGS_RESULT + */ +RD_EXPORT const rd_kafka_AlterConfigs_result_t * +rd_kafka_event_AlterConfigs_result (rd_kafka_event_t *rkev); + +/** + * @brief Get DescribeConfigs result. + * + * @returns the result of a DescribeConfigs request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_DESCRIBECONFIGS_RESULT + */ +RD_EXPORT const rd_kafka_DescribeConfigs_result_t * +rd_kafka_event_DescribeConfigs_result (rd_kafka_event_t *rkev); + +/** + * @returns the result of a DeleteRecords request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_DELETERECORDS_RESULT + */ +RD_EXPORT const rd_kafka_DeleteRecords_result_t * +rd_kafka_event_DeleteRecords_result (rd_kafka_event_t *rkev); + +/** + * @brief Get DeleteGroups result. + * + * @returns the result of a DeleteGroups request, or NULL if event is of + * different type. + * + * Event types: + * RD_KAFKA_EVENT_DELETEGROUPS_RESULT + */ +RD_EXPORT const rd_kafka_DeleteGroups_result_t * +rd_kafka_event_DeleteGroups_result (rd_kafka_event_t *rkev); + +/** + * @brief Get DeleteConsumerGroupOffsets result. + * + * @returns the result of a DeleteConsumerGroupOffsets request, or NULL if + * event is of different type. + * + * Event types: + * RD_KAFKA_EVENT_DELETECONSUMERGROUPOFFSETS_RESULT + */ +RD_EXPORT const rd_kafka_DeleteConsumerGroupOffsets_result_t * +rd_kafka_event_DeleteConsumerGroupOffsets_result (rd_kafka_event_t *rkev); + +/** + * @brief Poll a queue for an event for max \p timeout_ms. + * + * @returns an event, or NULL. + * + * @remark Use rd_kafka_event_destroy() to free the event. + * + * @sa rd_kafka_conf_set_background_event_cb() + */ +RD_EXPORT +rd_kafka_event_t *rd_kafka_queue_poll (rd_kafka_queue_t *rkqu, int timeout_ms); + +/** +* @brief Poll a queue for events served through callbacks for max \p timeout_ms. +* +* @returns the number of events served. +* +* @remark This API must only be used for queues with callbacks registered +* for all expected event types. E.g., not a message queue. +* +* @remark Also see rd_kafka_conf_set_background_event_cb() for triggering +* event callbacks from a librdkafka-managed background thread. +* +* @sa rd_kafka_conf_set_background_event_cb() +*/ +RD_EXPORT +int rd_kafka_queue_poll_callback (rd_kafka_queue_t *rkqu, int timeout_ms); + + +/**@}*/ + + +/** + * @name Plugin interface + * + * @brief A plugin interface that allows external runtime-loaded libraries + * to integrate with a client instance without modifications to + * the application code. + * + * Plugins are loaded when referenced through the `plugin.library.paths` + * configuration property and operates on the \c rd_kafka_conf_t + * object prior \c rd_kafka_t instance creation. + * + * @warning Plugins require the application to link librdkafka dynamically + * and not statically. Failure to do so will lead to missing symbols + * or finding symbols in another librdkafka library than the + * application was linked with. + */ + + +/** + * @brief Plugin's configuration initializer method called each time the + * library is referenced from configuration (even if previously loaded by + * another client instance). + * + * @remark This method MUST be implemented by plugins and have the symbol name + * \c conf_init + * + * @param conf Configuration set up to this point. + * @param plug_opaquep Plugin can set this pointer to a per-configuration + * opaque pointer. + * @param errstr String buffer of size \p errstr_size where plugin must write + * a human readable error string in the case the initializer + * fails (returns non-zero). + * @param errstr_size Maximum space (including \0) in \p errstr. + * + * @remark A plugin may add an on_conf_destroy() interceptor to clean up + * plugin-specific resources created in the plugin's conf_init() method. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code on error. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_plugin_f_conf_init_t) (rd_kafka_conf_t *conf, + void **plug_opaquep, + char *errstr, size_t errstr_size); + +/**@}*/ + + + +/** + * @name Interceptors + * + * @{ + * + * @brief A callback interface that allows message interception for both + * producer and consumer data pipelines. + * + * Except for the on_new(), on_conf_set(), on_conf_dup() and on_conf_destroy() + * interceptors, interceptors are added to the + * newly created rd_kafka_t client instance. These interceptors MUST only + * be added from on_new() and MUST NOT be added after rd_kafka_new() returns. + * + * The on_new(), on_conf_set(), on_conf_dup() and on_conf_destroy() interceptors + * are added to the configuration object which is later passed to + * rd_kafka_new() where on_new() is called to allow addition of + * other interceptors. + * + * Each interceptor reference consists of a display name (ic_name), + * a callback function, and an application-specified opaque value that is + * passed as-is to the callback. + * The ic_name must be unique for the interceptor implementation and is used + * to reject duplicate interceptor methods. + * + * Any number of interceptors can be added and they are called in the order + * they were added, unless otherwise noted. + * The list of registered interceptor methods are referred to as + * interceptor chains. + * + * @remark Contrary to the Java client the librdkafka interceptor interface + * does not support message key and value modification. + * Message mutability is discouraged in the Java client and the + * combination of serializers and headers cover most use-cases. + * + * @remark Interceptors are NOT copied to the new configuration on + * rd_kafka_conf_dup() since it would be hard for interceptors to + * track usage of the interceptor's opaque value. + * An interceptor should rely on the plugin, which will be copied + * in rd_kafka_conf_conf_dup(), to set up the initial interceptors. + * An interceptor should implement the on_conf_dup() method + * to manually set up its internal configuration on the newly created + * configuration object that is being copied-to based on the + * interceptor-specific configuration properties. + * conf_dup() should thus be treated the same as conf_init(). + * + * @remark Interceptors are keyed by the interceptor type (on_..()), the + * interceptor name (ic_name) and the interceptor method function. + * Duplicates are not allowed and the .._add_on_..() method will + * return RD_KAFKA_RESP_ERR__CONFLICT if attempting to add a duplicate + * method. + * The only exception is on_conf_destroy() which may be added multiple + * times by the same interceptor to allow proper cleanup of + * interceptor configuration state. + */ + + +/** + * @brief on_conf_set() is called from rd_kafka_*_conf_set() in the order + * the interceptors were added. + * + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * @param name The configuration property to set. + * @param val The configuration value to set, or NULL for reverting to default + * in which case the previous value should be freed. + * @param errstr A human readable error string in case the interceptor fails. + * @param errstr_size Maximum space (including \0) in \p errstr. + * + * @returns RD_KAFKA_CONF_OK if the property was known and successfully + * handled by the interceptor, RD_KAFKA_CONF_INVALID if the + * property was handled by the interceptor but the value was invalid, + * or RD_KAFKA_CONF_UNKNOWN if the interceptor did not handle + * this property, in which case the property is passed on on the + * interceptor in the chain, finally ending up at the built-in + * configuration handler. + */ +typedef rd_kafka_conf_res_t +(rd_kafka_interceptor_f_on_conf_set_t) (rd_kafka_conf_t *conf, + const char *name, const char *val, + char *errstr, size_t errstr_size, + void *ic_opaque); + + +/** + * @brief on_conf_dup() is called from rd_kafka_conf_dup() in the + * order the interceptors were added and is used to let + * an interceptor re-register its conf interecptors with a new + * opaque value. + * The on_conf_dup() method is called prior to the configuration from + * \p old_conf being copied to \p new_conf. + * + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an error code + * on failure (which is logged but otherwise ignored). + * + * @remark No on_conf_* interceptors are copied to the new configuration + * object on rd_kafka_conf_dup(). + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_conf_dup_t) (rd_kafka_conf_t *new_conf, + const rd_kafka_conf_t *old_conf, + size_t filter_cnt, + const char **filter, + void *ic_opaque); + + +/** + * @brief on_conf_destroy() is called from rd_kafka_*_conf_destroy() in the + * order the interceptors were added. + * + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_conf_destroy_t) (void *ic_opaque); + + +/** + * @brief on_new() is called from rd_kafka_new() prior toreturning + * the newly created client instance to the application. + * + * @param rk The client instance. + * @param conf The client instance's final configuration. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * @param errstr A human readable error string in case the interceptor fails. + * @param errstr_size Maximum space (including \0) in \p errstr. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + * + * @warning The \p rk client instance will not be fully set up when this + * interceptor is called and the interceptor MUST NOT call any + * other rk-specific APIs than rd_kafka_interceptor_add..(). + * + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_new_t) (rd_kafka_t *rk, const rd_kafka_conf_t *conf, + void *ic_opaque, + char *errstr, size_t errstr_size); + + +/** + * @brief on_destroy() is called from rd_kafka_destroy() or (rd_kafka_new() + * if rd_kafka_new() fails during initialization). + * + * @param rk The client instance. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_destroy_t) (rd_kafka_t *rk, void *ic_opaque); + + + + +/** + * @brief on_send() is called from rd_kafka_produce*() (et.al) prior to + * the partitioner being called. + * + * @param rk The client instance. + * @param rkmessage The message being produced. Immutable. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @remark This interceptor is only used by producer instances. + * + * @remark The \p rkmessage object is NOT mutable and MUST NOT be modified + * by the interceptor. + * + * @remark If the partitioner fails or an unknown partition was specified, + * the on_acknowledgement() interceptor chain will be called from + * within the rd_kafka_produce*() call to maintain send-acknowledgement + * symmetry. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_send_t) (rd_kafka_t *rk, + rd_kafka_message_t *rkmessage, + void *ic_opaque); + +/** + * @brief on_acknowledgement() is called to inform interceptors that a message + * was succesfully delivered or permanently failed delivery. + * The interceptor chain is called from internal librdkafka background + * threads, or rd_kafka_produce*() if the partitioner failed. + * + * @param rk The client instance. + * @param rkmessage The message being produced. Immutable. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @remark This interceptor is only used by producer instances. + * + * @remark The \p rkmessage object is NOT mutable and MUST NOT be modified + * by the interceptor. + * + * @warning The on_acknowledgement() method may be called from internal + * librdkafka threads. An on_acknowledgement() interceptor MUST NOT + * call any librdkafka API's associated with the \p rk, or perform + * any blocking or prolonged work. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_acknowledgement_t) (rd_kafka_t *rk, + rd_kafka_message_t *rkmessage, + void *ic_opaque); + + +/** + * @brief on_consume() is called just prior to passing the message to the + * application in rd_kafka_consumer_poll(), rd_kafka_consume*(), + * the event interface, etc. + * + * @param rk The client instance. + * @param rkmessage The message being consumed. Immutable. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @remark This interceptor is only used by consumer instances. + * + * @remark The \p rkmessage object is NOT mutable and MUST NOT be modified + * by the interceptor. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_consume_t) (rd_kafka_t *rk, + rd_kafka_message_t *rkmessage, + void *ic_opaque); + +/** + * @brief on_commit() is called on completed or failed offset commit. + * It is called from internal librdkafka threads. + * + * @param rk The client instance. + * @param offsets List of topic+partition+offset+error that were committed. + * The error message of each partition should be checked for + * error. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @remark This interceptor is only used by consumer instances. + * + * @warning The on_commit() interceptor is called from internal + * librdkafka threads. An on_commit() interceptor MUST NOT + * call any librdkafka API's associated with the \p rk, or perform + * any blocking or prolonged work. + * + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_commit_t) ( + rd_kafka_t *rk, + const rd_kafka_topic_partition_list_t *offsets, + rd_kafka_resp_err_t err, void *ic_opaque); + + +/** + * @brief on_request_sent() is called when a request has been fully written + * to a broker TCP connections socket. + * + * @param rk The client instance. + * @param sockfd Socket file descriptor. + * @param brokername Broker request is being sent to. + * @param brokerid Broker request is being sent to. + * @param ApiKey Kafka protocol request type. + * @param ApiVersion Kafka protocol request type version. + * @param Corrid Kafka protocol request correlation id. + * @param size Size of request. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @warning The on_request_sent() interceptor is called from internal + * librdkafka broker threads. An on_request_sent() interceptor MUST NOT + * call any librdkafka API's associated with the \p rk, or perform + * any blocking or prolonged work. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_request_sent_t) ( + rd_kafka_t *rk, + int sockfd, + const char *brokername, + int32_t brokerid, + int16_t ApiKey, + int16_t ApiVersion, + int32_t CorrId, + size_t size, + void *ic_opaque); + + +/** + * @brief on_response_received() is called when a protocol response has been + * fully received from a broker TCP connection socket but before the + * response payload is parsed. + * + * @param rk The client instance. + * @param sockfd Socket file descriptor (always -1). + * @param brokername Broker response was received from, possibly empty string + * on error. + * @param brokerid Broker response was received from. + * @param ApiKey Kafka protocol request type or -1 on error. + * @param ApiVersion Kafka protocol request type version or -1 on error. + * @param Corrid Kafka protocol request correlation id, possibly -1 on error. + * @param size Size of response, possibly 0 on error. + * @param rtt Request round-trip-time in microseconds, possibly -1 on error. + * @param err Receive error. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @warning The on_response_received() interceptor is called from internal + * librdkafka broker threads. An on_response_received() interceptor + * MUST NOT call any librdkafka API's associated with the \p rk, or + * perform any blocking or prolonged work. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_response_received_t) ( + rd_kafka_t *rk, + int sockfd, + const char *brokername, + int32_t brokerid, + int16_t ApiKey, + int16_t ApiVersion, + int32_t CorrId, + size_t size, + int64_t rtt, + rd_kafka_resp_err_t err, + void *ic_opaque); + + +/** + * @brief on_thread_start() is called from a newly created librdkafka-managed + * thread. + + * @param rk The client instance. + * @param thread_type Thread type. + * @param thread_name Human-readable thread name, may not be unique. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @warning The on_thread_start() interceptor is called from internal + * librdkafka threads. An on_thread_start() interceptor MUST NOT + * call any librdkafka API's associated with the \p rk, or perform + * any blocking or prolonged work. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_thread_start_t) ( + rd_kafka_t *rk, + rd_kafka_thread_type_t thread_type, + const char *thread_name, + void *ic_opaque); + + +/** + * @brief on_thread_exit() is called just prior to a librdkafka-managed + * thread exiting from the exiting thread itself. + * + * @param rk The client instance. + * @param thread_type Thread type.n + * @param thread_name Human-readable thread name, may not be unique. + * @param ic_opaque The interceptor's opaque pointer specified in ..add..(). + * + * @remark Depending on the thread type, librdkafka may execute additional + * code on the thread after on_thread_exit() returns. + * + * @warning The on_thread_exit() interceptor is called from internal + * librdkafka threads. An on_thread_exit() interceptor MUST NOT + * call any librdkafka API's associated with the \p rk, or perform + * any blocking or prolonged work. + * + * @returns an error code on failure, the error is logged but otherwise ignored. + */ +typedef rd_kafka_resp_err_t +(rd_kafka_interceptor_f_on_thread_exit_t) ( + rd_kafka_t *rk, + rd_kafka_thread_type_t thread_type, + const char *thread_name, + void *ic_opaque); + + + +/** + * @brief Append an on_conf_set() interceptor. + * + * @param conf Configuration object. + * @param ic_name Interceptor name, used in logging. + * @param on_conf_set Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_conf_interceptor_add_on_conf_set ( + rd_kafka_conf_t *conf, const char *ic_name, + rd_kafka_interceptor_f_on_conf_set_t *on_conf_set, + void *ic_opaque); + + +/** + * @brief Append an on_conf_dup() interceptor. + * + * @param conf Configuration object. + * @param ic_name Interceptor name, used in logging. + * @param on_conf_dup Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_conf_interceptor_add_on_conf_dup ( + rd_kafka_conf_t *conf, const char *ic_name, + rd_kafka_interceptor_f_on_conf_dup_t *on_conf_dup, + void *ic_opaque); + +/** + * @brief Append an on_conf_destroy() interceptor. + * + * @param conf Configuration object. + * @param ic_name Interceptor name, used in logging. + * @param on_conf_destroy Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR + * + * @remark Multiple on_conf_destroy() interceptors are allowed to be added + * to the same configuration object. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_conf_interceptor_add_on_conf_destroy ( + rd_kafka_conf_t *conf, const char *ic_name, + rd_kafka_interceptor_f_on_conf_destroy_t *on_conf_destroy, + void *ic_opaque); + + +/** + * @brief Append an on_new() interceptor. + * + * @param conf Configuration object. + * @param ic_name Interceptor name, used in logging. + * @param on_new Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @remark Since the on_new() interceptor is added to the configuration object + * it may be copied by rd_kafka_conf_dup(). + * An interceptor implementation must thus be able to handle + * the same interceptor,ic_opaque tuple to be used by multiple + * client instances. + * + * @remark An interceptor plugin should check the return value to make sure it + * has not already been added. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_conf_interceptor_add_on_new ( + rd_kafka_conf_t *conf, const char *ic_name, + rd_kafka_interceptor_f_on_new_t *on_new, + void *ic_opaque); + + + +/** + * @brief Append an on_destroy() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_destroy Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_destroy ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_destroy_t *on_destroy, + void *ic_opaque); + + +/** + * @brief Append an on_send() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_send Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_send ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_send_t *on_send, + void *ic_opaque); + +/** + * @brief Append an on_acknowledgement() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_acknowledgement Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_acknowledgement ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_acknowledgement_t *on_acknowledgement, + void *ic_opaque); + + +/** + * @brief Append an on_consume() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_consume Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_consume ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_consume_t *on_consume, + void *ic_opaque); + + +/** + * @brief Append an on_commit() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_commit() Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_commit ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_commit_t *on_commit, + void *ic_opaque); + + +/** + * @brief Append an on_request_sent() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_request_sent() Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_request_sent ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_request_sent_t *on_request_sent, + void *ic_opaque); + + +/** + * @brief Append an on_response_received() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_response_received() Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_response_received ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_response_received_t *on_response_received, + void *ic_opaque); + + +/** + * @brief Append an on_thread_start() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_thread_start() Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_thread_start ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_thread_start_t *on_thread_start, + void *ic_opaque); + + +/** + * @brief Append an on_thread_exit() interceptor. + * + * @param rk Client instance. + * @param ic_name Interceptor name, used in logging. + * @param on_thread_exit() Function pointer. + * @param ic_opaque Opaque value that will be passed to the function. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or RD_KAFKA_RESP_ERR__CONFLICT + * if an existing intercepted with the same \p ic_name and function + * has already been added to \p conf. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_interceptor_add_on_thread_exit ( + rd_kafka_t *rk, const char *ic_name, + rd_kafka_interceptor_f_on_thread_exit_t *on_thread_exit, + void *ic_opaque); + + + +/**@}*/ + + + +/** + * @name Auxiliary types + * + * @{ + */ + + + +/** + * @brief Topic result provides per-topic operation result information. + * + */ + +/** + * @returns the error code for the given topic result. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_topic_result_error (const rd_kafka_topic_result_t *topicres); + +/** + * @returns the human readable error string for the given topic result, + * or NULL if there was no error. + * + * @remark lifetime of the returned string is the same as the \p topicres. + */ +RD_EXPORT const char * +rd_kafka_topic_result_error_string (const rd_kafka_topic_result_t *topicres); + +/** + * @returns the name of the topic for the given topic result. + * @remark lifetime of the returned string is the same as the \p topicres. + * + */ +RD_EXPORT const char * +rd_kafka_topic_result_name (const rd_kafka_topic_result_t *topicres); + +/** + * @brief Group result provides per-group operation result information. + * + */ + +/** + * @returns the error for the given group result, or NULL on success. + * @remark lifetime of the returned error is the same as the \p groupres. + */ +RD_EXPORT const rd_kafka_error_t * +rd_kafka_group_result_error (const rd_kafka_group_result_t *groupres); + +/** + * @returns the name of the group for the given group result. + * @remark lifetime of the returned string is the same as the \p groupres. + * + */ +RD_EXPORT const char * +rd_kafka_group_result_name (const rd_kafka_group_result_t *groupres); + +/** + * @returns the partitions/offsets for the given group result, if applicable + * to the request type, else NULL. + * @remark lifetime of the returned list is the same as the \p groupres. + */ +RD_EXPORT const rd_kafka_topic_partition_list_t * +rd_kafka_group_result_partitions (const rd_kafka_group_result_t *groupres); + + +/**@}*/ + + +/** + * @name Admin API + * @{ + * + * @brief The Admin API enables applications to perform administrative + * Apache Kafka tasks, such as creating and deleting topics, + * altering and reading broker configuration, etc. + * + * The Admin API is asynchronous and makes use of librdkafka's standard + * \c rd_kafka_queue_t queues to propagate the result of an admin operation + * back to the application. + * The supplied queue may be any queue, such as a temporary single-call queue, + * a shared queue used for multiple requests, or even the main queue or + * consumer queues. + * + * Use \c rd_kafka_queue_poll() to collect the result of an admin operation + * from the queue of your choice, then extract the admin API-specific result + * type by using the corresponding \c rd_kafka_event_CreateTopics_result, + * \c rd_kafka_event_DescribeConfigs_result, etc, methods. + * Use the getter methods on the \c .._result_t type to extract response + * information and finally destroy the result and event by calling + * \c rd_kafka_event_destroy(). + * + * Use rd_kafka_event_error() and rd_kafka_event_error_string() to acquire + * the request-level error/success for an Admin API request. + * Even if the returned value is \c RD_KAFKA_RESP_ERR_NO_ERROR there + * may be individual objects (topics, resources, etc) that have failed. + * Extract per-object error information with the corresponding + * \c rd_kafka_..._result_topics|resources|..() to check per-object errors. + * + * Locally triggered errors: + * - \c RD_KAFKA_RESP_ERR__TIMED_OUT - (Controller) broker connection did not + * become available in the time allowed by AdminOption_set_request_timeout. + */ + + +/** + * @enum rd_kafka_admin_op_t + * + * @brief Admin operation enum name for use with rd_kafka_AdminOptions_new() + * + * @sa rd_kafka_AdminOptions_new() + */ +typedef enum rd_kafka_admin_op_t { + RD_KAFKA_ADMIN_OP_ANY = 0, /**< Default value */ + RD_KAFKA_ADMIN_OP_CREATETOPICS, /**< CreateTopics */ + RD_KAFKA_ADMIN_OP_DELETETOPICS, /**< DeleteTopics */ + RD_KAFKA_ADMIN_OP_CREATEPARTITIONS, /**< CreatePartitions */ + RD_KAFKA_ADMIN_OP_ALTERCONFIGS, /**< AlterConfigs */ + RD_KAFKA_ADMIN_OP_DESCRIBECONFIGS, /**< DescribeConfigs */ + RD_KAFKA_ADMIN_OP_DELETERECORDS, /**< DeleteRecords */ + RD_KAFKA_ADMIN_OP_DELETEGROUPS, /**< DeleteGroups */ + /** DeleteConsumerGroupOffsets */ + RD_KAFKA_ADMIN_OP_DELETECONSUMERGROUPOFFSETS, + RD_KAFKA_ADMIN_OP__CNT /**< Number of ops defined */ +} rd_kafka_admin_op_t; + +/** + * @brief AdminOptions provides a generic mechanism for setting optional + * parameters for the Admin API requests. + * + * @remark Since AdminOptions is decoupled from the actual request type + * there is no enforcement to prevent setting unrelated properties, + * e.g. setting validate_only on a DescribeConfigs request is allowed + * but is silently ignored by DescribeConfigs. + * Future versions may introduce such enforcement. + */ + + +typedef struct rd_kafka_AdminOptions_s rd_kafka_AdminOptions_t; + +/** + * @brief Create a new AdminOptions object. + * + * The options object is not modified by the Admin API request APIs, + * (e.g. CreateTopics) and may be reused for multiple calls. + * + * @param rk Client instance. + * @param for_api Specifies what Admin API this AdminOptions object will be used + * for, which will enforce what AdminOptions_set_..() calls may + * be used based on the API, causing unsupported set..() calls + * to fail. + * Specifying RD_KAFKA_ADMIN_OP_ANY disables the enforcement + * allowing any option to be set, even if the option + * is not used in a future call to an Admin API method. + * + * @returns a new AdminOptions object (which must be freed with + * rd_kafka_AdminOptions_destroy()), or NULL if \p for_api was set to + * an unknown API op type. + */ +RD_EXPORT rd_kafka_AdminOptions_t * +rd_kafka_AdminOptions_new (rd_kafka_t *rk, rd_kafka_admin_op_t for_api); + + +/** + * @brief Destroy a AdminOptions object. + */ +RD_EXPORT void rd_kafka_AdminOptions_destroy (rd_kafka_AdminOptions_t *options); + + +/** + * @brief Sets the overall request timeout, including broker lookup, + * request transmission, operation time on broker, and response. + * + * @param options Admin options. + * @param timeout_ms Timeout in milliseconds, use -1 for indefinite timeout. + * Defaults to `socket.timeout.ms`. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or + * RD_KAFKA_RESP_ERR__INVALID_ARG if timeout was out of range in which + * case an error string will be written \p errstr. + * + * @remark This option is valid for all Admin API requests. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_AdminOptions_set_request_timeout (rd_kafka_AdminOptions_t *options, + int timeout_ms, + char *errstr, size_t errstr_size); + + +/** + * @brief Sets the broker's operation timeout, such as the timeout for + * CreateTopics to complete the creation of topics on the controller + * before returning a result to the application. + * + * CreateTopics: values <= 0 will return immediately after triggering topic + * creation, while > 0 will wait this long for topic creation to propagate + * in cluster. Default: 60 seconds. + * + * DeleteTopics: same semantics as CreateTopics. + * CreatePartitions: same semantics as CreateTopics. + * + * @param options Admin options. + * @param timeout_ms Timeout in milliseconds. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or + * RD_KAFKA_RESP_ERR__INVALID_ARG if timeout was out of range in which + * case an error string will be written \p errstr. + * + * @remark This option is valid for CreateTopics, DeleteTopics, + * CreatePartitions, and DeleteRecords. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_AdminOptions_set_operation_timeout (rd_kafka_AdminOptions_t *options, + int timeout_ms, + char *errstr, size_t errstr_size); + + +/** + * @brief Tell broker to only validate the request, without performing + * the requested operation (create topics, etc). + * + * @param options Admin options. + * @param true_or_false Defaults to false. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an + * error code on failure in which case an error string will + * be written \p errstr. + * + * @remark This option is valid for CreateTopics, + * CreatePartitions, AlterConfigs. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_AdminOptions_set_validate_only (rd_kafka_AdminOptions_t *options, + int true_or_false, + char *errstr, size_t errstr_size); + + +/** + * @brief Override what broker the Admin request will be sent to. + * + * By default, Admin requests are sent to the controller broker, with + * the following exceptions: + * - AlterConfigs with a BROKER resource are sent to the broker id set + * as the resource name. + * - DescribeConfigs with a BROKER resource are sent to the broker id set + * as the resource name. + * + * @param options Admin Options. + * @param broker_id The broker to send the request to. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success or an + * error code on failure in which case an error string will + * be written \p errstr. + * + * @remark This API should typically not be used, but serves as a workaround + * if new resource types are to the broker that the client + * does not know where to send. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_AdminOptions_set_broker (rd_kafka_AdminOptions_t *options, + int32_t broker_id, + char *errstr, size_t errstr_size); + + + +/** + * @brief Set application opaque value that can be extracted from the + * result event using rd_kafka_event_opaque() + */ +RD_EXPORT void +rd_kafka_AdminOptions_set_opaque (rd_kafka_AdminOptions_t *options, + void *ev_opaque); + + + + + + +/* + * CreateTopics - create topics in cluster. + * + */ + + +/*! Defines a new topic to be created. */ +typedef struct rd_kafka_NewTopic_s rd_kafka_NewTopic_t; + +/** + * @brief Create a new NewTopic object. This object is later passed to + * rd_kafka_CreateTopics(). + * + * @param topic Topic name to create. + * @param num_partitions Number of partitions in topic, or -1 to use the + * broker's default partition count (>= 2.4.0). + * @param replication_factor Default replication factor for the topic's + * partitions, or -1 to use the broker's default + * replication factor (>= 2.4.0) or if + * set_replica_assignment() will be used. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * + * @returns a new allocated NewTopic object, or NULL if the input parameters + * are invalid. + * Use rd_kafka_NewTopic_destroy() to free object when done. + */ +RD_EXPORT rd_kafka_NewTopic_t * +rd_kafka_NewTopic_new (const char *topic, int num_partitions, + int replication_factor, + char *errstr, size_t errstr_size); + +/** + * @brief Destroy and free a NewTopic object previously created with + * rd_kafka_NewTopic_new() + */ +RD_EXPORT void +rd_kafka_NewTopic_destroy (rd_kafka_NewTopic_t *new_topic); + + +/** + * @brief Helper function to destroy all NewTopic objects in the \p new_topics + * array (of \p new_topic_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_NewTopic_destroy_array (rd_kafka_NewTopic_t **new_topics, + size_t new_topic_cnt); + + +/** + * @brief Set the replica (broker) assignment for \p partition to the + * replica set in \p broker_ids (of \p broker_id_cnt elements). + * + * @remark When this method is used, rd_kafka_NewTopic_new() must have + * been called with a \c replication_factor of -1. + * + * @remark An application must either set the replica assignment for + * all new partitions, or none. + * + * @remark If called, this function must be called consecutively for each + * partition, starting at 0. + * + * @remark Use rd_kafka_metadata() to retrieve the list of brokers + * in the cluster. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or an error code + * if the arguments were invalid. + * + * @sa rd_kafka_AdminOptions_set_validate_only() + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_NewTopic_set_replica_assignment (rd_kafka_NewTopic_t *new_topic, + int32_t partition, + int32_t *broker_ids, + size_t broker_id_cnt, + char *errstr, size_t errstr_size); + +/** + * @brief Set (broker-side) topic configuration name/value pair. + * + * @remark The name and value are not validated by the client, the validation + * takes place on the broker. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or an error code + * if the arguments were invalid. + * + * @sa rd_kafka_AdminOptions_set_validate_only() + * @sa http://kafka.apache.org/documentation.html#topicconfigs + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_NewTopic_set_config (rd_kafka_NewTopic_t *new_topic, + const char *name, const char *value); + + +/** + * @brief Create topics in cluster as specified by the \p new_topics + * array of size \p new_topic_cnt elements. + * + * @param rk Client instance. + * @param new_topics Array of new topics to create. + * @param new_topic_cnt Number of elements in \p new_topics array. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * Supported admin options: + * - rd_kafka_AdminOptions_set_validate_only() - default false + * - rd_kafka_AdminOptions_set_operation_timeout() - default 60 seconds + * - rd_kafka_AdminOptions_set_request_timeout() - default socket.timeout.ms + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_CREATETOPICS_RESULT + */ +RD_EXPORT void +rd_kafka_CreateTopics (rd_kafka_t *rk, + rd_kafka_NewTopic_t **new_topics, + size_t new_topic_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + +/* + * CreateTopics result type and methods + */ + +/** + * @brief Get an array of topic results from a CreateTopics result. + * + * The returned \p topics life-time is the same as the \p result object. + * + * @param result Result to get topics from. + * @param cntp Updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_topic_result_t ** +rd_kafka_CreateTopics_result_topics ( + const rd_kafka_CreateTopics_result_t *result, + size_t *cntp); + + + + + +/* + * DeleteTopics - delete topics from cluster + * + */ + +/*! Represents a topic to be deleted. */ +typedef struct rd_kafka_DeleteTopic_s rd_kafka_DeleteTopic_t; + +/** + * @brief Create a new DeleteTopic object. This object is later passed to + * rd_kafka_DeleteTopics(). + * + * @param topic Topic name to delete. + * + * @returns a new allocated DeleteTopic object. + * Use rd_kafka_DeleteTopic_destroy() to free object when done. + */ +RD_EXPORT rd_kafka_DeleteTopic_t * +rd_kafka_DeleteTopic_new (const char *topic); + +/** + * @brief Destroy and free a DeleteTopic object previously created with + * rd_kafka_DeleteTopic_new() + */ +RD_EXPORT void +rd_kafka_DeleteTopic_destroy (rd_kafka_DeleteTopic_t *del_topic); + +/** + * @brief Helper function to destroy all DeleteTopic objects in + * the \p del_topics array (of \p del_topic_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_DeleteTopic_destroy_array (rd_kafka_DeleteTopic_t **del_topics, + size_t del_topic_cnt); + +/** + * @brief Delete topics from cluster as specified by the \p topics + * array of size \p topic_cnt elements. + * + * @param rk Client instance. + * @param del_topics Array of topics to delete. + * @param del_topic_cnt Number of elements in \p topics array. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_DELETETOPICS_RESULT + */ +RD_EXPORT +void rd_kafka_DeleteTopics (rd_kafka_t *rk, + rd_kafka_DeleteTopic_t **del_topics, + size_t del_topic_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + + +/* + * DeleteTopics result type and methods + */ + +/** + * @brief Get an array of topic results from a DeleteTopics result. + * + * The returned \p topics life-time is the same as the \p result object. + * + * @param result Result to get topic results from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_topic_result_t ** +rd_kafka_DeleteTopics_result_topics ( + const rd_kafka_DeleteTopics_result_t *result, + size_t *cntp); + + + + + + +/* + * CreatePartitions - add partitions to topic. + * + */ + +/*! Defines a new partition to be created. */ +typedef struct rd_kafka_NewPartitions_s rd_kafka_NewPartitions_t; + +/** + * @brief Create a new NewPartitions. This object is later passed to + * rd_kafka_CreatePartitions() to increase the number of partitions + * to \p new_total_cnt for an existing topic. + * + * @param topic Topic name to create more partitions for. + * @param new_total_cnt Increase the topic's partition count to this value. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * @returns a new allocated NewPartitions object, or NULL if the + * input parameters are invalid. + * Use rd_kafka_NewPartitions_destroy() to free object when done. + */ +RD_EXPORT rd_kafka_NewPartitions_t * +rd_kafka_NewPartitions_new (const char *topic, size_t new_total_cnt, + char *errstr, size_t errstr_size); + +/** + * @brief Destroy and free a NewPartitions object previously created with + * rd_kafka_NewPartitions_new() + */ +RD_EXPORT void +rd_kafka_NewPartitions_destroy (rd_kafka_NewPartitions_t *new_parts); + +/** + * @brief Helper function to destroy all NewPartitions objects in the + * \p new_parts array (of \p new_parts_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_NewPartitions_destroy_array (rd_kafka_NewPartitions_t **new_parts, + size_t new_parts_cnt); + +/** + * @brief Set the replica (broker id) assignment for \p new_partition_idx to the + * replica set in \p broker_ids (of \p broker_id_cnt elements). + * + * @remark An application must either set the replica assignment for + * all new partitions, or none. + * + * @remark If called, this function must be called consecutively for each + * new partition being created, + * where \p new_partition_idx 0 is the first new partition, + * 1 is the second, and so on. + * + * @remark \p broker_id_cnt should match the topic's replication factor. + * + * @remark Use rd_kafka_metadata() to retrieve the list of brokers + * in the cluster. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR on success, or an error code + * if the arguments were invalid. + * + * @sa rd_kafka_AdminOptions_set_validate_only() + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_NewPartitions_set_replica_assignment (rd_kafka_NewPartitions_t *new_parts, + int32_t new_partition_idx, + int32_t *broker_ids, + size_t broker_id_cnt, + char *errstr, + size_t errstr_size); + + +/** + * @brief Create additional partitions for the given topics, as specified + * by the \p new_parts array of size \p new_parts_cnt elements. + * + * @param rk Client instance. + * @param new_parts Array of topics for which new partitions are to be created. + * @param new_parts_cnt Number of elements in \p new_parts array. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * Supported admin options: + * - rd_kafka_AdminOptions_set_validate_only() - default false + * - rd_kafka_AdminOptions_set_operation_timeout() - default 60 seconds + * - rd_kafka_AdminOptions_set_request_timeout() - default socket.timeout.ms + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_CREATEPARTITIONS_RESULT + */ +RD_EXPORT void +rd_kafka_CreatePartitions (rd_kafka_t *rk, + rd_kafka_NewPartitions_t **new_parts, + size_t new_parts_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + + +/* + * CreatePartitions result type and methods + */ + +/** + * @brief Get an array of topic results from a CreatePartitions result. + * + * The returned \p topics life-time is the same as the \p result object. + * + * @param result Result o get topic results from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_topic_result_t ** +rd_kafka_CreatePartitions_result_topics ( + const rd_kafka_CreatePartitions_result_t *result, + size_t *cntp); + + + + + +/* + * Cluster, broker, topic configuration entries, sources, etc. + * + */ + +/** + * @enum rd_kafka_ConfigSource_t + * + * @brief Apache Kafka config sources. + * + * @remark These entities relate to the cluster, not the local client. + * + * @sa rd_kafka_conf_set(), et.al. for local client configuration. + */ +typedef enum rd_kafka_ConfigSource_t { + /** Source unknown, e.g., in the ConfigEntry used for alter requests + * where source is not set */ + RD_KAFKA_CONFIG_SOURCE_UNKNOWN_CONFIG = 0, + /** Dynamic topic config that is configured for a specific topic */ + RD_KAFKA_CONFIG_SOURCE_DYNAMIC_TOPIC_CONFIG = 1, + /** Dynamic broker config that is configured for a specific broker */ + RD_KAFKA_CONFIG_SOURCE_DYNAMIC_BROKER_CONFIG = 2, + /** Dynamic broker config that is configured as default for all + * brokers in the cluster */ + RD_KAFKA_CONFIG_SOURCE_DYNAMIC_DEFAULT_BROKER_CONFIG = 3, + /** Static broker config provided as broker properties at startup + * (e.g. from server.properties file) */ + RD_KAFKA_CONFIG_SOURCE_STATIC_BROKER_CONFIG = 4, + /** Built-in default configuration for configs that have a + * default value */ + RD_KAFKA_CONFIG_SOURCE_DEFAULT_CONFIG = 5, + + /** Number of source types defined */ + RD_KAFKA_CONFIG_SOURCE__CNT, +} rd_kafka_ConfigSource_t; + + +/** + * @returns a string representation of the \p confsource. + */ +RD_EXPORT const char * +rd_kafka_ConfigSource_name (rd_kafka_ConfigSource_t confsource); + + +/*! Apache Kafka configuration entry. */ +typedef struct rd_kafka_ConfigEntry_s rd_kafka_ConfigEntry_t; + +/** + * @returns the configuration property name + */ +RD_EXPORT const char * +rd_kafka_ConfigEntry_name (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns the configuration value, may be NULL for sensitive or unset + * properties. + */ +RD_EXPORT const char * +rd_kafka_ConfigEntry_value (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns the config source. + */ +RD_EXPORT rd_kafka_ConfigSource_t +rd_kafka_ConfigEntry_source (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns 1 if the config property is read-only on the broker, else 0. + * @remark Shall only be used on a DescribeConfigs result, otherwise returns -1. + */ +RD_EXPORT int +rd_kafka_ConfigEntry_is_read_only (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns 1 if the config property is set to its default value on the broker, + * else 0. + * @remark Shall only be used on a DescribeConfigs result, otherwise returns -1. + */ +RD_EXPORT int +rd_kafka_ConfigEntry_is_default (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns 1 if the config property contains sensitive information (such as + * security configuration), else 0. + * @remark An application should take care not to include the value of + * sensitive configuration entries in its output. + * @remark Shall only be used on a DescribeConfigs result, otherwise returns -1. + */ +RD_EXPORT int +rd_kafka_ConfigEntry_is_sensitive (const rd_kafka_ConfigEntry_t *entry); + +/** + * @returns 1 if this entry is a synonym, else 0. + */ +RD_EXPORT int +rd_kafka_ConfigEntry_is_synonym (const rd_kafka_ConfigEntry_t *entry); + + +/** + * @returns the synonym config entry array. + * + * @param entry Entry to get synonyms for. + * @param cntp is updated to the number of elements in the array. + * + * @remark The lifetime of the returned entry is the same as \p conf . + * @remark Shall only be used on a DescribeConfigs result, + * otherwise returns NULL. + */ +RD_EXPORT const rd_kafka_ConfigEntry_t ** +rd_kafka_ConfigEntry_synonyms (const rd_kafka_ConfigEntry_t *entry, + size_t *cntp); + + + + +/*! Apache Kafka resource types */ +typedef enum rd_kafka_ResourceType_t { + RD_KAFKA_RESOURCE_UNKNOWN = 0, /**< Unknown */ + RD_KAFKA_RESOURCE_ANY = 1, /**< Any (used for lookups) */ + RD_KAFKA_RESOURCE_TOPIC = 2, /**< Topic */ + RD_KAFKA_RESOURCE_GROUP = 3, /**< Group */ + RD_KAFKA_RESOURCE_BROKER = 4, /**< Broker */ + RD_KAFKA_RESOURCE__CNT, /**< Number of resource types defined */ +} rd_kafka_ResourceType_t; + +/** + * @returns a string representation of the \p restype + */ +RD_EXPORT const char * +rd_kafka_ResourceType_name (rd_kafka_ResourceType_t restype); + +/*! Apache Kafka configuration resource. */ +typedef struct rd_kafka_ConfigResource_s rd_kafka_ConfigResource_t; + + +/** + * @brief Create new ConfigResource object. + * + * @param restype The resource type (e.g., RD_KAFKA_RESOURCE_TOPIC) + * @param resname The resource name (e.g., the topic name) + * + * @returns a newly allocated object + */ +RD_EXPORT rd_kafka_ConfigResource_t * +rd_kafka_ConfigResource_new (rd_kafka_ResourceType_t restype, + const char *resname); + +/** + * @brief Destroy and free a ConfigResource object previously created with + * rd_kafka_ConfigResource_new() + */ +RD_EXPORT void +rd_kafka_ConfigResource_destroy (rd_kafka_ConfigResource_t *config); + + +/** + * @brief Helper function to destroy all ConfigResource objects in + * the \p configs array (of \p config_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_ConfigResource_destroy_array (rd_kafka_ConfigResource_t **config, + size_t config_cnt); + + +/** + * @brief Set configuration name value pair. + * + * @param config ConfigResource to set config property on. + * @param name Configuration name, depends on resource type. + * @param value Configuration value, depends on resource type and \p name. + * Set to \c NULL to revert configuration value to default. + * + * This will overwrite the current value. + * + * @returns RD_KAFKA_RESP_ERR_NO_ERROR if config was added to resource, + * or RD_KAFKA_RESP_ERR__INVALID_ARG on invalid input. + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_ConfigResource_set_config (rd_kafka_ConfigResource_t *config, + const char *name, const char *value); + + +/** + * @brief Get an array of config entries from a ConfigResource object. + * + * The returned object life-times are the same as the \p config object. + * + * @param config ConfigResource to get configs from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_ConfigEntry_t ** +rd_kafka_ConfigResource_configs (const rd_kafka_ConfigResource_t *config, + size_t *cntp); + + + +/** + * @returns the ResourceType for \p config + */ +RD_EXPORT rd_kafka_ResourceType_t +rd_kafka_ConfigResource_type (const rd_kafka_ConfigResource_t *config); + +/** + * @returns the name for \p config + */ +RD_EXPORT const char * +rd_kafka_ConfigResource_name (const rd_kafka_ConfigResource_t *config); + +/** + * @returns the error for this resource from an AlterConfigs request + */ +RD_EXPORT rd_kafka_resp_err_t +rd_kafka_ConfigResource_error (const rd_kafka_ConfigResource_t *config); + +/** + * @returns the error string for this resource from an AlterConfigs + * request, or NULL if no error. + */ +RD_EXPORT const char * +rd_kafka_ConfigResource_error_string (const rd_kafka_ConfigResource_t *config); + + +/* + * AlterConfigs - alter cluster configuration. + * + */ + + +/** + * @brief Update the configuration for the specified resources. + * Updates are not transactional so they may succeed for a subset + * of the provided resources while the others fail. + * The configuration for a particular resource is updated atomically, + * replacing values using the provided ConfigEntrys and reverting + * unspecified ConfigEntrys to their default values. + * + * @remark Requires broker version >=0.11.0.0 + * + * @warning AlterConfigs will replace all existing configuration for + * the provided resources with the new configuration given, + * reverting all other configuration to their default values. + * + * @remark Multiple resources and resource types may be set, but at most one + * resource of type \c RD_KAFKA_RESOURCE_BROKER is allowed per call + * since these resource requests must be sent to the broker specified + * in the resource. + * + */ +RD_EXPORT +void rd_kafka_AlterConfigs (rd_kafka_t *rk, + rd_kafka_ConfigResource_t **configs, + size_t config_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + +/* + * AlterConfigs result type and methods + */ + +/** + * @brief Get an array of resource results from a AlterConfigs result. + * + * Use \c rd_kafka_ConfigResource_error() and + * \c rd_kafka_ConfigResource_error_string() to extract per-resource error + * results on the returned array elements. + * + * The returned object life-times are the same as the \p result object. + * + * @param result Result object to get resource results from. + * @param cntp is updated to the number of elements in the array. + * + * @returns an array of ConfigResource elements, or NULL if not available. + */ +RD_EXPORT const rd_kafka_ConfigResource_t ** +rd_kafka_AlterConfigs_result_resources ( + const rd_kafka_AlterConfigs_result_t *result, + size_t *cntp); + + + + + + +/* + * DescribeConfigs - retrieve cluster configuration. + * + */ + + +/** + * @brief Get configuration for the specified resources in \p configs. + * + * The returned configuration includes default values and the + * rd_kafka_ConfigEntry_is_default() or rd_kafka_ConfigEntry_source() + * methods may be used to distinguish them from user supplied values. + * + * The value of config entries where rd_kafka_ConfigEntry_is_sensitive() + * is true will always be NULL to avoid disclosing sensitive + * information, such as security settings. + * + * Configuration entries where rd_kafka_ConfigEntry_is_read_only() + * is true can't be updated (with rd_kafka_AlterConfigs()). + * + * Synonym configuration entries are returned if the broker supports + * it (broker version >= 1.1.0). See rd_kafka_ConfigEntry_synonyms(). + * + * @remark Requires broker version >=0.11.0.0 + * + * @remark Multiple resources and resource types may be requested, but at most + * one resource of type \c RD_KAFKA_RESOURCE_BROKER is allowed per call + * since these resource requests must be sent to the broker specified + * in the resource. + */ +RD_EXPORT +void rd_kafka_DescribeConfigs (rd_kafka_t *rk, + rd_kafka_ConfigResource_t **configs, + size_t config_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + + + +/* + * DescribeConfigs result type and methods + */ + +/** + * @brief Get an array of resource results from a DescribeConfigs result. + * + * The returned \p resources life-time is the same as the \p result object. + * + * @param result Result object to get resource results from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_ConfigResource_t ** +rd_kafka_DescribeConfigs_result_resources ( + const rd_kafka_DescribeConfigs_result_t *result, + size_t *cntp); + + +/* + * DeleteRecords - delete records (messages) from partitions + * + * + */ + +/**! Represents records to be deleted */ +typedef struct rd_kafka_DeleteRecords_s rd_kafka_DeleteRecords_t; + +/** + * @brief Create a new DeleteRecords object. This object is later passed to + * rd_kafka_DeleteRecords(). + * + * \p before_offsets must contain \c topic, \c partition, and + * \c offset is the offset before which the messages will + * be deleted (exclusive). + * Set \c offset to RD_KAFKA_OFFSET_END (high-watermark) in order to + * delete all data in the partition. + * + * @param before_offsets For each partition delete all messages up to but not + * including the specified offset. + * + * @returns a new allocated DeleteRecords object. + * Use rd_kafka_DeleteRecords_destroy() to free object when done. + */ +RD_EXPORT rd_kafka_DeleteRecords_t * +rd_kafka_DeleteRecords_new (const rd_kafka_topic_partition_list_t * + before_offsets); + +/** + * @brief Destroy and free a DeleteRecords object previously created with + * rd_kafka_DeleteRecords_new() + */ +RD_EXPORT void +rd_kafka_DeleteRecords_destroy (rd_kafka_DeleteRecords_t *del_records); + +/** + * @brief Helper function to destroy all DeleteRecords objects in + * the \p del_groups array (of \p del_group_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_DeleteRecords_destroy_array (rd_kafka_DeleteRecords_t **del_records, + size_t del_record_cnt); + +/** + * @brief Delete records (messages) in topic partitions older than the + * offsets provided. + * + * @param rk Client instance. + * @param del_records The offsets to delete (up to). + * Currently only one DeleteRecords_t (but containing + * multiple offsets) is supported. + * @param del_record_cnt The number of elements in del_records, must be 1. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * Supported admin options: + * - rd_kafka_AdminOptions_set_operation_timeout() - default 60 seconds. + * Controls how long the brokers will wait for records to be deleted. + * - rd_kafka_AdminOptions_set_request_timeout() - default socket.timeout.ms. + * Controls how long \c rdkafka will wait for the request to complete. + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_DELETERECORDS_RESULT + */ +RD_EXPORT void +rd_kafka_DeleteRecords (rd_kafka_t *rk, + rd_kafka_DeleteRecords_t **del_records, + size_t del_record_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + +/* + * DeleteRecords result type and methods + */ + +/** + * @brief Get a list of topic and partition results from a DeleteRecords result. + * The returned objects will contain \c topic, \c partition, \c offset + * and \c err. \c offset will be set to the post-deletion low-watermark + * (smallest available offset of all live replicas). \c err will be set + * per-partition if deletion failed. + * + * The returned object's life-time is the same as the \p result object. + */ +RD_EXPORT const rd_kafka_topic_partition_list_t * +rd_kafka_DeleteRecords_result_offsets ( + const rd_kafka_DeleteRecords_result_t *result); + +/* + * DeleteGroups - delete groups from cluster + * + * + */ + +/*! Represents a group to be deleted. */ +typedef struct rd_kafka_DeleteGroup_s rd_kafka_DeleteGroup_t; + +/** + * @brief Create a new DeleteGroup object. This object is later passed to + * rd_kafka_DeleteGroups(). + * + * @param group Name of group to delete. + * + * @returns a new allocated DeleteGroup object. + * Use rd_kafka_DeleteGroup_destroy() to free object when done. + */ +RD_EXPORT rd_kafka_DeleteGroup_t * +rd_kafka_DeleteGroup_new (const char *group); + +/** + * @brief Destroy and free a DeleteGroup object previously created with + * rd_kafka_DeleteGroup_new() + */ +RD_EXPORT void +rd_kafka_DeleteGroup_destroy (rd_kafka_DeleteGroup_t *del_group); + +/** + * @brief Helper function to destroy all DeleteGroup objects in + * the \p del_groups array (of \p del_group_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_DeleteGroup_destroy_array (rd_kafka_DeleteGroup_t **del_groups, + size_t del_group_cnt); + +/** + * @brief Delete groups from cluster as specified by the \p del_groups + * array of size \p del_group_cnt elements. + * + * @param rk Client instance. + * @param del_groups Array of groups to delete. + * @param del_group_cnt Number of elements in \p del_groups array. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_DELETEGROUPS_RESULT + */ +RD_EXPORT +void rd_kafka_DeleteGroups (rd_kafka_t *rk, + rd_kafka_DeleteGroup_t **del_groups, + size_t del_group_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + + +/* + * DeleteGroups result type and methods + */ + +/** + * @brief Get an array of group results from a DeleteGroups result. + * + * The returned groups life-time is the same as the \p result object. + * + * @param result Result to get group results from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_group_result_t ** +rd_kafka_DeleteGroups_result_groups ( + const rd_kafka_DeleteGroups_result_t *result, + size_t *cntp); + + +/* + * DeleteConsumerGroupOffsets - delete groups from cluster + * + * + */ + +/*! Represents consumer group committed offsets to be deleted. */ +typedef struct rd_kafka_DeleteConsumerGroupOffsets_s +rd_kafka_DeleteConsumerGroupOffsets_t; + +/** + * @brief Create a new DeleteConsumerGroupOffsets object. + * This object is later passed to rd_kafka_DeleteConsumerGroupOffsets(). + * + * @param group Consumer group id. + * @param partitions Partitions to delete committed offsets for. + * Only the topic and partition fields are used. + * + * @returns a new allocated DeleteConsumerGroupOffsets object. + * Use rd_kafka_DeleteConsumerGroupOffsets_destroy() to free + * object when done. + */ +RD_EXPORT rd_kafka_DeleteConsumerGroupOffsets_t * +rd_kafka_DeleteConsumerGroupOffsets_new (const char *group, + const rd_kafka_topic_partition_list_t + *partitions); + +/** + * @brief Destroy and free a DeleteConsumerGroupOffsets object previously + * created with rd_kafka_DeleteConsumerGroupOffsets_new() + */ +RD_EXPORT void +rd_kafka_DeleteConsumerGroupOffsets_destroy ( + rd_kafka_DeleteConsumerGroupOffsets_t *del_grpoffsets); + +/** + * @brief Helper function to destroy all DeleteConsumerGroupOffsets objects in + * the \p del_grpoffsets array (of \p del_grpoffsets_cnt elements). + * The array itself is not freed. + */ +RD_EXPORT void +rd_kafka_DeleteConsumerGroupOffsets_destroy_array ( + rd_kafka_DeleteConsumerGroupOffsets_t **del_grpoffsets, + size_t del_grpoffset_cnt); + +/** + * @brief Delete committed offsets for a set of partitions in a conusmer + * group. This will succeed at the partition level only if the group + * is not actively subscribed to the corresponding topic. + * + * @param rk Client instance. + * @param del_grpoffsets Array of group committed offsets to delete. + * MUST only be one single element. + * @param del_grpoffsets_cnt Number of elements in \p del_grpoffsets array. + * MUST always be 1. + * @param options Optional admin options, or NULL for defaults. + * @param rkqu Queue to emit result on. + * + * @remark The result event type emitted on the supplied queue is of type + * \c RD_KAFKA_EVENT_DELETECONSUMERGROUPOFFSETS_RESULT + * + * @remark The current implementation only supports one group per invocation. + */ +RD_EXPORT +void rd_kafka_DeleteConsumerGroupOffsets ( + rd_kafka_t *rk, + rd_kafka_DeleteConsumerGroupOffsets_t **del_grpoffsets, + size_t del_grpoffsets_cnt, + const rd_kafka_AdminOptions_t *options, + rd_kafka_queue_t *rkqu); + + + +/* + * DeleteConsumerGroupOffsets result type and methods + */ + +/** + * @brief Get an array of results from a DeleteConsumerGroupOffsets result. + * + * The returned groups life-time is the same as the \p result object. + * + * @param result Result to get group results from. + * @param cntp is updated to the number of elements in the array. + */ +RD_EXPORT const rd_kafka_group_result_t ** +rd_kafka_DeleteConsumerGroupOffsets_result_groups ( + const rd_kafka_DeleteConsumerGroupOffsets_result_t *result, + size_t *cntp); + + +/**@}*/ + + +/** + * @name Security APIs + * @{ + * + */ + +/** + * @brief Set SASL/OAUTHBEARER token and metadata + * + * @param rk Client instance. + * @param token_value the mandatory token value to set, often (but not + * necessarily) a JWS compact serialization as per + * https://tools.ietf.org/html/rfc7515#section-3.1. + * @param md_lifetime_ms when the token expires, in terms of the number of + * milliseconds since the epoch. + * @param md_principal_name the mandatory Kafka principal name associated + * with the token. + * @param extensions optional SASL extensions key-value array with + * \p extensions_size elements (number of keys * 2), where [i] is the key and + * [i+1] is the key's value, to be communicated to the broker + * as additional key-value pairs during the initial client response as per + * https://tools.ietf.org/html/rfc7628#section-3.1. The key-value pairs are + * copied. + * @param extension_size the number of SASL extension keys plus values, + * which must be a non-negative multiple of 2. + * @param errstr A human readable error string (nul-terminated) is written to + * this location that must be of at least \p errstr_size bytes. + * The \p errstr is only written in case of error. + * @param errstr_size Writable size in \p errstr. + * + * The SASL/OAUTHBEARER token refresh callback or event handler should invoke + * this method upon success. The extension keys must not include the reserved + * key "`auth`", and all extension keys and values must conform to the required + * format as per https://tools.ietf.org/html/rfc7628#section-3.1: + * + * key = 1*(ALPHA) + * value = *(VCHAR / SP / HTAB / CR / LF ) + * + * @returns \c RD_KAFKA_RESP_ERR_NO_ERROR on success, otherwise \p errstr set + * and:
+ * \c RD_KAFKA_RESP_ERR__INVALID_ARG if any of the arguments are + * invalid;
+ * \c RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED if SASL/OAUTHBEARER is not + * supported by this build;
+ * \c RD_KAFKA_RESP_ERR__STATE if SASL/OAUTHBEARER is supported but is + * not configured as the client's authentication mechanism.
+ * + * @sa rd_kafka_oauthbearer_set_token_failure + * @sa rd_kafka_conf_set_oauthbearer_token_refresh_cb + */ +RD_EXPORT +rd_kafka_resp_err_t +rd_kafka_oauthbearer_set_token (rd_kafka_t *rk, + const char *token_value, + int64_t md_lifetime_ms, + const char *md_principal_name, + const char **extensions, size_t extension_size, + char *errstr, size_t errstr_size); + +/** + * @brief SASL/OAUTHBEARER token refresh failure indicator. + * + * @param rk Client instance. + * @param errstr mandatory human readable error reason for failing to acquire + * a token. + * + * The SASL/OAUTHBEARER token refresh callback or event handler should invoke + * this method upon failure. + * + * @returns \c RD_KAFKA_RESP_ERR_NO_ERROR on success, otherwise:
+ * \c RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED if SASL/OAUTHBEARER is not + * supported by this build;
+ * \c RD_KAFKA_RESP_ERR__STATE if SASL/OAUTHBEARER is supported but is + * not configured as the client's authentication mechanism,
+ * \c RD_KAFKA_RESP_ERR__INVALID_ARG if no error string is supplied. + * + * @sa rd_kafka_oauthbearer_set_token + * @sa rd_kafka_conf_set_oauthbearer_token_refresh_cb + */ +RD_EXPORT +rd_kafka_resp_err_t +rd_kafka_oauthbearer_set_token_failure (rd_kafka_t *rk, const char *errstr); + +/**@}*/ + + +/** + * @name Transactional producer API + * + * The transactional producer operates on top of the idempotent producer, + * and provides full exactly-once semantics (EOS) for Apache Kafka when used + * with the transaction aware consumer (\c isolation.level=read_committed). + * + * A producer instance is configured for transactions by setting the + * \c transactional.id to an identifier unique for the application. This + * id will be used to fence stale transactions from previous instances of + * the application, typically following an outage or crash. + * + * After creating the transactional producer instance using rd_kafka_new() + * the transactional state must be initialized by calling + * rd_kafka_init_transactions(). This is a blocking call that will + * acquire a runtime producer id from the transaction coordinator broker + * as well as abort any stale transactions and fence any still running producer + * instances with the same \c transactional.id. + * + * Once transactions are initialized the application may begin a new + * transaction by calling rd_kafka_begin_transaction(). + * A producer instance may only have one single on-going transaction. + * + * Any messages produced after the transaction has been started will + * belong to the ongoing transaction and will be committed or aborted + * atomically. + * It is not permitted to produce messages outside a transaction + * boundary, e.g., before rd_kafka_begin_transaction() or after + * rd_kafka_commit_transaction(), rd_kafka_abort_transaction(), or after + * the current transaction has failed. + * + * If consumed messages are used as input to the transaction, the consumer + * instance must be configured with \c enable.auto.commit set to \c false. + * To commit the consumed offsets along with the transaction pass the + * list of consumed partitions and the last offset processed + 1 to + * rd_kafka_send_offsets_to_transaction() prior to committing the transaction. + * This allows an aborted transaction to be restarted using the previously + * committed offsets. + * + * To commit the produced messages, and any consumed offsets, to the + * current transaction, call rd_kafka_commit_transaction(). + * This call will block until the transaction has been fully committed or + * failed (typically due to fencing by a newer producer instance). + * + * Alternatively, if processing fails, or an abortable transaction error is + * raised, the transaction needs to be aborted by calling + * rd_kafka_abort_transaction() which marks any produced messages and + * offset commits as aborted. + * + * After the current transaction has been committed or aborted a new + * transaction may be started by calling rd_kafka_begin_transaction() again. + * + * @par Retriable errors + * Some error cases allow the attempted operation to be retried, this is + * indicated by the error object having the retriable flag set which can + * be detected by calling rd_kafka_error_is_retriable(). + * When this flag is set the application may retry the operation immediately + * or preferably after a shorter grace period (to avoid busy-looping). + * Retriable errors include timeouts, broker transport failures, etc. + * + * @par Abortable errors + * An ongoing transaction may fail permanently due to various errors, + * such as transaction coordinator becoming unavailable, write failures to the + * Apache Kafka log, under-replicated partitions, etc. + * At this point the producer application must abort the current transaction + * using rd_kafka_abort_transaction() and optionally start a new transaction + * by calling rd_kafka_begin_transaction(). + * Whether an error is abortable or not is detected by calling + * rd_kafka_error_txn_requires_abort() on the returned error object. + * + * @par Fatal errors + * While the underlying idempotent producer will typically only raise + * fatal errors for unrecoverable cluster errors where the idempotency + * guarantees can't be maintained, most of these are treated as abortable by + * the transactional producer since transactions may be aborted and retried + * in their entirety; + * The transactional producer on the other hand introduces a set of additional + * fatal errors which the application needs to handle by shutting down the + * producer and terminate. There is no way for a producer instance to recover + * from fatal errors. + * Whether an error is fatal or not is detected by calling + * rd_kafka_error_is_fatal() on the returned error object or by checking + * the global rd_kafka_fatal_error() code. + * Fatal errors are raised by triggering the \c error_cb (see the + * Fatal error chapter in INTRODUCTION.md for more information), and any + * sub-sequent transactional API calls will return RD_KAFKA_RESP_ERR__FATAL + * or have the fatal flag set (see rd_kafka_error_is_fatal()). + * The originating fatal error code can be retrieved by calling + * rd_kafka_fatal_error(). + * + * @par Handling of other errors + * For errors that have neither retriable, abortable or the fatal flag set + * it is not always obvious how to handle them. While some of these errors + * may be indicative of bugs in the application code, such as when + * an invalid parameter is passed to a method, other errors might originate + * from the broker and be passed thru as-is to the application. + * The general recommendation is to treat these errors, that have + * neither the retriable or abortable flags set, as fatal. + * + * @par Error handling example + * @code + * retry: + * rd_kafka_error_t *error; + * + * error = rd_kafka_commit_transaction(producer, 10*1000); + * if (!error) + * return success; + * else if (rd_kafka_error_txn_requires_abort(error)) { + * do_abort_transaction_and_reset_inputs(); + * } else if (rd_kafka_error_is_retriable(error)) { + * rd_kafka_error_destroy(error); + * goto retry; + * } else { // treat all other errors as fatal errors + * fatal_error(rd_kafka_error_string(error)); + * } + * rd_kafka_error_destroy(error); + * @endcode + * + * + * @{ + */ + + +/** + * @brief Initialize transactions for the producer instance. + * + * This function ensures any transactions initiated by previous instances + * of the producer with the same \c transactional.id are completed. + * If the previous instance failed with a transaction in progress the + * previous transaction will be aborted. + * This function needs to be called before any other transactional or + * produce functions are called when the \c transactional.id is configured. + * + * If the last transaction had begun completion (following transaction commit) + * but not yet finished, this function will await the previous transaction's + * completion. + * + * When any previous transactions have been fenced this function + * will acquire the internal producer id and epoch, used in all future + * transactional messages issued by this producer instance. + * + * @param rk Producer instance. + * @param timeout_ms The maximum time to block. On timeout the operation + * may continue in the background, depending on state, + * and it is okay to call init_transactions() again. + * + * @remark This function may block up to \p timeout_ms milliseconds. + * + * @returns NULL on success or an error object on failure. + * Check whether the returned error object permits retrying + * by calling rd_kafka_error_is_retriable(), or whether a fatal + * error has been raised by calling rd_kafka_error_is_fatal(). + * Error codes: + * RD_KAFKA_RESP_ERR__TIMED_OUT if the transaction coordinator + * could be not be contacted within \p timeout_ms (retriable), + * RD_KAFKA_RESP_ERR_COORDINATOR_NOT_AVAILABLE if the transaction + * coordinator is not available (retriable), + * RD_KAFKA_RESP_ERR_CONCURRENT_TRANSACTIONS if a previous transaction + * would not complete within \p timeout_ms (retriable), + * RD_KAFKA_RESP_ERR__STATE if transactions have already been started + * or upon fatal error, + * RD_KAFKA_RESP_ERR__UNSUPPORTED_FEATURE if the broker(s) do not + * support transactions ( +#include +#include "select_rdkafka.h" +#include "glue_rdkafka.h" + +void setup_rkmessage (rd_kafka_message_t *rkmessage, + rd_kafka_topic_t *rkt, int32_t partition, + const void *payload, size_t len, + void *key, size_t keyLen, void *opaque) { + rkmessage->rkt = rkt; + rkmessage->partition = partition; + rkmessage->payload = (void *)payload; + rkmessage->len = len; + rkmessage->key = (void *)key; + rkmessage->key_len = keyLen; + rkmessage->_private = opaque; +} +*/ +import "C" + +// TimestampType is a the Message timestamp type or source +// +type TimestampType int + +const ( + // TimestampNotAvailable indicates no timestamp was set, or not available due to lacking broker support + TimestampNotAvailable = TimestampType(C.RD_KAFKA_TIMESTAMP_NOT_AVAILABLE) + // TimestampCreateTime indicates timestamp set by producer (source time) + TimestampCreateTime = TimestampType(C.RD_KAFKA_TIMESTAMP_CREATE_TIME) + // TimestampLogAppendTime indicates timestamp set set by broker (store time) + TimestampLogAppendTime = TimestampType(C.RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME) +) + +func (t TimestampType) String() string { + switch t { + case TimestampCreateTime: + return "CreateTime" + case TimestampLogAppendTime: + return "LogAppendTime" + case TimestampNotAvailable: + fallthrough + default: + return "NotAvailable" + } +} + +// Message represents a Kafka message +type Message struct { + TopicPartition TopicPartition + Value []byte + Key []byte + Timestamp time.Time + TimestampType TimestampType + Opaque interface{} + Headers []Header +} + +// String returns a human readable representation of a Message. +// Key and payload are not represented. +func (m *Message) String() string { + var topic string + if m.TopicPartition.Topic != nil { + topic = *m.TopicPartition.Topic + } else { + topic = "" + } + return fmt.Sprintf("%s[%d]@%s", topic, m.TopicPartition.Partition, m.TopicPartition.Offset) +} + +func (h *handle) getRktFromMessage(msg *Message) (crkt *C.rd_kafka_topic_t) { + if msg.TopicPartition.Topic == nil { + return nil + } + + return h.getRkt(*msg.TopicPartition.Topic) +} + +// setupHeadersFromGlueMsg converts the C tmp headers in gMsg to +// Go Headers in msg. +// gMsg.tmphdrs will be freed. +func setupHeadersFromGlueMsg(msg *Message, gMsg *C.glue_msg_t) { + msg.Headers = make([]Header, gMsg.tmphdrsCnt) + for n := range msg.Headers { + tmphdr := (*[1 << 30]C.tmphdr_t)(unsafe.Pointer(gMsg.tmphdrs))[n] + msg.Headers[n].Key = C.GoString(tmphdr.key) + if tmphdr.val != nil { + msg.Headers[n].Value = C.GoBytes(unsafe.Pointer(tmphdr.val), C.int(tmphdr.size)) + } else { + msg.Headers[n].Value = nil + } + } + C.free(unsafe.Pointer(gMsg.tmphdrs)) +} + +func (h *handle) newMessageFromGlueMsg(gMsg *C.glue_msg_t) (msg *Message) { + msg = &Message{} + + if gMsg.ts != -1 { + ts := int64(gMsg.ts) + msg.TimestampType = TimestampType(gMsg.tstype) + msg.Timestamp = time.Unix(ts/1000, (ts%1000)*1000000) + } + + if gMsg.tmphdrsCnt > 0 { + setupHeadersFromGlueMsg(msg, gMsg) + } + + h.setupMessageFromC(msg, gMsg.msg) + + return msg +} + +// setupMessageFromC sets up a message object from a C rd_kafka_message_t +func (h *handle) setupMessageFromC(msg *Message, cmsg *C.rd_kafka_message_t) { + if cmsg.rkt != nil { + topic := h.getTopicNameFromRkt(cmsg.rkt) + msg.TopicPartition.Topic = &topic + } + msg.TopicPartition.Partition = int32(cmsg.partition) + if cmsg.payload != nil && h.msgFields.Value { + msg.Value = C.GoBytes(unsafe.Pointer(cmsg.payload), C.int(cmsg.len)) + } + if cmsg.key != nil && h.msgFields.Key { + msg.Key = C.GoBytes(unsafe.Pointer(cmsg.key), C.int(cmsg.key_len)) + } + if h.msgFields.Headers { + var gMsg C.glue_msg_t + gMsg.msg = cmsg + gMsg.want_hdrs = C.int8_t(1) + chdrsToTmphdrs(&gMsg) + if gMsg.tmphdrsCnt > 0 { + setupHeadersFromGlueMsg(msg, &gMsg) + } + } + msg.TopicPartition.Offset = Offset(cmsg.offset) + if cmsg.err != 0 { + msg.TopicPartition.Error = newError(cmsg.err) + } +} + +// newMessageFromC creates a new message object from a C rd_kafka_message_t +// NOTE: For use with Producer: does not set message timestamp fields. +func (h *handle) newMessageFromC(cmsg *C.rd_kafka_message_t) (msg *Message) { + msg = &Message{} + + h.setupMessageFromC(msg, cmsg) + + return msg +} + +// messageToC sets up cmsg as a clone of msg +func (h *handle) messageToC(msg *Message, cmsg *C.rd_kafka_message_t) { + var valp unsafe.Pointer + var keyp unsafe.Pointer + + // to circumvent Cgo constraints we need to allocate C heap memory + // for both Value and Key (one allocation back to back) + // and copy the bytes from Value and Key to the C memory. + // We later tell librdkafka (in produce()) to free the + // C memory pointer when it is done. + var payload unsafe.Pointer + + valueLen := 0 + keyLen := 0 + if msg.Value != nil { + valueLen = len(msg.Value) + } + if msg.Key != nil { + keyLen = len(msg.Key) + } + + allocLen := valueLen + keyLen + if allocLen > 0 { + payload = C.malloc(C.size_t(allocLen)) + if valueLen > 0 { + copy((*[1 << 30]byte)(payload)[0:valueLen], msg.Value) + valp = payload + } + if keyLen > 0 { + copy((*[1 << 30]byte)(payload)[valueLen:allocLen], msg.Key) + keyp = unsafe.Pointer(&((*[1 << 31]byte)(payload)[valueLen])) + } + } + + cmsg.rkt = h.getRktFromMessage(msg) + cmsg.partition = C.int32_t(msg.TopicPartition.Partition) + cmsg.payload = valp + cmsg.len = C.size_t(valueLen) + cmsg.key = keyp + cmsg.key_len = C.size_t(keyLen) + cmsg._private = nil +} + +// used for testing messageToC performance +func (h *handle) messageToCDummy(msg *Message) { + var cmsg C.rd_kafka_message_t + h.messageToC(msg, &cmsg) +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/metadata.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/metadata.go new file mode 100644 index 0000000..3b195f4 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/metadata.go @@ -0,0 +1,180 @@ +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "unsafe" +) + +/* +#include +#include "select_rdkafka.h" + +struct rd_kafka_metadata_broker *_getMetadata_broker_element(struct rd_kafka_metadata *m, int i) { + return &m->brokers[i]; +} + +struct rd_kafka_metadata_topic *_getMetadata_topic_element(struct rd_kafka_metadata *m, int i) { + return &m->topics[i]; +} + +struct rd_kafka_metadata_partition *_getMetadata_partition_element(struct rd_kafka_metadata *m, int topic_idx, int partition_idx) { + return &m->topics[topic_idx].partitions[partition_idx]; +} + +int32_t _get_int32_element (int32_t *arr, int i) { + return arr[i]; +} + +*/ +import "C" + +// BrokerMetadata contains per-broker metadata +type BrokerMetadata struct { + ID int32 + Host string + Port int +} + +// PartitionMetadata contains per-partition metadata +type PartitionMetadata struct { + ID int32 + Error Error + Leader int32 + Replicas []int32 + Isrs []int32 +} + +// TopicMetadata contains per-topic metadata +type TopicMetadata struct { + Topic string + Partitions []PartitionMetadata + Error Error +} + +// Metadata contains broker and topic metadata for all (matching) topics +type Metadata struct { + Brokers []BrokerMetadata + Topics map[string]TopicMetadata + + OriginatingBroker BrokerMetadata +} + +// getMetadata queries broker for cluster and topic metadata. +// If topic is non-nil only information about that topic is returned, else if +// allTopics is false only information about locally used topics is returned, +// else information about all topics is returned. +func getMetadata(H Handle, topic *string, allTopics bool, timeoutMs int) (*Metadata, error) { + h := H.gethandle() + + var rkt *C.rd_kafka_topic_t + if topic != nil { + rkt = h.getRkt(*topic) + } + + var cMd *C.struct_rd_kafka_metadata + cErr := C.rd_kafka_metadata(h.rk, bool2cint(allTopics), + rkt, &cMd, C.int(timeoutMs)) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cErr) + } + + m := Metadata{} + defer C.rd_kafka_metadata_destroy(cMd) + + m.Brokers = make([]BrokerMetadata, cMd.broker_cnt) + for i := 0; i < int(cMd.broker_cnt); i++ { + b := C._getMetadata_broker_element(cMd, C.int(i)) + m.Brokers[i] = BrokerMetadata{int32(b.id), C.GoString(b.host), + int(b.port)} + } + + m.Topics = make(map[string]TopicMetadata, int(cMd.topic_cnt)) + for i := 0; i < int(cMd.topic_cnt); i++ { + t := C._getMetadata_topic_element(cMd, C.int(i)) + + thisTopic := C.GoString(t.topic) + m.Topics[thisTopic] = TopicMetadata{Topic: thisTopic, + Error: newError(t.err), + Partitions: make([]PartitionMetadata, int(t.partition_cnt))} + + for j := 0; j < int(t.partition_cnt); j++ { + p := C._getMetadata_partition_element(cMd, C.int(i), C.int(j)) + m.Topics[thisTopic].Partitions[j] = PartitionMetadata{ + ID: int32(p.id), + Error: newError(p.err), + Leader: int32(p.leader)} + m.Topics[thisTopic].Partitions[j].Replicas = make([]int32, int(p.replica_cnt)) + for ir := 0; ir < int(p.replica_cnt); ir++ { + m.Topics[thisTopic].Partitions[j].Replicas[ir] = int32(C._get_int32_element(p.replicas, C.int(ir))) + } + + m.Topics[thisTopic].Partitions[j].Isrs = make([]int32, int(p.isr_cnt)) + for ii := 0; ii < int(p.isr_cnt); ii++ { + m.Topics[thisTopic].Partitions[j].Isrs[ii] = int32(C._get_int32_element(p.isrs, C.int(ii))) + } + } + } + + m.OriginatingBroker = BrokerMetadata{int32(cMd.orig_broker_id), + C.GoString(cMd.orig_broker_name), 0} + + return &m, nil +} + +// queryWatermarkOffsets returns the broker's low and high offsets for the given topic +// and partition. +func queryWatermarkOffsets(H Handle, topic string, partition int32, timeoutMs int) (low, high int64, err error) { + h := H.gethandle() + + ctopic := C.CString(topic) + defer C.free(unsafe.Pointer(ctopic)) + + var cLow, cHigh C.int64_t + + e := C.rd_kafka_query_watermark_offsets(h.rk, ctopic, C.int32_t(partition), + &cLow, &cHigh, C.int(timeoutMs)) + if e != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return 0, 0, newError(e) + } + + low = int64(cLow) + high = int64(cHigh) + return low, high, nil +} + +// getWatermarkOffsets returns the clients cached low and high offsets for the given topic +// and partition. +func getWatermarkOffsets(H Handle, topic string, partition int32) (low, high int64, err error) { + h := H.gethandle() + + ctopic := C.CString(topic) + defer C.free(unsafe.Pointer(ctopic)) + + var cLow, cHigh C.int64_t + + e := C.rd_kafka_get_watermark_offsets(h.rk, ctopic, C.int32_t(partition), + &cLow, &cHigh) + if e != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return 0, 0, newError(e) + } + + low = int64(cLow) + high = int64(cHigh) + + return low, high, nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/misc.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/misc.go new file mode 100644 index 0000000..6d602ce --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/misc.go @@ -0,0 +1,35 @@ +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import "C" + +// bool2int converts a bool to a C.int (1 or 0) +func bool2cint(b bool) C.int { + if b { + return 1 + } + return 0 +} + +// cint2bool converts a C.int to a bool +func cint2bool(v C.int) bool { + if v == 0 { + return false + } + return true +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/offset.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/offset.go new file mode 100644 index 0000000..4cb1819 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/offset.go @@ -0,0 +1,145 @@ +/** + * Copyright 2017 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "fmt" + "strconv" +) + +/* +#include +#include "select_rdkafka.h" + +static int64_t _c_rdkafka_offset_tail(int64_t rel) { + return RD_KAFKA_OFFSET_TAIL(rel); +} +*/ +import "C" + +// Offset type (int64) with support for canonical names +type Offset int64 + +// OffsetBeginning represents the earliest offset (logical) +const OffsetBeginning = Offset(C.RD_KAFKA_OFFSET_BEGINNING) + +// OffsetEnd represents the latest offset (logical) +const OffsetEnd = Offset(C.RD_KAFKA_OFFSET_END) + +// OffsetInvalid represents an invalid/unspecified offset +const OffsetInvalid = Offset(C.RD_KAFKA_OFFSET_INVALID) + +// OffsetStored represents a stored offset +const OffsetStored = Offset(C.RD_KAFKA_OFFSET_STORED) + +func (o Offset) String() string { + switch o { + case OffsetBeginning: + return "beginning" + case OffsetEnd: + return "end" + case OffsetInvalid: + return "unset" + case OffsetStored: + return "stored" + default: + return fmt.Sprintf("%d", int64(o)) + } +} + +// Set offset value, see NewOffset() +func (o *Offset) Set(offset interface{}) error { + n, err := NewOffset(offset) + + if err == nil { + *o = n + } + + return err +} + +// NewOffset creates a new Offset using the provided logical string, or an +// absolute int64 offset value. +// Logical offsets: "beginning", "earliest", "end", "latest", "unset", "invalid", "stored" +func NewOffset(offset interface{}) (Offset, error) { + + switch v := offset.(type) { + case string: + switch v { + case "beginning": + fallthrough + case "earliest": + return Offset(OffsetBeginning), nil + + case "end": + fallthrough + case "latest": + return Offset(OffsetEnd), nil + + case "unset": + fallthrough + case "invalid": + return Offset(OffsetInvalid), nil + + case "stored": + return Offset(OffsetStored), nil + + default: + off, err := strconv.Atoi(v) + return Offset(off), err + } + + case int: + return Offset((int64)(v)), nil + case int64: + return Offset(v), nil + default: + return OffsetInvalid, newErrorFromString(ErrInvalidArg, + fmt.Sprintf("Invalid offset type: %t", v)) + } +} + +// OffsetTail returns the logical offset relativeOffset from current end of partition +func OffsetTail(relativeOffset Offset) Offset { + return Offset(C._c_rdkafka_offset_tail(C.int64_t(relativeOffset))) +} + +// offsetsForTimes looks up offsets by timestamp for the given partitions. +// +// The returned offset for each partition is the earliest offset whose +// timestamp is greater than or equal to the given timestamp in the +// corresponding partition. If the provided timestamp exceeds that of the +// last message in the partition, a value of -1 will be returned. +// +// The timestamps to query are represented as `.Offset` in the `times` +// argument and the looked up offsets are represented as `.Offset` in the returned +// `offsets` list. +// +// The function will block for at most timeoutMs milliseconds. +// +// Duplicate Topic+Partitions are not supported. +// Per-partition errors may be returned in the `.Error` field. +func offsetsForTimes(H Handle, times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) { + cparts := newCPartsFromTopicPartitions(times) + defer C.rd_kafka_topic_partition_list_destroy(cparts) + cerr := C.rd_kafka_offsets_for_times(H.gethandle().rk, cparts, C.int(timeoutMs)) + if cerr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return nil, newError(cerr) + } + + return newTopicPartitionsFromCparts(cparts), nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/producer.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/producer.go new file mode 100644 index 0000000..f68854a --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/producer.go @@ -0,0 +1,918 @@ +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import ( + "context" + "fmt" + "math" + "time" + "unsafe" +) + +/* +#include +#include "select_rdkafka.h" +#include "glue_rdkafka.h" + + +#ifdef RD_KAFKA_V_HEADERS +// Convert tmphdrs to chdrs (created by this function). +// If tmphdr.size == -1: value is considered Null +// tmphdr.size == 0: value is considered empty (ignored) +// tmphdr.size > 0: value is considered non-empty +// +// WARNING: The header keys and values will be freed by this function. +void tmphdrs_to_chdrs (tmphdr_t *tmphdrs, size_t tmphdrsCnt, + rd_kafka_headers_t **chdrs) { + size_t i; + + *chdrs = rd_kafka_headers_new(tmphdrsCnt); + + for (i = 0 ; i < tmphdrsCnt ; i++) { + rd_kafka_header_add(*chdrs, + tmphdrs[i].key, -1, + tmphdrs[i].size == -1 ? NULL : + (tmphdrs[i].size == 0 ? "" : tmphdrs[i].val), + tmphdrs[i].size == -1 ? 0 : tmphdrs[i].size); + if (tmphdrs[i].size > 0) + free((void *)tmphdrs[i].val); + free((void *)tmphdrs[i].key); + } +} + +#else +void free_tmphdrs (tmphdr_t *tmphdrs, size_t tmphdrsCnt) { + size_t i; + for (i = 0 ; i < tmphdrsCnt ; i++) { + if (tmphdrs[i].size > 0) + free((void *)tmphdrs[i].val); + free((void *)tmphdrs[i].key); + } +} +#endif + + +rd_kafka_resp_err_t do_produce (rd_kafka_t *rk, + rd_kafka_topic_t *rkt, int32_t partition, + int msgflags, + int valIsNull, void *val, size_t val_len, + int keyIsNull, void *key, size_t key_len, + int64_t timestamp, + tmphdr_t *tmphdrs, size_t tmphdrsCnt, + uintptr_t cgoid) { + void *valp = valIsNull ? NULL : val; + void *keyp = keyIsNull ? NULL : key; +#ifdef RD_KAFKA_V_TIMESTAMP +rd_kafka_resp_err_t err; +#ifdef RD_KAFKA_V_HEADERS + rd_kafka_headers_t *hdrs = NULL; +#endif +#endif + + + if (tmphdrsCnt > 0) { +#ifdef RD_KAFKA_V_HEADERS + tmphdrs_to_chdrs(tmphdrs, tmphdrsCnt, &hdrs); +#else + free_tmphdrs(tmphdrs, tmphdrsCnt); + return RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED; +#endif + } + + +#ifdef RD_KAFKA_V_TIMESTAMP + err = rd_kafka_producev(rk, + RD_KAFKA_V_RKT(rkt), + RD_KAFKA_V_PARTITION(partition), + RD_KAFKA_V_MSGFLAGS(msgflags), + RD_KAFKA_V_VALUE(valp, val_len), + RD_KAFKA_V_KEY(keyp, key_len), + RD_KAFKA_V_TIMESTAMP(timestamp), +#ifdef RD_KAFKA_V_HEADERS + RD_KAFKA_V_HEADERS(hdrs), +#endif + RD_KAFKA_V_OPAQUE((void *)cgoid), + RD_KAFKA_V_END); +#ifdef RD_KAFKA_V_HEADERS + if (err && hdrs) + rd_kafka_headers_destroy(hdrs); +#endif + return err; +#else + if (timestamp) + return RD_KAFKA_RESP_ERR__NOT_IMPLEMENTED; + if (rd_kafka_produce(rkt, partition, msgflags, + valp, val_len, + keyp, key_len, + (void *)cgoid) == -1) + return rd_kafka_last_error(); + else + return RD_KAFKA_RESP_ERR_NO_ERROR; +#endif +} +*/ +import "C" + +// Producer implements a High-level Apache Kafka Producer instance +type Producer struct { + events chan Event + produceChannel chan *Message + handle handle + + // Terminates the poller() goroutine + pollerTermChan chan bool +} + +// String returns a human readable name for a Producer instance +func (p *Producer) String() string { + return p.handle.String() +} + +// get_handle implements the Handle interface +func (p *Producer) gethandle() *handle { + return &p.handle +} + +func (p *Producer) produce(msg *Message, msgFlags int, deliveryChan chan Event) error { + if msg == nil || msg.TopicPartition.Topic == nil || len(*msg.TopicPartition.Topic) == 0 { + return newErrorFromString(ErrInvalidArg, "") + } + + crkt := p.handle.getRkt(*msg.TopicPartition.Topic) + + // Three problems: + // 1) There's a difference between an empty Value or Key (length 0, proper pointer) and + // a null Value or Key (length 0, null pointer). + // 2) we need to be able to send a null Value or Key, but the unsafe.Pointer(&slice[0]) + // dereference can't be performed on a nil slice. + // 3) cgo's pointer checking requires the unsafe.Pointer(slice..) call to be made + // in the call to the C function. + // + // Solution: + // Keep track of whether the Value or Key were nil (1), but let the valp and keyp pointers + // point to a 1-byte slice (but the length to send is still 0) so that the dereference (2) + // works. + // Then perform the unsafe.Pointer() on the valp and keyp pointers (which now either point + // to the original msg.Value and msg.Key or to the 1-byte slices) in the call to C (3). + // + var valp []byte + var keyp []byte + oneByte := []byte{0} + var valIsNull C.int + var keyIsNull C.int + var valLen int + var keyLen int + + if msg.Value == nil { + valIsNull = 1 + valLen = 0 + valp = oneByte + } else { + valLen = len(msg.Value) + if valLen > 0 { + valp = msg.Value + } else { + valp = oneByte + } + } + + if msg.Key == nil { + keyIsNull = 1 + keyLen = 0 + keyp = oneByte + } else { + keyLen = len(msg.Key) + if keyLen > 0 { + keyp = msg.Key + } else { + keyp = oneByte + } + } + + var cgoid int + + // Per-message state that needs to be retained through the C code: + // delivery channel (if specified) + // message opaque (if specified) + // Since these cant be passed as opaque pointers to the C code, + // due to cgo constraints, we add them to a per-producer map for lookup + // when the C code triggers the callbacks or events. + if deliveryChan != nil || msg.Opaque != nil { + cgoid = p.handle.cgoPut(cgoDr{deliveryChan: deliveryChan, opaque: msg.Opaque}) + } + + var timestamp int64 + if !msg.Timestamp.IsZero() { + timestamp = msg.Timestamp.UnixNano() / 1000000 + } + + // Convert headers to C-friendly tmphdrs + var tmphdrs []C.tmphdr_t + tmphdrsCnt := len(msg.Headers) + + if tmphdrsCnt > 0 { + tmphdrs = make([]C.tmphdr_t, tmphdrsCnt) + + for n, hdr := range msg.Headers { + // Make a copy of the key + // to avoid runtime panic with + // foreign Go pointers in cgo. + tmphdrs[n].key = C.CString(hdr.Key) + if hdr.Value != nil { + tmphdrs[n].size = C.ssize_t(len(hdr.Value)) + if tmphdrs[n].size > 0 { + // Make a copy of the value + // to avoid runtime panic with + // foreign Go pointers in cgo. + tmphdrs[n].val = C.CBytes(hdr.Value) + } + } else { + // null value + tmphdrs[n].size = C.ssize_t(-1) + } + } + } else { + // no headers, need a dummy tmphdrs of size 1 to avoid index + // out of bounds panic in do_produce() call below. + // tmphdrsCnt will be 0. + tmphdrs = []C.tmphdr_t{{nil, nil, 0}} + } + + cErr := C.do_produce(p.handle.rk, crkt, + C.int32_t(msg.TopicPartition.Partition), + C.int(msgFlags)|C.RD_KAFKA_MSG_F_COPY, + valIsNull, unsafe.Pointer(&valp[0]), C.size_t(valLen), + keyIsNull, unsafe.Pointer(&keyp[0]), C.size_t(keyLen), + C.int64_t(timestamp), + (*C.tmphdr_t)(unsafe.Pointer(&tmphdrs[0])), C.size_t(tmphdrsCnt), + (C.uintptr_t)(cgoid)) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + if cgoid != 0 { + p.handle.cgoGet(cgoid) + } + return newError(cErr) + } + + return nil +} + +// Produce single message. +// This is an asynchronous call that enqueues the message on the internal +// transmit queue, thus returning immediately. +// The delivery report will be sent on the provided deliveryChan if specified, +// or on the Producer object's Events() channel if not. +// msg.Timestamp requires librdkafka >= 0.9.4 (else returns ErrNotImplemented), +// api.version.request=true, and broker >= 0.10.0.0. +// msg.Headers requires librdkafka >= 0.11.4 (else returns ErrNotImplemented), +// api.version.request=true, and broker >= 0.11.0.0. +// Returns an error if message could not be enqueued. +func (p *Producer) Produce(msg *Message, deliveryChan chan Event) error { + return p.produce(msg, 0, deliveryChan) +} + +// Produce a batch of messages. +// These batches do not relate to the message batches sent to the broker, the latter +// are collected on the fly internally in librdkafka. +// WARNING: This is an experimental API. +// NOTE: timestamps and headers are not supported with this API. +func (p *Producer) produceBatch(topic string, msgs []*Message, msgFlags int) error { + crkt := p.handle.getRkt(topic) + + cmsgs := make([]C.rd_kafka_message_t, len(msgs)) + for i, m := range msgs { + p.handle.messageToC(m, &cmsgs[i]) + } + r := C.rd_kafka_produce_batch(crkt, C.RD_KAFKA_PARTITION_UA, C.int(msgFlags)|C.RD_KAFKA_MSG_F_FREE, + (*C.rd_kafka_message_t)(&cmsgs[0]), C.int(len(msgs))) + if r == -1 { + return newError(C.rd_kafka_last_error()) + } + + return nil +} + +// Events returns the Events channel (read) +func (p *Producer) Events() chan Event { + return p.events +} + +// Logs returns the Log channel (if enabled), else nil +func (p *Producer) Logs() chan LogEvent { + return p.handle.logs +} + +// ProduceChannel returns the produce *Message channel (write) +func (p *Producer) ProduceChannel() chan *Message { + return p.produceChannel +} + +// Len returns the number of messages and requests waiting to be transmitted to the broker +// as well as delivery reports queued for the application. +// Includes messages on ProduceChannel. +func (p *Producer) Len() int { + return len(p.produceChannel) + len(p.events) + int(C.rd_kafka_outq_len(p.handle.rk)) +} + +// Flush and wait for outstanding messages and requests to complete delivery. +// Includes messages on ProduceChannel. +// Runs until value reaches zero or on timeoutMs. +// Returns the number of outstanding events still un-flushed. +func (p *Producer) Flush(timeoutMs int) int { + termChan := make(chan bool) // unused stand-in termChan + + d, _ := time.ParseDuration(fmt.Sprintf("%dms", timeoutMs)) + tEnd := time.Now().Add(d) + for p.Len() > 0 { + remain := tEnd.Sub(time.Now()).Seconds() + if remain <= 0.0 { + return p.Len() + } + + p.handle.eventPoll(p.events, + int(math.Min(100, remain*1000)), 1000, termChan) + } + + return 0 +} + +// Close a Producer instance. +// The Producer object or its channels are no longer usable after this call. +func (p *Producer) Close() { + // Wait for poller() (signaled by closing pollerTermChan) + // and channel_producer() (signaled by closing ProduceChannel) + close(p.pollerTermChan) + close(p.produceChannel) + p.handle.waitGroup.Wait() + + close(p.events) + + p.handle.cleanup() + + C.rd_kafka_destroy(p.handle.rk) +} + +const ( + // PurgeInFlight purges messages in-flight to or from the broker. + // Purging these messages will void any future acknowledgements from the + // broker, making it impossible for the application to know if these + // messages were successfully delivered or not. + // Retrying these messages may lead to duplicates. + PurgeInFlight = int(C.RD_KAFKA_PURGE_F_INFLIGHT) + + // PurgeQueue Purge messages in internal queues. + PurgeQueue = int(C.RD_KAFKA_PURGE_F_QUEUE) + + // PurgeNonBlocking Don't wait for background thread queue purging to finish. + PurgeNonBlocking = int(C.RD_KAFKA_PURGE_F_NON_BLOCKING) +) + +// Purge messages currently handled by this producer instance. +// +// flags is a combination of PurgeQueue, PurgeInFlight and PurgeNonBlocking. +// +// The application will need to call Poll(), Flush() or read the Events() channel +// after this call to serve delivery reports for the purged messages. +// +// Messages purged from internal queues fail with the delivery report +// error code set to ErrPurgeQueue, while purged messages that +// are in-flight to or from the broker will fail with the error code set to +// ErrPurgeInflight. +// +// Warning: Purging messages that are in-flight to or from the broker +// will ignore any sub-sequent acknowledgement for these messages +// received from the broker, effectively making it impossible +// for the application to know if the messages were successfully +// produced or not. This may result in duplicate messages if the +// application retries these messages at a later time. +// +// Note: This call may block for a short time while background thread +// queues are purged. +// +// Returns nil on success, ErrInvalidArg if the purge flags are invalid or unknown. +func (p *Producer) Purge(flags int) error { + cErr := C.rd_kafka_purge(p.handle.rk, C.int(flags)) + if cErr != C.RD_KAFKA_RESP_ERR_NO_ERROR { + return newError(cErr) + } + + return nil +} + +// NewProducer creates a new high-level Producer instance. +// +// conf is a *ConfigMap with standard librdkafka configuration properties. +// +// Supported special configuration properties (type, default): +// go.batch.producer (bool, false) - EXPERIMENTAL: Enable batch producer (for increased performance). +// These batches do not relate to Kafka message batches in any way. +// Note: timestamps and headers are not supported with this interface. +// go.delivery.reports (bool, true) - Forward per-message delivery reports to the +// Events() channel. +// go.delivery.report.fields (string, "key,value") - Comma separated list of fields to enable for delivery reports. +// Allowed values: all, none (or empty string), key, value, headers +// Warning: There is a performance penalty to include headers in the delivery report. +// go.events.channel.size (int, 1000000) - Events(). +// go.produce.channel.size (int, 1000000) - ProduceChannel() buffer size (in number of messages) +// go.logs.channel.enable (bool, false) - Forward log to Logs() channel. +// go.logs.channel (chan kafka.LogEvent, nil) - Forward logs to application-provided channel instead of Logs(). Requires go.logs.channel.enable=true. +// +func NewProducer(conf *ConfigMap) (*Producer, error) { + + err := versionCheck() + if err != nil { + return nil, err + } + + p := &Producer{} + + // before we do anything with the configuration, create a copy such that + // the original is not mutated. + confCopy := conf.clone() + + v, err := confCopy.extract("delivery.report.only.error", false) + if v == true { + // FIXME: The filtering of successful DRs must be done in + // the Go client to avoid cgoDr memory leaks. + return nil, newErrorFromString(ErrUnsupportedFeature, + "delivery.report.only.error=true is not currently supported by the Go client") + } + + v, err = confCopy.extract("go.batch.producer", false) + if err != nil { + return nil, err + } + batchProducer := v.(bool) + + v, err = confCopy.extract("go.delivery.reports", true) + if err != nil { + return nil, err + } + p.handle.fwdDr = v.(bool) + + v, err = confCopy.extract("go.delivery.report.fields", "key,value") + if err != nil { + return nil, err + } + + p.handle.msgFields, err = newMessageFieldsFrom(v) + if err != nil { + return nil, err + } + + v, err = confCopy.extract("go.events.channel.size", 1000000) + if err != nil { + return nil, err + } + eventsChanSize := v.(int) + + v, err = confCopy.extract("go.produce.channel.size", 1000000) + if err != nil { + return nil, err + } + produceChannelSize := v.(int) + + logsChanEnable, logsChan, err := confCopy.extractLogConfig() + if err != nil { + return nil, err + } + + if int(C.rd_kafka_version()) < 0x01000000 { + // produce.offset.report is no longer used in librdkafka >= v1.0.0 + v, _ = confCopy.extract("{topic}.produce.offset.report", nil) + if v == nil { + // Enable offset reporting by default, unless overriden. + confCopy.SetKey("{topic}.produce.offset.report", true) + } + } + + // Convert ConfigMap to librdkafka conf_t + cConf, err := confCopy.convert() + if err != nil { + return nil, err + } + + cErrstr := (*C.char)(C.malloc(C.size_t(256))) + defer C.free(unsafe.Pointer(cErrstr)) + + C.rd_kafka_conf_set_events(cConf, C.RD_KAFKA_EVENT_DR|C.RD_KAFKA_EVENT_STATS|C.RD_KAFKA_EVENT_ERROR|C.RD_KAFKA_EVENT_OAUTHBEARER_TOKEN_REFRESH) + + // Create librdkafka producer instance + p.handle.rk = C.rd_kafka_new(C.RD_KAFKA_PRODUCER, cConf, cErrstr, 256) + if p.handle.rk == nil { + return nil, newErrorFromCString(C.RD_KAFKA_RESP_ERR__INVALID_ARG, cErrstr) + } + + p.handle.p = p + p.handle.setup() + p.handle.rkq = C.rd_kafka_queue_get_main(p.handle.rk) + p.events = make(chan Event, eventsChanSize) + p.produceChannel = make(chan *Message, produceChannelSize) + p.pollerTermChan = make(chan bool) + + if logsChanEnable { + p.handle.setupLogQueue(logsChan, p.pollerTermChan) + } + + p.handle.waitGroup.Add(1) + go func() { + poller(p, p.pollerTermChan) + p.handle.waitGroup.Done() + }() + + // non-batch or batch producer, only one must be used + var producer func(*Producer) + if batchProducer { + producer = channelBatchProducer + } else { + producer = channelProducer + } + + p.handle.waitGroup.Add(1) + go func() { + producer(p) + p.handle.waitGroup.Done() + }() + + return p, nil +} + +// channel_producer serves the ProduceChannel channel +func channelProducer(p *Producer) { + for m := range p.produceChannel { + err := p.produce(m, C.RD_KAFKA_MSG_F_BLOCK, nil) + if err != nil { + m.TopicPartition.Error = err + p.events <- m + } + } +} + +// channelBatchProducer serves the ProduceChannel channel and attempts to +// improve cgo performance by using the produceBatch() interface. +func channelBatchProducer(p *Producer) { + var buffered = make(map[string][]*Message) + bufferedCnt := 0 + const batchSize int = 1000000 + totMsgCnt := 0 + totBatchCnt := 0 + + for m := range p.produceChannel { + buffered[*m.TopicPartition.Topic] = append(buffered[*m.TopicPartition.Topic], m) + bufferedCnt++ + + loop2: + for true { + select { + case m, ok := <-p.produceChannel: + if !ok { + break loop2 + } + if m == nil { + panic("nil message received on ProduceChannel") + } + if m.TopicPartition.Topic == nil { + panic(fmt.Sprintf("message without Topic received on ProduceChannel: %v", m)) + } + buffered[*m.TopicPartition.Topic] = append(buffered[*m.TopicPartition.Topic], m) + bufferedCnt++ + if bufferedCnt >= batchSize { + break loop2 + } + default: + break loop2 + } + } + + totBatchCnt++ + totMsgCnt += len(buffered) + + for topic, buffered2 := range buffered { + err := p.produceBatch(topic, buffered2, C.RD_KAFKA_MSG_F_BLOCK) + if err != nil { + for _, m = range buffered2 { + m.TopicPartition.Error = err + p.events <- m + } + } + } + + buffered = make(map[string][]*Message) + bufferedCnt = 0 + } +} + +// poller polls the rd_kafka_t handle for events until signalled for termination +func poller(p *Producer, termChan chan bool) { + for { + select { + case _ = <-termChan: + return + + default: + _, term := p.handle.eventPoll(p.events, 100, 1000, termChan) + if term { + return + } + break + } + } +} + +// GetMetadata queries broker for cluster and topic metadata. +// If topic is non-nil only information about that topic is returned, else if +// allTopics is false only information about locally used topics is returned, +// else information about all topics is returned. +// GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API. +func (p *Producer) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) { + return getMetadata(p, topic, allTopics, timeoutMs) +} + +// QueryWatermarkOffsets returns the broker's low and high offsets for the given topic +// and partition. +func (p *Producer) QueryWatermarkOffsets(topic string, partition int32, timeoutMs int) (low, high int64, err error) { + return queryWatermarkOffsets(p, topic, partition, timeoutMs) +} + +// OffsetsForTimes looks up offsets by timestamp for the given partitions. +// +// The returned offset for each partition is the earliest offset whose +// timestamp is greater than or equal to the given timestamp in the +// corresponding partition. If the provided timestamp exceeds that of the +// last message in the partition, a value of -1 will be returned. +// +// The timestamps to query are represented as `.Offset` in the `times` +// argument and the looked up offsets are represented as `.Offset` in the returned +// `offsets` list. +// +// The function will block for at most timeoutMs milliseconds. +// +// Duplicate Topic+Partitions are not supported. +// Per-partition errors may be returned in the `.Error` field. +func (p *Producer) OffsetsForTimes(times []TopicPartition, timeoutMs int) (offsets []TopicPartition, err error) { + return offsetsForTimes(p, times, timeoutMs) +} + +// GetFatalError returns an Error object if the client instance has raised a fatal error, else nil. +func (p *Producer) GetFatalError() error { + return getFatalError(p) +} + +// TestFatalError triggers a fatal error in the underlying client. +// This is to be used strictly for testing purposes. +func (p *Producer) TestFatalError(code ErrorCode, str string) ErrorCode { + return testFatalError(p, code, str) +} + +// SetOAuthBearerToken sets the the data to be transmitted +// to a broker during SASL/OAUTHBEARER authentication. It will return nil +// on success, otherwise an error if: +// 1) the token data is invalid (meaning an expiration time in the past +// or either a token value or an extension key or value that does not meet +// the regular expression requirements as per +// https://tools.ietf.org/html/rfc7628#section-3.1); +// 2) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 3) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (p *Producer) SetOAuthBearerToken(oauthBearerToken OAuthBearerToken) error { + return p.handle.setOAuthBearerToken(oauthBearerToken) +} + +// SetOAuthBearerTokenFailure sets the error message describing why token +// retrieval/setting failed; it also schedules a new token refresh event for 10 +// seconds later so the attempt may be retried. It will return nil on +// success, otherwise an error if: +// 1) SASL/OAUTHBEARER is not supported by the underlying librdkafka build; +// 2) SASL/OAUTHBEARER is supported but is not configured as the client's +// authentication mechanism. +func (p *Producer) SetOAuthBearerTokenFailure(errstr string) error { + return p.handle.setOAuthBearerTokenFailure(errstr) +} + +// Transactional API + +// InitTransactions Initializes transactions for the producer instance. +// +// This function ensures any transactions initiated by previous instances +// of the producer with the same `transactional.id` are completed. +// If the previous instance failed with a transaction in progress the +// previous transaction will be aborted. +// This function needs to be called before any other transactional or +// produce functions are called when the `transactional.id` is configured. +// +// If the last transaction had begun completion (following transaction commit) +// but not yet finished, this function will await the previous transaction's +// completion. +// +// When any previous transactions have been fenced this function +// will acquire the internal producer id and epoch, used in all future +// transactional messages issued by this producer instance. +// +// Upon successful return from this function the application has to perform at +// least one of the following operations within `transaction.timeout.ms` to +// avoid timing out the transaction on the broker: +// * `Produce()` (et.al) +// * `SendOffsetsToTransaction()` +// * `CommitTransaction()` +// * `AbortTransaction()` +// +// Parameters: +// * `ctx` - The maximum time to block, or nil for indefinite. +// On timeout the operation may continue in the background, +// depending on state, and it is okay to call `InitTransactions()` +// again. +// +// Returns nil on success or an error on failure. +// Check whether the returned error object permits retrying +// by calling `err.(kafka.Error).IsRetriable()`, or whether a fatal +// error has been raised by calling `err.(kafka.Error).IsFatal()`. +func (p *Producer) InitTransactions(ctx context.Context) error { + cError := C.rd_kafka_init_transactions(p.handle.rk, + cTimeoutFromContext(ctx)) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// BeginTransaction starts a new transaction. +// +// `InitTransactions()` must have been called successfully (once) +// before this function is called. +// +// Any messages produced, offsets sent (`SendOffsetsToTransaction()`), +// etc, after the successful return of this function will be part of +// the transaction and committed or aborted atomatically. +// +// Finish the transaction by calling `CommitTransaction()` or +// abort the transaction by calling `AbortTransaction()`. +// +// Returns nil on success or an error object on failure. +// Check whether a fatal error has been raised by +// calling `err.(kafka.Error).IsFatal()`. +// +// Note: With the transactional producer, `Produce()`, et.al, are only +// allowed during an on-going transaction, as started with this function. +// Any produce call outside an on-going transaction, or for a failed +// transaction, will fail. +func (p *Producer) BeginTransaction() error { + cError := C.rd_kafka_begin_transaction(p.handle.rk) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// SendOffsetsToTransaction sends a list of topic partition offsets to the +// consumer group coordinator for `consumerMetadata`, and marks the offsets +// as part part of the current transaction. +// These offsets will be considered committed only if the transaction is +// committed successfully. +// +// The offsets should be the next message your application will consume, +// i.e., the last processed message's offset + 1 for each partition. +// Either track the offsets manually during processing or use +// `consumer.Position()` (on the consumer) to get the current offsets for +// the partitions assigned to the consumer. +// +// Use this method at the end of a consume-transform-produce loop prior +// to committing the transaction with `CommitTransaction()`. +// +// Parameters: +// * `ctx` - The maximum amount of time to block, or nil for indefinite. +// * `offsets` - List of offsets to commit to the consumer group upon +// successful commit of the transaction. Offsets should be +// the next message to consume, e.g., last processed message + 1. +// * `consumerMetadata` - The current consumer group metadata as returned by +// `consumer.GetConsumerGroupMetadata()` on the consumer +// instance the provided offsets were consumed from. +// +// Note: The consumer must disable auto commits (set `enable.auto.commit` to false on the consumer). +// +// Note: Logical and invalid offsets (e.g., OffsetInvalid) in +// `offsets` will be ignored. If there are no valid offsets in +// `offsets` the function will return nil and no action will be taken. +// +// Returns nil on success or an error object on failure. +// Check whether the returned error object permits retrying +// by calling `err.(kafka.Error).IsRetriable()`, or whether an abortable +// or fatal error has been raised by calling +// `err.(kafka.Error).TxnRequiresAbort()` or `err.(kafka.Error).IsFatal()` +// respectively. +func (p *Producer) SendOffsetsToTransaction(ctx context.Context, offsets []TopicPartition, consumerMetadata *ConsumerGroupMetadata) error { + var cOffsets *C.rd_kafka_topic_partition_list_t + if offsets != nil { + cOffsets = newCPartsFromTopicPartitions(offsets) + defer C.rd_kafka_topic_partition_list_destroy(cOffsets) + } + + cgmd, err := deserializeConsumerGroupMetadata(consumerMetadata.serialized) + if err != nil { + return err + } + defer C.rd_kafka_consumer_group_metadata_destroy(cgmd) + + cError := C.rd_kafka_send_offsets_to_transaction( + p.handle.rk, + cOffsets, + cgmd, + cTimeoutFromContext(ctx)) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// CommitTransaction commits the current transaction. +// +// Any outstanding messages will be flushed (delivered) before actually +// committing the transaction. +// +// If any of the outstanding messages fail permanently the current +// transaction will enter the abortable error state and this +// function will return an abortable error, in this case the application +// must call `AbortTransaction()` before attempting a new +// transaction with `BeginTransaction()`. +// +// Parameters: +// * `ctx` - The maximum amount of time to block, or nil for indefinite. +// +// Note: This function will block until all outstanding messages are +// delivered and the transaction commit request has been successfully +// handled by the transaction coordinator, or until the `ctx` expires, +// which ever comes first. On timeout the application may +// call the function again. +// +// Note: Will automatically call `Flush()` to ensure all queued +// messages are delivered before attempting to commit the transaction. +// The application MUST serve the `producer.Events()` channel for delivery +// reports in a separate go-routine during this time. +// +// Returns nil on success or an error object on failure. +// Check whether the returned error object permits retrying +// by calling `err.(kafka.Error).IsRetriable()`, or whether an abortable +// or fatal error has been raised by calling +// `err.(kafka.Error).TxnRequiresAbort()` or `err.(kafka.Error).IsFatal()` +// respectively. +func (p *Producer) CommitTransaction(ctx context.Context) error { + cError := C.rd_kafka_commit_transaction(p.handle.rk, + cTimeoutFromContext(ctx)) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} + +// AbortTransaction aborts the ongoing transaction. +// +// This function should also be used to recover from non-fatal abortable +// transaction errors. +// +// Any outstanding messages will be purged and fail with +// `ErrPurgeInflight` or `ErrPurgeQueue`. +// +// Parameters: +// * `ctx` - The maximum amount of time to block, or nil for indefinite. +// +// Note: This function will block until all outstanding messages are purged +// and the transaction abort request has been successfully +// handled by the transaction coordinator, or until the `ctx` expires, +// which ever comes first. On timeout the application may +// call the function again. +// +// Note: Will automatically call `Purge()` and `Flush()` to ensure all queued +// and in-flight messages are purged before attempting to abort the transaction. +// The application MUST serve the `producer.Events()` channel for delivery +// reports in a separate go-routine during this time. +// +// Returns nil on success or an error object on failure. +// Check whether the returned error object permits retrying +// by calling `err.(kafka.Error).IsRetriable()`, or whether a fatal error +// has been raised by calling `err.(kafka.Error).IsFatal()`. +func (p *Producer) AbortTransaction(ctx context.Context) error { + cError := C.rd_kafka_abort_transaction(p.handle.rk, + cTimeoutFromContext(ctx)) + if cError != nil { + return newErrorFromCErrorDestroy(cError) + } + + return nil +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/select_rdkafka.h b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/select_rdkafka.h new file mode 100644 index 0000000..98fe330 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/select_rdkafka.h @@ -0,0 +1,29 @@ +/** + * Copyright 2020 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This file uses a preprocessor macro defined by the various build_*.go +// files to determine whether to import the bundled librdkafka header, or +// the system one. +// This is needed because cgo will automatically add -I. to the include +// path, so would find a bundled header instead of +// the system one if it were called librdkafka/rdkafka.h instead of +// librdkafka_vendor/rdkafka.h + +#ifdef USE_VENDORED_LIBRDKAFKA +#include "librdkafka_vendor/rdkafka.h" +#else +#include +#endif diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testconf-example.json b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testconf-example.json new file mode 100644 index 0000000..7024a9c --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testconf-example.json @@ -0,0 +1,8 @@ +{ + "Brokers": "mybroker or $BROKERS env", + "Topic": "test", + "GroupID": "testgroup", + "PerfMsgCount": 1000000, + "PerfMsgSize": 100, + "Config": ["api.version.request=true"] +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testhelpers.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testhelpers.go new file mode 100644 index 0000000..f9db896 --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/testhelpers.go @@ -0,0 +1,248 @@ +package kafka + +/** + * Copyright 2016 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ( + "context" + "encoding/json" + "fmt" + "math/rand" + "os" + "testing" + "time" +) + +/* +#include "select_rdkafka.h" +*/ +import "C" + +var testconf struct { + Brokers string + Topic string + GroupID string + PerfMsgCount int + PerfMsgSize int + Config []string + conf ConfigMap +} + +// testconf_read reads the test suite config file testconf.json which must +// contain at least Brokers and Topic string properties. +// Returns true if the testconf was found and usable, false if no such file, or panics +// if the file format is wrong. +func testconfRead() bool { + cf, err := os.Open("testconf.json") + if err != nil { + fmt.Fprintf(os.Stderr, "%% testconf.json not found - ignoring test\n") + return false + } + + // Default values + testconf.PerfMsgCount = 2000000 + testconf.PerfMsgSize = 100 + testconf.GroupID = "testgroup" + + jp := json.NewDecoder(cf) + err = jp.Decode(&testconf) + if err != nil { + panic(fmt.Sprintf("Failed to parse testconf: %s", err)) + } + + cf.Close() + + if testconf.Brokers[0] == '$' { + // Read broker list from environment variable + testconf.Brokers = os.Getenv(testconf.Brokers[1:]) + } + + if testconf.Brokers == "" || testconf.Topic == "" { + panic("Missing Brokers or Topic in testconf.json") + } + + return true +} + +// update existing ConfigMap with key=value pairs from testconf.Config +func (cm *ConfigMap) updateFromTestconf() error { + if testconf.Config == nil { + return nil + } + + // Translate "key=value" pairs in Config to ConfigMap + for _, s := range testconf.Config { + err := cm.Set(s) + if err != nil { + return err + } + } + + return nil + +} + +// Return the number of messages available in all partitions of a topic. +// WARNING: This uses watermark offsets so it will be incorrect for compacted topics. +func getMessageCountInTopic(topic string) (int, error) { + + // Create consumer + config := &ConfigMap{"bootstrap.servers": testconf.Brokers, + "group.id": testconf.GroupID} + config.updateFromTestconf() + + c, err := NewConsumer(config) + if err != nil { + return 0, err + } + defer c.Close() + + // get metadata for the topic to find out number of partitions + + metadata, err := c.GetMetadata(&topic, false, 5*1000) + if err != nil { + return 0, err + } + + t, ok := metadata.Topics[topic] + if !ok { + return 0, newError(C.RD_KAFKA_RESP_ERR__UNKNOWN_TOPIC) + } + + cnt := 0 + for _, p := range t.Partitions { + low, high, err := c.QueryWatermarkOffsets(topic, p.ID, 5*1000) + if err != nil { + continue + } + cnt += int(high - low) + } + + return cnt, nil +} + +// getBrokerList returns a list of brokers (ids) in the cluster +func getBrokerList(H Handle) (brokers []int32, err error) { + md, err := getMetadata(H, nil, true, 15*1000) + if err != nil { + return nil, err + } + + brokers = make([]int32, len(md.Brokers)) + for i, mdBroker := range md.Brokers { + brokers[i] = mdBroker.ID + } + + return brokers, nil +} + +// waitTopicInMetadata waits for the given topic to show up in metadata +func waitTopicInMetadata(H Handle, topic string, timeoutMs int) error { + d, _ := time.ParseDuration(fmt.Sprintf("%dms", timeoutMs)) + tEnd := time.Now().Add(d) + + for { + remain := tEnd.Sub(time.Now()).Seconds() + if remain < 0.0 { + return newErrorFromString(ErrTimedOut, + fmt.Sprintf("Timed out waiting for topic %s to appear in metadata", topic)) + } + + md, err := getMetadata(H, nil, true, int(remain*1000)) + if err != nil { + return err + } + + for _, t := range md.Topics { + if t.Topic != topic { + continue + } + if t.Error.Code() != ErrNoError || len(t.Partitions) < 1 { + continue + } + // Proper topic found in metadata + return nil + } + + time.Sleep(500 * 1000) // 500ms + } + +} + +func createAdminClient(t *testing.T) (a *AdminClient) { + numver, strver := LibraryVersion() + if numver < 0x000b0500 { + t.Skipf("Requires librdkafka >=0.11.5 (currently on %s, 0x%x)", strver, numver) + } + + if !testconfRead() { + t.Skipf("Missing testconf.json") + } + + conf := ConfigMap{"bootstrap.servers": testconf.Brokers} + conf.updateFromTestconf() + + /* + * Create producer and produce a couple of messages with and without + * headers. + */ + a, err := NewAdminClient(&conf) + if err != nil { + t.Fatalf("NewAdminClient: %v", err) + } + + return a +} + +func createTestTopic(t *testing.T, suffix string, numPartitions int, replicationFactor int) string { + rand.Seed(time.Now().Unix()) + + topic := fmt.Sprintf("%s-%s-%d", testconf.Topic, suffix, rand.Intn(100000)) + + a := createAdminClient(t) + defer a.Close() + + newTopics := []TopicSpecification{ + { + Topic: topic, + NumPartitions: numPartitions, + ReplicationFactor: replicationFactor, + }, + } + + maxDuration, err := time.ParseDuration("30s") + if err != nil { + t.Fatalf("%s", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), maxDuration) + defer cancel() + result, err := a.CreateTopics(ctx, newTopics, nil) + if err != nil { + t.Fatalf("CreateTopics() failed: %s", err) + } + + for _, res := range result { + if res.Error.Code() != ErrNoError { + t.Errorf("Failed to create topic %s: %s\n", + res.Topic, res.Error) + continue + } + + } + + return topic +} diff --git a/vendor/github.com/confluentinc/confluent-kafka-go/kafka/time.go b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/time.go new file mode 100644 index 0000000..ff93f0a --- /dev/null +++ b/vendor/github.com/confluentinc/confluent-kafka-go/kafka/time.go @@ -0,0 +1,55 @@ +/** + * Copyright 2019 Confluent Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kafka + +import "C" + +import ( + "context" + "time" +) + +const ( + cTimeoutInfinite = C.int(-1) // Blocks indefinitely until completion. + cTimeoutNoWait = C.int(0) // Returns immediately without blocking. +) + +// cTimeoutFromContext returns the remaining time after which work done on behalf of this context +// should be canceled, in milliseconds. +// +// If no deadline/timeout is set, or if the timeout does not fit in an int32, it returns +// cTimeoutInfinite; +// If there is no time left in this context, it returns cTimeoutNoWait. +func cTimeoutFromContext(ctx context.Context) C.int { + if ctx == nil { + return cTimeoutInfinite + } + timeout, hasTimeout := timeout(ctx) + if !hasTimeout { + return cTimeoutInfinite + } + if timeout <= 0 { + return cTimeoutNoWait + } + + timeoutMs := int64(timeout / time.Millisecond) + if int64(int32(timeoutMs)) < timeoutMs { + return cTimeoutInfinite + } + + return C.int(timeoutMs) +} diff --git a/vendor/github.com/pkg/errors/.gitignore b/vendor/github.com/pkg/errors/.gitignore new file mode 100644 index 0000000..daf913b --- /dev/null +++ b/vendor/github.com/pkg/errors/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/pkg/errors/.travis.yml b/vendor/github.com/pkg/errors/.travis.yml new file mode 100644 index 0000000..9159de0 --- /dev/null +++ b/vendor/github.com/pkg/errors/.travis.yml @@ -0,0 +1,10 @@ +language: go +go_import_path: github.com/pkg/errors +go: + - 1.11.x + - 1.12.x + - 1.13.x + - tip + +script: + - make check diff --git a/vendor/github.com/pkg/errors/LICENSE b/vendor/github.com/pkg/errors/LICENSE new file mode 100644 index 0000000..835ba3e --- /dev/null +++ b/vendor/github.com/pkg/errors/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2015, Dave Cheney +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/github.com/pkg/errors/Makefile b/vendor/github.com/pkg/errors/Makefile new file mode 100644 index 0000000..ce9d7cd --- /dev/null +++ b/vendor/github.com/pkg/errors/Makefile @@ -0,0 +1,44 @@ +PKGS := github.com/pkg/errors +SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS)) +GO := go + +check: test vet gofmt misspell unconvert staticcheck ineffassign unparam + +test: + $(GO) test $(PKGS) + +vet: | test + $(GO) vet $(PKGS) + +staticcheck: + $(GO) get honnef.co/go/tools/cmd/staticcheck + staticcheck -checks all $(PKGS) + +misspell: + $(GO) get github.com/client9/misspell/cmd/misspell + misspell \ + -locale GB \ + -error \ + *.md *.go + +unconvert: + $(GO) get github.com/mdempsky/unconvert + unconvert -v $(PKGS) + +ineffassign: + $(GO) get github.com/gordonklaus/ineffassign + find $(SRCDIRS) -name '*.go' | xargs ineffassign + +pedantic: check errcheck + +unparam: + $(GO) get mvdan.cc/unparam + unparam ./... + +errcheck: + $(GO) get github.com/kisielk/errcheck + errcheck $(PKGS) + +gofmt: + @echo Checking code is gofmted + @test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)" diff --git a/vendor/github.com/pkg/errors/README.md b/vendor/github.com/pkg/errors/README.md new file mode 100644 index 0000000..54dfdcb --- /dev/null +++ b/vendor/github.com/pkg/errors/README.md @@ -0,0 +1,59 @@ +# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge) + +Package errors provides simple error handling primitives. + +`go get github.com/pkg/errors` + +The traditional error handling idiom in Go is roughly akin to +```go +if err != nil { + return err +} +``` +which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error. + +## Adding context to an error + +The errors.Wrap function returns a new error that adds context to the original error. For example +```go +_, err := ioutil.ReadAll(r) +if err != nil { + return errors.Wrap(err, "read failed") +} +``` +## Retrieving the cause of an error + +Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`. +```go +type causer interface { + Cause() error +} +``` +`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example: +```go +switch err := errors.Cause(err).(type) { +case *MyError: + // handle specifically +default: + // unknown error +} +``` + +[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors). + +## Roadmap + +With the upcoming [Go2 error proposals](https://go.googlesource.com/proposal/+/master/design/go2draft.md) this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows: + +- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible) +- 1.0. Final release. + +## Contributing + +Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports. + +Before sending a PR, please discuss your change by raising an issue. + +## License + +BSD-2-Clause diff --git a/vendor/github.com/pkg/errors/appveyor.yml b/vendor/github.com/pkg/errors/appveyor.yml new file mode 100644 index 0000000..a932ead --- /dev/null +++ b/vendor/github.com/pkg/errors/appveyor.yml @@ -0,0 +1,32 @@ +version: build-{build}.{branch} + +clone_folder: C:\gopath\src\github.com\pkg\errors +shallow_clone: true # for startup speed + +environment: + GOPATH: C:\gopath + +platform: + - x64 + +# http://www.appveyor.com/docs/installed-software +install: + # some helpful output for debugging builds + - go version + - go env + # pre-installed MinGW at C:\MinGW is 32bit only + # but MSYS2 at C:\msys64 has mingw64 + - set PATH=C:\msys64\mingw64\bin;%PATH% + - gcc --version + - g++ --version + +build_script: + - go install -v ./... + +test_script: + - set PATH=C:\gopath\bin;%PATH% + - go test -v ./... + +#artifacts: +# - path: '%GOPATH%\bin\*.exe' +deploy: off diff --git a/vendor/github.com/pkg/errors/errors.go b/vendor/github.com/pkg/errors/errors.go new file mode 100644 index 0000000..161aea2 --- /dev/null +++ b/vendor/github.com/pkg/errors/errors.go @@ -0,0 +1,288 @@ +// Package errors provides simple error handling primitives. +// +// The traditional error handling idiom in Go is roughly akin to +// +// if err != nil { +// return err +// } +// +// which when applied recursively up the call stack results in error reports +// without context or debugging information. The errors package allows +// programmers to add context to the failure path in their code in a way +// that does not destroy the original value of the error. +// +// Adding context to an error +// +// The errors.Wrap function returns a new error that adds context to the +// original error by recording a stack trace at the point Wrap is called, +// together with the supplied message. For example +// +// _, err := ioutil.ReadAll(r) +// if err != nil { +// return errors.Wrap(err, "read failed") +// } +// +// If additional control is required, the errors.WithStack and +// errors.WithMessage functions destructure errors.Wrap into its component +// operations: annotating an error with a stack trace and with a message, +// respectively. +// +// Retrieving the cause of an error +// +// Using errors.Wrap constructs a stack of errors, adding context to the +// preceding error. Depending on the nature of the error it may be necessary +// to reverse the operation of errors.Wrap to retrieve the original error +// for inspection. Any error value which implements this interface +// +// type causer interface { +// Cause() error +// } +// +// can be inspected by errors.Cause. errors.Cause will recursively retrieve +// the topmost error that does not implement causer, which is assumed to be +// the original cause. For example: +// +// switch err := errors.Cause(err).(type) { +// case *MyError: +// // handle specifically +// default: +// // unknown error +// } +// +// Although the causer interface is not exported by this package, it is +// considered a part of its stable public interface. +// +// Formatted printing of errors +// +// All error values returned from this package implement fmt.Formatter and can +// be formatted by the fmt package. The following verbs are supported: +// +// %s print the error. If the error has a Cause it will be +// printed recursively. +// %v see %s +// %+v extended format. Each Frame of the error's StackTrace will +// be printed in detail. +// +// Retrieving the stack trace of an error or wrapper +// +// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are +// invoked. This information can be retrieved with the following interface: +// +// type stackTracer interface { +// StackTrace() errors.StackTrace +// } +// +// The returned errors.StackTrace type is defined as +// +// type StackTrace []Frame +// +// The Frame type represents a call site in the stack trace. Frame supports +// the fmt.Formatter interface that can be used for printing information about +// the stack trace of this error. For example: +// +// if err, ok := err.(stackTracer); ok { +// for _, f := range err.StackTrace() { +// fmt.Printf("%+s:%d\n", f, f) +// } +// } +// +// Although the stackTracer interface is not exported by this package, it is +// considered a part of its stable public interface. +// +// See the documentation for Frame.Format for more details. +package errors + +import ( + "fmt" + "io" +) + +// New returns an error with the supplied message. +// New also records the stack trace at the point it was called. +func New(message string) error { + return &fundamental{ + msg: message, + stack: callers(), + } +} + +// Errorf formats according to a format specifier and returns the string +// as a value that satisfies error. +// Errorf also records the stack trace at the point it was called. +func Errorf(format string, args ...interface{}) error { + return &fundamental{ + msg: fmt.Sprintf(format, args...), + stack: callers(), + } +} + +// fundamental is an error that has a message and a stack, but no caller. +type fundamental struct { + msg string + *stack +} + +func (f *fundamental) Error() string { return f.msg } + +func (f *fundamental) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + io.WriteString(s, f.msg) + f.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, f.msg) + case 'q': + fmt.Fprintf(s, "%q", f.msg) + } +} + +// WithStack annotates err with a stack trace at the point WithStack was called. +// If err is nil, WithStack returns nil. +func WithStack(err error) error { + if err == nil { + return nil + } + return &withStack{ + err, + callers(), + } +} + +type withStack struct { + error + *stack +} + +func (w *withStack) Cause() error { return w.error } + +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withStack) Unwrap() error { return w.error } + +func (w *withStack) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v", w.Cause()) + w.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, w.Error()) + case 'q': + fmt.Fprintf(s, "%q", w.Error()) + } +} + +// Wrap returns an error annotating err with a stack trace +// at the point Wrap is called, and the supplied message. +// If err is nil, Wrap returns nil. +func Wrap(err error, message string) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: message, + } + return &withStack{ + err, + callers(), + } +} + +// Wrapf returns an error annotating err with a stack trace +// at the point Wrapf is called, and the format specifier. +// If err is nil, Wrapf returns nil. +func Wrapf(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } + return &withStack{ + err, + callers(), + } +} + +// WithMessage annotates err with a new message. +// If err is nil, WithMessage returns nil. +func WithMessage(err error, message string) error { + if err == nil { + return nil + } + return &withMessage{ + cause: err, + msg: message, + } +} + +// WithMessagef annotates err with the format specifier. +// If err is nil, WithMessagef returns nil. +func WithMessagef(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + return &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } +} + +type withMessage struct { + cause error + msg string +} + +func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } +func (w *withMessage) Cause() error { return w.cause } + +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withMessage) Unwrap() error { return w.cause } + +func (w *withMessage) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v\n", w.Cause()) + io.WriteString(s, w.msg) + return + } + fallthrough + case 's', 'q': + io.WriteString(s, w.Error()) + } +} + +// Cause returns the underlying cause of the error, if possible. +// An error value has a cause if it implements the following +// interface: +// +// type causer interface { +// Cause() error +// } +// +// If the error does not implement Cause, the original error will +// be returned. If the error is nil, nil will be returned without further +// investigation. +func Cause(err error) error { + type causer interface { + Cause() error + } + + for err != nil { + cause, ok := err.(causer) + if !ok { + break + } + err = cause.Cause() + } + return err +} diff --git a/vendor/github.com/pkg/errors/go113.go b/vendor/github.com/pkg/errors/go113.go new file mode 100644 index 0000000..be0d10d --- /dev/null +++ b/vendor/github.com/pkg/errors/go113.go @@ -0,0 +1,38 @@ +// +build go1.13 + +package errors + +import ( + stderrors "errors" +) + +// Is reports whether any error in err's chain matches target. +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error is considered to match a target if it is equal to that target or if +// it implements a method Is(error) bool such that Is(target) returns true. +func Is(err, target error) bool { return stderrors.Is(err, target) } + +// As finds the first error in err's chain that matches target, and if so, sets +// target to that error value and returns true. +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error matches target if the error's concrete value is assignable to the value +// pointed to by target, or if the error has a method As(interface{}) bool such that +// As(target) returns true. In the latter case, the As method is responsible for +// setting target. +// +// As will panic if target is not a non-nil pointer to either a type that implements +// error, or to any interface type. As returns false if err is nil. +func As(err error, target interface{}) bool { return stderrors.As(err, target) } + +// Unwrap returns the result of calling the Unwrap method on err, if err's +// type contains an Unwrap method returning error. +// Otherwise, Unwrap returns nil. +func Unwrap(err error) error { + return stderrors.Unwrap(err) +} diff --git a/vendor/github.com/pkg/errors/stack.go b/vendor/github.com/pkg/errors/stack.go new file mode 100644 index 0000000..779a834 --- /dev/null +++ b/vendor/github.com/pkg/errors/stack.go @@ -0,0 +1,177 @@ +package errors + +import ( + "fmt" + "io" + "path" + "runtime" + "strconv" + "strings" +) + +// Frame represents a program counter inside a stack frame. +// For historical reasons if Frame is interpreted as a uintptr +// its value represents the program counter + 1. +type Frame uintptr + +// pc returns the program counter for this frame; +// multiple frames may have the same PC value. +func (f Frame) pc() uintptr { return uintptr(f) - 1 } + +// file returns the full path to the file that contains the +// function for this Frame's pc. +func (f Frame) file() string { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return "unknown" + } + file, _ := fn.FileLine(f.pc()) + return file +} + +// line returns the line number of source code of the +// function for this Frame's pc. +func (f Frame) line() int { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return 0 + } + _, line := fn.FileLine(f.pc()) + return line +} + +// name returns the name of this function, if known. +func (f Frame) name() string { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return "unknown" + } + return fn.Name() +} + +// Format formats the frame according to the fmt.Formatter interface. +// +// %s source file +// %d source line +// %n function name +// %v equivalent to %s:%d +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+s function name and path of source file relative to the compile time +// GOPATH separated by \n\t (\n\t) +// %+v equivalent to %+s:%d +func (f Frame) Format(s fmt.State, verb rune) { + switch verb { + case 's': + switch { + case s.Flag('+'): + io.WriteString(s, f.name()) + io.WriteString(s, "\n\t") + io.WriteString(s, f.file()) + default: + io.WriteString(s, path.Base(f.file())) + } + case 'd': + io.WriteString(s, strconv.Itoa(f.line())) + case 'n': + io.WriteString(s, funcname(f.name())) + case 'v': + f.Format(s, 's') + io.WriteString(s, ":") + f.Format(s, 'd') + } +} + +// MarshalText formats a stacktrace Frame as a text string. The output is the +// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs. +func (f Frame) MarshalText() ([]byte, error) { + name := f.name() + if name == "unknown" { + return []byte(name), nil + } + return []byte(fmt.Sprintf("%s %s:%d", name, f.file(), f.line())), nil +} + +// StackTrace is stack of Frames from innermost (newest) to outermost (oldest). +type StackTrace []Frame + +// Format formats the stack of Frames according to the fmt.Formatter interface. +// +// %s lists source files for each Frame in the stack +// %v lists the source file and line number for each Frame in the stack +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+v Prints filename, function, and line number for each Frame in the stack. +func (st StackTrace) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case s.Flag('+'): + for _, f := range st { + io.WriteString(s, "\n") + f.Format(s, verb) + } + case s.Flag('#'): + fmt.Fprintf(s, "%#v", []Frame(st)) + default: + st.formatSlice(s, verb) + } + case 's': + st.formatSlice(s, verb) + } +} + +// formatSlice will format this StackTrace into the given buffer as a slice of +// Frame, only valid when called with '%s' or '%v'. +func (st StackTrace) formatSlice(s fmt.State, verb rune) { + io.WriteString(s, "[") + for i, f := range st { + if i > 0 { + io.WriteString(s, " ") + } + f.Format(s, verb) + } + io.WriteString(s, "]") +} + +// stack represents a stack of program counters. +type stack []uintptr + +func (s *stack) Format(st fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case st.Flag('+'): + for _, pc := range *s { + f := Frame(pc) + fmt.Fprintf(st, "\n%+v", f) + } + } + } +} + +func (s *stack) StackTrace() StackTrace { + f := make([]Frame, len(*s)) + for i := 0; i < len(f); i++ { + f[i] = Frame((*s)[i]) + } + return f +} + +func callers() *stack { + const depth = 32 + var pcs [depth]uintptr + n := runtime.Callers(3, pcs[:]) + var st stack = pcs[0:n] + return &st +} + +// funcname removes the path prefix component of a function's name reported by func.Name(). +func funcname(name string) string { + i := strings.LastIndex(name, "/") + name = name[i+1:] + i = strings.Index(name, ".") + return name[i+1:] +} diff --git a/vendor/github.com/rs/zerolog/.gitignore b/vendor/github.com/rs/zerolog/.gitignore new file mode 100644 index 0000000..8ebe58b --- /dev/null +++ b/vendor/github.com/rs/zerolog/.gitignore @@ -0,0 +1,25 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test +tmp + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/rs/zerolog/CNAME b/vendor/github.com/rs/zerolog/CNAME new file mode 100644 index 0000000..9ce57a6 --- /dev/null +++ b/vendor/github.com/rs/zerolog/CNAME @@ -0,0 +1 @@ +zerolog.io \ No newline at end of file diff --git a/vendor/github.com/rs/zerolog/LICENSE b/vendor/github.com/rs/zerolog/LICENSE new file mode 100644 index 0000000..677e07f --- /dev/null +++ b/vendor/github.com/rs/zerolog/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Olivier Poitrey + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/rs/zerolog/README.md b/vendor/github.com/rs/zerolog/README.md new file mode 100644 index 0000000..47b1297 --- /dev/null +++ b/vendor/github.com/rs/zerolog/README.md @@ -0,0 +1,695 @@ +# Zero Allocation JSON Logger + +[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/zerolog.svg?branch=master)](https://travis-ci.org/rs/zerolog) [![Coverage](http://gocover.io/_badge/github.com/rs/zerolog)](http://gocover.io/github.com/rs/zerolog) + +The zerolog package provides a fast and simple logger dedicated to JSON output. + +Zerolog's API is designed to provide both a great developer experience and stunning [performance](#benchmarks). Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection. + +Uber's [zap](https://godoc.org/go.uber.org/zap) library pioneered this approach. Zerolog is taking this concept to the next level with a simpler to use API and even better performance. + +To keep the code base and the API simple, zerolog focuses on efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) [`zerolog.ConsoleWriter`](#pretty-logging). + +![Pretty Logging Image](pretty.png) + +## Who uses zerolog + +Find out [who uses zerolog](https://github.com/rs/zerolog/wiki/Who-uses-zerolog) and add your company / project to the list. + +## Features + +* [Blazing fast](#benchmarks) +* [Low to zero allocation](#benchmarks) +* [Leveled logging](#leveled-logging) +* [Sampling](#log-sampling) +* [Hooks](#hooks) +* [Contextual fields](#contextual-logging) +* `context.Context` integration +* [Integration with `net/http`](#integration-with-nethttp) +* [JSON and CBOR encoding formats](#binary-encoding) +* [Pretty logging for development](#pretty-logging) +* [Error Logging (with optional Stacktrace)](#error-logging) + +## Installation + +```bash +go get -u github.com/rs/zerolog/log +``` + +## Getting Started + +### Simple Logging Example + +For simple logging, import the global logger package **github.com/rs/zerolog/log** + +```go +package main + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + // UNIX Time is faster and smaller than most timestamps + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + log.Print("hello world") +} + +// Output: {"time":1516134303,"level":"debug","message":"hello world"} +``` +> Note: By default log writes to `os.Stderr` +> Note: The default log level for `log.Print` is *debug* + +### Contextual Logging + +**zerolog** allows data to be added to log messages in the form of key:value pairs. The data added to the message adds "context" about the log event that can be critical for debugging as well as myriad other purposes. An example of this is below: + +```go +package main + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + log.Debug(). + Str("Scale", "833 cents"). + Float64("Interval", 833.09). + Msg("Fibonacci is everywhere") + + log.Debug(). + Str("Name", "Tom"). + Send() +} + +// Output: {"level":"debug","Scale":"833 cents","Interval":833.09,"time":1562212768,"message":"Fibonacci is everywhere"} +// Output: {"level":"debug","Name":"Tom","time":1562212768} +``` + +> You'll note in the above example that when adding contextual fields, the fields are strongly typed. You can find the full list of supported fields [here](#standard-types) + +### Leveled Logging + +#### Simple Leveled Logging Example + +```go +package main + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + log.Info().Msg("hello world") +} + +// Output: {"time":1516134303,"level":"info","message":"hello world"} +``` + +> It is very important to note that when using the **zerolog** chaining API, as shown above (`log.Info().Msg("hello world"`), the chain must have either the `Msg` or `Msgf` method call. If you forget to add either of these, the log will not occur and there is no compile time error to alert you of this. + +**zerolog** allows for logging at the following levels (from highest to lowest): + +* panic (`zerolog.PanicLevel`, 5) +* fatal (`zerolog.FatalLevel`, 4) +* error (`zerolog.ErrorLevel`, 3) +* warn (`zerolog.WarnLevel`, 2) +* info (`zerolog.InfoLevel`, 1) +* debug (`zerolog.DebugLevel`, 0) +* trace (`zerolog.TraceLevel`, -1) + +You can set the Global logging level to any of these options using the `SetGlobalLevel` function in the zerolog package, passing in one of the given constants above, e.g. `zerolog.InfoLevel` would be the "info" level. Whichever level is chosen, all logs with a level greater than or equal to that level will be written. To turn off logging entirely, pass the `zerolog.Disabled` constant. + +#### Setting Global Log Level + +This example uses command-line flags to demonstrate various outputs depending on the chosen log level. + +```go +package main + +import ( + "flag" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + debug := flag.Bool("debug", false, "sets log level to debug") + + flag.Parse() + + // Default level for this example is info, unless debug flag is present + zerolog.SetGlobalLevel(zerolog.InfoLevel) + if *debug { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + log.Debug().Msg("This message appears only when log level set to Debug") + log.Info().Msg("This message appears when log level set to Debug or Info") + + if e := log.Debug(); e.Enabled() { + // Compute log output only if enabled. + value := "bar" + e.Str("foo", value).Msg("some debug message") + } +} +``` + +Info Output (no flag) + +```bash +$ ./logLevelExample +{"time":1516387492,"level":"info","message":"This message appears when log level set to Debug or Info"} +``` + +Debug Output (debug flag set) + +```bash +$ ./logLevelExample -debug +{"time":1516387573,"level":"debug","message":"This message appears only when log level set to Debug"} +{"time":1516387573,"level":"info","message":"This message appears when log level set to Debug or Info"} +{"time":1516387573,"level":"debug","foo":"bar","message":"some debug message"} +``` + +#### Logging without Level or Message + +You may choose to log without a specific level by using the `Log` method. You may also write without a message by setting an empty string in the `msg string` parameter of the `Msg` method. Both are demonstrated in the example below. + +```go +package main + +import ( + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + log.Log(). + Str("foo", "bar"). + Msg("") +} + +// Output: {"time":1494567715,"foo":"bar"} +``` + +### Error Logging + +You can log errors using the `Err` method + +```go +package main + +import ( + "errors" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + err := errors.New("seems we have an error here") + log.Error().Err(err).Msg("") +} + +// Output: {"level":"error","error":"seems we have an error here","time":1609085256} +``` + +> The default field name for errors is `error`, you can change this by setting `zerolog.ErrorFieldName` to meet your needs. + +#### Error Logging with Stacktrace + +Using `github.com/pkg/errors`, you can add a formatted stacktrace to your errors. + +```go +package main + +import ( + "github.com/pkg/errors" + "github.com/rs/zerolog/pkgerrors" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack + + err := outer() + log.Error().Stack().Err(err).Msg("") +} + +func inner() error { + return errors.New("seems we have an error here") +} + +func middle() error { + err := inner() + if err != nil { + return err + } + return nil +} + +func outer() error { + err := middle() + if err != nil { + return err + } + return nil +} + +// Output: {"level":"error","stack":[{"func":"inner","line":"20","source":"errors.go"},{"func":"middle","line":"24","source":"errors.go"},{"func":"outer","line":"32","source":"errors.go"},{"func":"main","line":"15","source":"errors.go"},{"func":"main","line":"204","source":"proc.go"},{"func":"goexit","line":"1374","source":"asm_amd64.s"}],"error":"seems we have an error here","time":1609086683} +``` + +> zerolog.ErrorStackMarshaler must be set in order for the stack to output anything. + +#### Logging Fatal Messages + +```go +package main + +import ( + "errors" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +func main() { + err := errors.New("A repo man spends his life getting into tense situations") + service := "myservice" + + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + + log.Fatal(). + Err(err). + Str("service", service). + Msgf("Cannot start %s", service) +} + +// Output: {"time":1516133263,"level":"fatal","error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"} +// exit status 1 +``` + +> NOTE: Using `Msgf` generates one allocation even when the logger is disabled. + + +### Create logger instance to manage different outputs + +```go +logger := zerolog.New(os.Stderr).With().Timestamp().Logger() + +logger.Info().Str("foo", "bar").Msg("hello world") + +// Output: {"level":"info","time":1494567715,"message":"hello world","foo":"bar"} +``` + +### Sub-loggers let you chain loggers with additional context + +```go +sublogger := log.With(). + Str("component", "foo"). + Logger() +sublogger.Info().Msg("hello world") + +// Output: {"level":"info","time":1494567715,"message":"hello world","component":"foo"} +``` + +### Pretty logging + +To log a human-friendly, colorized output, use `zerolog.ConsoleWriter`: + +```go +log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + +log.Info().Str("foo", "bar").Msg("Hello world") + +// Output: 3:04PM INF Hello World foo=bar +``` + +To customize the configuration and formatting: + +```go +output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} +output.FormatLevel = func(i interface{}) string { + return strings.ToUpper(fmt.Sprintf("| %-6s|", i)) +} +output.FormatMessage = func(i interface{}) string { + return fmt.Sprintf("***%s****", i) +} +output.FormatFieldName = func(i interface{}) string { + return fmt.Sprintf("%s:", i) +} +output.FormatFieldValue = func(i interface{}) string { + return strings.ToUpper(fmt.Sprintf("%s", i)) +} + +log := zerolog.New(output).With().Timestamp().Logger() + +log.Info().Str("foo", "bar").Msg("Hello World") + +// Output: 2006-01-02T15:04:05Z07:00 | INFO | ***Hello World**** foo:BAR +``` + +### Sub dictionary + +```go +log.Info(). + Str("foo", "bar"). + Dict("dict", zerolog.Dict(). + Str("bar", "baz"). + Int("n", 1), + ).Msg("hello world") + +// Output: {"level":"info","time":1494567715,"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"} +``` + +### Customize automatic field names + +```go +zerolog.TimestampFieldName = "t" +zerolog.LevelFieldName = "l" +zerolog.MessageFieldName = "m" + +log.Info().Msg("hello world") + +// Output: {"l":"info","t":1494567715,"m":"hello world"} +``` + +### Add contextual fields to the global logger + +```go +log.Logger = log.With().Str("foo", "bar").Logger() +``` + +### Add file and line number to log + +```go +log.Logger = log.With().Caller().Logger() +log.Info().Msg("hello world") + +// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"} +``` + + +### Thread-safe, lock-free, non-blocking writer + +If your writer might be slow or not thread-safe and you need your log producers to never get slowed down by a slow writer, you can use a `diode.Writer` as follow: + +```go +wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) { + fmt.Printf("Logger Dropped %d messages", missed) + }) +log := zerolog.New(wr) +log.Print("test") +``` + +You will need to install `code.cloudfoundry.org/go-diodes` to use this feature. + +### Log Sampling + +```go +sampled := log.Sample(&zerolog.BasicSampler{N: 10}) +sampled.Info().Msg("will be logged every 10 messages") + +// Output: {"time":1494567715,"level":"info","message":"will be logged every 10 messages"} +``` + +More advanced sampling: + +```go +// Will let 5 debug messages per period of 1 second. +// Over 5 debug message, 1 every 100 debug messages are logged. +// Other levels are not sampled. +sampled := log.Sample(zerolog.LevelSampler{ + DebugSampler: &zerolog.BurstSampler{ + Burst: 5, + Period: 1*time.Second, + NextSampler: &zerolog.BasicSampler{N: 100}, + }, +}) +sampled.Debug().Msg("hello world") + +// Output: {"time":1494567715,"level":"debug","message":"hello world"} +``` + +### Hooks + +```go +type SeverityHook struct{} + +func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { + if level != zerolog.NoLevel { + e.Str("severity", level.String()) + } +} + +hooked := log.Hook(SeverityHook{}) +hooked.Warn().Msg("") + +// Output: {"level":"warn","severity":"warn"} +``` + +### Pass a sub-logger by context + +```go +ctx := log.With().Str("component", "module").Logger().WithContext(ctx) + +log.Ctx(ctx).Info().Msg("hello world") + +// Output: {"component":"module","level":"info","message":"hello world"} +``` + +### Set as standard logger output + +```go +log := zerolog.New(os.Stdout).With(). + Str("foo", "bar"). + Logger() + +stdlog.SetFlags(0) +stdlog.SetOutput(log) + +stdlog.Print("hello world") + +// Output: {"foo":"bar","message":"hello world"} +``` + +### Integration with `net/http` + +The `github.com/rs/zerolog/hlog` package provides some helpers to integrate zerolog with `http.Handler`. + +In this example we use [alice](https://github.com/justinas/alice) to install logger for better readability. + +```go +log := zerolog.New(os.Stdout).With(). + Timestamp(). + Str("role", "my-service"). + Str("host", host). + Logger() + +c := alice.New() + +// Install the logger handler with default output on the console +c = c.Append(hlog.NewHandler(log)) + +// Install some provided extra handler to set some request's context fields. +// Thanks to that handler, all our logs will come with some prepopulated fields. +c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) { + hlog.FromRequest(r).Info(). + Str("method", r.Method). + Stringer("url", r.URL). + Int("status", status). + Int("size", size). + Dur("duration", duration). + Msg("") +})) +c = c.Append(hlog.RemoteAddrHandler("ip")) +c = c.Append(hlog.UserAgentHandler("user_agent")) +c = c.Append(hlog.RefererHandler("referer")) +c = c.Append(hlog.RequestIDHandler("req_id", "Request-Id")) + +// Here is your final handler +h := c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Get the logger from the request's context. You can safely assume it + // will be always there: if the handler is removed, hlog.FromRequest + // will return a no-op logger. + hlog.FromRequest(r).Info(). + Str("user", "current user"). + Str("status", "ok"). + Msg("Something happened") + + // Output: {"level":"info","time":"2001-02-03T04:05:06Z","role":"my-service","host":"local-hostname","req_id":"b4g0l5t6tfid6dtrapu0","user":"current user","status":"ok","message":"Something happened"} +})) +http.Handle("/", h) + +if err := http.ListenAndServe(":8080", nil); err != nil { + log.Fatal().Err(err).Msg("Startup failed") +} +``` + +## Multiple Log Output +`zerolog.MultiLevelWriter` may be used to send the log message to multiple outputs. +In this example, we send the log message to both `os.Stdout` and the in-built ConsoleWriter. +```go +func main() { + consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout} + + multi := zerolog.MultiLevelWriter(consoleWriter, os.Stdout) + + logger := zerolog.New(multi).With().Timestamp().Logger() + + logger.Info().Msg("Hello World!") +} + +// Output (Line 1: Console; Line 2: Stdout) +// 12:36PM INF Hello World! +// {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"} +``` + +## Global Settings + +Some settings can be changed and will by applied to all loggers: + +* `log.Logger`: You can set this value to customize the global logger (the one used by package level methods). +* `zerolog.SetGlobalLevel`: Can raise the minimum level of all loggers. Call this with `zerolog.Disabled` to disable logging altogether (quiet mode). +* `zerolog.DisableSampling`: If argument is `true`, all sampled loggers will stop sampling and issue 100% of their log events. +* `zerolog.TimestampFieldName`: Can be set to customize `Timestamp` field name. +* `zerolog.LevelFieldName`: Can be set to customize level field name. +* `zerolog.MessageFieldName`: Can be set to customize message field name. +* `zerolog.ErrorFieldName`: Can be set to customize `Err` field name. +* `zerolog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with `zerolog.TimeFormatUnix`, `zerolog.TimeFormatUnixMs` or `zerolog.TimeFormatUnixMicro`, times are formated as UNIX timestamp. +* `zerolog.DurationFieldUnit`: Can be set to customize the unit for time.Duration type fields added by `Dur` (default: `time.Millisecond`). +* `zerolog.DurationFieldInteger`: If set to `true`, `Dur` fields are formatted as integers instead of floats (default: `false`). +* `zerolog.ErrorHandler`: Called whenever zerolog fails to write an event on its output. If not set, an error is printed on the stderr. This handler must be thread safe and non-blocking. + +## Field Types + +### Standard Types + +* `Str` +* `Bool` +* `Int`, `Int8`, `Int16`, `Int32`, `Int64` +* `Uint`, `Uint8`, `Uint16`, `Uint32`, `Uint64` +* `Float32`, `Float64` + +### Advanced Fields + +* `Err`: Takes an `error` and renders it as a string using the `zerolog.ErrorFieldName` field name. +* `Func`: Run a `func` only if the level is enabled. +* `Timestamp`: Inserts a timestamp field with `zerolog.TimestampFieldName` field name, formatted using `zerolog.TimeFieldFormat`. +* `Time`: Adds a field with time formatted with `zerolog.TimeFieldFormat`. +* `Dur`: Adds a field with `time.Duration`. +* `Dict`: Adds a sub-key/value as a field of the event. +* `RawJSON`: Adds a field with an already encoded JSON (`[]byte`) +* `Hex`: Adds a field with value formatted as a hexadecimal string (`[]byte`) +* `Interface`: Uses reflection to marshal the type. + +Most fields are also available in the slice format (`Strs` for `[]string`, `Errs` for `[]error` etc.) + +## Binary Encoding + +In addition to the default JSON encoding, `zerolog` can produce binary logs using [CBOR](http://cbor.io) encoding. The choice of encoding can be decided at compile time using the build tag `binary_log` as follows: + +```bash +go build -tags binary_log . +``` + +To Decode binary encoded log files you can use any CBOR decoder. One has been tested to work +with zerolog library is [CSD](https://github.com/toravir/csd/). + +## Related Projects + +* [grpc-zerolog](https://github.com/cheapRoc/grpc-zerolog): Implementation of `grpclog.LoggerV2` interface using `zerolog` +* [overlog](https://github.com/Trendyol/overlog): Implementation of `Mapped Diagnostic Context` interface using `zerolog` +* [zerologr](https://github.com/go-logr/zerologr): Implementation of `logr.LogSink` interface using `zerolog` + +## Benchmarks + +See [logbench](http://hackemist.com/logbench/) for more comprehensive and up-to-date benchmarks. + +All operations are allocation free (those numbers *include* JSON encoding): + +```text +BenchmarkLogEmpty-8 100000000 19.1 ns/op 0 B/op 0 allocs/op +BenchmarkDisabled-8 500000000 4.07 ns/op 0 B/op 0 allocs/op +BenchmarkInfo-8 30000000 42.5 ns/op 0 B/op 0 allocs/op +BenchmarkContextFields-8 30000000 44.9 ns/op 0 B/op 0 allocs/op +BenchmarkLogFields-8 10000000 184 ns/op 0 B/op 0 allocs/op +``` + +There are a few Go logging benchmarks and comparisons that include zerolog. + +* [imkira/go-loggers-bench](https://github.com/imkira/go-loggers-bench) +* [uber-common/zap](https://github.com/uber-go/zap#performance) + +Using Uber's zap comparison benchmark: + +Log a message and 10 fields: + +| Library | Time | Bytes Allocated | Objects Allocated | +| :--- | :---: | :---: | :---: | +| zerolog | 767 ns/op | 552 B/op | 6 allocs/op | +| :zap: zap | 848 ns/op | 704 B/op | 2 allocs/op | +| :zap: zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op | +| go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op | +| lion | 5392 ns/op | 5807 B/op | 63 allocs/op | +| logrus | 5661 ns/op | 6092 B/op | 78 allocs/op | +| apex/log | 15332 ns/op | 3832 B/op | 65 allocs/op | +| log15 | 20657 ns/op | 5632 B/op | 93 allocs/op | + +Log a message with a logger that already has 10 fields of context: + +| Library | Time | Bytes Allocated | Objects Allocated | +| :--- | :---: | :---: | :---: | +| zerolog | 52 ns/op | 0 B/op | 0 allocs/op | +| :zap: zap | 283 ns/op | 0 B/op | 0 allocs/op | +| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op | +| lion | 2702 ns/op | 4074 B/op | 38 allocs/op | +| go-kit | 3378 ns/op | 3046 B/op | 52 allocs/op | +| logrus | 4309 ns/op | 4564 B/op | 63 allocs/op | +| apex/log | 13456 ns/op | 2898 B/op | 51 allocs/op | +| log15 | 14179 ns/op | 2642 B/op | 44 allocs/op | + +Log a static string, without any context or `printf`-style templating: + +| Library | Time | Bytes Allocated | Objects Allocated | +| :--- | :---: | :---: | :---: | +| zerolog | 50 ns/op | 0 B/op | 0 allocs/op | +| :zap: zap | 236 ns/op | 0 B/op | 0 allocs/op | +| standard library | 453 ns/op | 80 B/op | 2 allocs/op | +| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op | +| go-kit | 508 ns/op | 656 B/op | 13 allocs/op | +| lion | 771 ns/op | 1224 B/op | 10 allocs/op | +| logrus | 1244 ns/op | 1505 B/op | 27 allocs/op | +| apex/log | 2751 ns/op | 584 B/op | 11 allocs/op | +| log15 | 5181 ns/op | 1592 B/op | 26 allocs/op | + +## Caveats + +Note that zerolog does no de-duplication of fields. Using the same key multiple times creates multiple keys in final JSON: + +```go +logger := zerolog.New(os.Stderr).With().Timestamp().Logger() +logger.Info(). + Timestamp(). + Msg("dup") +// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"} +``` + +In this case, many consumers will take the last value, but this is not guaranteed; check yours if in doubt. diff --git a/vendor/github.com/rs/zerolog/_config.yml b/vendor/github.com/rs/zerolog/_config.yml new file mode 100644 index 0000000..a1e896d --- /dev/null +++ b/vendor/github.com/rs/zerolog/_config.yml @@ -0,0 +1 @@ +remote_theme: rs/gh-readme diff --git a/vendor/github.com/rs/zerolog/array.go b/vendor/github.com/rs/zerolog/array.go new file mode 100644 index 0000000..666dded --- /dev/null +++ b/vendor/github.com/rs/zerolog/array.go @@ -0,0 +1,240 @@ +package zerolog + +import ( + "net" + "sync" + "time" +) + +var arrayPool = &sync.Pool{ + New: func() interface{} { + return &Array{ + buf: make([]byte, 0, 500), + } + }, +} + +// Array is used to prepopulate an array of items +// which can be re-used to add to log messages. +type Array struct { + buf []byte +} + +func putArray(a *Array) { + // Proper usage of a sync.Pool requires each entry to have approximately + // the same memory cost. To obtain this property when the stored type + // contains a variably-sized buffer, we add a hard limit on the maximum buffer + // to place back in the pool. + // + // See https://golang.org/issue/23199 + const maxSize = 1 << 16 // 64KiB + if cap(a.buf) > maxSize { + return + } + arrayPool.Put(a) +} + +// Arr creates an array to be added to an Event or Context. +func Arr() *Array { + a := arrayPool.Get().(*Array) + a.buf = a.buf[:0] + return a +} + +// MarshalZerologArray method here is no-op - since data is +// already in the needed format. +func (*Array) MarshalZerologArray(*Array) { +} + +func (a *Array) write(dst []byte) []byte { + dst = enc.AppendArrayStart(dst) + if len(a.buf) > 0 { + dst = append(append(dst, a.buf...)) + } + dst = enc.AppendArrayEnd(dst) + putArray(a) + return dst +} + +// Object marshals an object that implement the LogObjectMarshaler +// interface and append append it to the array. +func (a *Array) Object(obj LogObjectMarshaler) *Array { + e := Dict() + obj.MarshalZerologObject(e) + e.buf = enc.AppendEndMarker(e.buf) + a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...) + putEvent(e) + return a +} + +// Str append append the val as a string to the array. +func (a *Array) Str(val string) *Array { + a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val) + return a +} + +// Bytes append append the val as a string to the array. +func (a *Array) Bytes(val []byte) *Array { + a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val) + return a +} + +// Hex append append the val as a hex string to the array. +func (a *Array) Hex(val []byte) *Array { + a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val) + return a +} + +// RawJSON adds already encoded JSON to the array. +func (a *Array) RawJSON(val []byte) *Array { + a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val) + return a +} + +// Err serializes and appends the err to the array. +func (a *Array) Err(err error) *Array { + switch m := ErrorMarshalFunc(err).(type) { + case LogObjectMarshaler: + e := newEvent(nil, 0) + e.buf = e.buf[:0] + e.appendObject(m) + a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...) + putEvent(e) + case error: + if m == nil || isNilValue(m) { + a.buf = enc.AppendNil(enc.AppendArrayDelim(a.buf)) + } else { + a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error()) + } + case string: + a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m) + default: + a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), m) + } + + return a +} + +// Bool append append the val as a bool to the array. +func (a *Array) Bool(b bool) *Array { + a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b) + return a +} + +// Int append append i as a int to the array. +func (a *Array) Int(i int) *Array { + a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Int8 append append i as a int8 to the array. +func (a *Array) Int8(i int8) *Array { + a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Int16 append append i as a int16 to the array. +func (a *Array) Int16(i int16) *Array { + a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Int32 append append i as a int32 to the array. +func (a *Array) Int32(i int32) *Array { + a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Int64 append append i as a int64 to the array. +func (a *Array) Int64(i int64) *Array { + a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Uint append append i as a uint to the array. +func (a *Array) Uint(i uint) *Array { + a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Uint8 append append i as a uint8 to the array. +func (a *Array) Uint8(i uint8) *Array { + a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Uint16 append append i as a uint16 to the array. +func (a *Array) Uint16(i uint16) *Array { + a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Uint32 append append i as a uint32 to the array. +func (a *Array) Uint32(i uint32) *Array { + a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Uint64 append append i as a uint64 to the array. +func (a *Array) Uint64(i uint64) *Array { + a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i) + return a +} + +// Float32 append append f as a float32 to the array. +func (a *Array) Float32(f float32) *Array { + a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f) + return a +} + +// Float64 append append f as a float64 to the array. +func (a *Array) Float64(f float64) *Array { + a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f) + return a +} + +// Time append append t formated as string using zerolog.TimeFieldFormat. +func (a *Array) Time(t time.Time) *Array { + a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat) + return a +} + +// Dur append append d to the array. +func (a *Array) Dur(d time.Duration) *Array { + a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger) + return a +} + +// Interface append append i marshaled using reflection. +func (a *Array) Interface(i interface{}) *Array { + if obj, ok := i.(LogObjectMarshaler); ok { + return a.Object(obj) + } + a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i) + return a +} + +// IPAddr adds IPv4 or IPv6 address to the array +func (a *Array) IPAddr(ip net.IP) *Array { + a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip) + return a +} + +// IPPrefix adds IPv4 or IPv6 Prefix (IP + mask) to the array +func (a *Array) IPPrefix(pfx net.IPNet) *Array { + a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx) + return a +} + +// MACAddr adds a MAC (Ethernet) address to the array +func (a *Array) MACAddr(ha net.HardwareAddr) *Array { + a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha) + return a +} + +// Dict adds the dict Event to the array +func (a *Array) Dict(dict *Event) *Array { + dict.buf = enc.AppendEndMarker(dict.buf) + a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...) + return a +} diff --git a/vendor/github.com/rs/zerolog/console.go b/vendor/github.com/rs/zerolog/console.go new file mode 100644 index 0000000..a8e12b2 --- /dev/null +++ b/vendor/github.com/rs/zerolog/console.go @@ -0,0 +1,409 @@ +package zerolog + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "sync" + "time" +) + +const ( + colorBlack = iota + 30 + colorRed + colorGreen + colorYellow + colorBlue + colorMagenta + colorCyan + colorWhite + + colorBold = 1 + colorDarkGray = 90 +) + +var ( + consoleBufPool = sync.Pool{ + New: func() interface{} { + return bytes.NewBuffer(make([]byte, 0, 100)) + }, + } +) + +const ( + consoleDefaultTimeFormat = time.Kitchen +) + +// Formatter transforms the input into a formatted string. +type Formatter func(interface{}) string + +// ConsoleWriter parses the JSON input and writes it in an +// (optionally) colorized, human-friendly format to Out. +type ConsoleWriter struct { + // Out is the output destination. + Out io.Writer + + // NoColor disables the colorized output. + NoColor bool + + // TimeFormat specifies the format for timestamp in output. + TimeFormat string + + // PartsOrder defines the order of parts in output. + PartsOrder []string + + // PartsExclude defines parts to not display in output. + PartsExclude []string + + FormatTimestamp Formatter + FormatLevel Formatter + FormatCaller Formatter + FormatMessage Formatter + FormatFieldName Formatter + FormatFieldValue Formatter + FormatErrFieldName Formatter + FormatErrFieldValue Formatter +} + +// NewConsoleWriter creates and initializes a new ConsoleWriter. +func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter { + w := ConsoleWriter{ + Out: os.Stdout, + TimeFormat: consoleDefaultTimeFormat, + PartsOrder: consoleDefaultPartsOrder(), + } + + for _, opt := range options { + opt(&w) + } + + return w +} + +// Write transforms the JSON input with formatters and appends to w.Out. +func (w ConsoleWriter) Write(p []byte) (n int, err error) { + if w.PartsOrder == nil { + w.PartsOrder = consoleDefaultPartsOrder() + } + + var buf = consoleBufPool.Get().(*bytes.Buffer) + defer func() { + buf.Reset() + consoleBufPool.Put(buf) + }() + + var evt map[string]interface{} + p = decodeIfBinaryToBytes(p) + d := json.NewDecoder(bytes.NewReader(p)) + d.UseNumber() + err = d.Decode(&evt) + if err != nil { + return n, fmt.Errorf("cannot decode event: %s", err) + } + + for _, p := range w.PartsOrder { + w.writePart(buf, evt, p) + } + + w.writeFields(evt, buf) + + err = buf.WriteByte('\n') + if err != nil { + return n, err + } + _, err = buf.WriteTo(w.Out) + return len(p), err +} + +// writeFields appends formatted key-value pairs to buf. +func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) { + var fields = make([]string, 0, len(evt)) + for field := range evt { + switch field { + case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName: + continue + } + fields = append(fields, field) + } + sort.Strings(fields) + + if len(fields) > 0 { + buf.WriteByte(' ') + } + + // Move the "error" field to the front + ei := sort.Search(len(fields), func(i int) bool { return fields[i] >= ErrorFieldName }) + if ei < len(fields) && fields[ei] == ErrorFieldName { + fields[ei] = "" + fields = append([]string{ErrorFieldName}, fields...) + var xfields = make([]string, 0, len(fields)) + for _, field := range fields { + if field == "" { // Skip empty fields + continue + } + xfields = append(xfields, field) + } + fields = xfields + } + + for i, field := range fields { + var fn Formatter + var fv Formatter + + if field == ErrorFieldName { + if w.FormatErrFieldName == nil { + fn = consoleDefaultFormatErrFieldName(w.NoColor) + } else { + fn = w.FormatErrFieldName + } + + if w.FormatErrFieldValue == nil { + fv = consoleDefaultFormatErrFieldValue(w.NoColor) + } else { + fv = w.FormatErrFieldValue + } + } else { + if w.FormatFieldName == nil { + fn = consoleDefaultFormatFieldName(w.NoColor) + } else { + fn = w.FormatFieldName + } + + if w.FormatFieldValue == nil { + fv = consoleDefaultFormatFieldValue + } else { + fv = w.FormatFieldValue + } + } + + buf.WriteString(fn(field)) + + switch fValue := evt[field].(type) { + case string: + if needsQuote(fValue) { + buf.WriteString(fv(strconv.Quote(fValue))) + } else { + buf.WriteString(fv(fValue)) + } + case json.Number: + buf.WriteString(fv(fValue)) + default: + b, err := json.Marshal(fValue) + if err != nil { + fmt.Fprintf(buf, colorize("[error: %v]", colorRed, w.NoColor), err) + } else { + fmt.Fprint(buf, fv(b)) + } + } + + if i < len(fields)-1 { // Skip space for last field + buf.WriteByte(' ') + } + } +} + +// writePart appends a formatted part to buf. +func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) { + var f Formatter + + if w.PartsExclude != nil && len(w.PartsExclude) > 0 { + for _, exclude := range w.PartsExclude { + if exclude == p { + return + } + } + } + + switch p { + case LevelFieldName: + if w.FormatLevel == nil { + f = consoleDefaultFormatLevel(w.NoColor) + } else { + f = w.FormatLevel + } + case TimestampFieldName: + if w.FormatTimestamp == nil { + f = consoleDefaultFormatTimestamp(w.TimeFormat, w.NoColor) + } else { + f = w.FormatTimestamp + } + case MessageFieldName: + if w.FormatMessage == nil { + f = consoleDefaultFormatMessage + } else { + f = w.FormatMessage + } + case CallerFieldName: + if w.FormatCaller == nil { + f = consoleDefaultFormatCaller(w.NoColor) + } else { + f = w.FormatCaller + } + default: + if w.FormatFieldValue == nil { + f = consoleDefaultFormatFieldValue + } else { + f = w.FormatFieldValue + } + } + + var s = f(evt[p]) + + if len(s) > 0 { + buf.WriteString(s) + if p != w.PartsOrder[len(w.PartsOrder)-1] { // Skip space for last part + buf.WriteByte(' ') + } + } +} + +// needsQuote returns true when the string s should be quoted in output. +func needsQuote(s string) bool { + for i := range s { + if s[i] < 0x20 || s[i] > 0x7e || s[i] == ' ' || s[i] == '\\' || s[i] == '"' { + return true + } + } + return false +} + +// colorize returns the string s wrapped in ANSI code c, unless disabled is true. +func colorize(s interface{}, c int, disabled bool) string { + if disabled { + return fmt.Sprintf("%s", s) + } + return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s) +} + +// ----- DEFAULT FORMATTERS --------------------------------------------------- + +func consoleDefaultPartsOrder() []string { + return []string{ + TimestampFieldName, + LevelFieldName, + CallerFieldName, + MessageFieldName, + } +} + +func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter { + if timeFormat == "" { + timeFormat = consoleDefaultTimeFormat + } + return func(i interface{}) string { + t := "" + switch tt := i.(type) { + case string: + ts, err := time.Parse(TimeFieldFormat, tt) + if err != nil { + t = tt + } else { + t = ts.Format(timeFormat) + } + case json.Number: + i, err := tt.Int64() + if err != nil { + t = tt.String() + } else { + var sec, nsec int64 = i, 0 + switch TimeFieldFormat { + case TimeFormatUnixMs: + nsec = int64(time.Duration(i) * time.Millisecond) + sec = 0 + case TimeFormatUnixMicro: + nsec = int64(time.Duration(i) * time.Microsecond) + sec = 0 + } + ts := time.Unix(sec, nsec).UTC() + t = ts.Format(timeFormat) + } + } + return colorize(t, colorDarkGray, noColor) + } +} + +func consoleDefaultFormatLevel(noColor bool) Formatter { + return func(i interface{}) string { + var l string + if ll, ok := i.(string); ok { + switch ll { + case LevelTraceValue: + l = colorize("TRC", colorMagenta, noColor) + case LevelDebugValue: + l = colorize("DBG", colorYellow, noColor) + case LevelInfoValue: + l = colorize("INF", colorGreen, noColor) + case LevelWarnValue: + l = colorize("WRN", colorRed, noColor) + case LevelErrorValue: + l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor) + case LevelFatalValue: + l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor) + case LevelPanicValue: + l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor) + default: + l = colorize("???", colorBold, noColor) + } + } else { + if i == nil { + l = colorize("???", colorBold, noColor) + } else { + l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3] + } + } + return l + } +} + +func consoleDefaultFormatCaller(noColor bool) Formatter { + return func(i interface{}) string { + var c string + if cc, ok := i.(string); ok { + c = cc + } + if len(c) > 0 { + if cwd, err := os.Getwd(); err == nil { + if rel, err := filepath.Rel(cwd, c); err == nil { + c = rel + } + } + c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor) + } + return c + } +} + +func consoleDefaultFormatMessage(i interface{}) string { + if i == nil { + return "" + } + return fmt.Sprintf("%s", i) +} + +func consoleDefaultFormatFieldName(noColor bool) Formatter { + return func(i interface{}) string { + return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor) + } +} + +func consoleDefaultFormatFieldValue(i interface{}) string { + return fmt.Sprintf("%s", i) +} + +func consoleDefaultFormatErrFieldName(noColor bool) Formatter { + return func(i interface{}) string { + return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor) + } +} + +func consoleDefaultFormatErrFieldValue(noColor bool) Formatter { + return func(i interface{}) string { + return colorize(fmt.Sprintf("%s", i), colorRed, noColor) + } +} diff --git a/vendor/github.com/rs/zerolog/context.go b/vendor/github.com/rs/zerolog/context.go new file mode 100644 index 0000000..f398e31 --- /dev/null +++ b/vendor/github.com/rs/zerolog/context.go @@ -0,0 +1,433 @@ +package zerolog + +import ( + "fmt" + "io/ioutil" + "math" + "net" + "time" +) + +// Context configures a new sub-logger with contextual fields. +type Context struct { + l Logger +} + +// Logger returns the logger with the context previously set. +func (c Context) Logger() Logger { + return c.l +} + +// Fields is a helper function to use a map or slice to set fields using type assertion. +// Only map[string]interface{} and []interface{} are accepted. []interface{} must +// alternate string keys and arbitrary values, and extraneous ones are ignored. +func (c Context) Fields(fields interface{}) Context { + c.l.context = appendFields(c.l.context, fields) + return c +} + +// Dict adds the field key with the dict to the logger context. +func (c Context) Dict(key string, dict *Event) Context { + dict.buf = enc.AppendEndMarker(dict.buf) + c.l.context = append(enc.AppendKey(c.l.context, key), dict.buf...) + putEvent(dict) + return c +} + +// Array adds the field key with an array to the event context. +// Use zerolog.Arr() to create the array or pass a type that +// implement the LogArrayMarshaler interface. +func (c Context) Array(key string, arr LogArrayMarshaler) Context { + c.l.context = enc.AppendKey(c.l.context, key) + if arr, ok := arr.(*Array); ok { + c.l.context = arr.write(c.l.context) + return c + } + var a *Array + if aa, ok := arr.(*Array); ok { + a = aa + } else { + a = Arr() + arr.MarshalZerologArray(a) + } + c.l.context = a.write(c.l.context) + return c +} + +// Object marshals an object that implement the LogObjectMarshaler interface. +func (c Context) Object(key string, obj LogObjectMarshaler) Context { + e := newEvent(levelWriterAdapter{ioutil.Discard}, 0) + e.Object(key, obj) + c.l.context = enc.AppendObjectData(c.l.context, e.buf) + putEvent(e) + return c +} + +// EmbedObject marshals and Embeds an object that implement the LogObjectMarshaler interface. +func (c Context) EmbedObject(obj LogObjectMarshaler) Context { + e := newEvent(levelWriterAdapter{ioutil.Discard}, 0) + e.EmbedObject(obj) + c.l.context = enc.AppendObjectData(c.l.context, e.buf) + putEvent(e) + return c +} + +// Str adds the field key with val as a string to the logger context. +func (c Context) Str(key, val string) Context { + c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val) + return c +} + +// Strs adds the field key with val as a string to the logger context. +func (c Context) Strs(key string, vals []string) Context { + c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals) + return c +} + +// Stringer adds the field key with val.String() (or null if val is nil) to the logger context. +func (c Context) Stringer(key string, val fmt.Stringer) Context { + if val != nil { + c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val.String()) + return c + } + + c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), nil) + return c +} + +// Bytes adds the field key with val as a []byte to the logger context. +func (c Context) Bytes(key string, val []byte) Context { + c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val) + return c +} + +// Hex adds the field key with val as a hex string to the logger context. +func (c Context) Hex(key string, val []byte) Context { + c.l.context = enc.AppendHex(enc.AppendKey(c.l.context, key), val) + return c +} + +// RawJSON adds already encoded JSON to context. +// +// No sanity check is performed on b; it must not contain carriage returns and +// be valid JSON. +func (c Context) RawJSON(key string, b []byte) Context { + c.l.context = appendJSON(enc.AppendKey(c.l.context, key), b) + return c +} + +// AnErr adds the field key with serialized err to the logger context. +func (c Context) AnErr(key string, err error) Context { + switch m := ErrorMarshalFunc(err).(type) { + case nil: + return c + case LogObjectMarshaler: + return c.Object(key, m) + case error: + if m == nil || isNilValue(m) { + return c + } else { + return c.Str(key, m.Error()) + } + case string: + return c.Str(key, m) + default: + return c.Interface(key, m) + } +} + +// Errs adds the field key with errs as an array of serialized errors to the +// logger context. +func (c Context) Errs(key string, errs []error) Context { + arr := Arr() + for _, err := range errs { + switch m := ErrorMarshalFunc(err).(type) { + case LogObjectMarshaler: + arr = arr.Object(m) + case error: + if m == nil || isNilValue(m) { + arr = arr.Interface(nil) + } else { + arr = arr.Str(m.Error()) + } + case string: + arr = arr.Str(m) + default: + arr = arr.Interface(m) + } + } + + return c.Array(key, arr) +} + +// Err adds the field "error" with serialized err to the logger context. +func (c Context) Err(err error) Context { + return c.AnErr(ErrorFieldName, err) +} + +// Bool adds the field key with val as a bool to the logger context. +func (c Context) Bool(key string, b bool) Context { + c.l.context = enc.AppendBool(enc.AppendKey(c.l.context, key), b) + return c +} + +// Bools adds the field key with val as a []bool to the logger context. +func (c Context) Bools(key string, b []bool) Context { + c.l.context = enc.AppendBools(enc.AppendKey(c.l.context, key), b) + return c +} + +// Int adds the field key with i as a int to the logger context. +func (c Context) Int(key string, i int) Context { + c.l.context = enc.AppendInt(enc.AppendKey(c.l.context, key), i) + return c +} + +// Ints adds the field key with i as a []int to the logger context. +func (c Context) Ints(key string, i []int) Context { + c.l.context = enc.AppendInts(enc.AppendKey(c.l.context, key), i) + return c +} + +// Int8 adds the field key with i as a int8 to the logger context. +func (c Context) Int8(key string, i int8) Context { + c.l.context = enc.AppendInt8(enc.AppendKey(c.l.context, key), i) + return c +} + +// Ints8 adds the field key with i as a []int8 to the logger context. +func (c Context) Ints8(key string, i []int8) Context { + c.l.context = enc.AppendInts8(enc.AppendKey(c.l.context, key), i) + return c +} + +// Int16 adds the field key with i as a int16 to the logger context. +func (c Context) Int16(key string, i int16) Context { + c.l.context = enc.AppendInt16(enc.AppendKey(c.l.context, key), i) + return c +} + +// Ints16 adds the field key with i as a []int16 to the logger context. +func (c Context) Ints16(key string, i []int16) Context { + c.l.context = enc.AppendInts16(enc.AppendKey(c.l.context, key), i) + return c +} + +// Int32 adds the field key with i as a int32 to the logger context. +func (c Context) Int32(key string, i int32) Context { + c.l.context = enc.AppendInt32(enc.AppendKey(c.l.context, key), i) + return c +} + +// Ints32 adds the field key with i as a []int32 to the logger context. +func (c Context) Ints32(key string, i []int32) Context { + c.l.context = enc.AppendInts32(enc.AppendKey(c.l.context, key), i) + return c +} + +// Int64 adds the field key with i as a int64 to the logger context. +func (c Context) Int64(key string, i int64) Context { + c.l.context = enc.AppendInt64(enc.AppendKey(c.l.context, key), i) + return c +} + +// Ints64 adds the field key with i as a []int64 to the logger context. +func (c Context) Ints64(key string, i []int64) Context { + c.l.context = enc.AppendInts64(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uint adds the field key with i as a uint to the logger context. +func (c Context) Uint(key string, i uint) Context { + c.l.context = enc.AppendUint(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uints adds the field key with i as a []uint to the logger context. +func (c Context) Uints(key string, i []uint) Context { + c.l.context = enc.AppendUints(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uint8 adds the field key with i as a uint8 to the logger context. +func (c Context) Uint8(key string, i uint8) Context { + c.l.context = enc.AppendUint8(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uints8 adds the field key with i as a []uint8 to the logger context. +func (c Context) Uints8(key string, i []uint8) Context { + c.l.context = enc.AppendUints8(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uint16 adds the field key with i as a uint16 to the logger context. +func (c Context) Uint16(key string, i uint16) Context { + c.l.context = enc.AppendUint16(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uints16 adds the field key with i as a []uint16 to the logger context. +func (c Context) Uints16(key string, i []uint16) Context { + c.l.context = enc.AppendUints16(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uint32 adds the field key with i as a uint32 to the logger context. +func (c Context) Uint32(key string, i uint32) Context { + c.l.context = enc.AppendUint32(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uints32 adds the field key with i as a []uint32 to the logger context. +func (c Context) Uints32(key string, i []uint32) Context { + c.l.context = enc.AppendUints32(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uint64 adds the field key with i as a uint64 to the logger context. +func (c Context) Uint64(key string, i uint64) Context { + c.l.context = enc.AppendUint64(enc.AppendKey(c.l.context, key), i) + return c +} + +// Uints64 adds the field key with i as a []uint64 to the logger context. +func (c Context) Uints64(key string, i []uint64) Context { + c.l.context = enc.AppendUints64(enc.AppendKey(c.l.context, key), i) + return c +} + +// Float32 adds the field key with f as a float32 to the logger context. +func (c Context) Float32(key string, f float32) Context { + c.l.context = enc.AppendFloat32(enc.AppendKey(c.l.context, key), f) + return c +} + +// Floats32 adds the field key with f as a []float32 to the logger context. +func (c Context) Floats32(key string, f []float32) Context { + c.l.context = enc.AppendFloats32(enc.AppendKey(c.l.context, key), f) + return c +} + +// Float64 adds the field key with f as a float64 to the logger context. +func (c Context) Float64(key string, f float64) Context { + c.l.context = enc.AppendFloat64(enc.AppendKey(c.l.context, key), f) + return c +} + +// Floats64 adds the field key with f as a []float64 to the logger context. +func (c Context) Floats64(key string, f []float64) Context { + c.l.context = enc.AppendFloats64(enc.AppendKey(c.l.context, key), f) + return c +} + +type timestampHook struct{} + +func (ts timestampHook) Run(e *Event, level Level, msg string) { + e.Timestamp() +} + +var th = timestampHook{} + +// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key. +// To customize the key name, change zerolog.TimestampFieldName. +// +// NOTE: It won't dedupe the "time" key if the *Context has one already. +func (c Context) Timestamp() Context { + c.l = c.l.Hook(th) + return c +} + +// Time adds the field key with t formated as string using zerolog.TimeFieldFormat. +func (c Context) Time(key string, t time.Time) Context { + c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat) + return c +} + +// Times adds the field key with t formated as string using zerolog.TimeFieldFormat. +func (c Context) Times(key string, t []time.Time) Context { + c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat) + return c +} + +// Dur adds the fields key with d divided by unit and stored as a float. +func (c Context) Dur(key string, d time.Duration) Context { + c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) + return c +} + +// Durs adds the fields key with d divided by unit and stored as a float. +func (c Context) Durs(key string, d []time.Duration) Context { + c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger) + return c +} + +// Interface adds the field key with obj marshaled using reflection. +func (c Context) Interface(key string, i interface{}) Context { + c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), i) + return c +} + +type callerHook struct { + callerSkipFrameCount int +} + +func newCallerHook(skipFrameCount int) callerHook { + return callerHook{callerSkipFrameCount: skipFrameCount} +} + +func (ch callerHook) Run(e *Event, level Level, msg string) { + switch ch.callerSkipFrameCount { + case useGlobalSkipFrameCount: + // Extra frames to skip (added by hook infra). + e.caller(CallerSkipFrameCount + contextCallerSkipFrameCount) + default: + // Extra frames to skip (added by hook infra). + e.caller(ch.callerSkipFrameCount + contextCallerSkipFrameCount) + } +} + +// useGlobalSkipFrameCount acts as a flag to informat callerHook.Run +// to use the global CallerSkipFrameCount. +const useGlobalSkipFrameCount = math.MinInt32 + +// ch is the default caller hook using the global CallerSkipFrameCount. +var ch = newCallerHook(useGlobalSkipFrameCount) + +// Caller adds the file:line of the caller with the zerolog.CallerFieldName key. +func (c Context) Caller() Context { + c.l = c.l.Hook(ch) + return c +} + +// CallerWithSkipFrameCount adds the file:line of the caller with the zerolog.CallerFieldName key. +// The specified skipFrameCount int will override the global CallerSkipFrameCount for this context's respective logger. +// If set to -1 the global CallerSkipFrameCount will be used. +func (c Context) CallerWithSkipFrameCount(skipFrameCount int) Context { + c.l = c.l.Hook(newCallerHook(skipFrameCount)) + return c +} + +// Stack enables stack trace printing for the error passed to Err(). +func (c Context) Stack() Context { + c.l.stack = true + return c +} + +// IPAddr adds IPv4 or IPv6 Address to the context +func (c Context) IPAddr(key string, ip net.IP) Context { + c.l.context = enc.AppendIPAddr(enc.AppendKey(c.l.context, key), ip) + return c +} + +// IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the context +func (c Context) IPPrefix(key string, pfx net.IPNet) Context { + c.l.context = enc.AppendIPPrefix(enc.AppendKey(c.l.context, key), pfx) + return c +} + +// MACAddr adds MAC address to the context +func (c Context) MACAddr(key string, ha net.HardwareAddr) Context { + c.l.context = enc.AppendMACAddr(enc.AppendKey(c.l.context, key), ha) + return c +} diff --git a/vendor/github.com/rs/zerolog/ctx.go b/vendor/github.com/rs/zerolog/ctx.go new file mode 100644 index 0000000..966d17e --- /dev/null +++ b/vendor/github.com/rs/zerolog/ctx.go @@ -0,0 +1,51 @@ +package zerolog + +import ( + "context" +) + +var disabledLogger *Logger + +func init() { + SetGlobalLevel(TraceLevel) + l := Nop() + disabledLogger = &l +} + +type ctxKey struct{} + +// WithContext returns a copy of ctx with l associated. If an instance of Logger +// is already in the context, the context is not updated. +// +// For instance, to add a field to an existing logger in the context, use this +// notation: +// +// ctx := r.Context() +// l := zerolog.Ctx(ctx) +// l.UpdateContext(func(c Context) Context { +// return c.Str("bar", "baz") +// }) +func (l *Logger) WithContext(ctx context.Context) context.Context { + if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok { + if lp == l { + // Do not store same logger. + return ctx + } + } else if l.level == Disabled { + // Do not store disabled logger. + return ctx + } + return context.WithValue(ctx, ctxKey{}, l) +} + +// Ctx returns the Logger associated with the ctx. If no logger +// is associated, DefaultContextLogger is returned, unless DefaultContextLogger +// is nil, in which case a disabled logger is returned. +func Ctx(ctx context.Context) *Logger { + if l, ok := ctx.Value(ctxKey{}).(*Logger); ok { + return l + } else if l = DefaultContextLogger; l != nil { + return l + } + return disabledLogger +} diff --git a/vendor/github.com/rs/zerolog/encoder.go b/vendor/github.com/rs/zerolog/encoder.go new file mode 100644 index 0000000..09b24e8 --- /dev/null +++ b/vendor/github.com/rs/zerolog/encoder.go @@ -0,0 +1,56 @@ +package zerolog + +import ( + "net" + "time" +) + +type encoder interface { + AppendArrayDelim(dst []byte) []byte + AppendArrayEnd(dst []byte) []byte + AppendArrayStart(dst []byte) []byte + AppendBeginMarker(dst []byte) []byte + AppendBool(dst []byte, val bool) []byte + AppendBools(dst []byte, vals []bool) []byte + AppendBytes(dst, s []byte) []byte + AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte + AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte + AppendEndMarker(dst []byte) []byte + AppendFloat32(dst []byte, val float32) []byte + AppendFloat64(dst []byte, val float64) []byte + AppendFloats32(dst []byte, vals []float32) []byte + AppendFloats64(dst []byte, vals []float64) []byte + AppendHex(dst, s []byte) []byte + AppendIPAddr(dst []byte, ip net.IP) []byte + AppendIPPrefix(dst []byte, pfx net.IPNet) []byte + AppendInt(dst []byte, val int) []byte + AppendInt16(dst []byte, val int16) []byte + AppendInt32(dst []byte, val int32) []byte + AppendInt64(dst []byte, val int64) []byte + AppendInt8(dst []byte, val int8) []byte + AppendInterface(dst []byte, i interface{}) []byte + AppendInts(dst []byte, vals []int) []byte + AppendInts16(dst []byte, vals []int16) []byte + AppendInts32(dst []byte, vals []int32) []byte + AppendInts64(dst []byte, vals []int64) []byte + AppendInts8(dst []byte, vals []int8) []byte + AppendKey(dst []byte, key string) []byte + AppendLineBreak(dst []byte) []byte + AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte + AppendNil(dst []byte) []byte + AppendObjectData(dst []byte, o []byte) []byte + AppendString(dst []byte, s string) []byte + AppendStrings(dst []byte, vals []string) []byte + AppendTime(dst []byte, t time.Time, format string) []byte + AppendTimes(dst []byte, vals []time.Time, format string) []byte + AppendUint(dst []byte, val uint) []byte + AppendUint16(dst []byte, val uint16) []byte + AppendUint32(dst []byte, val uint32) []byte + AppendUint64(dst []byte, val uint64) []byte + AppendUint8(dst []byte, val uint8) []byte + AppendUints(dst []byte, vals []uint) []byte + AppendUints16(dst []byte, vals []uint16) []byte + AppendUints32(dst []byte, vals []uint32) []byte + AppendUints64(dst []byte, vals []uint64) []byte + AppendUints8(dst []byte, vals []uint8) []byte +} diff --git a/vendor/github.com/rs/zerolog/encoder_cbor.go b/vendor/github.com/rs/zerolog/encoder_cbor.go new file mode 100644 index 0000000..7b0dafe --- /dev/null +++ b/vendor/github.com/rs/zerolog/encoder_cbor.go @@ -0,0 +1,42 @@ +// +build binary_log + +package zerolog + +// This file contains bindings to do binary encoding. + +import ( + "github.com/rs/zerolog/internal/cbor" +) + +var ( + _ encoder = (*cbor.Encoder)(nil) + + enc = cbor.Encoder{} +) + +func init() { + // using closure to reflect the changes at runtime. + cbor.JSONMarshalFunc = func(v interface{}) ([]byte, error) { + return InterfaceMarshalFunc(v) + } +} + +func appendJSON(dst []byte, j []byte) []byte { + return cbor.AppendEmbeddedJSON(dst, j) +} + +// decodeIfBinaryToString - converts a binary formatted log msg to a +// JSON formatted String Log message. +func decodeIfBinaryToString(in []byte) string { + return cbor.DecodeIfBinaryToString(in) +} + +func decodeObjectToStr(in []byte) string { + return cbor.DecodeObjectToStr(in) +} + +// decodeIfBinaryToBytes - converts a binary formatted log msg to a +// JSON formatted Bytes Log message. +func decodeIfBinaryToBytes(in []byte) []byte { + return cbor.DecodeIfBinaryToBytes(in) +} diff --git a/vendor/github.com/rs/zerolog/encoder_json.go b/vendor/github.com/rs/zerolog/encoder_json.go new file mode 100644 index 0000000..0e0450e --- /dev/null +++ b/vendor/github.com/rs/zerolog/encoder_json.go @@ -0,0 +1,39 @@ +// +build !binary_log + +package zerolog + +// encoder_json.go file contains bindings to generate +// JSON encoded byte stream. + +import ( + "github.com/rs/zerolog/internal/json" +) + +var ( + _ encoder = (*json.Encoder)(nil) + + enc = json.Encoder{} +) + +func init() { + // using closure to reflect the changes at runtime. + json.JSONMarshalFunc = func(v interface{}) ([]byte, error) { + return InterfaceMarshalFunc(v) + } +} + +func appendJSON(dst []byte, j []byte) []byte { + return append(dst, j...) +} + +func decodeIfBinaryToString(in []byte) string { + return string(in) +} + +func decodeObjectToStr(in []byte) string { + return string(in) +} + +func decodeIfBinaryToBytes(in []byte) []byte { + return in +} diff --git a/vendor/github.com/rs/zerolog/event.go b/vendor/github.com/rs/zerolog/event.go new file mode 100644 index 0000000..b409955 --- /dev/null +++ b/vendor/github.com/rs/zerolog/event.go @@ -0,0 +1,773 @@ +package zerolog + +import ( + "fmt" + "net" + "os" + "runtime" + "sync" + "time" +) + +var eventPool = &sync.Pool{ + New: func() interface{} { + return &Event{ + buf: make([]byte, 0, 500), + } + }, +} + +// Event represents a log event. It is instanced by one of the level method of +// Logger and finalized by the Msg or Msgf method. +type Event struct { + buf []byte + w LevelWriter + level Level + done func(msg string) + stack bool // enable error stack trace + ch []Hook // hooks from context + skipFrame int // The number of additional frames to skip when printing the caller. +} + +func putEvent(e *Event) { + // Proper usage of a sync.Pool requires each entry to have approximately + // the same memory cost. To obtain this property when the stored type + // contains a variably-sized buffer, we add a hard limit on the maximum buffer + // to place back in the pool. + // + // See https://golang.org/issue/23199 + const maxSize = 1 << 16 // 64KiB + if cap(e.buf) > maxSize { + return + } + eventPool.Put(e) +} + +// LogObjectMarshaler provides a strongly-typed and encoding-agnostic interface +// to be implemented by types used with Event/Context's Object methods. +type LogObjectMarshaler interface { + MarshalZerologObject(e *Event) +} + +// LogArrayMarshaler provides a strongly-typed and encoding-agnostic interface +// to be implemented by types used with Event/Context's Array methods. +type LogArrayMarshaler interface { + MarshalZerologArray(a *Array) +} + +func newEvent(w LevelWriter, level Level) *Event { + e := eventPool.Get().(*Event) + e.buf = e.buf[:0] + e.ch = nil + e.buf = enc.AppendBeginMarker(e.buf) + e.w = w + e.level = level + e.stack = false + e.skipFrame = 0 + return e +} + +func (e *Event) write() (err error) { + if e == nil { + return nil + } + if e.level != Disabled { + e.buf = enc.AppendEndMarker(e.buf) + e.buf = enc.AppendLineBreak(e.buf) + if e.w != nil { + _, err = e.w.WriteLevel(e.level, e.buf) + } + } + putEvent(e) + return +} + +// Enabled return false if the *Event is going to be filtered out by +// log level or sampling. +func (e *Event) Enabled() bool { + return e != nil && e.level != Disabled +} + +// Discard disables the event so Msg(f) won't print it. +func (e *Event) Discard() *Event { + if e == nil { + return e + } + e.level = Disabled + return nil +} + +// Msg sends the *Event with msg added as the message field if not empty. +// +// NOTICE: once this method is called, the *Event should be disposed. +// Calling Msg twice can have unexpected result. +func (e *Event) Msg(msg string) { + if e == nil { + return + } + e.msg(msg) +} + +// Send is equivalent to calling Msg(""). +// +// NOTICE: once this method is called, the *Event should be disposed. +func (e *Event) Send() { + if e == nil { + return + } + e.msg("") +} + +// Msgf sends the event with formatted msg added as the message field if not empty. +// +// NOTICE: once this method is called, the *Event should be disposed. +// Calling Msgf twice can have unexpected result. +func (e *Event) Msgf(format string, v ...interface{}) { + if e == nil { + return + } + e.msg(fmt.Sprintf(format, v...)) +} + +func (e *Event) msg(msg string) { + for _, hook := range e.ch { + hook.Run(e, e.level, msg) + } + if msg != "" { + e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg) + } + if e.done != nil { + defer e.done(msg) + } + if err := e.write(); err != nil { + if ErrorHandler != nil { + ErrorHandler(err) + } else { + fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v\n", err) + } + } +} + +// Fields is a helper function to use a map or slice to set fields using type assertion. +// Only map[string]interface{} and []interface{} are accepted. []interface{} must +// alternate string keys and arbitrary values, and extraneous ones are ignored. +func (e *Event) Fields(fields interface{}) *Event { + if e == nil { + return e + } + e.buf = appendFields(e.buf, fields) + return e +} + +// Dict adds the field key with a dict to the event context. +// Use zerolog.Dict() to create the dictionary. +func (e *Event) Dict(key string, dict *Event) *Event { + if e == nil { + return e + } + dict.buf = enc.AppendEndMarker(dict.buf) + e.buf = append(enc.AppendKey(e.buf, key), dict.buf...) + putEvent(dict) + return e +} + +// Dict creates an Event to be used with the *Event.Dict method. +// Call usual field methods like Str, Int etc to add fields to this +// event and give it as argument the *Event.Dict method. +func Dict() *Event { + return newEvent(nil, 0) +} + +// Array adds the field key with an array to the event context. +// Use zerolog.Arr() to create the array or pass a type that +// implement the LogArrayMarshaler interface. +func (e *Event) Array(key string, arr LogArrayMarshaler) *Event { + if e == nil { + return e + } + e.buf = enc.AppendKey(e.buf, key) + var a *Array + if aa, ok := arr.(*Array); ok { + a = aa + } else { + a = Arr() + arr.MarshalZerologArray(a) + } + e.buf = a.write(e.buf) + return e +} + +func (e *Event) appendObject(obj LogObjectMarshaler) { + e.buf = enc.AppendBeginMarker(e.buf) + obj.MarshalZerologObject(e) + e.buf = enc.AppendEndMarker(e.buf) +} + +// Object marshals an object that implement the LogObjectMarshaler interface. +func (e *Event) Object(key string, obj LogObjectMarshaler) *Event { + if e == nil { + return e + } + e.buf = enc.AppendKey(e.buf, key) + if obj == nil { + e.buf = enc.AppendNil(e.buf) + + return e + } + + e.appendObject(obj) + return e +} + +// Func allows an anonymous func to run only if the event is enabled. +func (e *Event) Func(f func(e *Event)) *Event { + if e != nil && e.Enabled() { + f(e) + } + return e +} + +// EmbedObject marshals an object that implement the LogObjectMarshaler interface. +func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event { + if e == nil { + return e + } + if obj == nil { + return e + } + obj.MarshalZerologObject(e) + return e +} + +// Str adds the field key with val as a string to the *Event context. +func (e *Event) Str(key, val string) *Event { + if e == nil { + return e + } + e.buf = enc.AppendString(enc.AppendKey(e.buf, key), val) + return e +} + +// Strs adds the field key with vals as a []string to the *Event context. +func (e *Event) Strs(key string, vals []string) *Event { + if e == nil { + return e + } + e.buf = enc.AppendStrings(enc.AppendKey(e.buf, key), vals) + return e +} + +// Stringer adds the field key with val.String() (or null if val is nil) +// to the *Event context. +func (e *Event) Stringer(key string, val fmt.Stringer) *Event { + if e == nil { + return e + } + e.buf = enc.AppendStringer(enc.AppendKey(e.buf, key), val) + return e +} + +// Stringers adds the field key with vals where each individual val +// is used as val.String() (or null if val is empty) to the *Event +// context. +func (e *Event) Stringers(key string, vals []fmt.Stringer) *Event { + if e == nil { + return e + } + e.buf = enc.AppendStringers(enc.AppendKey(e.buf, key), vals) + return e +} + +// Bytes adds the field key with val as a string to the *Event context. +// +// Runes outside of normal ASCII ranges will be hex-encoded in the resulting +// JSON. +func (e *Event) Bytes(key string, val []byte) *Event { + if e == nil { + return e + } + e.buf = enc.AppendBytes(enc.AppendKey(e.buf, key), val) + return e +} + +// Hex adds the field key with val as a hex string to the *Event context. +func (e *Event) Hex(key string, val []byte) *Event { + if e == nil { + return e + } + e.buf = enc.AppendHex(enc.AppendKey(e.buf, key), val) + return e +} + +// RawJSON adds already encoded JSON to the log line under key. +// +// No sanity check is performed on b; it must not contain carriage returns and +// be valid JSON. +func (e *Event) RawJSON(key string, b []byte) *Event { + if e == nil { + return e + } + e.buf = appendJSON(enc.AppendKey(e.buf, key), b) + return e +} + +// AnErr adds the field key with serialized err to the *Event context. +// If err is nil, no field is added. +func (e *Event) AnErr(key string, err error) *Event { + if e == nil { + return e + } + switch m := ErrorMarshalFunc(err).(type) { + case nil: + return e + case LogObjectMarshaler: + return e.Object(key, m) + case error: + if m == nil || isNilValue(m) { + return e + } else { + return e.Str(key, m.Error()) + } + case string: + return e.Str(key, m) + default: + return e.Interface(key, m) + } +} + +// Errs adds the field key with errs as an array of serialized errors to the +// *Event context. +func (e *Event) Errs(key string, errs []error) *Event { + if e == nil { + return e + } + arr := Arr() + for _, err := range errs { + switch m := ErrorMarshalFunc(err).(type) { + case LogObjectMarshaler: + arr = arr.Object(m) + case error: + arr = arr.Err(m) + case string: + arr = arr.Str(m) + default: + arr = arr.Interface(m) + } + } + + return e.Array(key, arr) +} + +// Err adds the field "error" with serialized err to the *Event context. +// If err is nil, no field is added. +// +// To customize the key name, change zerolog.ErrorFieldName. +// +// If Stack() has been called before and zerolog.ErrorStackMarshaler is defined, +// the err is passed to ErrorStackMarshaler and the result is appended to the +// zerolog.ErrorStackFieldName. +func (e *Event) Err(err error) *Event { + if e == nil { + return e + } + if e.stack && ErrorStackMarshaler != nil { + switch m := ErrorStackMarshaler(err).(type) { + case nil: + case LogObjectMarshaler: + e.Object(ErrorStackFieldName, m) + case error: + if m != nil && !isNilValue(m) { + e.Str(ErrorStackFieldName, m.Error()) + } + case string: + e.Str(ErrorStackFieldName, m) + default: + e.Interface(ErrorStackFieldName, m) + } + } + return e.AnErr(ErrorFieldName, err) +} + +// Stack enables stack trace printing for the error passed to Err(). +// +// ErrorStackMarshaler must be set for this method to do something. +func (e *Event) Stack() *Event { + if e != nil { + e.stack = true + } + return e +} + +// Bool adds the field key with val as a bool to the *Event context. +func (e *Event) Bool(key string, b bool) *Event { + if e == nil { + return e + } + e.buf = enc.AppendBool(enc.AppendKey(e.buf, key), b) + return e +} + +// Bools adds the field key with val as a []bool to the *Event context. +func (e *Event) Bools(key string, b []bool) *Event { + if e == nil { + return e + } + e.buf = enc.AppendBools(enc.AppendKey(e.buf, key), b) + return e +} + +// Int adds the field key with i as a int to the *Event context. +func (e *Event) Int(key string, i int) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInt(enc.AppendKey(e.buf, key), i) + return e +} + +// Ints adds the field key with i as a []int to the *Event context. +func (e *Event) Ints(key string, i []int) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInts(enc.AppendKey(e.buf, key), i) + return e +} + +// Int8 adds the field key with i as a int8 to the *Event context. +func (e *Event) Int8(key string, i int8) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInt8(enc.AppendKey(e.buf, key), i) + return e +} + +// Ints8 adds the field key with i as a []int8 to the *Event context. +func (e *Event) Ints8(key string, i []int8) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInts8(enc.AppendKey(e.buf, key), i) + return e +} + +// Int16 adds the field key with i as a int16 to the *Event context. +func (e *Event) Int16(key string, i int16) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInt16(enc.AppendKey(e.buf, key), i) + return e +} + +// Ints16 adds the field key with i as a []int16 to the *Event context. +func (e *Event) Ints16(key string, i []int16) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInts16(enc.AppendKey(e.buf, key), i) + return e +} + +// Int32 adds the field key with i as a int32 to the *Event context. +func (e *Event) Int32(key string, i int32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInt32(enc.AppendKey(e.buf, key), i) + return e +} + +// Ints32 adds the field key with i as a []int32 to the *Event context. +func (e *Event) Ints32(key string, i []int32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInts32(enc.AppendKey(e.buf, key), i) + return e +} + +// Int64 adds the field key with i as a int64 to the *Event context. +func (e *Event) Int64(key string, i int64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInt64(enc.AppendKey(e.buf, key), i) + return e +} + +// Ints64 adds the field key with i as a []int64 to the *Event context. +func (e *Event) Ints64(key string, i []int64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendInts64(enc.AppendKey(e.buf, key), i) + return e +} + +// Uint adds the field key with i as a uint to the *Event context. +func (e *Event) Uint(key string, i uint) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUint(enc.AppendKey(e.buf, key), i) + return e +} + +// Uints adds the field key with i as a []int to the *Event context. +func (e *Event) Uints(key string, i []uint) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUints(enc.AppendKey(e.buf, key), i) + return e +} + +// Uint8 adds the field key with i as a uint8 to the *Event context. +func (e *Event) Uint8(key string, i uint8) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUint8(enc.AppendKey(e.buf, key), i) + return e +} + +// Uints8 adds the field key with i as a []int8 to the *Event context. +func (e *Event) Uints8(key string, i []uint8) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUints8(enc.AppendKey(e.buf, key), i) + return e +} + +// Uint16 adds the field key with i as a uint16 to the *Event context. +func (e *Event) Uint16(key string, i uint16) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUint16(enc.AppendKey(e.buf, key), i) + return e +} + +// Uints16 adds the field key with i as a []int16 to the *Event context. +func (e *Event) Uints16(key string, i []uint16) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUints16(enc.AppendKey(e.buf, key), i) + return e +} + +// Uint32 adds the field key with i as a uint32 to the *Event context. +func (e *Event) Uint32(key string, i uint32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUint32(enc.AppendKey(e.buf, key), i) + return e +} + +// Uints32 adds the field key with i as a []int32 to the *Event context. +func (e *Event) Uints32(key string, i []uint32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUints32(enc.AppendKey(e.buf, key), i) + return e +} + +// Uint64 adds the field key with i as a uint64 to the *Event context. +func (e *Event) Uint64(key string, i uint64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUint64(enc.AppendKey(e.buf, key), i) + return e +} + +// Uints64 adds the field key with i as a []int64 to the *Event context. +func (e *Event) Uints64(key string, i []uint64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendUints64(enc.AppendKey(e.buf, key), i) + return e +} + +// Float32 adds the field key with f as a float32 to the *Event context. +func (e *Event) Float32(key string, f float32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendFloat32(enc.AppendKey(e.buf, key), f) + return e +} + +// Floats32 adds the field key with f as a []float32 to the *Event context. +func (e *Event) Floats32(key string, f []float32) *Event { + if e == nil { + return e + } + e.buf = enc.AppendFloats32(enc.AppendKey(e.buf, key), f) + return e +} + +// Float64 adds the field key with f as a float64 to the *Event context. +func (e *Event) Float64(key string, f float64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendFloat64(enc.AppendKey(e.buf, key), f) + return e +} + +// Floats64 adds the field key with f as a []float64 to the *Event context. +func (e *Event) Floats64(key string, f []float64) *Event { + if e == nil { + return e + } + e.buf = enc.AppendFloats64(enc.AppendKey(e.buf, key), f) + return e +} + +// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key. +// To customize the key name, change zerolog.TimestampFieldName. +// +// NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one +// already. +func (e *Event) Timestamp() *Event { + if e == nil { + return e + } + e.buf = enc.AppendTime(enc.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat) + return e +} + +// Time adds the field key with t formated as string using zerolog.TimeFieldFormat. +func (e *Event) Time(key string, t time.Time) *Event { + if e == nil { + return e + } + e.buf = enc.AppendTime(enc.AppendKey(e.buf, key), t, TimeFieldFormat) + return e +} + +// Times adds the field key with t formated as string using zerolog.TimeFieldFormat. +func (e *Event) Times(key string, t []time.Time) *Event { + if e == nil { + return e + } + e.buf = enc.AppendTimes(enc.AppendKey(e.buf, key), t, TimeFieldFormat) + return e +} + +// Dur adds the field key with duration d stored as zerolog.DurationFieldUnit. +// If zerolog.DurationFieldInteger is true, durations are rendered as integer +// instead of float. +func (e *Event) Dur(key string, d time.Duration) *Event { + if e == nil { + return e + } + e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + return e +} + +// Durs adds the field key with duration d stored as zerolog.DurationFieldUnit. +// If zerolog.DurationFieldInteger is true, durations are rendered as integer +// instead of float. +func (e *Event) Durs(key string, d []time.Duration) *Event { + if e == nil { + return e + } + e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + return e +} + +// TimeDiff adds the field key with positive duration between time t and start. +// If time t is not greater than start, duration will be 0. +// Duration format follows the same principle as Dur(). +func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event { + if e == nil { + return e + } + var d time.Duration + if t.After(start) { + d = t.Sub(start) + } + e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger) + return e +} + +// Interface adds the field key with i marshaled using reflection. +func (e *Event) Interface(key string, i interface{}) *Event { + if e == nil { + return e + } + if obj, ok := i.(LogObjectMarshaler); ok { + return e.Object(key, obj) + } + e.buf = enc.AppendInterface(enc.AppendKey(e.buf, key), i) + return e +} + +// CallerSkipFrame instructs any future Caller calls to skip the specified number of frames. +// This includes those added via hooks from the context. +func (e *Event) CallerSkipFrame(skip int) *Event { + if e == nil { + return e + } + e.skipFrame += skip + return e +} + +// Caller adds the file:line of the caller with the zerolog.CallerFieldName key. +// The argument skip is the number of stack frames to ascend +// Skip If not passed, use the global variable CallerSkipFrameCount +func (e *Event) Caller(skip ...int) *Event { + sk := CallerSkipFrameCount + if len(skip) > 0 { + sk = skip[0] + CallerSkipFrameCount + } + return e.caller(sk) +} + +func (e *Event) caller(skip int) *Event { + if e == nil { + return e + } + _, file, line, ok := runtime.Caller(skip + e.skipFrame) + if !ok { + return e + } + e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(file, line)) + return e +} + +// IPAddr adds IPv4 or IPv6 Address to the event +func (e *Event) IPAddr(key string, ip net.IP) *Event { + if e == nil { + return e + } + e.buf = enc.AppendIPAddr(enc.AppendKey(e.buf, key), ip) + return e +} + +// IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the event +func (e *Event) IPPrefix(key string, pfx net.IPNet) *Event { + if e == nil { + return e + } + e.buf = enc.AppendIPPrefix(enc.AppendKey(e.buf, key), pfx) + return e +} + +// MACAddr adds MAC address to the event +func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event { + if e == nil { + return e + } + e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha) + return e +} diff --git a/vendor/github.com/rs/zerolog/fields.go b/vendor/github.com/rs/zerolog/fields.go new file mode 100644 index 0000000..c1eb5ce --- /dev/null +++ b/vendor/github.com/rs/zerolog/fields.go @@ -0,0 +1,277 @@ +package zerolog + +import ( + "encoding/json" + "net" + "sort" + "time" + "unsafe" +) + +func isNilValue(i interface{}) bool { + return (*[2]uintptr)(unsafe.Pointer(&i))[1] == 0 +} + +func appendFields(dst []byte, fields interface{}) []byte { + switch fields := fields.(type) { + case []interface{}: + if n := len(fields); n&0x1 == 1 { // odd number + fields = fields[:n-1] + } + dst = appendFieldList(dst, fields) + case map[string]interface{}: + keys := make([]string, 0, len(fields)) + for key := range fields { + keys = append(keys, key) + } + sort.Strings(keys) + kv := make([]interface{}, 2) + for _, key := range keys { + kv[0], kv[1] = key, fields[key] + dst = appendFieldList(dst, kv) + } + } + return dst +} + +func appendFieldList(dst []byte, kvList []interface{}) []byte { + for i, n := 0, len(kvList); i < n; i += 2 { + key, val := kvList[i], kvList[i+1] + if key, ok := key.(string); ok { + dst = enc.AppendKey(dst, key) + } else { + continue + } + if val, ok := val.(LogObjectMarshaler); ok { + e := newEvent(nil, 0) + e.buf = e.buf[:0] + e.appendObject(val) + dst = append(dst, e.buf...) + putEvent(e) + continue + } + switch val := val.(type) { + case string: + dst = enc.AppendString(dst, val) + case []byte: + dst = enc.AppendBytes(dst, val) + case error: + switch m := ErrorMarshalFunc(val).(type) { + case LogObjectMarshaler: + e := newEvent(nil, 0) + e.buf = e.buf[:0] + e.appendObject(m) + dst = append(dst, e.buf...) + putEvent(e) + case error: + if m == nil || isNilValue(m) { + dst = enc.AppendNil(dst) + } else { + dst = enc.AppendString(dst, m.Error()) + } + case string: + dst = enc.AppendString(dst, m) + default: + dst = enc.AppendInterface(dst, m) + } + case []error: + dst = enc.AppendArrayStart(dst) + for i, err := range val { + switch m := ErrorMarshalFunc(err).(type) { + case LogObjectMarshaler: + e := newEvent(nil, 0) + e.buf = e.buf[:0] + e.appendObject(m) + dst = append(dst, e.buf...) + putEvent(e) + case error: + if m == nil || isNilValue(m) { + dst = enc.AppendNil(dst) + } else { + dst = enc.AppendString(dst, m.Error()) + } + case string: + dst = enc.AppendString(dst, m) + default: + dst = enc.AppendInterface(dst, m) + } + + if i < (len(val) - 1) { + enc.AppendArrayDelim(dst) + } + } + dst = enc.AppendArrayEnd(dst) + case bool: + dst = enc.AppendBool(dst, val) + case int: + dst = enc.AppendInt(dst, val) + case int8: + dst = enc.AppendInt8(dst, val) + case int16: + dst = enc.AppendInt16(dst, val) + case int32: + dst = enc.AppendInt32(dst, val) + case int64: + dst = enc.AppendInt64(dst, val) + case uint: + dst = enc.AppendUint(dst, val) + case uint8: + dst = enc.AppendUint8(dst, val) + case uint16: + dst = enc.AppendUint16(dst, val) + case uint32: + dst = enc.AppendUint32(dst, val) + case uint64: + dst = enc.AppendUint64(dst, val) + case float32: + dst = enc.AppendFloat32(dst, val) + case float64: + dst = enc.AppendFloat64(dst, val) + case time.Time: + dst = enc.AppendTime(dst, val, TimeFieldFormat) + case time.Duration: + dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger) + case *string: + if val != nil { + dst = enc.AppendString(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *bool: + if val != nil { + dst = enc.AppendBool(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *int: + if val != nil { + dst = enc.AppendInt(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *int8: + if val != nil { + dst = enc.AppendInt8(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *int16: + if val != nil { + dst = enc.AppendInt16(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *int32: + if val != nil { + dst = enc.AppendInt32(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *int64: + if val != nil { + dst = enc.AppendInt64(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *uint: + if val != nil { + dst = enc.AppendUint(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *uint8: + if val != nil { + dst = enc.AppendUint8(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *uint16: + if val != nil { + dst = enc.AppendUint16(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *uint32: + if val != nil { + dst = enc.AppendUint32(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *uint64: + if val != nil { + dst = enc.AppendUint64(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *float32: + if val != nil { + dst = enc.AppendFloat32(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *float64: + if val != nil { + dst = enc.AppendFloat64(dst, *val) + } else { + dst = enc.AppendNil(dst) + } + case *time.Time: + if val != nil { + dst = enc.AppendTime(dst, *val, TimeFieldFormat) + } else { + dst = enc.AppendNil(dst) + } + case *time.Duration: + if val != nil { + dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger) + } else { + dst = enc.AppendNil(dst) + } + case []string: + dst = enc.AppendStrings(dst, val) + case []bool: + dst = enc.AppendBools(dst, val) + case []int: + dst = enc.AppendInts(dst, val) + case []int8: + dst = enc.AppendInts8(dst, val) + case []int16: + dst = enc.AppendInts16(dst, val) + case []int32: + dst = enc.AppendInts32(dst, val) + case []int64: + dst = enc.AppendInts64(dst, val) + case []uint: + dst = enc.AppendUints(dst, val) + // case []uint8: + // dst = enc.AppendUints8(dst, val) + case []uint16: + dst = enc.AppendUints16(dst, val) + case []uint32: + dst = enc.AppendUints32(dst, val) + case []uint64: + dst = enc.AppendUints64(dst, val) + case []float32: + dst = enc.AppendFloats32(dst, val) + case []float64: + dst = enc.AppendFloats64(dst, val) + case []time.Time: + dst = enc.AppendTimes(dst, val, TimeFieldFormat) + case []time.Duration: + dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger) + case nil: + dst = enc.AppendNil(dst) + case net.IP: + dst = enc.AppendIPAddr(dst, val) + case net.IPNet: + dst = enc.AppendIPPrefix(dst, val) + case net.HardwareAddr: + dst = enc.AppendMACAddr(dst, val) + case json.RawMessage: + dst = appendJSON(dst, val) + default: + dst = enc.AppendInterface(dst, val) + } + } + return dst +} diff --git a/vendor/github.com/rs/zerolog/globals.go b/vendor/github.com/rs/zerolog/globals.go new file mode 100644 index 0000000..e561d8f --- /dev/null +++ b/vendor/github.com/rs/zerolog/globals.go @@ -0,0 +1,138 @@ +package zerolog + +import ( + "encoding/json" + "strconv" + "sync/atomic" + "time" +) + +const ( + // TimeFormatUnix defines a time format that makes time fields to be + // serialized as Unix timestamp integers. + TimeFormatUnix = "" + + // TimeFormatUnixMs defines a time format that makes time fields to be + // serialized as Unix timestamp integers in milliseconds. + TimeFormatUnixMs = "UNIXMS" + + // TimeFormatUnixMicro defines a time format that makes time fields to be + // serialized as Unix timestamp integers in microseconds. + TimeFormatUnixMicro = "UNIXMICRO" +) + +var ( + // TimestampFieldName is the field name used for the timestamp field. + TimestampFieldName = "time" + + // LevelFieldName is the field name used for the level field. + LevelFieldName = "level" + + // LevelTraceValue is the value used for the trace level field. + LevelTraceValue = "trace" + // LevelDebugValue is the value used for the debug level field. + LevelDebugValue = "debug" + // LevelInfoValue is the value used for the info level field. + LevelInfoValue = "info" + // LevelWarnValue is the value used for the warn level field. + LevelWarnValue = "warn" + // LevelErrorValue is the value used for the error level field. + LevelErrorValue = "error" + // LevelFatalValue is the value used for the fatal level field. + LevelFatalValue = "fatal" + // LevelPanicValue is the value used for the panic level field. + LevelPanicValue = "panic" + + // LevelFieldMarshalFunc allows customization of global level field marshaling. + LevelFieldMarshalFunc = func(l Level) string { + return l.String() + } + + // MessageFieldName is the field name used for the message field. + MessageFieldName = "message" + + // ErrorFieldName is the field name used for error fields. + ErrorFieldName = "error" + + // CallerFieldName is the field name used for caller field. + CallerFieldName = "caller" + + // CallerSkipFrameCount is the number of stack frames to skip to find the caller. + CallerSkipFrameCount = 2 + + // CallerMarshalFunc allows customization of global caller marshaling + CallerMarshalFunc = func(file string, line int) string { + return file + ":" + strconv.Itoa(line) + } + + // ErrorStackFieldName is the field name used for error stacks. + ErrorStackFieldName = "stack" + + // ErrorStackMarshaler extract the stack from err if any. + ErrorStackMarshaler func(err error) interface{} + + // ErrorMarshalFunc allows customization of global error marshaling + ErrorMarshalFunc = func(err error) interface{} { + return err + } + + // InterfaceMarshalFunc allows customization of interface marshaling. + // Default: "encoding/json.Marshal" + InterfaceMarshalFunc = json.Marshal + + // TimeFieldFormat defines the time format of the Time field type. If set to + // TimeFormatUnix, TimeFormatUnixMs or TimeFormatUnixMicro, the time is formatted as an UNIX + // timestamp as integer. + TimeFieldFormat = time.RFC3339 + + // TimestampFunc defines the function called to generate a timestamp. + TimestampFunc = time.Now + + // DurationFieldUnit defines the unit for time.Duration type fields added + // using the Dur method. + DurationFieldUnit = time.Millisecond + + // DurationFieldInteger renders Dur fields as integer instead of float if + // set to true. + DurationFieldInteger = false + + // ErrorHandler is called whenever zerolog fails to write an event on its + // output. If not set, an error is printed on the stderr. This handler must + // be thread safe and non-blocking. + ErrorHandler func(err error) + + // DefaultContextLogger is returned from Ctx() if there is no logger associated + // with the context. + DefaultContextLogger *Logger +) + +var ( + gLevel = new(int32) + disableSampling = new(int32) +) + +// SetGlobalLevel sets the global override for log level. If this +// values is raised, all Loggers will use at least this value. +// +// To globally disable logs, set GlobalLevel to Disabled. +func SetGlobalLevel(l Level) { + atomic.StoreInt32(gLevel, int32(l)) +} + +// GlobalLevel returns the current global log level +func GlobalLevel() Level { + return Level(atomic.LoadInt32(gLevel)) +} + +// DisableSampling will disable sampling in all Loggers if true. +func DisableSampling(v bool) { + var i int32 + if v { + i = 1 + } + atomic.StoreInt32(disableSampling, i) +} + +func samplingDisabled() bool { + return atomic.LoadInt32(disableSampling) == 1 +} diff --git a/vendor/github.com/rs/zerolog/go112.go b/vendor/github.com/rs/zerolog/go112.go new file mode 100644 index 0000000..e7b5a1b --- /dev/null +++ b/vendor/github.com/rs/zerolog/go112.go @@ -0,0 +1,7 @@ +// +build go1.12 + +package zerolog + +// Since go 1.12, some auto generated init functions are hidden from +// runtime.Caller. +const contextCallerSkipFrameCount = 2 diff --git a/vendor/github.com/rs/zerolog/hook.go b/vendor/github.com/rs/zerolog/hook.go new file mode 100644 index 0000000..ec6effc --- /dev/null +++ b/vendor/github.com/rs/zerolog/hook.go @@ -0,0 +1,64 @@ +package zerolog + +// Hook defines an interface to a log hook. +type Hook interface { + // Run runs the hook with the event. + Run(e *Event, level Level, message string) +} + +// HookFunc is an adaptor to allow the use of an ordinary function +// as a Hook. +type HookFunc func(e *Event, level Level, message string) + +// Run implements the Hook interface. +func (h HookFunc) Run(e *Event, level Level, message string) { + h(e, level, message) +} + +// LevelHook applies a different hook for each level. +type LevelHook struct { + NoLevelHook, TraceHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook +} + +// Run implements the Hook interface. +func (h LevelHook) Run(e *Event, level Level, message string) { + switch level { + case TraceLevel: + if h.TraceHook != nil { + h.TraceHook.Run(e, level, message) + } + case DebugLevel: + if h.DebugHook != nil { + h.DebugHook.Run(e, level, message) + } + case InfoLevel: + if h.InfoHook != nil { + h.InfoHook.Run(e, level, message) + } + case WarnLevel: + if h.WarnHook != nil { + h.WarnHook.Run(e, level, message) + } + case ErrorLevel: + if h.ErrorHook != nil { + h.ErrorHook.Run(e, level, message) + } + case FatalLevel: + if h.FatalHook != nil { + h.FatalHook.Run(e, level, message) + } + case PanicLevel: + if h.PanicHook != nil { + h.PanicHook.Run(e, level, message) + } + case NoLevel: + if h.NoLevelHook != nil { + h.NoLevelHook.Run(e, level, message) + } + } +} + +// NewLevelHook returns a new LevelHook. +func NewLevelHook() LevelHook { + return LevelHook{} +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/README.md b/vendor/github.com/rs/zerolog/internal/cbor/README.md new file mode 100644 index 0000000..92c2e8c --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/README.md @@ -0,0 +1,56 @@ +## Reference: + CBOR Encoding is described in [RFC7049](https://tools.ietf.org/html/rfc7049) + +## Comparison of JSON vs CBOR + +Two main areas of reduction are: + +1. CPU usage to write a log msg +2. Size (in bytes) of log messages. + + +CPU Usage savings are below: +``` +name JSON time/op CBOR time/op delta +Info-32 15.3ns ± 1% 11.7ns ± 3% -23.78% (p=0.000 n=9+10) +ContextFields-32 16.2ns ± 2% 12.3ns ± 3% -23.97% (p=0.000 n=9+9) +ContextAppend-32 6.70ns ± 0% 6.20ns ± 0% -7.44% (p=0.000 n=9+9) +LogFields-32 66.4ns ± 0% 24.6ns ± 2% -62.89% (p=0.000 n=10+9) +LogArrayObject-32 911ns ±11% 768ns ± 6% -15.64% (p=0.000 n=10+10) +LogFieldType/Floats-32 70.3ns ± 2% 29.5ns ± 1% -57.98% (p=0.000 n=10+10) +LogFieldType/Err-32 14.0ns ± 3% 12.1ns ± 8% -13.20% (p=0.000 n=8+10) +LogFieldType/Dur-32 17.2ns ± 2% 13.1ns ± 1% -24.27% (p=0.000 n=10+9) +LogFieldType/Object-32 54.3ns ±11% 52.3ns ± 7% ~ (p=0.239 n=10+10) +LogFieldType/Ints-32 20.3ns ± 2% 15.1ns ± 2% -25.50% (p=0.000 n=9+10) +LogFieldType/Interfaces-32 642ns ±11% 621ns ± 9% ~ (p=0.118 n=10+10) +LogFieldType/Interface(Objects)-32 635ns ±13% 632ns ± 9% ~ (p=0.592 n=10+10) +LogFieldType/Times-32 294ns ± 0% 27ns ± 1% -90.71% (p=0.000 n=10+9) +LogFieldType/Durs-32 121ns ± 0% 33ns ± 2% -72.44% (p=0.000 n=9+9) +LogFieldType/Interface(Object)-32 56.6ns ± 8% 52.3ns ± 8% -7.54% (p=0.007 n=10+10) +LogFieldType/Errs-32 17.8ns ± 3% 16.1ns ± 2% -9.71% (p=0.000 n=10+9) +LogFieldType/Time-32 40.5ns ± 1% 12.7ns ± 6% -68.66% (p=0.000 n=8+9) +LogFieldType/Bool-32 12.0ns ± 5% 10.2ns ± 2% -15.18% (p=0.000 n=10+8) +LogFieldType/Bools-32 17.2ns ± 2% 12.6ns ± 4% -26.63% (p=0.000 n=10+10) +LogFieldType/Int-32 12.3ns ± 2% 11.2ns ± 4% -9.27% (p=0.000 n=9+10) +LogFieldType/Float-32 16.7ns ± 1% 12.6ns ± 2% -24.42% (p=0.000 n=7+9) +LogFieldType/Str-32 12.7ns ± 7% 11.3ns ± 7% -10.88% (p=0.000 n=10+9) +LogFieldType/Strs-32 20.3ns ± 3% 18.2ns ± 3% -10.25% (p=0.000 n=9+10) +LogFieldType/Interface-32 183ns ±12% 175ns ± 9% ~ (p=0.078 n=10+10) +``` + +Log message size savings is greatly dependent on the number and type of fields in the log message. +Assuming this log message (with an Integer, timestamp and string, in addition to level). + +`{"level":"error","Fault":41650,"time":"2018-04-01T15:18:19-07:00","message":"Some Message"}` + +Two measurements were done for the log file sizes - one without any compression, second +using [compress/zlib](https://golang.org/pkg/compress/zlib/). + +Results for 10,000 log messages: + +| Log Format | Plain File Size (in KB) | Compressed File Size (in KB) | +| :--- | :---: | :---: | +| JSON | 920 | 28 | +| CBOR | 550 | 28 | + +The example used to calculate the above data is available in [Examples](examples). diff --git a/vendor/github.com/rs/zerolog/internal/cbor/base.go b/vendor/github.com/rs/zerolog/internal/cbor/base.go new file mode 100644 index 0000000..51fe86c --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/base.go @@ -0,0 +1,19 @@ +package cbor + +// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice. +// Making it package level instead of embedded in Encoder brings +// some extra efforts at importing, but avoids value copy when the functions +// of Encoder being invoked. +// DO REMEMBER to set this variable at importing, or +// you might get a nil pointer dereference panic at runtime. +var JSONMarshalFunc func(v interface{}) ([]byte, error) + +type Encoder struct{} + +// AppendKey adds a key (string) to the binary encoded log message +func (e Encoder) AppendKey(dst []byte, key string) []byte { + if len(dst) < 1 { + dst = e.AppendBeginMarker(dst) + } + return e.AppendString(dst, key) +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/cbor.go b/vendor/github.com/rs/zerolog/internal/cbor/cbor.go new file mode 100644 index 0000000..969f591 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/cbor.go @@ -0,0 +1,100 @@ +// Package cbor provides primitives for storing different data +// in the CBOR (binary) format. CBOR is defined in RFC7049. +package cbor + +import "time" + +const ( + majorOffset = 5 + additionalMax = 23 + + // Non Values. + additionalTypeBoolFalse byte = 20 + additionalTypeBoolTrue byte = 21 + additionalTypeNull byte = 22 + + // Integer (+ve and -ve) Sub-types. + additionalTypeIntUint8 byte = 24 + additionalTypeIntUint16 byte = 25 + additionalTypeIntUint32 byte = 26 + additionalTypeIntUint64 byte = 27 + + // Float Sub-types. + additionalTypeFloat16 byte = 25 + additionalTypeFloat32 byte = 26 + additionalTypeFloat64 byte = 27 + additionalTypeBreak byte = 31 + + // Tag Sub-types. + additionalTypeTimestamp byte = 01 + + // Extended Tags - from https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml + additionalTypeTagNetworkAddr uint16 = 260 + additionalTypeTagNetworkPrefix uint16 = 261 + additionalTypeEmbeddedJSON uint16 = 262 + additionalTypeTagHexString uint16 = 263 + + // Unspecified number of elements. + additionalTypeInfiniteCount byte = 31 +) +const ( + majorTypeUnsignedInt byte = iota << majorOffset // Major type 0 + majorTypeNegativeInt // Major type 1 + majorTypeByteString // Major type 2 + majorTypeUtf8String // Major type 3 + majorTypeArray // Major type 4 + majorTypeMap // Major type 5 + majorTypeTags // Major type 6 + majorTypeSimpleAndFloat // Major type 7 +) + +const ( + maskOutAdditionalType byte = (7 << majorOffset) + maskOutMajorType byte = 31 +) + +const ( + float32Nan = "\xfa\x7f\xc0\x00\x00" + float32PosInfinity = "\xfa\x7f\x80\x00\x00" + float32NegInfinity = "\xfa\xff\x80\x00\x00" + float64Nan = "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00" + float64PosInfinity = "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00" + float64NegInfinity = "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00" +) + +// IntegerTimeFieldFormat indicates the format of timestamp decoded +// from an integer (time in seconds). +var IntegerTimeFieldFormat = time.RFC3339 + +// NanoTimeFieldFormat indicates the format of timestamp decoded +// from a float value (time in seconds and nano seconds). +var NanoTimeFieldFormat = time.RFC3339Nano + +func appendCborTypePrefix(dst []byte, major byte, number uint64) []byte { + byteCount := 8 + var minor byte + switch { + case number < 256: + byteCount = 1 + minor = additionalTypeIntUint8 + + case number < 65536: + byteCount = 2 + minor = additionalTypeIntUint16 + + case number < 4294967296: + byteCount = 4 + minor = additionalTypeIntUint32 + + default: + byteCount = 8 + minor = additionalTypeIntUint64 + + } + dst = append(dst, byte(major|minor)) + byteCount-- + for ; byteCount >= 0; byteCount-- { + dst = append(dst, byte(number>>(uint(byteCount)*8))) + } + return dst +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/decode_stream.go b/vendor/github.com/rs/zerolog/internal/cbor/decode_stream.go new file mode 100644 index 0000000..e3cf3b7 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/decode_stream.go @@ -0,0 +1,614 @@ +package cbor + +// This file contains code to decode a stream of CBOR Data into JSON. + +import ( + "bufio" + "bytes" + "fmt" + "io" + "math" + "net" + "runtime" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +var decodeTimeZone *time.Location + +const hexTable = "0123456789abcdef" + +const isFloat32 = 4 +const isFloat64 = 8 + +func readNBytes(src *bufio.Reader, n int) []byte { + ret := make([]byte, n) + for i := 0; i < n; i++ { + ch, e := src.ReadByte() + if e != nil { + panic(fmt.Errorf("Tried to Read %d Bytes.. But hit end of file", n)) + } + ret[i] = ch + } + return ret +} + +func readByte(src *bufio.Reader) byte { + b, e := src.ReadByte() + if e != nil { + panic(fmt.Errorf("Tried to Read 1 Byte.. But hit end of file")) + } + return b +} + +func decodeIntAdditonalType(src *bufio.Reader, minor byte) int64 { + val := int64(0) + if minor <= 23 { + val = int64(minor) + } else { + bytesToRead := 0 + switch minor { + case additionalTypeIntUint8: + bytesToRead = 1 + case additionalTypeIntUint16: + bytesToRead = 2 + case additionalTypeIntUint32: + bytesToRead = 4 + case additionalTypeIntUint64: + bytesToRead = 8 + default: + panic(fmt.Errorf("Invalid Additional Type: %d in decodeInteger (expected <28)", minor)) + } + pb := readNBytes(src, bytesToRead) + for i := 0; i < bytesToRead; i++ { + val = val * 256 + val += int64(pb[i]) + } + } + return val +} + +func decodeInteger(src *bufio.Reader) int64 { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeUnsignedInt && major != majorTypeNegativeInt { + panic(fmt.Errorf("Major type is: %d in decodeInteger!! (expected 0 or 1)", major)) + } + val := decodeIntAdditonalType(src, minor) + if major == 0 { + return val + } + return (-1 - val) +} + +func decodeFloat(src *bufio.Reader) (float64, int) { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeSimpleAndFloat { + panic(fmt.Errorf("Incorrect Major type is: %d in decodeFloat", major)) + } + + switch minor { + case additionalTypeFloat16: + panic(fmt.Errorf("float16 is not suppported in decodeFloat")) + + case additionalTypeFloat32: + pb := readNBytes(src, 4) + switch string(pb) { + case float32Nan: + return math.NaN(), isFloat32 + case float32PosInfinity: + return math.Inf(0), isFloat32 + case float32NegInfinity: + return math.Inf(-1), isFloat32 + } + n := uint32(0) + for i := 0; i < 4; i++ { + n = n * 256 + n += uint32(pb[i]) + } + val := math.Float32frombits(n) + return float64(val), isFloat32 + case additionalTypeFloat64: + pb := readNBytes(src, 8) + switch string(pb) { + case float64Nan: + return math.NaN(), isFloat64 + case float64PosInfinity: + return math.Inf(0), isFloat64 + case float64NegInfinity: + return math.Inf(-1), isFloat64 + } + n := uint64(0) + for i := 0; i < 8; i++ { + n = n * 256 + n += uint64(pb[i]) + } + val := math.Float64frombits(n) + return val, isFloat64 + } + panic(fmt.Errorf("Invalid Additional Type: %d in decodeFloat", minor)) +} + +func decodeStringComplex(dst []byte, s string, pos uint) []byte { + i := int(pos) + start := 0 + + for i < len(s) { + b := s[i] + if b >= utf8.RuneSelf { + r, size := utf8.DecodeRuneInString(s[i:]) + if r == utf8.RuneError && size == 1 { + // In case of error, first append previous simple characters to + // the byte slice if any and append a replacement character code + // in place of the invalid sequence. + if start < i { + dst = append(dst, s[start:i]...) + } + dst = append(dst, `\ufffd`...) + i += size + start = i + continue + } + i += size + continue + } + if b >= 0x20 && b <= 0x7e && b != '\\' && b != '"' { + i++ + continue + } + // We encountered a character that needs to be encoded. + // Let's append the previous simple characters to the byte slice + // and switch our operation to read and encode the remainder + // characters byte-by-byte. + if start < i { + dst = append(dst, s[start:i]...) + } + switch b { + case '"', '\\': + dst = append(dst, '\\', b) + case '\b': + dst = append(dst, '\\', 'b') + case '\f': + dst = append(dst, '\\', 'f') + case '\n': + dst = append(dst, '\\', 'n') + case '\r': + dst = append(dst, '\\', 'r') + case '\t': + dst = append(dst, '\\', 't') + default: + dst = append(dst, '\\', 'u', '0', '0', hexTable[b>>4], hexTable[b&0xF]) + } + i++ + start = i + } + if start < len(s) { + dst = append(dst, s[start:]...) + } + return dst +} + +func decodeString(src *bufio.Reader, noQuotes bool) []byte { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeByteString { + panic(fmt.Errorf("Major type is: %d in decodeString", major)) + } + result := []byte{} + if !noQuotes { + result = append(result, '"') + } + length := decodeIntAdditonalType(src, minor) + len := int(length) + pbs := readNBytes(src, len) + result = append(result, pbs...) + if noQuotes { + return result + } + return append(result, '"') +} + +func decodeUTF8String(src *bufio.Reader) []byte { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeUtf8String { + panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major)) + } + result := []byte{'"'} + length := decodeIntAdditonalType(src, minor) + len := int(length) + pbs := readNBytes(src, len) + + for i := 0; i < len; i++ { + // Check if the character needs encoding. Control characters, slashes, + // and the double quote need json encoding. Bytes above the ascii + // boundary needs utf8 encoding. + if pbs[i] < 0x20 || pbs[i] > 0x7e || pbs[i] == '\\' || pbs[i] == '"' { + // We encountered a character that needs to be encoded. Switch + // to complex version of the algorithm. + dst := []byte{'"'} + dst = decodeStringComplex(dst, string(pbs), uint(i)) + return append(dst, '"') + } + } + // The string has no need for encoding an therefore is directly + // appended to the byte slice. + result = append(result, pbs...) + return append(result, '"') +} + +func array2Json(src *bufio.Reader, dst io.Writer) { + dst.Write([]byte{'['}) + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeArray { + panic(fmt.Errorf("Major type is: %d in array2Json", major)) + } + len := 0 + unSpecifiedCount := false + if minor == additionalTypeInfiniteCount { + unSpecifiedCount = true + } else { + length := decodeIntAdditonalType(src, minor) + len = int(length) + } + for i := 0; unSpecifiedCount || i < len; i++ { + if unSpecifiedCount { + pb, e := src.Peek(1) + if e != nil { + panic(e) + } + if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) { + readByte(src) + break + } + } + cbor2JsonOneObject(src, dst) + if unSpecifiedCount { + pb, e := src.Peek(1) + if e != nil { + panic(e) + } + if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) { + readByte(src) + break + } + dst.Write([]byte{','}) + } else if i+1 < len { + dst.Write([]byte{','}) + } + } + dst.Write([]byte{']'}) +} + +func map2Json(src *bufio.Reader, dst io.Writer) { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeMap { + panic(fmt.Errorf("Major type is: %d in map2Json", major)) + } + len := 0 + unSpecifiedCount := false + if minor == additionalTypeInfiniteCount { + unSpecifiedCount = true + } else { + length := decodeIntAdditonalType(src, minor) + len = int(length) + } + dst.Write([]byte{'{'}) + for i := 0; unSpecifiedCount || i < len; i++ { + if unSpecifiedCount { + pb, e := src.Peek(1) + if e != nil { + panic(e) + } + if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) { + readByte(src) + break + } + } + cbor2JsonOneObject(src, dst) + if i%2 == 0 { + // Even position values are keys. + dst.Write([]byte{':'}) + } else { + if unSpecifiedCount { + pb, e := src.Peek(1) + if e != nil { + panic(e) + } + if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) { + readByte(src) + break + } + dst.Write([]byte{','}) + } else if i+1 < len { + dst.Write([]byte{','}) + } + } + } + dst.Write([]byte{'}'}) +} + +func decodeTagData(src *bufio.Reader) []byte { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeTags { + panic(fmt.Errorf("Major type is: %d in decodeTagData", major)) + } + switch minor { + case additionalTypeTimestamp: + return decodeTimeStamp(src) + + // Tag value is larger than 256 (so uint16). + case additionalTypeIntUint16: + val := decodeIntAdditonalType(src, minor) + + switch uint16(val) { + case additionalTypeEmbeddedJSON: + pb := readByte(src) + dataMajor := pb & maskOutAdditionalType + if dataMajor != majorTypeByteString { + panic(fmt.Errorf("Unsupported embedded Type: %d in decodeEmbeddedJSON", dataMajor)) + } + src.UnreadByte() + return decodeString(src, true) + + case additionalTypeTagNetworkAddr: + octets := decodeString(src, true) + ss := []byte{'"'} + switch len(octets) { + case 6: // MAC address. + ha := net.HardwareAddr(octets) + ss = append(append(ss, ha.String()...), '"') + case 4: // IPv4 address. + fallthrough + case 16: // IPv6 address. + ip := net.IP(octets) + ss = append(append(ss, ip.String()...), '"') + default: + panic(fmt.Errorf("Unexpected Network Address length: %d (expected 4,6,16)", len(octets))) + } + return ss + + case additionalTypeTagNetworkPrefix: + pb := readByte(src) + if pb != byte(majorTypeMap|0x1) { + panic(fmt.Errorf("IP Prefix is NOT of MAP of 1 elements as expected")) + } + octets := decodeString(src, true) + val := decodeInteger(src) + ip := net.IP(octets) + var mask net.IPMask + pfxLen := int(val) + if len(octets) == 4 { + mask = net.CIDRMask(pfxLen, 32) + } else { + mask = net.CIDRMask(pfxLen, 128) + } + ipPfx := net.IPNet{IP: ip, Mask: mask} + ss := []byte{'"'} + ss = append(append(ss, ipPfx.String()...), '"') + return ss + + case additionalTypeTagHexString: + octets := decodeString(src, true) + ss := []byte{'"'} + for _, v := range octets { + ss = append(ss, hexTable[v>>4], hexTable[v&0x0f]) + } + return append(ss, '"') + + default: + panic(fmt.Errorf("Unsupported Additional Tag Type: %d in decodeTagData", val)) + } + } + panic(fmt.Errorf("Unsupported Additional Type: %d in decodeTagData", minor)) +} + +func decodeTimeStamp(src *bufio.Reader) []byte { + pb := readByte(src) + src.UnreadByte() + tsMajor := pb & maskOutAdditionalType + if tsMajor == majorTypeUnsignedInt || tsMajor == majorTypeNegativeInt { + n := decodeInteger(src) + t := time.Unix(n, 0) + if decodeTimeZone != nil { + t = t.In(decodeTimeZone) + } else { + t = t.In(time.UTC) + } + tsb := []byte{} + tsb = append(tsb, '"') + tsb = t.AppendFormat(tsb, IntegerTimeFieldFormat) + tsb = append(tsb, '"') + return tsb + } else if tsMajor == majorTypeSimpleAndFloat { + n, _ := decodeFloat(src) + secs := int64(n) + n -= float64(secs) + n *= float64(1e9) + t := time.Unix(secs, int64(n)) + if decodeTimeZone != nil { + t = t.In(decodeTimeZone) + } else { + t = t.In(time.UTC) + } + tsb := []byte{} + tsb = append(tsb, '"') + tsb = t.AppendFormat(tsb, NanoTimeFieldFormat) + tsb = append(tsb, '"') + return tsb + } + panic(fmt.Errorf("TS format is neigther int nor float: %d", tsMajor)) +} + +func decodeSimpleFloat(src *bufio.Reader) []byte { + pb := readByte(src) + major := pb & maskOutAdditionalType + minor := pb & maskOutMajorType + if major != majorTypeSimpleAndFloat { + panic(fmt.Errorf("Major type is: %d in decodeSimpleFloat", major)) + } + switch minor { + case additionalTypeBoolTrue: + return []byte("true") + case additionalTypeBoolFalse: + return []byte("false") + case additionalTypeNull: + return []byte("null") + case additionalTypeFloat16: + fallthrough + case additionalTypeFloat32: + fallthrough + case additionalTypeFloat64: + src.UnreadByte() + v, bc := decodeFloat(src) + ba := []byte{} + switch { + case math.IsNaN(v): + return []byte("\"NaN\"") + case math.IsInf(v, 1): + return []byte("\"+Inf\"") + case math.IsInf(v, -1): + return []byte("\"-Inf\"") + } + if bc == isFloat32 { + ba = strconv.AppendFloat(ba, v, 'f', -1, 32) + } else if bc == isFloat64 { + ba = strconv.AppendFloat(ba, v, 'f', -1, 64) + } else { + panic(fmt.Errorf("Invalid Float precision from decodeFloat: %d", bc)) + } + return ba + default: + panic(fmt.Errorf("Invalid Additional Type: %d in decodeSimpleFloat", minor)) + } +} + +func cbor2JsonOneObject(src *bufio.Reader, dst io.Writer) { + pb, e := src.Peek(1) + if e != nil { + panic(e) + } + major := (pb[0] & maskOutAdditionalType) + + switch major { + case majorTypeUnsignedInt: + fallthrough + case majorTypeNegativeInt: + n := decodeInteger(src) + dst.Write([]byte(strconv.Itoa(int(n)))) + + case majorTypeByteString: + s := decodeString(src, false) + dst.Write(s) + + case majorTypeUtf8String: + s := decodeUTF8String(src) + dst.Write(s) + + case majorTypeArray: + array2Json(src, dst) + + case majorTypeMap: + map2Json(src, dst) + + case majorTypeTags: + s := decodeTagData(src) + dst.Write(s) + + case majorTypeSimpleAndFloat: + s := decodeSimpleFloat(src) + dst.Write(s) + } +} + +func moreBytesToRead(src *bufio.Reader) bool { + _, e := src.ReadByte() + if e == nil { + src.UnreadByte() + return true + } + return false +} + +// Cbor2JsonManyObjects decodes all the CBOR Objects read from src +// reader. It keeps on decoding until reader returns EOF (error when reading). +// Decoded string is written to the dst. At the end of every CBOR Object +// newline is written to the output stream. +// +// Returns error (if any) that was encountered during decode. +// The child functions will generate a panic when error is encountered and +// this function will recover non-runtime Errors and return the reason as error. +func Cbor2JsonManyObjects(src io.Reader, dst io.Writer) (err error) { + defer func() { + if r := recover(); r != nil { + if _, ok := r.(runtime.Error); ok { + panic(r) + } + err = r.(error) + } + }() + bufRdr := bufio.NewReader(src) + for moreBytesToRead(bufRdr) { + cbor2JsonOneObject(bufRdr, dst) + dst.Write([]byte("\n")) + } + return nil +} + +// Detect if the bytes to be printed is Binary or not. +func binaryFmt(p []byte) bool { + if len(p) > 0 && p[0] > 0x7F { + return true + } + return false +} + +func getReader(str string) *bufio.Reader { + return bufio.NewReader(strings.NewReader(str)) +} + +// DecodeIfBinaryToString converts a binary formatted log msg to a +// JSON formatted String Log message - suitable for printing to Console/Syslog. +func DecodeIfBinaryToString(in []byte) string { + if binaryFmt(in) { + var b bytes.Buffer + Cbor2JsonManyObjects(strings.NewReader(string(in)), &b) + return b.String() + } + return string(in) +} + +// DecodeObjectToStr checks if the input is a binary format, if so, +// it will decode a single Object and return the decoded string. +func DecodeObjectToStr(in []byte) string { + if binaryFmt(in) { + var b bytes.Buffer + cbor2JsonOneObject(getReader(string(in)), &b) + return b.String() + } + return string(in) +} + +// DecodeIfBinaryToBytes checks if the input is a binary format, if so, +// it will decode all Objects and return the decoded string as byte array. +func DecodeIfBinaryToBytes(in []byte) []byte { + if binaryFmt(in) { + var b bytes.Buffer + Cbor2JsonManyObjects(bytes.NewReader(in), &b) + return b.Bytes() + } + return in +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/string.go b/vendor/github.com/rs/zerolog/internal/cbor/string.go new file mode 100644 index 0000000..e7f90df --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/string.go @@ -0,0 +1,95 @@ +package cbor + +import "fmt" + +// AppendStrings encodes and adds an array of strings to the dst byte array. +func (e Encoder) AppendStrings(dst []byte, vals []string) []byte { + major := majorTypeArray + l := len(vals) + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendString(dst, v) + } + return dst +} + +// AppendString encodes and adds a string to the dst byte array. +func (Encoder) AppendString(dst []byte, s string) []byte { + major := majorTypeUtf8String + + l := len(s) + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l)) + } + return append(dst, s...) +} + +// AppendStringers encodes and adds an array of Stringer values +// to the dst byte array. +func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte { + if len(vals) == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + dst = e.AppendArrayStart(dst) + dst = e.AppendStringer(dst, vals[0]) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = e.AppendStringer(dst, val) + } + } + return e.AppendArrayEnd(dst) +} + +// AppendStringer encodes and adds the Stringer value to the dst +// byte array. +func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte { + if val == nil { + return e.AppendNil(dst) + } + return e.AppendString(dst, val.String()) +} + +// AppendBytes encodes and adds an array of bytes to the dst byte array. +func (Encoder) AppendBytes(dst, s []byte) []byte { + major := majorTypeByteString + + l := len(s) + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + return append(dst, s...) +} + +// AppendEmbeddedJSON adds a tag and embeds input JSON as such. +func AppendEmbeddedJSON(dst, s []byte) []byte { + major := majorTypeTags + minor := additionalTypeEmbeddedJSON + + // Append the TAG to indicate this is Embedded JSON. + dst = append(dst, byte(major|additionalTypeIntUint16)) + dst = append(dst, byte(minor>>8)) + dst = append(dst, byte(minor&0xff)) + + // Append the JSON Object as Byte String. + major = majorTypeByteString + + l := len(s) + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + return append(dst, s...) +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/time.go b/vendor/github.com/rs/zerolog/internal/cbor/time.go new file mode 100644 index 0000000..12f6a1d --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/time.go @@ -0,0 +1,93 @@ +package cbor + +import ( + "time" +) + +func appendIntegerTimestamp(dst []byte, t time.Time) []byte { + major := majorTypeTags + minor := additionalTypeTimestamp + dst = append(dst, byte(major|minor)) + secs := t.Unix() + var val uint64 + if secs < 0 { + major = majorTypeNegativeInt + val = uint64(-secs - 1) + } else { + major = majorTypeUnsignedInt + val = uint64(secs) + } + dst = appendCborTypePrefix(dst, major, uint64(val)) + return dst +} + +func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte { + major := majorTypeTags + minor := additionalTypeTimestamp + dst = append(dst, byte(major|minor)) + secs := t.Unix() + nanos := t.Nanosecond() + var val float64 + val = float64(secs)*1.0 + float64(nanos)*1E-9 + return e.AppendFloat64(dst, val) +} + +// AppendTime encodes and adds a timestamp to the dst byte array. +func (e Encoder) AppendTime(dst []byte, t time.Time, unused string) []byte { + utc := t.UTC() + if utc.Nanosecond() == 0 { + return appendIntegerTimestamp(dst, utc) + } + return e.appendFloatTimestamp(dst, utc) +} + +// AppendTimes encodes and adds an array of timestamps to the dst byte array. +func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + + for _, t := range vals { + dst = e.AppendTime(dst, t, unused) + } + return dst +} + +// AppendDuration encodes and adds a duration to the dst byte array. +// useInt field indicates whether to store the duration as seconds (integer) or +// as seconds+nanoseconds (float). +func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { + if useInt { + return e.AppendInt64(dst, int64(d/unit)) + } + return e.AppendFloat64(dst, float64(d)/float64(unit)) +} + +// AppendDurations encodes and adds an array of durations to the dst byte array. +// useInt field indicates whether to store the duration as seconds (integer) or +// as seconds+nanoseconds (float). +func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, d := range vals { + dst = e.AppendDuration(dst, d, unit, useInt) + } + return dst +} diff --git a/vendor/github.com/rs/zerolog/internal/cbor/types.go b/vendor/github.com/rs/zerolog/internal/cbor/types.go new file mode 100644 index 0000000..a41c797 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/cbor/types.go @@ -0,0 +1,477 @@ +package cbor + +import ( + "fmt" + "math" + "net" +) + +// AppendNil inserts a 'Nil' object into the dst byte array. +func (Encoder) AppendNil(dst []byte) []byte { + return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeNull)) +} + +// AppendBeginMarker inserts a map start into the dst byte array. +func (Encoder) AppendBeginMarker(dst []byte) []byte { + return append(dst, byte(majorTypeMap|additionalTypeInfiniteCount)) +} + +// AppendEndMarker inserts a map end into the dst byte array. +func (Encoder) AppendEndMarker(dst []byte) []byte { + return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak)) +} + +// AppendObjectData takes an object in form of a byte array and appends to dst. +func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { + // BeginMarker is present in the dst, which + // should not be copied when appending to existing data. + return append(dst, o[1:]...) +} + +// AppendArrayStart adds markers to indicate the start of an array. +func (Encoder) AppendArrayStart(dst []byte) []byte { + return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount)) +} + +// AppendArrayEnd adds markers to indicate the end of an array. +func (Encoder) AppendArrayEnd(dst []byte) []byte { + return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak)) +} + +// AppendArrayDelim adds markers to indicate end of a particular array element. +func (Encoder) AppendArrayDelim(dst []byte) []byte { + //No delimiters needed in cbor + return dst +} + +// AppendLineBreak is a noop that keep API compat with json encoder. +func (Encoder) AppendLineBreak(dst []byte) []byte { + // No line breaks needed in binary format. + return dst +} + +// AppendBool encodes and inserts a boolean value into the dst byte array. +func (Encoder) AppendBool(dst []byte, val bool) []byte { + b := additionalTypeBoolFalse + if val { + b = additionalTypeBoolTrue + } + return append(dst, byte(majorTypeSimpleAndFloat|b)) +} + +// AppendBools encodes and inserts an array of boolean values into the dst byte array. +func (e Encoder) AppendBools(dst []byte, vals []bool) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendBool(dst, v) + } + return dst +} + +// AppendInt encodes and inserts an integer value into the dst byte array. +func (Encoder) AppendInt(dst []byte, val int) []byte { + major := majorTypeUnsignedInt + contentVal := val + if val < 0 { + major = majorTypeNegativeInt + contentVal = -val - 1 + } + if contentVal <= additionalMax { + lb := byte(contentVal) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(contentVal)) + } + return dst +} + +// AppendInts encodes and inserts an array of integer values into the dst byte array. +func (e Encoder) AppendInts(dst []byte, vals []int) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendInt(dst, v) + } + return dst +} + +// AppendInt8 encodes and inserts an int8 value into the dst byte array. +func (e Encoder) AppendInt8(dst []byte, val int8) []byte { + return e.AppendInt(dst, int(val)) +} + +// AppendInts8 encodes and inserts an array of integer values into the dst byte array. +func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendInt(dst, int(v)) + } + return dst +} + +// AppendInt16 encodes and inserts a int16 value into the dst byte array. +func (e Encoder) AppendInt16(dst []byte, val int16) []byte { + return e.AppendInt(dst, int(val)) +} + +// AppendInts16 encodes and inserts an array of int16 values into the dst byte array. +func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendInt(dst, int(v)) + } + return dst +} + +// AppendInt32 encodes and inserts a int32 value into the dst byte array. +func (e Encoder) AppendInt32(dst []byte, val int32) []byte { + return e.AppendInt(dst, int(val)) +} + +// AppendInts32 encodes and inserts an array of int32 values into the dst byte array. +func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendInt(dst, int(v)) + } + return dst +} + +// AppendInt64 encodes and inserts a int64 value into the dst byte array. +func (Encoder) AppendInt64(dst []byte, val int64) []byte { + major := majorTypeUnsignedInt + contentVal := val + if val < 0 { + major = majorTypeNegativeInt + contentVal = -val - 1 + } + if contentVal <= additionalMax { + lb := byte(contentVal) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(contentVal)) + } + return dst +} + +// AppendInts64 encodes and inserts an array of int64 values into the dst byte array. +func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendInt64(dst, v) + } + return dst +} + +// AppendUint encodes and inserts an unsigned integer value into the dst byte array. +func (e Encoder) AppendUint(dst []byte, val uint) []byte { + return e.AppendInt64(dst, int64(val)) +} + +// AppendUints encodes and inserts an array of unsigned integer values into the dst byte array. +func (e Encoder) AppendUints(dst []byte, vals []uint) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendUint(dst, v) + } + return dst +} + +// AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array. +func (e Encoder) AppendUint8(dst []byte, val uint8) []byte { + return e.AppendUint(dst, uint(val)) +} + +// AppendUints8 encodes and inserts an array of uint8 values into the dst byte array. +func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendUint8(dst, v) + } + return dst +} + +// AppendUint16 encodes and inserts a uint16 value into the dst byte array. +func (e Encoder) AppendUint16(dst []byte, val uint16) []byte { + return e.AppendUint(dst, uint(val)) +} + +// AppendUints16 encodes and inserts an array of uint16 values into the dst byte array. +func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendUint16(dst, v) + } + return dst +} + +// AppendUint32 encodes and inserts a uint32 value into the dst byte array. +func (e Encoder) AppendUint32(dst []byte, val uint32) []byte { + return e.AppendUint(dst, uint(val)) +} + +// AppendUints32 encodes and inserts an array of uint32 values into the dst byte array. +func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendUint32(dst, v) + } + return dst +} + +// AppendUint64 encodes and inserts a uint64 value into the dst byte array. +func (Encoder) AppendUint64(dst []byte, val uint64) []byte { + major := majorTypeUnsignedInt + contentVal := val + if contentVal <= additionalMax { + lb := byte(contentVal) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(contentVal)) + } + return dst +} + +// AppendUints64 encodes and inserts an array of uint64 values into the dst byte array. +func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendUint64(dst, v) + } + return dst +} + +// AppendFloat32 encodes and inserts a single precision float value into the dst byte array. +func (Encoder) AppendFloat32(dst []byte, val float32) []byte { + switch { + case math.IsNaN(float64(val)): + return append(dst, "\xfa\x7f\xc0\x00\x00"...) + case math.IsInf(float64(val), 1): + return append(dst, "\xfa\x7f\x80\x00\x00"...) + case math.IsInf(float64(val), -1): + return append(dst, "\xfa\xff\x80\x00\x00"...) + } + major := majorTypeSimpleAndFloat + subType := additionalTypeFloat32 + n := math.Float32bits(val) + var buf [4]byte + for i := uint(0); i < 4; i++ { + buf[i] = byte(n >> ((3 - i) * 8)) + } + return append(append(dst, byte(major|subType)), buf[0], buf[1], buf[2], buf[3]) +} + +// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array. +func (e Encoder) AppendFloats32(dst []byte, vals []float32) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendFloat32(dst, v) + } + return dst +} + +// AppendFloat64 encodes and inserts a double precision float value into the dst byte array. +func (Encoder) AppendFloat64(dst []byte, val float64) []byte { + switch { + case math.IsNaN(val): + return append(dst, "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"...) + case math.IsInf(val, 1): + return append(dst, "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00"...) + case math.IsInf(val, -1): + return append(dst, "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00"...) + } + major := majorTypeSimpleAndFloat + subType := additionalTypeFloat64 + n := math.Float64bits(val) + dst = append(dst, byte(major|subType)) + for i := uint(1); i <= 8; i++ { + b := byte(n >> ((8 - i) * 8)) + dst = append(dst, b) + } + return dst +} + +// AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array. +func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte { + major := majorTypeArray + l := len(vals) + if l == 0 { + return e.AppendArrayEnd(e.AppendArrayStart(dst)) + } + if l <= additionalMax { + lb := byte(l) + dst = append(dst, byte(major|lb)) + } else { + dst = appendCborTypePrefix(dst, major, uint64(l)) + } + for _, v := range vals { + dst = e.AppendFloat64(dst, v) + } + return dst +} + +// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst. +func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte { + marshaled, err := JSONMarshalFunc(i) + if err != nil { + return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) + } + return AppendEmbeddedJSON(dst, marshaled) +} + +// AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6). +func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte { + dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) + dst = append(dst, byte(additionalTypeTagNetworkAddr>>8)) + dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff)) + return e.AppendBytes(dst, ip) +} + +// AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length). +func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { + dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) + dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8)) + dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff)) + + // Prefix is a tuple (aka MAP of 1 pair of elements) - + // first element is prefix, second is mask length. + dst = append(dst, byte(majorTypeMap|0x1)) + dst = e.AppendBytes(dst, pfx.IP) + maskLen, _ := pfx.Mask.Size() + return e.AppendUint8(dst, uint8(maskLen)) +} + +// AppendMACAddr encodes and inserts an Hardware (MAC) address. +func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { + dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) + dst = append(dst, byte(additionalTypeTagNetworkAddr>>8)) + dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff)) + return e.AppendBytes(dst, ha) +} + +// AppendHex adds a TAG and inserts a hex bytes as a string. +func (e Encoder) AppendHex(dst []byte, val []byte) []byte { + dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16)) + dst = append(dst, byte(additionalTypeTagHexString>>8)) + dst = append(dst, byte(additionalTypeTagHexString&0xff)) + return e.AppendBytes(dst, val) +} diff --git a/vendor/github.com/rs/zerolog/internal/json/base.go b/vendor/github.com/rs/zerolog/internal/json/base.go new file mode 100644 index 0000000..09ec59f --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/json/base.go @@ -0,0 +1,19 @@ +package json + +// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice. +// Making it package level instead of embedded in Encoder brings +// some extra efforts at importing, but avoids value copy when the functions +// of Encoder being invoked. +// DO REMEMBER to set this variable at importing, or +// you might get a nil pointer dereference panic at runtime. +var JSONMarshalFunc func(v interface{}) ([]byte, error) + +type Encoder struct{} + +// AppendKey appends a new key to the output JSON. +func (e Encoder) AppendKey(dst []byte, key string) []byte { + if dst[len(dst)-1] != '{' { + dst = append(dst, ',') + } + return append(e.AppendString(dst, key), ':') +} diff --git a/vendor/github.com/rs/zerolog/internal/json/bytes.go b/vendor/github.com/rs/zerolog/internal/json/bytes.go new file mode 100644 index 0000000..de64120 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/json/bytes.go @@ -0,0 +1,85 @@ +package json + +import "unicode/utf8" + +// AppendBytes is a mirror of appendString with []byte arg +func (Encoder) AppendBytes(dst, s []byte) []byte { + dst = append(dst, '"') + for i := 0; i < len(s); i++ { + if !noEscapeTable[s[i]] { + dst = appendBytesComplex(dst, s, i) + return append(dst, '"') + } + } + dst = append(dst, s...) + return append(dst, '"') +} + +// AppendHex encodes the input bytes to a hex string and appends +// the encoded string to the input byte slice. +// +// The operation loops though each byte and encodes it as hex using +// the hex lookup table. +func (Encoder) AppendHex(dst, s []byte) []byte { + dst = append(dst, '"') + for _, v := range s { + dst = append(dst, hex[v>>4], hex[v&0x0f]) + } + return append(dst, '"') +} + +// appendBytesComplex is a mirror of the appendStringComplex +// with []byte arg +func appendBytesComplex(dst, s []byte, i int) []byte { + start := 0 + for i < len(s) { + b := s[i] + if b >= utf8.RuneSelf { + r, size := utf8.DecodeRune(s[i:]) + if r == utf8.RuneError && size == 1 { + if start < i { + dst = append(dst, s[start:i]...) + } + dst = append(dst, `\ufffd`...) + i += size + start = i + continue + } + i += size + continue + } + if noEscapeTable[b] { + i++ + continue + } + // We encountered a character that needs to be encoded. + // Let's append the previous simple characters to the byte slice + // and switch our operation to read and encode the remainder + // characters byte-by-byte. + if start < i { + dst = append(dst, s[start:i]...) + } + switch b { + case '"', '\\': + dst = append(dst, '\\', b) + case '\b': + dst = append(dst, '\\', 'b') + case '\f': + dst = append(dst, '\\', 'f') + case '\n': + dst = append(dst, '\\', 'n') + case '\r': + dst = append(dst, '\\', 'r') + case '\t': + dst = append(dst, '\\', 't') + default: + dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF]) + } + i++ + start = i + } + if start < len(s) { + dst = append(dst, s[start:]...) + } + return dst +} diff --git a/vendor/github.com/rs/zerolog/internal/json/string.go b/vendor/github.com/rs/zerolog/internal/json/string.go new file mode 100644 index 0000000..46698b5 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/json/string.go @@ -0,0 +1,149 @@ +package json + +import ( + "fmt" + "unicode/utf8" +) + +const hex = "0123456789abcdef" + +var noEscapeTable = [256]bool{} + +func init() { + for i := 0; i <= 0x7e; i++ { + noEscapeTable[i] = i >= 0x20 && i != '\\' && i != '"' + } +} + +// AppendStrings encodes the input strings to json and +// appends the encoded string list to the input byte slice. +func (e Encoder) AppendStrings(dst []byte, vals []string) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = e.AppendString(dst, vals[0]) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = e.AppendString(append(dst, ','), val) + } + } + dst = append(dst, ']') + return dst +} + +// AppendString encodes the input string to json and appends +// the encoded string to the input byte slice. +// +// The operation loops though each byte in the string looking +// for characters that need json or utf8 encoding. If the string +// does not need encoding, then the string is appended in it's +// entirety to the byte slice. +// If we encounter a byte that does need encoding, switch up +// the operation and perform a byte-by-byte read-encode-append. +func (Encoder) AppendString(dst []byte, s string) []byte { + // Start with a double quote. + dst = append(dst, '"') + // Loop through each character in the string. + for i := 0; i < len(s); i++ { + // Check if the character needs encoding. Control characters, slashes, + // and the double quote need json encoding. Bytes above the ascii + // boundary needs utf8 encoding. + if !noEscapeTable[s[i]] { + // We encountered a character that needs to be encoded. Switch + // to complex version of the algorithm. + dst = appendStringComplex(dst, s, i) + return append(dst, '"') + } + } + // The string has no need for encoding an therefore is directly + // appended to the byte slice. + dst = append(dst, s...) + // End with a double quote + return append(dst, '"') +} + +// AppendStringers encodes the provided Stringer list to json and +// appends the encoded Stringer list to the input byte slice. +func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = e.AppendStringer(dst, vals[0]) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = e.AppendStringer(append(dst, ','), val) + } + } + return append(dst, ']') +} + +// AppendStringer encodes the input Stringer to json and appends the +// encoded Stringer value to the input byte slice. +func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte { + if val == nil { + return e.AppendInterface(dst, nil) + } + return e.AppendString(dst, val.String()) +} + +//// appendStringComplex is used by appendString to take over an in +// progress JSON string encoding that encountered a character that needs +// to be encoded. +func appendStringComplex(dst []byte, s string, i int) []byte { + start := 0 + for i < len(s) { + b := s[i] + if b >= utf8.RuneSelf { + r, size := utf8.DecodeRuneInString(s[i:]) + if r == utf8.RuneError && size == 1 { + // In case of error, first append previous simple characters to + // the byte slice if any and append a remplacement character code + // in place of the invalid sequence. + if start < i { + dst = append(dst, s[start:i]...) + } + dst = append(dst, `\ufffd`...) + i += size + start = i + continue + } + i += size + continue + } + if noEscapeTable[b] { + i++ + continue + } + // We encountered a character that needs to be encoded. + // Let's append the previous simple characters to the byte slice + // and switch our operation to read and encode the remainder + // characters byte-by-byte. + if start < i { + dst = append(dst, s[start:i]...) + } + switch b { + case '"', '\\': + dst = append(dst, '\\', b) + case '\b': + dst = append(dst, '\\', 'b') + case '\f': + dst = append(dst, '\\', 'f') + case '\n': + dst = append(dst, '\\', 'n') + case '\r': + dst = append(dst, '\\', 'r') + case '\t': + dst = append(dst, '\\', 't') + default: + dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF]) + } + i++ + start = i + } + if start < len(s) { + dst = append(dst, s[start:]...) + } + return dst +} diff --git a/vendor/github.com/rs/zerolog/internal/json/time.go b/vendor/github.com/rs/zerolog/internal/json/time.go new file mode 100644 index 0000000..5aff6be --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/json/time.go @@ -0,0 +1,106 @@ +package json + +import ( + "strconv" + "time" +) + +const ( + // Import from zerolog/global.go + timeFormatUnix = "" + timeFormatUnixMs = "UNIXMS" + timeFormatUnixMicro = "UNIXMICRO" +) + +// AppendTime formats the input time with the given format +// and appends the encoded string to the input byte slice. +func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte { + switch format { + case timeFormatUnix: + return e.AppendInt64(dst, t.Unix()) + case timeFormatUnixMs: + return e.AppendInt64(dst, t.UnixNano()/1000000) + case timeFormatUnixMicro: + return e.AppendInt64(dst, t.UnixNano()/1000) + } + return append(t.AppendFormat(append(dst, '"'), format), '"') +} + +// AppendTimes converts the input times with the given format +// and appends the encoded string list to the input byte slice. +func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte { + switch format { + case timeFormatUnix: + return appendUnixTimes(dst, vals) + case timeFormatUnixMs: + return appendUnixMsTimes(dst, vals) + } + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = append(vals[0].AppendFormat(append(dst, '"'), format), '"') + if len(vals) > 1 { + for _, t := range vals[1:] { + dst = append(t.AppendFormat(append(dst, ',', '"'), format), '"') + } + } + dst = append(dst, ']') + return dst +} + +func appendUnixTimes(dst []byte, vals []time.Time) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, vals[0].Unix(), 10) + if len(vals) > 1 { + for _, t := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10) + } + } + dst = append(dst, ']') + return dst +} + +func appendUnixMsTimes(dst []byte, vals []time.Time) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, vals[0].UnixNano()/1000000, 10) + if len(vals) > 1 { + for _, t := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/1000000, 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendDuration formats the input duration with the given unit & format +// and appends the encoded string to the input byte slice. +func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte { + if useInt { + return strconv.AppendInt(dst, int64(d/unit), 10) + } + return e.AppendFloat64(dst, float64(d)/float64(unit)) +} + +// AppendDurations formats the input durations with the given unit & format +// and appends the encoded string list to the input byte slice. +func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = e.AppendDuration(dst, vals[0], unit, useInt) + if len(vals) > 1 { + for _, d := range vals[1:] { + dst = e.AppendDuration(append(dst, ','), d, unit, useInt) + } + } + dst = append(dst, ']') + return dst +} diff --git a/vendor/github.com/rs/zerolog/internal/json/types.go b/vendor/github.com/rs/zerolog/internal/json/types.go new file mode 100644 index 0000000..9e35266 --- /dev/null +++ b/vendor/github.com/rs/zerolog/internal/json/types.go @@ -0,0 +1,405 @@ +package json + +import ( + "fmt" + "math" + "net" + "strconv" +) + +// AppendNil inserts a 'Nil' object into the dst byte array. +func (Encoder) AppendNil(dst []byte) []byte { + return append(dst, "null"...) +} + +// AppendBeginMarker inserts a map start into the dst byte array. +func (Encoder) AppendBeginMarker(dst []byte) []byte { + return append(dst, '{') +} + +// AppendEndMarker inserts a map end into the dst byte array. +func (Encoder) AppendEndMarker(dst []byte) []byte { + return append(dst, '}') +} + +// AppendLineBreak appends a line break. +func (Encoder) AppendLineBreak(dst []byte) []byte { + return append(dst, '\n') +} + +// AppendArrayStart adds markers to indicate the start of an array. +func (Encoder) AppendArrayStart(dst []byte) []byte { + return append(dst, '[') +} + +// AppendArrayEnd adds markers to indicate the end of an array. +func (Encoder) AppendArrayEnd(dst []byte) []byte { + return append(dst, ']') +} + +// AppendArrayDelim adds markers to indicate end of a particular array element. +func (Encoder) AppendArrayDelim(dst []byte) []byte { + if len(dst) > 0 { + return append(dst, ',') + } + return dst +} + +// AppendBool converts the input bool to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendBool(dst []byte, val bool) []byte { + return strconv.AppendBool(dst, val) +} + +// AppendBools encodes the input bools to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendBools(dst []byte, vals []bool) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendBool(dst, vals[0]) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendBool(append(dst, ','), val) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInt converts the input int to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendInt(dst []byte, val int) []byte { + return strconv.AppendInt(dst, int64(val), 10) +} + +// AppendInts encodes the input ints to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendInts(dst []byte, vals []int) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, int64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), int64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInt8 converts the input []int8 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendInt8(dst []byte, val int8) []byte { + return strconv.AppendInt(dst, int64(val), 10) +} + +// AppendInts8 encodes the input int8s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendInts8(dst []byte, vals []int8) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, int64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), int64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInt16 converts the input int16 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendInt16(dst []byte, val int16) []byte { + return strconv.AppendInt(dst, int64(val), 10) +} + +// AppendInts16 encodes the input int16s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendInts16(dst []byte, vals []int16) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, int64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), int64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInt32 converts the input int32 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendInt32(dst []byte, val int32) []byte { + return strconv.AppendInt(dst, int64(val), 10) +} + +// AppendInts32 encodes the input int32s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendInts32(dst []byte, vals []int32) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, int64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), int64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInt64 converts the input int64 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendInt64(dst []byte, val int64) []byte { + return strconv.AppendInt(dst, val, 10) +} + +// AppendInts64 encodes the input int64s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendInts64(dst []byte, vals []int64) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendInt(dst, vals[0], 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendInt(append(dst, ','), val, 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendUint converts the input uint to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendUint(dst []byte, val uint) []byte { + return strconv.AppendUint(dst, uint64(val), 10) +} + +// AppendUints encodes the input uints to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendUints(dst []byte, vals []uint) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendUint(dst, uint64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendUint8 converts the input uint8 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendUint8(dst []byte, val uint8) []byte { + return strconv.AppendUint(dst, uint64(val), 10) +} + +// AppendUints8 encodes the input uint8s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendUint(dst, uint64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendUint16 converts the input uint16 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendUint16(dst []byte, val uint16) []byte { + return strconv.AppendUint(dst, uint64(val), 10) +} + +// AppendUints16 encodes the input uint16s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendUint(dst, uint64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendUint32 converts the input uint32 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendUint32(dst []byte, val uint32) []byte { + return strconv.AppendUint(dst, uint64(val), 10) +} + +// AppendUints32 encodes the input uint32s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendUint(dst, uint64(vals[0]), 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendUint(append(dst, ','), uint64(val), 10) + } + } + dst = append(dst, ']') + return dst +} + +// AppendUint64 converts the input uint64 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendUint64(dst []byte, val uint64) []byte { + return strconv.AppendUint(dst, uint64(val), 10) +} + +// AppendUints64 encodes the input uint64s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = strconv.AppendUint(dst, vals[0], 10) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = strconv.AppendUint(append(dst, ','), val, 10) + } + } + dst = append(dst, ']') + return dst +} + +func appendFloat(dst []byte, val float64, bitSize int) []byte { + // JSON does not permit NaN or Infinity. A typical JSON encoder would fail + // with an error, but a logging library wants the data to get thru so we + // make a tradeoff and store those types as string. + switch { + case math.IsNaN(val): + return append(dst, `"NaN"`...) + case math.IsInf(val, 1): + return append(dst, `"+Inf"`...) + case math.IsInf(val, -1): + return append(dst, `"-Inf"`...) + } + return strconv.AppendFloat(dst, val, 'f', -1, bitSize) +} + +// AppendFloat32 converts the input float32 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendFloat32(dst []byte, val float32) []byte { + return appendFloat(dst, float64(val), 32) +} + +// AppendFloats32 encodes the input float32s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendFloats32(dst []byte, vals []float32) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = appendFloat(dst, float64(vals[0]), 32) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = appendFloat(append(dst, ','), float64(val), 32) + } + } + dst = append(dst, ']') + return dst +} + +// AppendFloat64 converts the input float64 to a string and +// appends the encoded string to the input byte slice. +func (Encoder) AppendFloat64(dst []byte, val float64) []byte { + return appendFloat(dst, val, 64) +} + +// AppendFloats64 encodes the input float64s to json and +// appends the encoded string list to the input byte slice. +func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte { + if len(vals) == 0 { + return append(dst, '[', ']') + } + dst = append(dst, '[') + dst = appendFloat(dst, vals[0], 64) + if len(vals) > 1 { + for _, val := range vals[1:] { + dst = appendFloat(append(dst, ','), val, 64) + } + } + dst = append(dst, ']') + return dst +} + +// AppendInterface marshals the input interface to a string and +// appends the encoded string to the input byte slice. +func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte { + marshaled, err := JSONMarshalFunc(i) + if err != nil { + return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err)) + } + return append(dst, marshaled...) +} + +// AppendObjectData takes in an object that is already in a byte array +// and adds it to the dst. +func (Encoder) AppendObjectData(dst []byte, o []byte) []byte { + // Three conditions apply here: + // 1. new content starts with '{' - which should be dropped OR + // 2. new content starts with '{' - which should be replaced with ',' + // to separate with existing content OR + // 3. existing content has already other fields + if o[0] == '{' { + if len(dst) > 1 { + dst = append(dst, ',') + } + o = o[1:] + } else if len(dst) > 1 { + dst = append(dst, ',') + } + return append(dst, o...) +} + +// AppendIPAddr adds IPv4 or IPv6 address to dst. +func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte { + return e.AppendString(dst, ip.String()) +} + +// AppendIPPrefix adds IPv4 or IPv6 Prefix (address & mask) to dst. +func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte { + return e.AppendString(dst, pfx.String()) + +} + +// AppendMACAddr adds MAC address to dst. +func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte { + return e.AppendString(dst, ha.String()) +} diff --git a/vendor/github.com/rs/zerolog/log.go b/vendor/github.com/rs/zerolog/log.go new file mode 100644 index 0000000..6227386 --- /dev/null +++ b/vendor/github.com/rs/zerolog/log.go @@ -0,0 +1,457 @@ +// Package zerolog provides a lightweight logging library dedicated to JSON logging. +// +// A global Logger can be use for simple logging: +// +// import "github.com/rs/zerolog/log" +// +// log.Info().Msg("hello world") +// // Output: {"time":1494567715,"level":"info","message":"hello world"} +// +// NOTE: To import the global logger, import the "log" subpackage "github.com/rs/zerolog/log". +// +// Fields can be added to log messages: +// +// log.Info().Str("foo", "bar").Msg("hello world") +// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"} +// +// Create logger instance to manage different outputs: +// +// logger := zerolog.New(os.Stderr).With().Timestamp().Logger() +// logger.Info(). +// Str("foo", "bar"). +// Msg("hello world") +// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"} +// +// Sub-loggers let you chain loggers with additional context: +// +// sublogger := log.With().Str("component": "foo").Logger() +// sublogger.Info().Msg("hello world") +// // Output: {"time":1494567715,"level":"info","message":"hello world","component":"foo"} +// +// Level logging +// +// zerolog.SetGlobalLevel(zerolog.InfoLevel) +// +// log.Debug().Msg("filtered out message") +// log.Info().Msg("routed message") +// +// if e := log.Debug(); e.Enabled() { +// // Compute log output only if enabled. +// value := compute() +// e.Str("foo": value).Msg("some debug message") +// } +// // Output: {"level":"info","time":1494567715,"routed message"} +// +// Customize automatic field names: +// +// log.TimestampFieldName = "t" +// log.LevelFieldName = "p" +// log.MessageFieldName = "m" +// +// log.Info().Msg("hello world") +// // Output: {"t":1494567715,"p":"info","m":"hello world"} +// +// Log with no level and message: +// +// log.Log().Str("foo","bar").Msg("") +// // Output: {"time":1494567715,"foo":"bar"} +// +// Add contextual fields to global Logger: +// +// log.Logger = log.With().Str("foo", "bar").Logger() +// +// Sample logs: +// +// sampled := log.Sample(&zerolog.BasicSampler{N: 10}) +// sampled.Info().Msg("will be logged every 10 messages") +// +// Log with contextual hooks: +// +// // Create the hook: +// type SeverityHook struct{} +// +// func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { +// if level != zerolog.NoLevel { +// e.Str("severity", level.String()) +// } +// } +// +// // And use it: +// var h SeverityHook +// log := zerolog.New(os.Stdout).Hook(h) +// log.Warn().Msg("") +// // Output: {"level":"warn","severity":"warn"} +// +// +// Caveats +// +// There is no fields deduplication out-of-the-box. +// Using the same key multiple times creates new key in final JSON each time. +// +// logger := zerolog.New(os.Stderr).With().Timestamp().Logger() +// logger.Info(). +// Timestamp(). +// Msg("dup") +// // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"} +// +// In this case, many consumers will take the last value, +// but this is not guaranteed; check yours if in doubt. +package zerolog + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "strconv" +) + +// Level defines log levels. +type Level int8 + +const ( + // DebugLevel defines debug log level. + DebugLevel Level = iota + // InfoLevel defines info log level. + InfoLevel + // WarnLevel defines warn log level. + WarnLevel + // ErrorLevel defines error log level. + ErrorLevel + // FatalLevel defines fatal log level. + FatalLevel + // PanicLevel defines panic log level. + PanicLevel + // NoLevel defines an absent log level. + NoLevel + // Disabled disables the logger. + Disabled + + // TraceLevel defines trace log level. + TraceLevel Level = -1 + // Values less than TraceLevel are handled as numbers. +) + +func (l Level) String() string { + switch l { + case TraceLevel: + return LevelTraceValue + case DebugLevel: + return LevelDebugValue + case InfoLevel: + return LevelInfoValue + case WarnLevel: + return LevelWarnValue + case ErrorLevel: + return LevelErrorValue + case FatalLevel: + return LevelFatalValue + case PanicLevel: + return LevelPanicValue + case Disabled: + return "disabled" + case NoLevel: + return "" + } + return strconv.Itoa(int(l)) +} + +// ParseLevel converts a level string into a zerolog Level value. +// returns an error if the input string does not match known values. +func ParseLevel(levelStr string) (Level, error) { + switch levelStr { + case LevelFieldMarshalFunc(TraceLevel): + return TraceLevel, nil + case LevelFieldMarshalFunc(DebugLevel): + return DebugLevel, nil + case LevelFieldMarshalFunc(InfoLevel): + return InfoLevel, nil + case LevelFieldMarshalFunc(WarnLevel): + return WarnLevel, nil + case LevelFieldMarshalFunc(ErrorLevel): + return ErrorLevel, nil + case LevelFieldMarshalFunc(FatalLevel): + return FatalLevel, nil + case LevelFieldMarshalFunc(PanicLevel): + return PanicLevel, nil + case LevelFieldMarshalFunc(Disabled): + return Disabled, nil + case LevelFieldMarshalFunc(NoLevel): + return NoLevel, nil + } + i, err := strconv.Atoi(levelStr) + if err != nil { + return NoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr) + } + if i > 127 || i < -128 { + return NoLevel, fmt.Errorf("Out-Of-Bounds Level: '%d', defaulting to NoLevel", i) + } + return Level(i), nil +} + +// A Logger represents an active logging object that generates lines +// of JSON output to an io.Writer. Each logging operation makes a single +// call to the Writer's Write method. There is no guarantee on access +// serialization to the Writer. If your Writer is not thread safe, +// you may consider a sync wrapper. +type Logger struct { + w LevelWriter + level Level + sampler Sampler + context []byte + hooks []Hook + stack bool +} + +// New creates a root logger with given output writer. If the output writer implements +// the LevelWriter interface, the WriteLevel method will be called instead of the Write +// one. +// +// Each logging operation makes a single call to the Writer's Write method. There is no +// guarantee on access serialization to the Writer. If your Writer is not thread safe, +// you may consider using sync wrapper. +func New(w io.Writer) Logger { + if w == nil { + w = ioutil.Discard + } + lw, ok := w.(LevelWriter) + if !ok { + lw = levelWriterAdapter{w} + } + return Logger{w: lw, level: TraceLevel} +} + +// Nop returns a disabled logger for which all operation are no-op. +func Nop() Logger { + return New(nil).Level(Disabled) +} + +// Output duplicates the current logger and sets w as its output. +func (l Logger) Output(w io.Writer) Logger { + l2 := New(w) + l2.level = l.level + l2.sampler = l.sampler + l2.stack = l.stack + if len(l.hooks) > 0 { + l2.hooks = append(l2.hooks, l.hooks...) + } + if l.context != nil { + l2.context = make([]byte, len(l.context), cap(l.context)) + copy(l2.context, l.context) + } + return l2 +} + +// With creates a child logger with the field added to its context. +func (l Logger) With() Context { + context := l.context + l.context = make([]byte, 0, 500) + if context != nil { + l.context = append(l.context, context...) + } else { + // This is needed for AppendKey to not check len of input + // thus making it inlinable + l.context = enc.AppendBeginMarker(l.context) + } + return Context{l} +} + +// UpdateContext updates the internal logger's context. +// +// Use this method with caution. If unsure, prefer the With method. +func (l *Logger) UpdateContext(update func(c Context) Context) { + if l == disabledLogger { + return + } + if cap(l.context) == 0 { + l.context = make([]byte, 0, 500) + } + if len(l.context) == 0 { + l.context = enc.AppendBeginMarker(l.context) + } + c := update(Context{*l}) + l.context = c.l.context +} + +// Level creates a child logger with the minimum accepted level set to level. +func (l Logger) Level(lvl Level) Logger { + l.level = lvl + return l +} + +// GetLevel returns the current Level of l. +func (l Logger) GetLevel() Level { + return l.level +} + +// Sample returns a logger with the s sampler. +func (l Logger) Sample(s Sampler) Logger { + l.sampler = s + return l +} + +// Hook returns a logger with the h Hook. +func (l Logger) Hook(h Hook) Logger { + l.hooks = append(l.hooks, h) + return l +} + +// Trace starts a new message with trace level. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Trace() *Event { + return l.newEvent(TraceLevel, nil) +} + +// Debug starts a new message with debug level. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Debug() *Event { + return l.newEvent(DebugLevel, nil) +} + +// Info starts a new message with info level. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Info() *Event { + return l.newEvent(InfoLevel, nil) +} + +// Warn starts a new message with warn level. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Warn() *Event { + return l.newEvent(WarnLevel, nil) +} + +// Error starts a new message with error level. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Error() *Event { + return l.newEvent(ErrorLevel, nil) +} + +// Err starts a new message with error level with err as a field if not nil or +// with info level if err is nil. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Err(err error) *Event { + if err != nil { + return l.Error().Err(err) + } + + return l.Info() +} + +// Fatal starts a new message with fatal level. The os.Exit(1) function +// is called by the Msg method, which terminates the program immediately. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Fatal() *Event { + return l.newEvent(FatalLevel, func(msg string) { os.Exit(1) }) +} + +// Panic starts a new message with panic level. The panic() function +// is called by the Msg method, which stops the ordinary flow of a goroutine. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Panic() *Event { + return l.newEvent(PanicLevel, func(msg string) { panic(msg) }) +} + +// WithLevel starts a new message with level. Unlike Fatal and Panic +// methods, WithLevel does not terminate the program or stop the ordinary +// flow of a gourotine when used with their respective levels. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) WithLevel(level Level) *Event { + switch level { + case TraceLevel: + return l.Trace() + case DebugLevel: + return l.Debug() + case InfoLevel: + return l.Info() + case WarnLevel: + return l.Warn() + case ErrorLevel: + return l.Error() + case FatalLevel: + return l.newEvent(FatalLevel, nil) + case PanicLevel: + return l.newEvent(PanicLevel, nil) + case NoLevel: + return l.Log() + case Disabled: + return nil + default: + return l.newEvent(level, nil) + } +} + +// Log starts a new message with no level. Setting GlobalLevel to Disabled +// will still disable events produced by this method. +// +// You must call Msg on the returned event in order to send the event. +func (l *Logger) Log() *Event { + return l.newEvent(NoLevel, nil) +} + +// Print sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Print. +func (l *Logger) Print(v ...interface{}) { + if e := l.Debug(); e.Enabled() { + e.CallerSkipFrame(1).Msg(fmt.Sprint(v...)) + } +} + +// Printf sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Printf. +func (l *Logger) Printf(format string, v ...interface{}) { + if e := l.Debug(); e.Enabled() { + e.CallerSkipFrame(1).Msg(fmt.Sprintf(format, v...)) + } +} + +// Write implements the io.Writer interface. This is useful to set as a writer +// for the standard library log. +func (l Logger) Write(p []byte) (n int, err error) { + n = len(p) + if n > 0 && p[n-1] == '\n' { + // Trim CR added by stdlog. + p = p[0 : n-1] + } + l.Log().CallerSkipFrame(1).Msg(string(p)) + return +} + +func (l *Logger) newEvent(level Level, done func(string)) *Event { + enabled := l.should(level) + if !enabled { + return nil + } + e := newEvent(l.w, level) + e.done = done + e.ch = l.hooks + if level != NoLevel && LevelFieldName != "" { + e.Str(LevelFieldName, LevelFieldMarshalFunc(level)) + } + if l.context != nil && len(l.context) > 1 { + e.buf = enc.AppendObjectData(e.buf, l.context) + } + if l.stack { + e.Stack() + } + return e +} + +// should returns true if the log event should be logged. +func (l *Logger) should(lvl Level) bool { + if lvl < l.level || lvl < GlobalLevel() { + return false + } + if l.sampler != nil && !samplingDisabled() { + return l.sampler.Sample(lvl) + } + return true +} diff --git a/vendor/github.com/rs/zerolog/log/log.go b/vendor/github.com/rs/zerolog/log/log.go new file mode 100644 index 0000000..a96ec50 --- /dev/null +++ b/vendor/github.com/rs/zerolog/log/log.go @@ -0,0 +1,131 @@ +// Package log provides a global logger for zerolog. +package log + +import ( + "context" + "fmt" + "io" + "os" + + "github.com/rs/zerolog" +) + +// Logger is the global logger. +var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger() + +// Output duplicates the global logger and sets w as its output. +func Output(w io.Writer) zerolog.Logger { + return Logger.Output(w) +} + +// With creates a child logger with the field added to its context. +func With() zerolog.Context { + return Logger.With() +} + +// Level creates a child logger with the minimum accepted level set to level. +func Level(level zerolog.Level) zerolog.Logger { + return Logger.Level(level) +} + +// Sample returns a logger with the s sampler. +func Sample(s zerolog.Sampler) zerolog.Logger { + return Logger.Sample(s) +} + +// Hook returns a logger with the h Hook. +func Hook(h zerolog.Hook) zerolog.Logger { + return Logger.Hook(h) +} + +// Err starts a new message with error level with err as a field if not nil or +// with info level if err is nil. +// +// You must call Msg on the returned event in order to send the event. +func Err(err error) *zerolog.Event { + return Logger.Err(err) +} + +// Trace starts a new message with trace level. +// +// You must call Msg on the returned event in order to send the event. +func Trace() *zerolog.Event { + return Logger.Trace() +} + +// Debug starts a new message with debug level. +// +// You must call Msg on the returned event in order to send the event. +func Debug() *zerolog.Event { + return Logger.Debug() +} + +// Info starts a new message with info level. +// +// You must call Msg on the returned event in order to send the event. +func Info() *zerolog.Event { + return Logger.Info() +} + +// Warn starts a new message with warn level. +// +// You must call Msg on the returned event in order to send the event. +func Warn() *zerolog.Event { + return Logger.Warn() +} + +// Error starts a new message with error level. +// +// You must call Msg on the returned event in order to send the event. +func Error() *zerolog.Event { + return Logger.Error() +} + +// Fatal starts a new message with fatal level. The os.Exit(1) function +// is called by the Msg method. +// +// You must call Msg on the returned event in order to send the event. +func Fatal() *zerolog.Event { + return Logger.Fatal() +} + +// Panic starts a new message with panic level. The message is also sent +// to the panic function. +// +// You must call Msg on the returned event in order to send the event. +func Panic() *zerolog.Event { + return Logger.Panic() +} + +// WithLevel starts a new message with level. +// +// You must call Msg on the returned event in order to send the event. +func WithLevel(level zerolog.Level) *zerolog.Event { + return Logger.WithLevel(level) +} + +// Log starts a new message with no level. Setting zerolog.GlobalLevel to +// zerolog.Disabled will still disable events produced by this method. +// +// You must call Msg on the returned event in order to send the event. +func Log() *zerolog.Event { + return Logger.Log() +} + +// Print sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Print. +func Print(v ...interface{}) { + Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...)) +} + +// Printf sends a log event using debug level and no extra field. +// Arguments are handled in the manner of fmt.Printf. +func Printf(format string, v ...interface{}) { + Logger.Debug().CallerSkipFrame(1).Msgf(format, v...) +} + +// Ctx returns the Logger associated with the ctx. If no logger +// is associated, a disabled logger is returned. +func Ctx(ctx context.Context) *zerolog.Logger { + return zerolog.Ctx(ctx) +} diff --git a/vendor/github.com/rs/zerolog/not_go112.go b/vendor/github.com/rs/zerolog/not_go112.go new file mode 100644 index 0000000..4c43c9e --- /dev/null +++ b/vendor/github.com/rs/zerolog/not_go112.go @@ -0,0 +1,5 @@ +// +build !go1.12 + +package zerolog + +const contextCallerSkipFrameCount = 3 diff --git a/vendor/github.com/rs/zerolog/pretty.png b/vendor/github.com/rs/zerolog/pretty.png new file mode 100644 index 0000000..2420336 Binary files /dev/null and b/vendor/github.com/rs/zerolog/pretty.png differ diff --git a/vendor/github.com/rs/zerolog/sampler.go b/vendor/github.com/rs/zerolog/sampler.go new file mode 100644 index 0000000..1be98c4 --- /dev/null +++ b/vendor/github.com/rs/zerolog/sampler.go @@ -0,0 +1,134 @@ +package zerolog + +import ( + "math/rand" + "sync/atomic" + "time" +) + +var ( + // Often samples log every ~ 10 events. + Often = RandomSampler(10) + // Sometimes samples log every ~ 100 events. + Sometimes = RandomSampler(100) + // Rarely samples log every ~ 1000 events. + Rarely = RandomSampler(1000) +) + +// Sampler defines an interface to a log sampler. +type Sampler interface { + // Sample returns true if the event should be part of the sample, false if + // the event should be dropped. + Sample(lvl Level) bool +} + +// RandomSampler use a PRNG to randomly sample an event out of N events, +// regardless of their level. +type RandomSampler uint32 + +// Sample implements the Sampler interface. +func (s RandomSampler) Sample(lvl Level) bool { + if s <= 0 { + return false + } + if rand.Intn(int(s)) != 0 { + return false + } + return true +} + +// BasicSampler is a sampler that will send every Nth events, regardless of +// their level. +type BasicSampler struct { + N uint32 + counter uint32 +} + +// Sample implements the Sampler interface. +func (s *BasicSampler) Sample(lvl Level) bool { + n := s.N + if n == 1 { + return true + } + c := atomic.AddUint32(&s.counter, 1) + return c%n == 1 +} + +// BurstSampler lets Burst events pass per Period then pass the decision to +// NextSampler. If Sampler is not set, all subsequent events are rejected. +type BurstSampler struct { + // Burst is the maximum number of event per period allowed before calling + // NextSampler. + Burst uint32 + // Period defines the burst period. If 0, NextSampler is always called. + Period time.Duration + // NextSampler is the sampler used after the burst is reached. If nil, + // events are always rejected after the burst. + NextSampler Sampler + + counter uint32 + resetAt int64 +} + +// Sample implements the Sampler interface. +func (s *BurstSampler) Sample(lvl Level) bool { + if s.Burst > 0 && s.Period > 0 { + if s.inc() <= s.Burst { + return true + } + } + if s.NextSampler == nil { + return false + } + return s.NextSampler.Sample(lvl) +} + +func (s *BurstSampler) inc() uint32 { + now := time.Now().UnixNano() + resetAt := atomic.LoadInt64(&s.resetAt) + var c uint32 + if now > resetAt { + c = 1 + atomic.StoreUint32(&s.counter, c) + newResetAt := now + s.Period.Nanoseconds() + reset := atomic.CompareAndSwapInt64(&s.resetAt, resetAt, newResetAt) + if !reset { + // Lost the race with another goroutine trying to reset. + c = atomic.AddUint32(&s.counter, 1) + } + } else { + c = atomic.AddUint32(&s.counter, 1) + } + return c +} + +// LevelSampler applies a different sampler for each level. +type LevelSampler struct { + TraceSampler, DebugSampler, InfoSampler, WarnSampler, ErrorSampler Sampler +} + +func (s LevelSampler) Sample(lvl Level) bool { + switch lvl { + case TraceLevel: + if s.TraceSampler != nil { + return s.TraceSampler.Sample(lvl) + } + case DebugLevel: + if s.DebugSampler != nil { + return s.DebugSampler.Sample(lvl) + } + case InfoLevel: + if s.InfoSampler != nil { + return s.InfoSampler.Sample(lvl) + } + case WarnLevel: + if s.WarnSampler != nil { + return s.WarnSampler.Sample(lvl) + } + case ErrorLevel: + if s.ErrorSampler != nil { + return s.ErrorSampler.Sample(lvl) + } + } + return true +} diff --git a/vendor/github.com/rs/zerolog/syslog.go b/vendor/github.com/rs/zerolog/syslog.go new file mode 100644 index 0000000..c408283 --- /dev/null +++ b/vendor/github.com/rs/zerolog/syslog.go @@ -0,0 +1,80 @@ +// +build !windows +// +build !binary_log + +package zerolog + +import ( + "io" +) + +// See http://cee.mitre.org/language/1.0-beta1/clt.html#syslog +// or https://www.rsyslog.com/json-elasticsearch/ +const ceePrefix = "@cee:" + +// SyslogWriter is an interface matching a syslog.Writer struct. +type SyslogWriter interface { + io.Writer + Debug(m string) error + Info(m string) error + Warning(m string) error + Err(m string) error + Emerg(m string) error + Crit(m string) error +} + +type syslogWriter struct { + w SyslogWriter + prefix string +} + +// SyslogLevelWriter wraps a SyslogWriter and call the right syslog level +// method matching the zerolog level. +func SyslogLevelWriter(w SyslogWriter) LevelWriter { + return syslogWriter{w, ""} +} + +// SyslogCEEWriter wraps a SyslogWriter with a SyslogLevelWriter that adds a +// MITRE CEE prefix for JSON syslog entries, compatible with rsyslog +// and syslog-ng JSON logging support. +// See https://www.rsyslog.com/json-elasticsearch/ +func SyslogCEEWriter(w SyslogWriter) LevelWriter { + return syslogWriter{w, ceePrefix} +} + +func (sw syslogWriter) Write(p []byte) (n int, err error) { + var pn int + if sw.prefix != "" { + pn, err = sw.w.Write([]byte(sw.prefix)) + if err != nil { + return pn, err + } + } + n, err = sw.w.Write(p) + return pn + n, err +} + +// WriteLevel implements LevelWriter interface. +func (sw syslogWriter) WriteLevel(level Level, p []byte) (n int, err error) { + switch level { + case TraceLevel: + case DebugLevel: + err = sw.w.Debug(sw.prefix + string(p)) + case InfoLevel: + err = sw.w.Info(sw.prefix + string(p)) + case WarnLevel: + err = sw.w.Warning(sw.prefix + string(p)) + case ErrorLevel: + err = sw.w.Err(sw.prefix + string(p)) + case FatalLevel: + err = sw.w.Emerg(sw.prefix + string(p)) + case PanicLevel: + err = sw.w.Crit(sw.prefix + string(p)) + case NoLevel: + err = sw.w.Info(sw.prefix + string(p)) + default: + panic("invalid level") + } + // Any CEE prefix is not part of the message, so we don't include its length + n = len(p) + return +} diff --git a/vendor/github.com/rs/zerolog/writer.go b/vendor/github.com/rs/zerolog/writer.go new file mode 100644 index 0000000..26f5e63 --- /dev/null +++ b/vendor/github.com/rs/zerolog/writer.go @@ -0,0 +1,154 @@ +package zerolog + +import ( + "bytes" + "io" + "path" + "runtime" + "strconv" + "strings" + "sync" +) + +// LevelWriter defines as interface a writer may implement in order +// to receive level information with payload. +type LevelWriter interface { + io.Writer + WriteLevel(level Level, p []byte) (n int, err error) +} + +type levelWriterAdapter struct { + io.Writer +} + +func (lw levelWriterAdapter) WriteLevel(l Level, p []byte) (n int, err error) { + return lw.Write(p) +} + +type syncWriter struct { + mu sync.Mutex + lw LevelWriter +} + +// SyncWriter wraps w so that each call to Write is synchronized with a mutex. +// This syncer can be used to wrap the call to writer's Write method if it is +// not thread safe. Note that you do not need this wrapper for os.File Write +// operations on POSIX and Windows systems as they are already thread-safe. +func SyncWriter(w io.Writer) io.Writer { + if lw, ok := w.(LevelWriter); ok { + return &syncWriter{lw: lw} + } + return &syncWriter{lw: levelWriterAdapter{w}} +} + +// Write implements the io.Writer interface. +func (s *syncWriter) Write(p []byte) (n int, err error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.lw.Write(p) +} + +// WriteLevel implements the LevelWriter interface. +func (s *syncWriter) WriteLevel(l Level, p []byte) (n int, err error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.lw.WriteLevel(l, p) +} + +type multiLevelWriter struct { + writers []LevelWriter +} + +func (t multiLevelWriter) Write(p []byte) (n int, err error) { + for _, w := range t.writers { + if _n, _err := w.Write(p); err == nil { + n = _n + if _err != nil { + err = _err + } else if _n != len(p) { + err = io.ErrShortWrite + } + } + } + return n, err +} + +func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) { + for _, w := range t.writers { + if _n, _err := w.WriteLevel(l, p); err == nil { + n = _n + if _err != nil { + err = _err + } else if _n != len(p) { + err = io.ErrShortWrite + } + } + } + return n, err +} + +// MultiLevelWriter creates a writer that duplicates its writes to all the +// provided writers, similar to the Unix tee(1) command. If some writers +// implement LevelWriter, their WriteLevel method will be used instead of Write. +func MultiLevelWriter(writers ...io.Writer) LevelWriter { + lwriters := make([]LevelWriter, 0, len(writers)) + for _, w := range writers { + if lw, ok := w.(LevelWriter); ok { + lwriters = append(lwriters, lw) + } else { + lwriters = append(lwriters, levelWriterAdapter{w}) + } + } + return multiLevelWriter{lwriters} +} + +// TestingLog is the logging interface of testing.TB. +type TestingLog interface { + Log(args ...interface{}) + Logf(format string, args ...interface{}) + Helper() +} + +// TestWriter is a writer that writes to testing.TB. +type TestWriter struct { + T TestingLog + + // Frame skips caller frames to capture the original file and line numbers. + Frame int +} + +// NewTestWriter creates a writer that logs to the testing.TB. +func NewTestWriter(t TestingLog) TestWriter { + return TestWriter{T: t} +} + +// Write to testing.TB. +func (t TestWriter) Write(p []byte) (n int, err error) { + t.T.Helper() + + n = len(p) + + // Strip trailing newline because t.Log always adds one. + p = bytes.TrimRight(p, "\n") + + // Try to correct the log file and line number to the caller. + if t.Frame > 0 { + _, origFile, origLine, _ := runtime.Caller(1) + _, frameFile, frameLine, ok := runtime.Caller(1 + t.Frame) + if ok { + erase := strings.Repeat("\b", len(path.Base(origFile))+len(strconv.Itoa(origLine))+3) + t.T.Logf("%s%s:%d: %s", erase, path.Base(frameFile), frameLine, p) + return n, err + } + } + t.T.Log(string(p)) + + return n, err +} + +// ConsoleTestWriter creates an option that correctly sets the file frame depth for testing.TB log. +func ConsoleTestWriter(t TestingLog) func(w *ConsoleWriter) { + return func(w *ConsoleWriter) { + w.Out = TestWriter{T: t, Frame: 6} + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/.gitignore b/vendor/gopkg.in/tucnak/telebot.v2/.gitignore new file mode 100644 index 0000000..c81da31 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/.gitignore @@ -0,0 +1,34 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof + +.idea +.DS_Store +coverage.txt + +# Terraform artifacts +*.zip +.terraform* +terraform* +/examples/awslambdaechobot/awslambdaechobot diff --git a/vendor/gopkg.in/tucnak/telebot.v2/.travis.yml b/vendor/gopkg.in/tucnak/telebot.v2/.travis.yml new file mode 100644 index 0000000..569d3ea --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/.travis.yml @@ -0,0 +1,13 @@ +language: go + +go: + - 1.13.x + +install: + - go get -t -v + +script: + - go test -coverprofile=coverage.txt -covermode=atomic + +after_success: + - if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash <(curl -s https://codecov.io/bash); fi diff --git a/vendor/gopkg.in/tucnak/telebot.v2/LICENSE b/vendor/gopkg.in/tucnak/telebot.v2/LICENSE new file mode 100644 index 0000000..2965b84 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 llya Kowalewski + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/gopkg.in/tucnak/telebot.v2/README.md b/vendor/gopkg.in/tucnak/telebot.v2/README.md new file mode 100644 index 0000000..1fdcf94 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/README.md @@ -0,0 +1,474 @@ +# Telebot +>"I never knew creating Telegram bots could be so _sexy_!" + +[![GoDoc](https://godoc.org/gopkg.in/tucnak/telebot.v2?status.svg)](https://godoc.org/gopkg.in/tucnak/telebot.v2) +[![Travis](https://travis-ci.org/tucnak/telebot.svg?branch=v2)](https://travis-ci.org/tucnak/telebot) +[![codecov.io](https://codecov.io/gh/tucnak/telebot/coverage.svg?branch=develop)](https://codecov.io/gh/tucnak/telebot) +[![Discuss on Telegram](https://img.shields.io/badge/telegram-discuss-0088cc.svg)](https://t.me/go_telebot) + +```bash +go get -u gopkg.in/tucnak/telebot.v2 +``` + +* [Overview](#overview) +* [Getting Started](#getting-started) + - [Poller](#poller) + - [Commands](#commands) + - [Files](#files) + - [Sendable](#sendable) + - [Editable](#editable) + - [Keyboards](#keyboards) + - [Inline mode](#inline-mode) +* [Contributing](#contributing) +* [Donate](#donate) +* [License](#license) + +# Overview +Telebot is a bot framework for [Telegram Bot API](https://core.telegram.org/bots/api). +This package provides the best of its kind API for command routing, inline query requests and keyboards, as well +as callbacks. Actually, I went a couple steps further, so instead of making a 1:1 API wrapper I chose to focus on +the beauty of API and performance. Some of the strong sides of telebot are: + +* Real concise API +* Command routing +* Middleware +* Transparent File API +* Effortless bot callbacks + +All the methods of telebot API are _extremely_ easy to memorize and get used to. Also, consider Telebot a +highload-ready solution. I'll test and benchmark the most popular actions and if necessary, optimize +against them without sacrificing API quality. + +# Getting Started +Let's take a look at the minimal telebot setup: +```go +package main + +import ( + "log" + "time" + + tb "gopkg.in/tucnak/telebot.v2" +) + +func main() { + b, err := tb.NewBot(tb.Settings{ + // You can also set custom API URL. + // If field is empty it equals to "https://api.telegram.org". + URL: "http://195.129.111.17:8012", + + Token: "TOKEN_HERE", + Poller: &tb.LongPoller{Timeout: 10 * time.Second}, + }) + + if err != nil { + log.Fatal(err) + return + } + + b.Handle("/hello", func(m *tb.Message) { + b.Send(m.Sender, "Hello World!") + }) + + b.Start() +} + +``` + +Simple, innit? Telebot's routing system takes care of delivering updates +to their endpoints, so in order to get to handle any meaningful event, +all you got to do is just plug your function to one of the Telebot-provided +endpoints. You can find the full list +[here](https://godoc.org/gopkg.in/tucnak/telebot.v2#pkg-constants). + +```go +b, _ := tb.NewBot(settings) + +b.Handle(tb.OnText, func(m *tb.Message) { + // all the text messages that weren't + // captured by existing handlers +}) + +b.Handle(tb.OnPhoto, func(m *tb.Message) { + // photos only +}) + +b.Handle(tb.OnChannelPost, func (m *tb.Message) { + // channel posts only +}) + +b.Handle(tb.OnQuery, func (q *tb.Query) { + // incoming inline queries +}) +``` + +There's dozens of supported endpoints (see package consts). Let me know +if you'd like to see some endpoint or endpoint idea implemented. This system +is completely extensible, so I can introduce them without breaking +backwards-compatibility. + +## Poller +Telebot doesn't really care how you provide it with incoming updates, as long +as you set it up with a Poller, or call ProcessUpdate for each update (see +[examples/awslambdaechobot](examples/awslambdaechobot)): + +```go +// Poller is a provider of Updates. +// +// All pollers must implement Poll(), which accepts bot +// pointer and subscription channel and start polling +// synchronously straight away. +type Poller interface { + // Poll is supposed to take the bot object + // subscription channel and start polling + // for Updates immediately. + // + // Poller must listen for stop constantly and close + // it as soon as it's done polling. + Poll(b *Bot, updates chan Update, stop chan struct{}) +} +``` + +Telegram Bot API supports long polling and webhook integration. Poller means you +can plug telebot into whatever existing bot infrastructure (load balancers?) you +need, if you need to. Another great thing about pollers is that you can chain +them, making some sort of middleware: +```go +poller := &tb.LongPoller{Timeout: 15 * time.Second} +spamProtected := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool { + if upd.Message == nil { + return true + } + + if strings.Contains(upd.Message.Text, "spam") { + return false + } + + return true +}) + +bot, _ := tb.NewBot(tb.Settings{ + // ... + Poller: spamProtected, +}) + +// graceful shutdown +time.AfterFunc(N * time.Second, b.Stop) + +// blocks until shutdown +bot.Start() + +fmt.Println(poller.LastUpdateID) // 134237 +``` + +## Commands +When handling commands, Telebot supports both direct (`/command`) and group-like +syntax (`/command@botname`) and will never deliver messages addressed to some +other bot, even if [privacy mode](https://core.telegram.org/bots#privacy-mode) is off. +For simplified deep-linking, telebot also extracts payload: +```go +// Command: /start +b.Handle("/start", func(m *tb.Message) { + if !m.Private() { + return + } + + fmt.Println(m.Payload) // +}) +``` + +## Files +>Telegram allows files up to 20 MB in size. + +Telebot allows to both upload (from disk / by URL) and download (from Telegram) +and files in bot's scope. Also, sending any kind of media with a File created +from disk will upload the file to Telegram automatically: +```go +a := &tb.Audio{File: tb.FromDisk("file.ogg")} + +fmt.Println(a.OnDisk()) // true +fmt.Println(a.InCloud()) // false + +// Will upload the file from disk and send it to recipient +bot.Send(recipient, a) + +// Next time you'll be sending this very *Audio, Telebot won't +// re-upload the same file but rather utilize its Telegram FileID +bot.Send(otherRecipient, a) + +fmt.Println(a.OnDisk()) // true +fmt.Println(a.InCloud()) // true +fmt.Println(a.FileID) // +``` + +You might want to save certain `File`s in order to avoid re-uploading. Feel free +to marshal them into whatever format, `File` only contain public fields, so no +data will ever be lost. + +## Sendable +Send is undoubtedly the most important method in Telebot. `Send()` accepts a +`Recipient` (could be user, group or a channel) and a `Sendable`. Other types other than +the telebot-provided media types (`Photo`, `Audio`, `Video`, etc.) are `Sendable`. +If you create composite types of your own, and they satisfy the `Sendable` interface, +Telebot will be able to send them out. + +```go +// Sendable is any object that can send itself. +// +// This is pretty cool, since it lets bots implement +// custom Sendables for complex kinds of media or +// chat objects spanning across multiple messages. +type Sendable interface { + Send(*Bot, Recipient, *SendOptions) (*Message, error) +} +``` + +The only type at the time that doesn't fit `Send()` is `Album` and there is a reason +for that. Albums were added not so long ago, so they are slightly quirky for backwards +compatibilities sake. In fact, an `Album` can be sent, but never received. Instead, +Telegram returns a `[]Message`, one for each media object in the album: +```go +p := &tb.Photo{File: tb.FromDisk("chicken.jpg")} +v := &tb.Video{File: tb.FromURL("http://video.mp4")} + +msgs, err := b.SendAlbum(user, tb.Album{p, v}) +``` + +### Send options +Send options are objects and flags you can pass to `Send()`, `Edit()` and friends +as optional arguments (following the recipient and the text/media). The most +important one is called `SendOptions`, it lets you control _all_ the properties of +the message supported by Telegram. The only drawback is that it's rather +inconvenient to use at times, so `Send()` supports multiple shorthands: +```go +// regular send options +b.Send(user, "text", &tb.SendOptions{ + // ... +}) + +// ReplyMarkup is a part of SendOptions, +// but often it's the only option you need +b.Send(user, "text", &tb.ReplyMarkup{ + // ... +}) + +// flags: no notification && no web link preview +b.Send(user, "text", tb.Silent, tb.NoPreview) +``` + +Full list of supported option-flags you can find +[here](https://github.com/tucnak/telebot/blob/v2/options.go#L9). + +## Editable +If you want to edit some existing message, you don't really need to store the +original `*Message` object. In fact, upon edit, Telegram only requires `chat_id` +and `message_id`. So you don't really need the Message as the whole. Also you +might want to store references to certain messages in the database, so I thought +it made sense for *any* Go struct to be editable as a Telegram message, to implement +`Editable`: +```go +// Editable is an interface for all objects that +// provide "message signature", a pair of 32-bit +// message ID and 64-bit chat ID, both required +// for edit operations. +// +// Use case: DB model struct for messages to-be +// edited with, say two columns: msg_id,chat_id +// could easily implement MessageSig() making +// instances of stored messages editable. +type Editable interface { + // MessageSig is a "message signature". + // + // For inline messages, return chatID = 0. + MessageSig() (messageID string, chatID int64) +} +``` + +For example, `Message` type is Editable. Here is the implementation of `StoredMessage` +type, provided by telebot: +```go +// StoredMessage is an example struct suitable for being +// stored in the database as-is or being embedded into +// a larger struct, which is often the case (you might +// want to store some metadata alongside, or might not.) +type StoredMessage struct { + MessageID string `sql:"message_id" json:"message_id"` + ChatID int64 `sql:"chat_id" json:"chat_id"` +} + +func (x StoredMessage) MessageSig() (string, int64) { + return x.MessageID, x.ChatID +} +``` + +Why bother at all? Well, it allows you to do things like this: +```go +// just two integer columns in the database +var msgs []tb.StoredMessage +db.Find(&msgs) // gorm syntax + +for _, msg := range msgs { + bot.Edit(&msg, "Updated text") + // or + bot.Delete(&msg) +} +``` + +I find it incredibly neat. Worth noting, at this point of time there exists +another method in the Edit family, `EditCaption()` which is of a pretty +rare use, so I didn't bother including it to `Edit()`, just like I did with +`SendAlbum()` as it would inevitably lead to unnecessary complications. +```go +var m *Message + +// change caption of a photo, audio, etc. +bot.EditCaption(m, "new caption") +``` + +## Keyboards +Telebot supports both kinds of keyboards Telegram provides: reply and inline +keyboards. Any button can also act as an endpoints for `Handle()`. + +In `v2.2` we're introducing a little more convenient way in building keyboards. +The main goal is to avoid a lot of boilerplate and to make code clearer. + +```go +func main() { + b, _ := tb.NewBot(tb.Settings{...}) + + var ( + // Universal markup builders. + menu = &tb.ReplyMarkup{ResizeReplyKeyboard: true} + selector = &tb.ReplyMarkup{} + + // Reply buttons. + btnHelp = menu.Text("ℹ Help") + btnSettings = menu.Text("⚙ Settings") + + // Inline buttons. + // + // Pressing it will cause the client to + // send the bot a callback. + // + // Make sure Unique stays unique as per button kind, + // as it has to be for callback routing to work. + // + btnPrev = selector.Data("⬅", "prev", ...) + btnNext = selector.Data("➡", "next", ...) + ) + + menu.Reply( + menu.Row(btnHelp), + menu.Row(btnSettings), + ) + selector.Inline( + selector.Row(btnPrev, btnNext), + ) + + // Command: /start + b.Handle("/start", func(m *tb.Message) { + if !m.Private() { + return + } + + b.Send(m.Sender, "Hello!", menu) + }) + + // On reply button pressed (message) + b.Handle(&btnHelp, func(m *tb.Message) {...}) + + // On inline button pressed (callback) + b.Handle(&btnPrev, func(c *tb.Callback) { + // ... + // Always respond! + b.Respond(c, &tb.CallbackResponse{...}) + }) + + b.Start() +} +``` + +You can use markup constructor for every type of possible buttons: +```go +r := &tb.ReplyMarkup{} + +// Reply buttons: +r.Text("Hello!") +r.Contact("Send phone number") +r.Location("Send location") +r.Poll(tb.PollQuiz) + +// Inline buttons: +r.Data("Show help", "help") // data is optional +r.Data("Delete item", "delete", item.ID) +r.URL("Visit", "https://google.com") +r.Query("Search", query) +r.QueryChat("Share", query) +r.Login("Login", &tb.Login{...}) +``` + +## Inline mode +So if you want to handle incoming inline queries you better plug the `tb.OnQuery` +endpoint and then use the `Answer()` method to send a list of inline queries +back. I think at the time of writing, telebot supports all of the provided result +types (but not the cached ones). This is how it looks like: + +```go +b.Handle(tb.OnQuery, func(q *tb.Query) { + urls := []string{ + "http://photo.jpg", + "http://photo2.jpg", + } + + results := make(tb.Results, len(urls)) // []tb.Result + for i, url := range urls { + result := &tb.PhotoResult{ + URL: url, + + // required for photos + ThumbURL: url, + } + + results[i] = result + // needed to set a unique string ID for each result + results[i].SetResultID(strconv.Itoa(i)) + } + + err := b.Answer(q, &tb.QueryResponse{ + Results: results, + CacheTime: 60, // a minute + }) + + if err != nil { + log.Println(err) + } +}) +``` + +There's not much to talk about really. It also supports some form of authentication +through deep-linking. For that, use fields `SwitchPMText` and `SwitchPMParameter` +of `QueryResponse`. + +# Contributing + +1. Fork it +2. Clone develop: `git clone -b develop https://github.com/tucnak/telebot` +3. Create your feature branch: `git checkout -b new-feature` +4. Make changes and add them: `git add .` +5. Commit: `git commit -m "Add some feature"` +6. Push: `git push origin new-feature` +7. Pull request + +# Donate + +I do coding for fun but I also try to search for interesting solutions and +optimize them as much as possible. +If you feel like it's a good piece of software, I wouldn't mind a tip! + +Litecoin: `ltc1qskt5ltrtyg7esfjm0ftx6jnacwffhpzpqmerus` + +Ethereum: `0xB78A2Ac1D83a0aD0b993046F9fDEfC5e619efCAB` + +# License + +Telebot is distributed under MIT. diff --git a/vendor/gopkg.in/tucnak/telebot.v2/admin.go b/vendor/gopkg.in/tucnak/telebot.v2/admin.go new file mode 100644 index 0000000..21faed6 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/admin.go @@ -0,0 +1,275 @@ +package telebot + +import ( + "encoding/json" + "strconv" + "time" +) + +// ChatInviteLink object represents an invite for a chat. +type ChatInviteLink struct { + // The invite link. + InviteLink string `json:"invite_link"` + + // The creator of the link. + Creator *User `json:"creator"` + + // If the link is primary. + IsPrimary bool `json:"is_primary"` + + // If the link is revoked. + IsRevoked bool `json:"is_revoked"` + + // (Optional) Point in time when the link will expire, use + // ChatInviteLink.ExpireDate() to get time.Time + ExpireUnixtime int64 `json:"expire_date,omitempty"` + + // (Optional) Maximum number of users that can be members of + // the chat simultaneously. + MemberLimit int `json:"member_limit,omitempty"` +} + +// ExpireDate returns the moment of the link expiration in local time. +func (c *ChatInviteLink) ExpireDate() time.Time { + return time.Unix(c.ExpireUnixtime, 0) +} + +// ChatMemberUpdated object represents changes in the status of a chat member. +type ChatMemberUpdated struct { + // Chat where the user belongs to. + Chat Chat `json:"chat"` + + // From which user the action was triggered. + From User `json:"from"` + + // Unixtime, use ChatMemberUpdated.Time() to get time.Time + Unixtime int64 `json:"date"` + + // Previous information about the chat member. + OldChatMember *ChatMember `json:"old_chat_member"` + + // New information about the chat member. + NewChatMember *ChatMember `json:"new_chat_member"` + + // (Optional) InviteLink which was used by the user to + // join the chat; for joining by invite link events only. + InviteLink *ChatInviteLink `json:"invite_link"` +} + +// Time returns the moment of the change in local time. +func (c *ChatMemberUpdated) Time() time.Time { + return time.Unix(c.Unixtime, 0) +} + +// Rights is a list of privileges available to chat members. +type Rights struct { + CanBeEdited bool `json:"can_be_edited"` + CanChangeInfo bool `json:"can_change_info"` + CanPostMessages bool `json:"can_post_messages"` + CanEditMessages bool `json:"can_edit_messages"` + CanDeleteMessages bool `json:"can_delete_messages"` + CanInviteUsers bool `json:"can_invite_users"` + CanRestrictMembers bool `json:"can_restrict_members"` + CanPinMessages bool `json:"can_pin_messages"` + CanPromoteMembers bool `json:"can_promote_members"` + CanSendMessages bool `json:"can_send_messages"` + CanSendMedia bool `json:"can_send_media_messages"` + CanSendPolls bool `json:"can_send_polls"` + CanSendOther bool `json:"can_send_other_messages"` + CanAddPreviews bool `json:"can_add_web_page_previews"` + CanManageVoiceChats bool `json:"can_manage_voice_chats"` + CanManageChat bool `json:"can_manage_chat"` +} + +// NoRights is the default Rights{}. +func NoRights() Rights { return Rights{} } + +// NoRestrictions should be used when un-restricting or +// un-promoting user. +// +// member.Rights = tb.NoRestrictions() +// bot.Restrict(chat, member) +// +func NoRestrictions() Rights { + return Rights{ + CanBeEdited: true, + CanChangeInfo: false, + CanPostMessages: false, + CanEditMessages: false, + CanDeleteMessages: false, + CanInviteUsers: false, + CanRestrictMembers: false, + CanPinMessages: false, + CanPromoteMembers: false, + CanSendMessages: true, + CanSendMedia: true, + CanSendPolls: true, + CanSendOther: true, + CanAddPreviews: true, + CanManageVoiceChats: false, + CanManageChat: false, + } +} + +// AdminRights could be used to promote user to admin. +func AdminRights() Rights { + return Rights{ + CanBeEdited: true, + CanChangeInfo: true, + CanPostMessages: true, + CanEditMessages: true, + CanDeleteMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPinMessages: true, + CanPromoteMembers: true, + CanSendMessages: true, + CanSendMedia: true, + CanSendPolls: true, + CanSendOther: true, + CanAddPreviews: true, + CanManageVoiceChats: true, + CanManageChat: true, + } +} + +// Forever is a ExpireUnixtime of "forever" banning. +func Forever() int64 { + return time.Now().Add(367 * 24 * time.Hour).Unix() +} + +// Ban will ban user from chat until `member.RestrictedUntil`. +func (b *Bot) Ban(chat *Chat, member *ChatMember, revokeMessages ...bool) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": member.User.Recipient(), + "until_date": strconv.FormatInt(member.RestrictedUntil, 10), + } + if len(revokeMessages) > 0 { + params["revoke_messages"] = strconv.FormatBool(revokeMessages[0]) + } + + _, err := b.Raw("kickChatMember", params) + return err +} + +// Unban will unban user from chat, who would have thought eh? +// forBanned does nothing if the user is not banned. +func (b *Bot) Unban(chat *Chat, user *User, forBanned ...bool) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": user.Recipient(), + } + + if len(forBanned) > 0 { + params["only_if_banned"] = strconv.FormatBool(forBanned[0]) + } + + _, err := b.Raw("unbanChatMember", params) + return err +} + +// Restrict lets you restrict a subset of member's rights until +// member.RestrictedUntil, such as: +// +// * can send messages +// * can send media +// * can send other +// * can add web page previews +// +func (b *Bot) Restrict(chat *Chat, member *ChatMember) error { + prv, until := member.Rights, member.RestrictedUntil + + params := map[string]interface{}{ + "chat_id": chat.Recipient(), + "user_id": member.User.Recipient(), + "until_date": strconv.FormatInt(until, 10), + } + embedRights(params, prv) + + _, err := b.Raw("restrictChatMember", params) + return err +} + +// Promote lets you update member's admin rights, such as: +// +// * can change info +// * can post messages +// * can edit messages +// * can delete messages +// * can invite users +// * can restrict members +// * can pin messages +// * can promote members +// +func (b *Bot) Promote(chat *Chat, member *ChatMember) error { + prv := member.Rights + + params := map[string]interface{}{ + "chat_id": chat.Recipient(), + "user_id": member.User.Recipient(), + "is_anonymous": member.Anonymous, + } + embedRights(params, prv) + + _, err := b.Raw("promoteChatMember", params) + return err +} + +// AdminsOf returns a member list of chat admins. +// +// On success, returns an Array of ChatMember objects that +// contains information about all chat administrators except other bots. +// If the chat is a group or a supergroup and +// no administrators were appointed, only the creator will be returned. +func (b *Bot) AdminsOf(chat *Chat) ([]ChatMember, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + data, err := b.Raw("getChatAdministrators", params) + if err != nil { + return nil, err + } + + var resp struct { + Result []ChatMember + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} + +// Len returns the number of members in a chat. +func (b *Bot) Len(chat *Chat) (int, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + data, err := b.Raw("getChatMembersCount", params) + if err != nil { + return 0, err + } + + var resp struct { + Result int + } + if err := json.Unmarshal(data, &resp); err != nil { + return 0, wrapError(err) + } + return resp.Result, nil +} + +// SetAdminTitle sets a custom title for an administrator. +// A title should be 0-16 characters length, emoji are not allowed. +func (b *Bot) SetAdminTitle(chat *Chat, user *User, title string) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": user.Recipient(), + "custom_title": title, + } + + _, err := b.Raw("setChatAdministratorCustomTitle", params) + return err +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/api.go b/vendor/gopkg.in/tucnak/telebot.v2/api.go new file mode 100644 index 0000000..e20d7ce --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/api.go @@ -0,0 +1,232 @@ +package telebot + +import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "log" + "mime/multipart" + "net/http" + "os" + "strconv" + "strings" + "time" + + "github.com/pkg/errors" +) + +// Raw lets you call any method of Bot API manually. +// It also handles API errors, so you only need to unwrap +// result field from json data. +func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) { + url := b.URL + "/bot" + b.Token + "/" + method + + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(payload); err != nil { + return nil, err + } + + resp, err := b.client.Post(url, "application/json", &buf) + if err != nil { + return nil, wrapError(err) + } + resp.Close = true + defer resp.Body.Close() + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, wrapError(err) + } + + if b.verbose { + body, _ := json.Marshal(payload) + body = bytes.ReplaceAll(body, []byte(`\"`), []byte(`"`)) + body = bytes.ReplaceAll(body, []byte(`"{`), []byte(`{`)) + body = bytes.ReplaceAll(body, []byte(`}"`), []byte(`}`)) + + indent := func(b []byte) string { + buf.Reset() + json.Indent(&buf, b, "", "\t") + return buf.String() + } + + log.Printf("[verbose] telebot: sent request\n"+ + "Method: %v\nParams: %v\nResponse: %v", + method, indent(body), indent(data)) + } + + // returning data as well + return data, extractOk(data) +} + +func (b *Bot) sendFiles(method string, files map[string]File, params map[string]string) ([]byte, error) { + rawFiles := map[string]interface{}{} + for name, f := range files { + switch { + case f.InCloud(): + params[name] = f.FileID + case f.FileURL != "": + params[name] = f.FileURL + case f.OnDisk(): + rawFiles[name] = f.FileLocal + case f.FileReader != nil: + rawFiles[name] = f.FileReader + default: + return nil, errors.Errorf("telebot: File for field %s doesn't exist", name) + } + } + + if len(rawFiles) == 0 { + return b.Raw(method, params) + } + + pipeReader, pipeWriter := io.Pipe() + writer := multipart.NewWriter(pipeWriter) + + go func() { + defer pipeWriter.Close() + + for field, file := range rawFiles { + if err := addFileToWriter(writer, files[field].fileName, field, file); err != nil { + pipeWriter.CloseWithError(err) + return + } + } + for field, value := range params { + if err := writer.WriteField(field, value); err != nil { + pipeWriter.CloseWithError(err) + return + } + } + if err := writer.Close(); err != nil { + pipeWriter.CloseWithError(err) + return + } + }() + + url := b.URL + "/bot" + b.Token + "/" + method + + resp, err := b.client.Post(url, writer.FormDataContentType(), pipeReader) + if err != nil { + err = wrapError(err) + pipeReader.CloseWithError(err) + return nil, err + } + resp.Close = true + defer resp.Body.Close() + + if resp.StatusCode == http.StatusInternalServerError { + return nil, ErrInternal + } + + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, wrapError(err) + } + + return data, extractOk(data) +} + +func addFileToWriter(writer *multipart.Writer, filename, field string, file interface{}) error { + var reader io.Reader + if r, ok := file.(io.Reader); ok { + reader = r + } else if path, ok := file.(string); ok { + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + reader = f + } else { + return errors.Errorf("telebot: File for field %v should be an io.ReadCloser or string", field) + } + + part, err := writer.CreateFormFile(field, filename) + if err != nil { + return err + } + + _, err = io.Copy(part, reader) + return err +} + +func (b *Bot) sendText(to Recipient, text string, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "text": text, + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendMessage", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +func (b *Bot) sendObject(f *File, what string, params map[string]string, files map[string]File) (*Message, error) { + sendWhat := "send" + strings.Title(what) + + if what == "videoNote" { + what = "video_note" + } + + sendFiles := map[string]File{what: *f} + for k, v := range files { + sendFiles[k] = v + } + + data, err := b.sendFiles(sendWhat, sendFiles, params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +func (b *Bot) getMe() (*User, error) { + data, err := b.Raw("getMe", nil) + if err != nil { + return nil, err + } + + var resp struct { + Result *User + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil + +} + +func (b *Bot) getUpdates(offset, limit int, timeout time.Duration, allowed []string) ([]Update, error) { + params := map[string]string{ + "offset": strconv.Itoa(offset), + "timeout": strconv.Itoa(int(timeout / time.Second)), + } + + if limit != 0 { + params["limit"] = strconv.Itoa(limit) + } + if len(allowed) > 0 { + data, _ := json.Marshal(allowed) + params["allowed_updates"] = string(data) + } + + data, err := b.Raw("getUpdates", params) + if err != nil { + return nil, err + } + + var resp struct { + Result []Update + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/bot.go b/vendor/gopkg.in/tucnak/telebot.v2/bot.go new file mode 100644 index 0000000..aad7e34 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/bot.go @@ -0,0 +1,1779 @@ +package telebot + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path" + "regexp" + "strconv" + "strings" + + "github.com/pkg/errors" +) + +// NewBot does try to build a Bot with token `token`, which +// is a secret API key assigned to particular bot. +func NewBot(pref Settings) (*Bot, error) { + if pref.Updates == 0 { + pref.Updates = 100 + } + + client := pref.Client + if client == nil { + client = http.DefaultClient + } + + if pref.URL == "" { + pref.URL = DefaultApiURL + } + + bot := &Bot{ + Token: pref.Token, + URL: pref.URL, + Updates: make(chan Update, pref.Updates), + Poller: pref.Poller, + + handlers: make(map[string]interface{}), + synchronous: pref.Synchronous, + verbose: pref.Verbose, + parseMode: pref.ParseMode, + stop: make(chan struct{}), + reporter: pref.Reporter, + client: client, + } + + if pref.Offline { + bot.Me = &User{} + } else { + user, err := bot.getMe() + if err != nil { + return nil, err + } + bot.Me = user + } + + return bot, nil +} + +// Bot represents a separate Telegram bot instance. +type Bot struct { + Me *User + Token string + URL string + Updates chan Update + Poller Poller + + handlers map[string]interface{} + synchronous bool + verbose bool + parseMode ParseMode + reporter func(error) + stop chan struct{} + client *http.Client +} + +// Settings represents a utility struct for passing certain +// properties of a bot around and is required to make bots. +type Settings struct { + // Telegram API Url + URL string + + // Telegram token + Token string + + // Updates channel capacity + Updates int // Default: 100 + + // Poller is the provider of Updates. + Poller Poller + + // Synchronous prevents handlers from running in parallel. + // It makes ProcessUpdate return after the handler is finished. + Synchronous bool + + // Verbose forces bot to log all upcoming requests. + // Use for debugging purposes only. + Verbose bool + + // ParseMode used to set default parse mode of all sent messages. + // It attaches to every send, edit or whatever method. You also + // will be able to override the default mode by passing a new one. + ParseMode ParseMode + + // Reporter is a callback function that will get called + // on any panics recovered from endpoint handlers. + Reporter func(error) + + // HTTP Client used to make requests to telegram api + Client *http.Client + + // Offline allows to create a bot without network for testing purposes. + Offline bool +} + +// Update object represents an incoming update. +type Update struct { + ID int `json:"update_id"` + + Message *Message `json:"message,omitempty"` + EditedMessage *Message `json:"edited_message,omitempty"` + ChannelPost *Message `json:"channel_post,omitempty"` + EditedChannelPost *Message `json:"edited_channel_post,omitempty"` + Callback *Callback `json:"callback_query,omitempty"` + Query *Query `json:"inline_query,omitempty"` + ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,omitempty"` + ShippingQuery *ShippingQuery `json:"shipping_query,omitempty"` + PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"` + Poll *Poll `json:"poll,omitempty"` + PollAnswer *PollAnswer `json:"poll_answer,omitempty"` + MyChatMember *ChatMemberUpdated `json:"my_chat_member,omitempty"` + ChatMember *ChatMemberUpdated `json:"chat_member,omitempty"` +} + +// Command represents a bot command. +type Command struct { + // Text is a text of the command, 1-32 characters. + // Can contain only lowercase English letters, digits and underscores. + Text string `json:"command"` + + // Description of the command, 3-256 characters. + Description string `json:"description"` +} + +// Handle lets you set the handler for some command name or +// one of the supported endpoints. +// +// Example: +// +// b.Handle("/help", func (m *tb.Message) {}) +// b.Handle(tb.OnText, func (m *tb.Message) {}) +// b.Handle(tb.OnQuery, func (q *tb.Query) {}) +// +// // make a hook for one of your preserved (by-pointer) inline buttons. +// b.Handle(&inlineButton, func (c *tb.Callback) {}) +// +func (b *Bot) Handle(endpoint interface{}, handler interface{}) { + switch end := endpoint.(type) { + case string: + b.handlers[end] = handler + case CallbackEndpoint: + b.handlers[end.CallbackUnique()] = handler + default: + panic("telebot: unsupported endpoint") + } +} + +var ( + cmdRx = regexp.MustCompile(`^(/\w+)(@(\w+))?(\s|$)(.+)?`) + cbackRx = regexp.MustCompile(`^\f([-\w]+)(\|(.+))?$`) +) + +// Start brings bot into motion by consuming incoming +// updates (see Bot.Updates channel). +func (b *Bot) Start() { + if b.Poller == nil { + panic("telebot: can't start without a poller") + } + + stop := make(chan struct{}) + go b.Poller.Poll(b, b.Updates, stop) + + for { + select { + // handle incoming updates + case upd := <-b.Updates: + b.ProcessUpdate(upd) + // call to stop polling + case <-b.stop: + close(stop) + return + } + } +} + +// Stop gracefully shuts the poller down. +func (b *Bot) Stop() { + b.stop <- struct{}{} +} + +// ProcessUpdate processes a single incoming update. +// A started bot calls this function automatically. +func (b *Bot) ProcessUpdate(upd Update) { + if upd.Message != nil { + m := upd.Message + + if m.PinnedMessage != nil { + b.handle(OnPinned, m) + return + } + + // Commands + if m.Text != "" { + // Filtering malicious messages + if m.Text[0] == '\a' { + return + } + + match := cmdRx.FindAllStringSubmatch(m.Text, -1) + if match != nil { + // Syntax: "@ " + + command, botName := match[0][1], match[0][3] + if botName != "" && !strings.EqualFold(b.Me.Username, botName) { + return + } + + m.Payload = match[0][5] + if b.handle(command, m) { + return + } + } + + // 1:1 satisfaction + if b.handle(m.Text, m) { + return + } + + b.handle(OnText, m) + return + } + + if b.handleMedia(m) { + return + } + + if m.Invoice != nil { + b.handle(OnInvoice, m) + return + } + + if m.Payment != nil { + b.handle(OnPayment, m) + return + } + + wasAdded := (m.UserJoined != nil && m.UserJoined.ID == b.Me.ID) || + (m.UsersJoined != nil && isUserInList(b.Me, m.UsersJoined)) + if m.GroupCreated || m.SuperGroupCreated || wasAdded { + b.handle(OnAddedToGroup, m) + return + } + + if m.UsersJoined != nil { + for index := range m.UsersJoined { + // Shallow copy message to prevent data race in async mode + mm := *m + mm.UserJoined = &m.UsersJoined[index] + b.handle(OnUserJoined, &mm) + } + return + } + + if m.UserJoined != nil { + b.handle(OnUserJoined, m) + return + } + + if m.UserLeft != nil { + b.handle(OnUserLeft, m) + return + } + + if m.NewGroupTitle != "" { + b.handle(OnNewGroupTitle, m) + return + } + + if m.NewGroupPhoto != nil { + b.handle(OnNewGroupPhoto, m) + return + } + + if m.GroupPhotoDeleted { + b.handle(OnGroupPhotoDeleted, m) + return + } + + if m.GroupCreated { + b.handle(OnGroupCreated, m) + return + } + + if m.SuperGroupCreated { + b.handle(OnSuperGroupCreated, m) + return + } + + if m.ChannelCreated { + b.handle(OnChannelCreated, m) + return + } + + if m.MigrateTo != 0 { + if handler, ok := b.handlers[OnMigration]; ok { + handler, ok := handler.(func(int64, int64)) + if !ok { + panic("telebot: migration handler is bad") + } + + b.runHandler(func() { handler(m.Chat.ID, m.MigrateTo) }) + } + + return + } + + if m.VoiceChatStarted != nil { + if handler, ok := b.handlers[OnVoiceChatStarted]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: voice chat started handler is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + + if m.VoiceChatEnded != nil { + if handler, ok := b.handlers[OnVoiceChatEnded]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: voice chat ended handler is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + + if m.VoiceChatParticipantsInvited != nil { + if handler, ok := b.handlers[OnVoiceChatParticipantsInvited]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: voice chat participants invited handler is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + + if m.ProximityAlert != nil { + if handler, ok := b.handlers[OnProximityAlert]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: proximity alert handler is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + + if m.AutoDeleteTimer != nil { + if handler, ok := b.handlers[OnAutoDeleteTimer]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: auto delete timer handler is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + + if m.VoiceChatSchedule != nil { + if handler, ok := b.handlers[OnVoiceChatScheduled]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic("telebot: voice chat scheduled is bad") + } + + b.runHandler(func() { handler(m) }) + } + + return + } + } + + if upd.EditedMessage != nil { + b.handle(OnEdited, upd.EditedMessage) + return + } + + if upd.ChannelPost != nil { + m := upd.ChannelPost + + if m.PinnedMessage != nil { + b.handle(OnPinned, m) + return + } + + b.handle(OnChannelPost, upd.ChannelPost) + return + } + + if upd.EditedChannelPost != nil { + b.handle(OnEditedChannelPost, upd.EditedChannelPost) + return + } + + if upd.Callback != nil { + if upd.Callback.Data != "" { + if upd.Callback.MessageID != "" { + upd.Callback.Message = &Message{ + // InlineID indicates that message + // is inline so we have only its id + InlineID: upd.Callback.MessageID, + } + } + + data := upd.Callback.Data + if data[0] == '\f' { + match := cbackRx.FindAllStringSubmatch(data, -1) + if match != nil { + unique, payload := match[0][1], match[0][3] + + if handler, ok := b.handlers["\f"+unique]; ok { + handler, ok := handler.(func(*Callback)) + if !ok { + panic(fmt.Errorf("telebot: %s callback handler is bad", unique)) + } + + upd.Callback.Data = payload + b.runHandler(func() { handler(upd.Callback) }) + + return + } + } + } + } + + if handler, ok := b.handlers[OnCallback]; ok { + handler, ok := handler.(func(*Callback)) + if !ok { + panic("telebot: callback handler is bad") + } + + b.runHandler(func() { handler(upd.Callback) }) + } + + return + } + + if upd.Query != nil { + if handler, ok := b.handlers[OnQuery]; ok { + handler, ok := handler.(func(*Query)) + if !ok { + panic("telebot: query handler is bad") + } + + b.runHandler(func() { handler(upd.Query) }) + } + + return + } + + if upd.ChosenInlineResult != nil { + if handler, ok := b.handlers[OnChosenInlineResult]; ok { + handler, ok := handler.(func(*ChosenInlineResult)) + if !ok { + panic("telebot: chosen inline result handler is bad") + } + + b.runHandler(func() { handler(upd.ChosenInlineResult) }) + } + + return + } + + if upd.ShippingQuery != nil { + if handler, ok := b.handlers[OnShipping]; ok { + handler, ok := handler.(func(*ShippingQuery)) + if !ok { + panic("telebot: shipping query handler is bad") + } + + b.runHandler(func() { handler(upd.ShippingQuery) }) + } + + return + } + + if upd.PreCheckoutQuery != nil { + if handler, ok := b.handlers[OnCheckout]; ok { + handler, ok := handler.(func(*PreCheckoutQuery)) + if !ok { + panic("telebot: pre checkout query handler is bad") + } + + b.runHandler(func() { handler(upd.PreCheckoutQuery) }) + } + + return + } + + if upd.Poll != nil { + if handler, ok := b.handlers[OnPoll]; ok { + handler, ok := handler.(func(*Poll)) + if !ok { + panic("telebot: poll handler is bad") + } + + b.runHandler(func() { handler(upd.Poll) }) + } + + return + } + + if upd.PollAnswer != nil { + if handler, ok := b.handlers[OnPollAnswer]; ok { + handler, ok := handler.(func(*PollAnswer)) + if !ok { + panic("telebot: poll answer handler is bad") + } + + b.runHandler(func() { handler(upd.PollAnswer) }) + } + + return + } + + if upd.MyChatMember != nil { + if handler, ok := b.handlers[OnMyChatMember]; ok { + handler, ok := handler.(func(*ChatMemberUpdated)) + if !ok { + panic("telebot: my chat member handler is bad") + } + + b.runHandler(func() { handler(upd.MyChatMember) }) + } + + return + } + + if upd.ChatMember != nil { + if handler, ok := b.handlers[OnChatMember]; ok { + handler, ok := handler.(func(*ChatMemberUpdated)) + if !ok { + panic("telebot: chat member handler is bad") + } + + b.runHandler(func() { handler(upd.ChatMember) }) + } + + return + } +} + +func (b *Bot) handle(end string, m *Message) bool { + if handler, ok := b.handlers[end]; ok { + handler, ok := handler.(func(*Message)) + if !ok { + panic(fmt.Errorf("telebot: %s handler is bad", end)) + } + + b.runHandler(func() { handler(m) }) + + return true + } + + return false +} + +func (b *Bot) handleMedia(m *Message) bool { + switch { + case m.Photo != nil: + b.handle(OnPhoto, m) + case m.Voice != nil: + b.handle(OnVoice, m) + case m.Audio != nil: + b.handle(OnAudio, m) + case m.Animation != nil: + b.handle(OnAnimation, m) + case m.Document != nil: + b.handle(OnDocument, m) + case m.Sticker != nil: + b.handle(OnSticker, m) + case m.Video != nil: + b.handle(OnVideo, m) + case m.VideoNote != nil: + b.handle(OnVideoNote, m) + case m.Contact != nil: + b.handle(OnContact, m) + case m.Location != nil: + b.handle(OnLocation, m) + case m.Venue != nil: + b.handle(OnVenue, m) + case m.Dice != nil: + b.handle(OnDice, m) + case m.Game != nil: + b.handle(OnGame, m) + default: + return false + } + return true +} + +// Send accepts 2+ arguments, starting with destination chat, followed by +// some Sendable (or string!) and optional send options. +// +// Note: since most arguments are of type interface{}, but have pointer +// method receivers, make sure to pass them by-pointer, NOT by-value. +// +// What is a send option exactly? It can be one of the following types: +// +// - *SendOptions (the actual object accepted by Telegram API) +// - *ReplyMarkup (a component of SendOptions) +// - Option (a shortcut flag for popular options) +// - ParseMode (HTML, Markdown, etc) +// +func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Message, error) { + if to == nil { + return nil, ErrBadRecipient + } + + sendOpts := extractOptions(options) + + switch object := what.(type) { + case string: + return b.sendText(to, object, sendOpts) + case Sendable: + return object.Send(b, to, sendOpts) + default: + return nil, ErrUnsupportedWhat + } +} + +// SendAlbum sends multiple instances of media as a single message. +// +// From all existing options, it only supports tb.Silent. +func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Message, error) { + if to == nil { + return nil, ErrBadRecipient + } + + sendOpts := extractOptions(options) + + media := make([]string, len(a)) + files := make(map[string]File) + + for i, x := range a { + var ( + repr string + data []byte + file = x.MediaFile() + ) + + switch { + case file.InCloud(): + repr = file.FileID + case file.FileURL != "": + repr = file.FileURL + case file.OnDisk() || file.FileReader != nil: + repr = "attach://" + strconv.Itoa(i) + files[strconv.Itoa(i)] = *file + default: + return nil, errors.Errorf("telebot: album entry #%d does not exist", i) + } + + switch y := x.(type) { + case *Photo: + data, _ = json.Marshal(struct { + Type string `json:"type"` + Media string `json:"media"` + Caption string `json:"caption,omitempty"` + ParseMode string `json:"parse_mode,omitempty"` + }{ + Type: "photo", + Media: repr, + Caption: y.Caption, + ParseMode: sendOpts.ParseMode, + }) + case *Video: + data, _ = json.Marshal(struct { + Type string `json:"type"` + Caption string `json:"caption"` + Media string `json:"media"` + Width int `json:"width,omitempty"` + Height int `json:"height,omitempty"` + Duration int `json:"duration,omitempty"` + SupportsStreaming bool `json:"supports_streaming,omitempty"` + ParseMode string `json:"parse_mode,omitempty"` + }{ + Type: "video", + Caption: y.Caption, + Media: repr, + Width: y.Width, + Height: y.Height, + Duration: y.Duration, + SupportsStreaming: y.SupportsStreaming, + ParseMode: sendOpts.ParseMode, + }) + case *Audio: + data, _ = json.Marshal(struct { + Type string `json:"type"` + Media string `json:"media"` + Caption string `json:"caption,omitempty"` + Duration int `json:"duration,omitempty"` + Performer string `json:"performer,omitempty"` + Title string `json:"title,omitempty"` + ParseMode string `json:"parse_mode,omitempty"` + }{ + Type: "audio", + Media: repr, + Caption: y.Caption, + Duration: y.Duration, + Performer: y.Performer, + Title: y.Title, + ParseMode: sendOpts.ParseMode, + }) + case *Document: + data, _ = json.Marshal(struct { + Type string `json:"type"` + Media string `json:"media"` + Caption string `json:"caption,omitempty"` + ParseMode string `json:"parse_mode,omitempty"` + }{ + Type: "document", + Media: repr, + Caption: y.Caption, + ParseMode: sendOpts.ParseMode, + }) + default: + return nil, errors.Errorf("telebot: album entry #%d is not valid", i) + } + + media[i] = string(data) + } + + params := map[string]string{ + "chat_id": to.Recipient(), + "media": "[" + strings.Join(media, ",") + "]", + } + b.embedSendOptions(params, sendOpts) + + data, err := b.sendFiles("sendMediaGroup", files, params) + if err != nil { + return nil, err + } + + var resp struct { + Result []Message + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + + for attachName := range files { + i, _ := strconv.Atoi(attachName) + r := resp.Result[i] + + var newID string + switch { + case r.Photo != nil: + newID = r.Photo.FileID + case r.Video != nil: + newID = r.Video.FileID + case r.Audio != nil: + newID = r.Audio.FileID + case r.Document != nil: + newID = r.Document.FileID + } + + a[i].MediaFile().FileID = newID + } + + return resp.Result, nil +} + +// Reply behaves just like Send() with an exception of "reply-to" indicator. +// +// This function will panic upon nil Message. +func (b *Bot) Reply(to *Message, what interface{}, options ...interface{}) (*Message, error) { + sendOpts := extractOptions(options) + if sendOpts == nil { + sendOpts = &SendOptions{} + } + + sendOpts.ReplyTo = to + return b.Send(to.Chat, what, sendOpts) +} + +// Forward behaves just like Send() but of all options it only supports Silent (see Bots API). +// +// This function will panic upon nil Editable. +func (b *Bot) Forward(to Recipient, msg Editable, options ...interface{}) (*Message, error) { + if to == nil { + return nil, ErrBadRecipient + } + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": to.Recipient(), + "from_chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw("forwardMessage", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Copy behaves just like Forward() but the copied message doesn't have a link to the original message (see Bots API). +// +// This function will panic upon nil Editable. +func (b *Bot) Copy(to Recipient, msg Editable, options ...interface{}) (*Message, error) { + if to == nil { + return nil, ErrBadRecipient + } + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": to.Recipient(), + "from_chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw("copyMessage", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Edit is magic, it lets you change already sent message. +// +// If edited message is sent by the bot, returns it, +// otherwise returns nil and ErrTrueResult. +// +// Use cases: +// +// b.Edit(m, m.Text, newMarkup) +// b.Edit(m, "new text", tb.ModeHTML) +// b.Edit(m, &tb.ReplyMarkup{...}) +// b.Edit(m, &tb.Photo{File: ...}) +// b.Edit(m, tb.Location{42.1337, 69.4242}) +// +// This function will panic upon nil Editable. +func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Message, error) { + var ( + method string + params = make(map[string]string) + ) + + switch v := what.(type) { + case *ReplyMarkup: + return b.EditReplyMarkup(msg, v) + case InputMedia: + return b.EditMedia(msg, v, options...) + case string: + method = "editMessageText" + params["text"] = v + case Location: + method = "editMessageLiveLocation" + params["latitude"] = fmt.Sprintf("%f", v.Lat) + params["longitude"] = fmt.Sprintf("%f", v.Lng) + if v.HorizontalAccuracy != nil { + params["horizontal_accuracy"] = fmt.Sprintf("%f", *v.HorizontalAccuracy) + } + if v.Heading != 0 { + params["heading"] = strconv.Itoa(v.Heading) + } + if v.ProximityAlertRadius != 0 { + params["proximity_alert_radius"] = strconv.Itoa(v.Heading) + } + default: + return nil, ErrUnsupportedWhat + } + + msgID, chatID := msg.MessageSig() + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw(method, params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// EditReplyMarkup edits reply markup of already sent message. +// Pass nil or empty ReplyMarkup to delete it from the message. +// +// If edited message is sent by the bot, returns it, +// otherwise returns nil and ErrTrueResult. +// +// On success, returns edited message object. +// This function will panic upon nil Editable. +func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) { + msgID, chatID := msg.MessageSig() + params := make(map[string]string) + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + if markup == nil { + // will delete reply markup + markup = &ReplyMarkup{} + } + + processButtons(markup.InlineKeyboard) + data, _ := json.Marshal(markup) + params["reply_markup"] = string(data) + + data, err := b.Raw("editMessageReplyMarkup", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// EditCaption edits already sent photo caption with known recipient and message id. +// +// If edited message is sent by the bot, returns it, +// otherwise returns nil and ErrTrueResult. +// +// On success, returns edited message object. +// This function will panic upon nil Editable. +func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{}) (*Message, error) { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "caption": caption, + } + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw("editMessageCaption", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// EditMedia edits already sent media with known recipient and message id. +// +// If edited message is sent by the bot, returns it, +// otherwise returns nil and ErrTrueResult. +// +// Use cases: +// +// b.EditMedia(m, &tb.Photo{File: tb.FromDisk("chicken.jpg")}) +// b.EditMedia(m, &tb.Video{File: tb.FromURL("http://video.mp4")}) +// +// This function will panic upon nil Editable. +func (b *Bot) EditMedia(msg Editable, media InputMedia, options ...interface{}) (*Message, error) { + var ( + repr string + thumb *Photo + + thumbName = "thumb" + file = media.MediaFile() + files = make(map[string]File) + ) + + switch { + case file.InCloud(): + repr = file.FileID + case file.FileURL != "": + repr = file.FileURL + case file.OnDisk() || file.FileReader != nil: + s := file.FileLocal + if file.FileReader != nil { + s = "0" + } else if s == thumbName { + thumbName = "thumb2" + } + + repr = "attach://" + s + files[s] = *file + default: + return nil, errors.Errorf("telebot: can't edit media, it does not exist") + } + + type FileJSON struct { + // All types. + Type string `json:"type"` + Caption string `json:"caption"` + Media string `json:"media"` + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // Video. + Width int `json:"width,omitempty"` + Height int `json:"height,omitempty"` + SupportsStreaming bool `json:"supports_streaming,omitempty"` + + // Video and audio. + Duration int `json:"duration,omitempty"` + + // Document. + FileName string `json:"file_name"` + + // Document, video and audio. + Thumbnail string `json:"thumb,omitempty"` + MIME string `json:"mime_type,omitempty"` + + // Audio. + Title string `json:"title,omitempty"` + Performer string `json:"performer,omitempty"` + } + + result := &FileJSON{Media: repr} + + switch m := media.(type) { + case *Photo: + result.Type = "photo" + result.Caption = m.Caption + case *Video: + result.Type = "video" + result.Caption = m.Caption + result.Width = m.Width + result.Height = m.Height + result.Duration = m.Duration + result.SupportsStreaming = m.SupportsStreaming + result.MIME = m.MIME + thumb = m.Thumbnail + case *Document: + result.Type = "document" + result.Caption = m.Caption + result.FileName = m.FileName + result.MIME = m.MIME + thumb = m.Thumbnail + case *Audio: + result.Type = "audio" + result.Caption = m.Caption + result.Duration = m.Duration + result.MIME = m.MIME + result.Title = m.Title + result.Performer = m.Performer + thumb = m.Thumbnail + default: + return nil, errors.Errorf("telebot: media entry is not valid") + } + + msgID, chatID := msg.MessageSig() + params := make(map[string]string) + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + if sendOpts != nil { + result.ParseMode = sendOpts.ParseMode + } + if thumb != nil { + result.Thumbnail = "attach://" + thumbName + files[thumbName] = *thumb.MediaFile() + } + + data, _ := json.Marshal(result) + params["media"] = string(data) + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + data, err := b.sendFiles("editMessageMedia", files, params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Delete removes the message, including service messages, +// with the following limitations: +// +// * A message can only be deleted if it was sent less than 48 hours ago. +// * Bots can delete outgoing messages in groups and supergroups. +// * Bots granted can_post_messages permissions can delete outgoing +// messages in channels. +// * If the bot is an administrator of a group, it can delete any message there. +// * If the bot has can_delete_messages permission in a supergroup or a +// channel, it can delete any message there. +// +// This function will panic upon nil Editable. +func (b *Bot) Delete(msg Editable) error { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + _, err := b.Raw("deleteMessage", params) + return err +} + +// Notify updates the chat action for recipient. +// +// Chat action is a status message that recipient would see where +// you typically see "Harry is typing" status message. The only +// difference is that bots' chat actions live only for 5 seconds +// and die just once the client receives a message from the bot. +// +// Currently, Telegram supports only a narrow range of possible +// actions, these are aligned as constants of this package. +func (b *Bot) Notify(to Recipient, action ChatAction) error { + if to == nil { + return ErrBadRecipient + } + + params := map[string]string{ + "chat_id": to.Recipient(), + "action": string(action), + } + + _, err := b.Raw("sendChatAction", params) + return err +} + +// Ship replies to the shipping query, if you sent an invoice +// requesting an address and the parameter is_flexible was specified. +// +// Usage: +// +// b.Ship(query) // OK +// b.Ship(query, opts...) // OK with options +// b.Ship(query, "Oops!") // Error message +// +func (b *Bot) Ship(query *ShippingQuery, what ...interface{}) error { + params := map[string]string{ + "shipping_query_id": query.ID, + } + + if len(what) == 0 { + params["ok"] = "True" + } else if s, ok := what[0].(string); ok { + params["ok"] = "False" + params["error_message"] = s + } else { + var opts []ShippingOption + for _, v := range what { + opt, ok := v.(ShippingOption) + if !ok { + return ErrUnsupportedWhat + } + opts = append(opts, opt) + } + + params["ok"] = "True" + data, _ := json.Marshal(opts) + params["shipping_options"] = string(data) + } + + _, err := b.Raw("answerShippingQuery", params) + return err +} + +// Accept finalizes the deal. +func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error { + params := map[string]string{ + "pre_checkout_query_id": query.ID, + } + + if len(errorMessage) == 0 { + params["ok"] = "True" + } else { + params["ok"] = "False" + params["error_message"] = errorMessage[0] + } + + _, err := b.Raw("answerPreCheckoutQuery", params) + return err +} + +// Answer sends a response for a given inline query. A query can only +// be responded to once, subsequent attempts to respond to the same query +// will result in an error. +func (b *Bot) Answer(query *Query, resp *QueryResponse) error { + resp.QueryID = query.ID + + for _, result := range resp.Results { + result.Process() + } + + _, err := b.Raw("answerInlineQuery", resp) + return err +} + +// Respond sends a response for a given callback query. A callback can +// only be responded to once, subsequent attempts to respond to the same callback +// will result in an error. +// +// Example: +// +// bot.Respond(c) +// bot.Respond(c, response) +// +func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error { + var r *CallbackResponse + if resp == nil { + r = &CallbackResponse{} + } else { + r = resp[0] + } + + r.CallbackID = c.ID + _, err := b.Raw("answerCallbackQuery", r) + return err +} + +// FileByID returns full file object including File.FilePath, allowing you to +// download the file from the server. +// +// Usually, Telegram-provided File objects miss FilePath so you might need to +// perform an additional request to fetch them. +func (b *Bot) FileByID(fileID string) (File, error) { + params := map[string]string{ + "file_id": fileID, + } + + data, err := b.Raw("getFile", params) + if err != nil { + return File{}, err + } + + var resp struct { + Result File + } + if err := json.Unmarshal(data, &resp); err != nil { + return File{}, wrapError(err) + } + return resp.Result, nil +} + +// Download saves the file from Telegram servers locally. +// +// Maximum file size to download is 20 MB. +func (b *Bot) Download(file *File, localFilename string) error { + reader, err := b.GetFile(file) + if err != nil { + return wrapError(err) + } + defer reader.Close() + + out, err := os.Create(localFilename) + if err != nil { + return wrapError(err) + } + defer out.Close() + + _, err = io.Copy(out, reader) + if err != nil { + return wrapError(err) + } + + file.FileLocal = localFilename + return nil +} + +// GetFile gets a file from Telegram servers. +func (b *Bot) GetFile(file *File) (io.ReadCloser, error) { + f, err := b.FileByID(file.FileID) + if err != nil { + return nil, err + } + + url := b.URL + "/file/bot" + b.Token + "/" + f.FilePath + file.FilePath = f.FilePath // saving file path + + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, wrapError(err) + } + + resp, err := b.client.Do(req) + if err != nil { + return nil, wrapError(err) + } + + if resp.StatusCode != http.StatusOK { + resp.Body.Close() + return nil, errors.Errorf("telebot: expected status 200 but got %s", resp.Status) + } + + return resp.Body, nil +} + +// StopLiveLocation stops broadcasting live message location +// before Location.LivePeriod expires. +// +// If the message is sent by the bot, returns it, +// otherwise returns nil and ErrTrueResult. +// +// It supports tb.ReplyMarkup. +// This function will panic upon nil Editable. +func (b *Bot) StopLiveLocation(msg Editable, options ...interface{}) (*Message, error) { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw("stopMessageLiveLocation", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// StopPoll stops a poll which was sent by the bot and returns +// the stopped Poll object with the final results. +// +// It supports ReplyMarkup. +// This function will panic upon nil Editable. +func (b *Bot) StopPoll(msg Editable, options ...interface{}) (*Poll, error) { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + data, err := b.Raw("stopPoll", params) + if err != nil { + return nil, err + } + + var resp struct { + Result *Poll + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} + +// GetInviteLink should be used to export chat's invite link. +func (b *Bot) GetInviteLink(chat *Chat) (string, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + data, err := b.Raw("exportChatInviteLink", params) + if err != nil { + return "", err + } + + var resp struct { + Result string + } + if err := json.Unmarshal(data, &resp); err != nil { + return "", wrapError(err) + } + return resp.Result, nil +} + +// SetGroupTitle should be used to update group title. +func (b *Bot) SetGroupTitle(chat *Chat, title string) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "title": title, + } + + _, err := b.Raw("setChatTitle", params) + return err +} + +// SetGroupDescription should be used to update group description. +func (b *Bot) SetGroupDescription(chat *Chat, description string) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "description": description, + } + + _, err := b.Raw("setChatDescription", params) + return err +} + +// SetGroupPhoto should be used to update group photo. +func (b *Bot) SetGroupPhoto(chat *Chat, p *Photo) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + _, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params) + return err +} + +// SetGroupStickerSet should be used to update group's group sticker set. +func (b *Bot) SetGroupStickerSet(chat *Chat, setName string) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "sticker_set_name": setName, + } + + _, err := b.Raw("setChatStickerSet", params) + return err +} + +// SetGroupPermissions sets default chat permissions for all members. +func (b *Bot) SetGroupPermissions(chat *Chat, perms Rights) error { + params := map[string]interface{}{ + "chat_id": chat.Recipient(), + } + embedRights(params, perms) + + _, err := b.Raw("setChatPermissions", params) + return err +} + +// DeleteGroupPhoto should be used to just remove group photo. +func (b *Bot) DeleteGroupPhoto(chat *Chat) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + _, err := b.Raw("deleteChatPhoto", params) + return err +} + +// DeleteGroupStickerSet should be used to just remove group sticker set. +func (b *Bot) DeleteGroupStickerSet(chat *Chat) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + _, err := b.Raw("deleteChatStickerSet", params) + return err +} + +// Leave makes bot leave a group, supergroup or channel. +func (b *Bot) Leave(chat *Chat) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + _, err := b.Raw("leaveChat", params) + return err +} + +// Pin pins a message in a supergroup or a channel. +// +// It supports tb.Silent option. +// This function will panic upon nil Editable. +func (b *Bot) Pin(msg Editable, options ...interface{}) error { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "chat_id": strconv.FormatInt(chatID, 10), + "message_id": msgID, + } + + sendOpts := extractOptions(options) + b.embedSendOptions(params, sendOpts) + + _, err := b.Raw("pinChatMessage", params) + return err +} + +// Unpin unpins a message in a supergroup or a channel. +// +// It supports tb.Silent option. +// MessageID is a specific pinned message +func (b *Bot) Unpin(chat *Chat, messageID ...int) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + if len(messageID) > 0 { + params["message_id"] = strconv.Itoa(messageID[0]) + } + + _, err := b.Raw("unpinChatMessage", params) + return err +} + +// UnpinAll unpins all messages in a supergroup or a channel. +// +// It supports tb.Silent option. +func (b *Bot) UnpinAll(chat *Chat) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + + _, err := b.Raw("unpinAllChatMessages", params) + return err +} + +// ChatByID fetches chat info of its ID. +// +// Including current name of the user for one-on-one conversations, +// current username of a user, group or channel, etc. +// +// Returns a Chat object on success. +func (b *Bot) ChatByID(id string) (*Chat, error) { + params := map[string]string{ + "chat_id": id, + } + + data, err := b.Raw("getChat", params) + if err != nil { + return nil, err + } + + var resp struct { + Result *Chat + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + if resp.Result.Type == ChatChannel && resp.Result.Username == "" { + resp.Result.Type = ChatChannelPrivate + } + return resp.Result, nil +} + +// ProfilePhotosOf returns list of profile pictures for a user. +func (b *Bot) ProfilePhotosOf(user *User) ([]Photo, error) { + params := map[string]string{ + "user_id": user.Recipient(), + } + + data, err := b.Raw("getUserProfilePhotos", params) + if err != nil { + return nil, err + } + + var resp struct { + Result struct { + Count int `json:"total_count"` + Photos []Photo `json:"photos"` + } + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result.Photos, nil +} + +// ChatMemberOf returns information about a member of a chat. +// +// Returns a ChatMember object on success. +func (b *Bot) ChatMemberOf(chat *Chat, user *User) (*ChatMember, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": user.Recipient(), + } + + data, err := b.Raw("getChatMember", params) + if err != nil { + return nil, err + } + + var resp struct { + Result *ChatMember + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} + +// FileURLByID returns direct url for files using FileID which you can get from +// File object. It returns a file:/// URL when the target file is on the +// local disk (it happens if you are using a local Bot API server, see +// https://core.telegram.org/bots/api#using-a-local-bot-api-server for details). +func (b *Bot) FileURLByID(fileID string) (string, error) { + f, err := b.FileByID(fileID) + if err != nil { + return "", err + } + + if path.IsAbs(f.FilePath) { + return "file://" + f.FilePath, nil + } + + return b.URL + "/file/bot" + b.Token + "/" + f.FilePath, nil +} + +// GetCommands returns the current list of the bot's commands. +func (b *Bot) GetCommands() ([]Command, error) { + data, err := b.Raw("getMyCommands", nil) + if err != nil { + return nil, err + } + + var resp struct { + Result []Command + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} + +// SetCommands changes the list of the bot's commands. +func (b *Bot) SetCommands(cmds []Command) error { + data, _ := json.Marshal(cmds) + + params := map[string]string{ + "commands": string(data), + } + + _, err := b.Raw("setMyCommands", params) + return err +} + +func (b *Bot) NewMarkup() *ReplyMarkup { + return &ReplyMarkup{} +} + +// Logout logs out from the cloud Bot API server before launching the bot locally. +func (b *Bot) Logout() (bool, error) { + data, err := b.Raw("logOut", nil) + if err != nil { + return false, err + } + + var resp struct { + Result bool `json:"result"` + } + if err := json.Unmarshal(data, &resp); err != nil { + return false, wrapError(err) + } + + return resp.Result, nil +} + +// Close closes the bot instance before moving it from one local server to another. +func (b *Bot) Close() (bool, error) { + data, err := b.Raw("close", nil) + if err != nil { + return false, err + } + + var resp struct { + Result bool `json:"result"` + } + if err := json.Unmarshal(data, &resp); err != nil { + return false, wrapError(err) + } + + return resp.Result, nil +} + +// CreateInviteLink creates an additional invite link for a chat. +func (b *Bot) CreateInviteLink(chat *Chat, link *ChatInviteLink) (*ChatInviteLink, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + if link != nil { + params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) + params["member_limit"] = strconv.Itoa(link.MemberLimit) + } + + data, err := b.Raw("createChatInviteLink", params) + if err != nil { + return nil, err + } + + var resp struct { + Result ChatInviteLink `json:"result"` + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + + return &resp.Result, nil +} + +// EditInviteLink edits a non-primary invite link created by the bot. +func (b *Bot) EditInviteLink(chat *Chat, link *ChatInviteLink) (*ChatInviteLink, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + } + if link != nil { + params["invite_link"] = link.InviteLink + params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) + params["member_limit"] = strconv.Itoa(link.MemberLimit) + } + + data, err := b.Raw("editChatInviteLink", params) + if err != nil { + return nil, err + } + + var resp struct { + Result ChatInviteLink `json:"result"` + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + + return &resp.Result, nil +} + +// RevokeInviteLink revokes an invite link created by the bot. +func (b *Bot) RevokeInviteLink(chat *Chat, link string) (*ChatInviteLink, error) { + params := map[string]string{ + "chat_id": chat.Recipient(), + "invite_link": link, + } + + data, err := b.Raw("revokeChatInviteLink", params) + if err != nil { + return nil, err + } + + var resp struct { + Result ChatInviteLink `json:"result"` + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + + return &resp.Result, nil +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/callbacks.go b/vendor/gopkg.in/tucnak/telebot.v2/callbacks.go new file mode 100644 index 0000000..ee3bf12 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/callbacks.go @@ -0,0 +1,136 @@ +package telebot + +import "encoding/json" + +// CallbackEndpoint is an interface any element capable +// of responding to a callback `\f`. +type CallbackEndpoint interface { + CallbackUnique() string +} + +// Callback object represents a query from a callback button in an +// inline keyboard. +type Callback struct { + ID string `json:"id"` + + // For message sent to channels, Sender may be empty + Sender *User `json:"from"` + + // Message will be set if the button that originated the query + // was attached to a message sent by a bot. + Message *Message `json:"message"` + + // MessageID will be set if the button was attached to a message + // sent via the bot in inline mode. + MessageID string `json:"inline_message_id"` + + // Data associated with the callback button. Be aware that + // a bad client can send arbitrary data in this field. + Data string `json:"data"` +} + +// IsInline says whether message is an inline message. +func (c *Callback) IsInline() bool { + return c.MessageID != "" +} + +// CallbackResponse builds a response to a Callback query. +// +// See also: https://core.telegram.org/bots/api#answerCallbackQuery +type CallbackResponse struct { + // The ID of the callback to which this is a response. + // + // Note: Telebot sets this field automatically! + CallbackID string `json:"callback_query_id"` + + // Text of the notification. If not specified, nothing will be + // shown to the user. + Text string `json:"text,omitempty"` + + // (Optional) If true, an alert will be shown by the client instead + // of a notification at the top of the chat screen. Defaults to false. + ShowAlert bool `json:"show_alert,omitempty"` + + // (Optional) URL that will be opened by the user's client. + // If you have created a Game and accepted the conditions via + // @BotFather, specify the URL that opens your game. + // + // Note: this will only work if the query comes from a game + // callback button. Otherwise, you may use deep-linking: + // https://telegram.me/your_bot?start=XXXX + URL string `json:"url,omitempty"` +} + +// InlineButton represents a button displayed in the message. +type InlineButton struct { + // Unique slagish name for this kind of button, + // try to be as specific as possible. + // + // It will be used as a callback endpoint. + Unique string `json:"unique,omitempty"` + + Text string `json:"text"` + URL string `json:"url,omitempty"` + Data string `json:"callback_data,omitempty"` + InlineQuery string `json:"switch_inline_query,omitempty"` + InlineQueryChat string `json:"switch_inline_query_current_chat"` + Login *Login `json:"login_url,omitempty"` +} + +// With returns a copy of the button with data. +func (t *InlineButton) With(data string) *InlineButton { + return &InlineButton{ + Unique: t.Unique, + Text: t.Text, + URL: t.URL, + InlineQuery: t.InlineQuery, + InlineQueryChat: t.InlineQueryChat, + Login: t.Login, + Data: data, + } +} + +// CallbackUnique returns InlineButton.Unique. +func (t *InlineButton) CallbackUnique() string { + return "\f" + t.Unique +} + +// CallbackUnique returns KeyboardButton.Text. +func (t *ReplyButton) CallbackUnique() string { + return t.Text +} + +// CallbackUnique implements CallbackEndpoint. +func (t *Btn) CallbackUnique() string { + if t.Unique != "" { + return "\f" + t.Unique + } + return t.Text +} + +// Login represents a parameter of the inline keyboard button +// used to automatically authorize a user. Serves as a great replacement +// for the Telegram Login Widget when the user is coming from Telegram. +type Login struct { + URL string `json:"url"` + Text string `json:"forward_text,omitempty"` + Username string `json:"bot_username,omitempty"` + WriteAccess bool `json:"request_write_access,omitempty"` +} + +// MarshalJSON implements json.Marshaler interface. +// It needed to avoid InlineQueryChat and Login fields conflict. +// If you have Login field in your button, InlineQueryChat must be skipped. +func (t *InlineButton) MarshalJSON() ([]byte, error) { + type InlineButtonJSON InlineButton + + if t.Login != nil { + return json.Marshal(struct { + InlineButtonJSON + InlineQueryChat string `json:"switch_inline_query_current_chat,omitempty"` + }{ + InlineButtonJSON: InlineButtonJSON(*t), + }) + } + return json.Marshal(InlineButtonJSON(*t)) +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/chat.go b/vendor/gopkg.in/tucnak/telebot.v2/chat.go new file mode 100644 index 0000000..2f8e961 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/chat.go @@ -0,0 +1,119 @@ +package telebot + +import "strconv" + +// User object represents a Telegram user, bot. +type User struct { + ID int64 `json:"id"` + + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Username string `json:"username"` + LanguageCode string `json:"language_code"` + IsBot bool `json:"is_bot"` + + // Returns only in getMe + CanJoinGroups bool `json:"can_join_groups"` + CanReadMessages bool `json:"can_read_all_group_messages"` + SupportsInline bool `json:"supports_inline_queries"` +} + +// Recipient returns user ID (see Recipient interface). +func (u *User) Recipient() string { + return strconv.FormatInt(u.ID, 10) +} + +// Chat object represents a Telegram user, bot, group or a channel. +type Chat struct { + ID int64 `json:"id"` + + // See ChatType and consts. + Type ChatType `json:"type"` + + // Won't be there for ChatPrivate. + Title string `json:"title"` + + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Username string `json:"username"` + + // Still shows whether the user is a member + // of the chat at the moment of the request. + Still bool `json:"is_member,omitempty"` + + // Returns only in getChat + Bio string `json:"bio,omitempty"` + Photo *ChatPhoto `json:"photo,omitempty"` + Description string `json:"description,omitempty"` + InviteLink string `json:"invite_link,omitempty"` + PinnedMessage *Message `json:"pinned_message,omitempty"` + Permissions *Rights `json:"permissions,omitempty"` + SlowMode int `json:"slow_mode_delay,omitempty"` + StickerSet string `json:"sticker_set_name,omitempty"` + CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"` + LinkedChatID int64 `json:"linked_chat_id,omitempty"` + ChatLocation *ChatLocation `json:"location,omitempty"` +} + +type ChatLocation struct { + Location Location `json:"location,omitempty"` + Address string `json:"address,omitempty"` +} + +// ChatPhoto object represents a chat photo. +type ChatPhoto struct { + // File identifiers of small (160x160) chat photo + SmallFileID string `json:"small_file_id"` + SmallFileUniqueID string `json:"small_file_unique_id"` + + // File identifiers of big (640x640) chat photo + BigFileID string `json:"big_file_id"` + BigFileUniqueID string `json:"big_file_unique_id"` +} + +// Recipient returns chat ID (see Recipient interface). +func (c *Chat) Recipient() string { + return strconv.FormatInt(c.ID, 10) +} + +// ChatMember object represents information about a single chat member. +type ChatMember struct { + Rights + + User *User `json:"user"` + Role MemberStatus `json:"status"` + Title string `json:"custom_title"` + Anonymous bool `json:"is_anonymous"` + + // Date when restrictions will be lifted for the user, unix time. + // + // If user is restricted for more than 366 days or less than + // 30 seconds from the current time, they are considered to be + // restricted forever. + // + // Use tb.Forever(). + // + RestrictedUntil int64 `json:"until_date,omitempty"` +} + +// ChatID represents a chat or an user integer ID, which can be used +// as recipient in bot methods. It is very useful in cases where +// you have special group IDs, for example in your config, and don't +// want to wrap it into *tb.Chat every time you send messages. +// +// Example: +// +// group := tb.ChatID(-100756389456) +// b.Send(group, "Hello!") +// +// type Config struct { +// AdminGroup tb.ChatID `json:"admin_group"` +// } +// b.Send(conf.AdminGroup, "Hello!") +// +type ChatID int64 + +// Recipient returns chat ID (see Recipient interface). +func (i ChatID) Recipient() string { + return strconv.FormatInt(int64(i), 10) +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/data_payments.go b/vendor/gopkg.in/tucnak/telebot.v2/data_payments.go new file mode 100644 index 0000000..99efa76 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/data_payments.go @@ -0,0 +1,3 @@ +package telebot + +const dataCurrencies = `{"AED":{"code":"AED","title":"United Arab Emirates Dirham","symbol":"AED","native":"\u062f.\u0625.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"367","max_amount":"3673200"},"AFN":{"code":"AFN","title":"Afghan Afghani","symbol":"AFN","native":"\u060b","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"7554","max_amount":"75540495"},"ALL":{"code":"ALL","title":"Albanian Lek","symbol":"ALL","native":"Lek","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":false,"exp":2,"min_amount":"10908","max_amount":"109085036"},"AMD":{"code":"AMD","title":"Armenian Dram","symbol":"AMD","native":"\u0564\u0580.","thousands_sep":",","decimal_sep":".","symbol_left":false,"space_between":true,"exp":2,"min_amount":"48398","max_amount":"483984962"},"ARS":{"code":"ARS","title":"Argentine Peso","symbol":"ARS","native":"$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"3720","max_amount":"37202998"},"AUD":{"code":"AUD","title":"Australian Dollar","symbol":"AU$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"139","max_amount":"1392750"},"AZN":{"code":"AZN","title":"Azerbaijani Manat","symbol":"AZN","native":"\u043c\u0430\u043d.","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"170","max_amount":"1702500"},"BAM":{"code":"BAM","title":"Bosnia & Herzegovina Convertible Mark","symbol":"BAM","native":"KM","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"171","max_amount":"1715550"},"BDT":{"code":"BDT","title":"Bangladeshi Taka","symbol":"BDT","native":"\u09f3","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"8336","max_amount":"83367500"},"BGN":{"code":"BGN","title":"Bulgarian Lev","symbol":"BGN","native":"\u043b\u0432.","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"171","max_amount":"1716850"},"BND":{"code":"BND","title":"Brunei Dollar","symbol":"BND","native":"$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":false,"exp":2,"min_amount":"134","max_amount":"1349850"},"BOB":{"code":"BOB","title":"Bolivian Boliviano","symbol":"BOB","native":"Bs","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"687","max_amount":"6877150"},"BRL":{"code":"BRL","title":"Brazilian Real","symbol":"R$","native":"R$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"377","max_amount":"3775397"},"CAD":{"code":"CAD","title":"Canadian Dollar","symbol":"CA$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"132","max_amount":"1321950"},"CHF":{"code":"CHF","title":"Swiss Franc","symbol":"CHF","native":"CHF","thousands_sep":"'","decimal_sep":".","symbol_left":false,"space_between":true,"exp":2,"min_amount":"99","max_amount":"993220"},"CLP":{"code":"CLP","title":"Chilean Peso","symbol":"CLP","native":"$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":0,"min_amount":"666","max_amount":"6665199"},"CNY":{"code":"CNY","title":"Chinese Renminbi Yuan","symbol":"CN\u00a5","native":"CN\u00a5","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"674","max_amount":"6747298"},"COP":{"code":"COP","title":"Colombian Peso","symbol":"COP","native":"$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"315595","max_amount":"3155950000"},"CRC":{"code":"CRC","title":"Costa Rican Col\u00f3n","symbol":"CRC","native":"\u20a1","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":false,"exp":2,"min_amount":"60113","max_amount":"601130282"},"CZK":{"code":"CZK","title":"Czech Koruna","symbol":"CZK","native":"K\u010d","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"2251","max_amount":"22510978"},"DKK":{"code":"DKK","title":"Danish Krone","symbol":"DKK","native":"kr","thousands_sep":"","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"654","max_amount":"6545403"},"DOP":{"code":"DOP","title":"Dominican Peso","symbol":"DOP","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"5032","max_amount":"50329504"},"DZD":{"code":"DZD","title":"Algerian Dinar","symbol":"DZD","native":"\u062f.\u062c.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"11872","max_amount":"118729869"},"EGP":{"code":"EGP","title":"Egyptian Pound","symbol":"EGP","native":"\u062c.\u0645.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"1791","max_amount":"17912012"},"EUR":{"code":"EUR","title":"Euro","symbol":"\u20ac","native":"\u20ac","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"87","max_amount":"877155"},"GBP":{"code":"GBP","title":"British Pound","symbol":"\u00a3","native":"\u00a3","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"75","max_amount":"757605"},"GEL":{"code":"GEL","title":"Georgian Lari","symbol":"GEL","native":"GEL","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"266","max_amount":"2663750"},"GTQ":{"code":"GTQ","title":"Guatemalan Quetzal","symbol":"GTQ","native":"Q","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"768","max_amount":"7689850"},"HKD":{"code":"HKD","title":"Hong Kong Dollar","symbol":"HK$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"784","max_amount":"7845505"},"HNL":{"code":"HNL","title":"Honduran Lempira","symbol":"HNL","native":"L","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"2427","max_amount":"24277502"},"HRK":{"code":"HRK","title":"Croatian Kuna","symbol":"HRK","native":"kn","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"650","max_amount":"6506302"},"HUF":{"code":"HUF","title":"Hungarian Forint","symbol":"HUF","native":"Ft","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"27844","max_amount":"278440341"},"IDR":{"code":"IDR","title":"Indonesian Rupiah","symbol":"IDR","native":"Rp","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":false,"exp":2,"min_amount":"1406555","max_amount":"14065550000"},"ILS":{"code":"ILS","title":"Israeli New Sheqel","symbol":"\u20aa","native":"\u20aa","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"366","max_amount":"3668230"},"INR":{"code":"INR","title":"Indian Rupee","symbol":"\u20b9","native":"\u20b9","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"7090","max_amount":"70900503"},"ISK":{"code":"ISK","title":"Icelandic Kr\u00f3na","symbol":"ISK","native":"kr","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":0,"min_amount":"119","max_amount":"1195599"},"JMD":{"code":"JMD","title":"Jamaican Dollar","symbol":"JMD","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"13153","max_amount":"131539958"},"JPY":{"code":"JPY","title":"Japanese Yen","symbol":"\u00a5","native":"\uffe5","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":0,"min_amount":"109","max_amount":"1095549"},"KES":{"code":"KES","title":"Kenyan Shilling","symbol":"KES","native":"Ksh","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"10032","max_amount":"100322011"},"KGS":{"code":"KGS","title":"Kyrgyzstani Som","symbol":"KGS","native":"KGS","thousands_sep":"\u00a0","decimal_sep":"-","symbol_left":false,"space_between":true,"exp":2,"min_amount":"6982","max_amount":"69820300"},"KRW":{"code":"KRW","title":"South Korean Won","symbol":"\u20a9","native":"\u20a9","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":0,"min_amount":"1119","max_amount":"11190001"},"KZT":{"code":"KZT","title":"Kazakhstani Tenge","symbol":"KZT","native":"\u20b8","thousands_sep":"\u00a0","decimal_sep":"-","symbol_left":true,"space_between":false,"exp":2,"min_amount":"37767","max_amount":"377674954"},"LBP":{"code":"LBP","title":"Lebanese Pound","symbol":"LBP","native":"\u0644.\u0644.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"150080","max_amount":"1500802255"},"LKR":{"code":"LKR","title":"Sri Lankan Rupee","symbol":"LKR","native":"\u0dbb\u0dd4.","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"18078","max_amount":"180789638"},"MAD":{"code":"MAD","title":"Moroccan Dirham","symbol":"MAD","native":"\u062f.\u0645.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"955","max_amount":"9554850"},"MDL":{"code":"MDL","title":"Moldovan Leu","symbol":"MDL","native":"MDL","thousands_sep":",","decimal_sep":".","symbol_left":false,"space_between":true,"exp":2,"min_amount":"1703","max_amount":"17038967"},"MNT":{"code":"MNT","title":"Mongolian T\u00f6gr\u00f6g","symbol":"MNT","native":"MNT","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":true,"space_between":false,"exp":2,"min_amount":"261750","max_amount":"2617500000"},"MUR":{"code":"MUR","title":"Mauritian Rupee","symbol":"MUR","native":"MUR","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"3438","max_amount":"34384499"},"MVR":{"code":"MVR","title":"Maldivian Rufiyaa","symbol":"MVR","native":"MVR","thousands_sep":",","decimal_sep":".","symbol_left":false,"space_between":true,"exp":2,"min_amount":"1550","max_amount":"15501063"},"MXN":{"code":"MXN","title":"Mexican Peso","symbol":"MX$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"1898","max_amount":"18988704"},"MYR":{"code":"MYR","title":"Malaysian Ringgit","symbol":"MYR","native":"RM","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"412","max_amount":"4124501"},"MZN":{"code":"MZN","title":"Mozambican Metical","symbol":"MZN","native":"MTn","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"6188","max_amount":"61889913"},"NGN":{"code":"NGN","title":"Nigerian Naira","symbol":"NGN","native":"\u20a6","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"36174","max_amount":"361749532"},"NIO":{"code":"NIO","title":"Nicaraguan C\u00f3rdoba","symbol":"NIO","native":"C$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"3241","max_amount":"32415503"},"NOK":{"code":"NOK","title":"Norwegian Krone","symbol":"NOK","native":"kr","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"851","max_amount":"8510100"},"NPR":{"code":"NPR","title":"Nepalese Rupee","symbol":"NPR","native":"\u0928\u0947\u0930\u0942","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"11299","max_amount":"112995016"},"NZD":{"code":"NZD","title":"New Zealand Dollar","symbol":"NZ$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"146","max_amount":"1461850"},"PAB":{"code":"PAB","title":"Panamanian Balboa","symbol":"PAB","native":"B\/.","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"99","max_amount":"995290"},"PEN":{"code":"PEN","title":"Peruvian Nuevo Sol","symbol":"PEN","native":"S\/.","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"333","max_amount":"3331250"},"PHP":{"code":"PHP","title":"Philippine Peso","symbol":"PHP","native":"\u20b1","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"5260","max_amount":"52602981"},"PKR":{"code":"PKR","title":"Pakistani Rupee","symbol":"PKR","native":"\u20a8","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"13921","max_amount":"139214990"},"PLN":{"code":"PLN","title":"Polish Z\u0142oty","symbol":"PLN","native":"z\u0142","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"376","max_amount":"3764026"},"PYG":{"code":"PYG","title":"Paraguayan Guaran\u00ed","symbol":"PYG","native":"\u20b2","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":0,"min_amount":"6013","max_amount":"60134502"},"QAR":{"code":"QAR","title":"Qatari Riyal","symbol":"QAR","native":"\u0631.\u0642.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"364","max_amount":"3641101"},"RON":{"code":"RON","title":"Romanian Leu","symbol":"RON","native":"RON","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"417","max_amount":"4172003"},"RSD":{"code":"RSD","title":"Serbian Dinar","symbol":"RSD","native":"\u0434\u0438\u043d.","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"10391","max_amount":"103910127"},"RUB":{"code":"RUB","title":"Russian Ruble","symbol":"RUB","native":"\u0440\u0443\u0431.","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"6598","max_amount":"65986027"},"SAR":{"code":"SAR","title":"Saudi Riyal","symbol":"SAR","native":"\u0631.\u0633.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"373","max_amount":"3732650"},"SEK":{"code":"SEK","title":"Swedish Krona","symbol":"SEK","native":"kr","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"904","max_amount":"9047896"},"SGD":{"code":"SGD","title":"Singapore Dollar","symbol":"SGD","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"135","max_amount":"1353897"},"THB":{"code":"THB","title":"Thai Baht","symbol":"\u0e3f","native":"\u0e3f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"3156","max_amount":"31563499"},"TJS":{"code":"TJS","title":"Tajikistani Somoni","symbol":"TJS","native":"TJS","thousands_sep":"\u00a0","decimal_sep":";","symbol_left":false,"space_between":true,"exp":2,"min_amount":"938","max_amount":"9389950"},"TRY":{"code":"TRY","title":"Turkish Lira","symbol":"TRY","native":"TL","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"526","max_amount":"5267200"},"TTD":{"code":"TTD","title":"Trinidad and Tobago Dollar","symbol":"TTD","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"675","max_amount":"6757850"},"TWD":{"code":"TWD","title":"New Taiwan Dollar","symbol":"NT$","native":"NT$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"3072","max_amount":"30722993"},"TZS":{"code":"TZS","title":"Tanzanian Shilling","symbol":"TZS","native":"TSh","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"230200","max_amount":"2302000188"},"UAH":{"code":"UAH","title":"Ukrainian Hryvnia","symbol":"UAH","native":"\u20b4","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":false,"exp":2,"min_amount":"2764","max_amount":"27648991"},"UGX":{"code":"UGX","title":"Ugandan Shilling","symbol":"UGX","native":"USh","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":0,"min_amount":"3657","max_amount":"36575502"},"USD":{"code":"USD","title":"United States Dollar","symbol":"$","native":"$","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":false,"exp":2,"min_amount":"100","max_amount":1000000},"UYU":{"code":"UYU","title":"Uruguayan Peso","symbol":"UYU","native":"$","thousands_sep":".","decimal_sep":",","symbol_left":true,"space_between":true,"exp":2,"min_amount":"3246","max_amount":"32469503"},"UZS":{"code":"UZS","title":"Uzbekistani Som","symbol":"UZS","native":"UZS","thousands_sep":"\u00a0","decimal_sep":",","symbol_left":false,"space_between":true,"exp":2,"min_amount":"832759","max_amount":"8327599915"},"VND":{"code":"VND","title":"Vietnamese \u0110\u1ed3ng","symbol":"\u20ab","native":"\u20ab","thousands_sep":".","decimal_sep":",","symbol_left":false,"space_between":true,"exp":0,"min_amount":"23084","max_amount":"230840500"},"YER":{"code":"YER","title":"Yemeni Rial","symbol":"YER","native":"\u0631.\u064a.\u200f","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"25030","max_amount":"250301249"},"ZAR":{"code":"ZAR","title":"South African Rand","symbol":"ZAR","native":"R","thousands_sep":",","decimal_sep":".","symbol_left":true,"space_between":true,"exp":2,"min_amount":"1362","max_amount":"13620106"}}` diff --git a/vendor/gopkg.in/tucnak/telebot.v2/editable.go b/vendor/gopkg.in/tucnak/telebot.v2/editable.go new file mode 100644 index 0000000..ec1fb5b --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/editable.go @@ -0,0 +1,30 @@ +package telebot + +// Editable is an interface for all objects that +// provide "message signature", a pair of 32-bit +// message ID and 64-bit chat ID, both required +// for edit operations. +// +// Use case: DB model struct for messages to-be +// edited with, say two columns: msg_id,chat_id +// could easily implement MessageSig() making +// instances of stored messages editable. +type Editable interface { + // MessageSig is a "message signature". + // + // For inline messages, return chatID = 0. + MessageSig() (messageID string, chatID int64) +} + +// StoredMessage is an example struct suitable for being +// stored in the database as-is or being embedded into +// a larger struct, which is often the case (you might +// want to store some metadata alongside, or might not.) +type StoredMessage struct { + MessageID string `sql:"message_id" json:"message_id"` + ChatID int64 `sql:"chat_id" json:"chat_id"` +} + +func (x StoredMessage) MessageSig() (string, int64) { + return x.MessageID, x.ChatID +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/errors.go b/vendor/gopkg.in/tucnak/telebot.v2/errors.go new file mode 100644 index 0000000..e6a31e2 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/errors.go @@ -0,0 +1,184 @@ +package telebot + +import ( + "fmt" + "strings" +) + +type APIError struct { + Code int + Description string + Message string + Parameters map[string]interface{} +} + +type FloodError struct { + *APIError + RetryAfter int +} + +// ʔ returns description of error. +// A tiny shortcut to make code clearer. +func (err *APIError) ʔ() string { + return err.Description +} + +// Error implements error interface. +func (err *APIError) Error() string { + msg := err.Message + if msg == "" { + split := strings.Split(err.Description, ": ") + if len(split) == 2 { + msg = split[1] + } else { + msg = err.Description + } + } + return fmt.Sprintf("telegram: %s (%d)", msg, err.Code) +} + +// NewAPIError returns new APIError instance with given description. +// First element of msgs is Description. The second is optional Message. +func NewAPIError(code int, msgs ...string) *APIError { + err := &APIError{Code: code} + if len(msgs) >= 1 { + err.Description = msgs[0] + } + if len(msgs) >= 2 { + err.Message = msgs[1] + } + return err +} + +var ( + // General errors + ErrUnauthorized = NewAPIError(401, "Unauthorized") + ErrNotStartedByUser = NewAPIError(403, "Forbidden: bot can't initiate conversation with a user") + ErrBlockedByUser = NewAPIError(401, "Forbidden: bot was blocked by the user") + ErrUserIsDeactivated = NewAPIError(401, "Forbidden: user is deactivated") + ErrNotFound = NewAPIError(404, "Not Found") + ErrInternal = NewAPIError(500, "Internal Server Error") + + // Bad request errors + ErrTooLarge = NewAPIError(400, "Request Entity Too Large") + ErrMessageTooLong = NewAPIError(400, "Bad Request: message is too long") + ErrToForwardNotFound = NewAPIError(400, "Bad Request: message to forward not found") + ErrToReplyNotFound = NewAPIError(400, "Bad Request: reply message not found") + ErrToDeleteNotFound = NewAPIError(400, "Bad Request: message to delete not found") + ErrEmptyMessage = NewAPIError(400, "Bad Request: message must be non-empty") + ErrEmptyText = NewAPIError(400, "Bad Request: text is empty") + ErrEmptyChatID = NewAPIError(400, "Bad Request: chat_id is empty") + ErrChatNotFound = NewAPIError(400, "Bad Request: chat not found") + ErrMessageNotModified = NewAPIError(400, "Bad Request: message is not modified") + ErrSameMessageContent = NewAPIError(400, "Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message") + ErrCantEditMessage = NewAPIError(400, "Bad Request: message can't be edited") + ErrButtonDataInvalid = NewAPIError(400, "Bad Request: BUTTON_DATA_INVALID") + ErrWrongTypeOfContent = NewAPIError(400, "Bad Request: wrong type of the web page content") + ErrBadURLContent = NewAPIError(400, "Bad Request: failed to get HTTP URL content") + ErrWrongFileID = NewAPIError(400, "Bad Request: wrong file identifier/HTTP URL specified") + ErrWrongFileIDSymbol = NewAPIError(400, "Bad Request: wrong remote file id specified: can't unserialize it. Wrong last symbol") + ErrWrongFileIDLength = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong string length") + ErrWrongFileIDCharacter = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong character in the string") + ErrWrongFileIDPadding = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong padding in the string") + ErrFailedImageProcess = NewAPIError(400, "Bad Request: IMAGE_PROCESS_FAILED", "Image process failed") + ErrInvalidStickerSet = NewAPIError(400, "Bad Request: STICKERSET_INVALID", "Stickerset is invalid") + ErrBadPollOptions = NewAPIError(400, "Bad Request: expected an Array of String as options") + ErrGroupMigrated = NewAPIError(400, "Bad Request: group chat was upgraded to a supergroup chat") + + // No rights errors + ErrNoRightsToRestrict = NewAPIError(400, "Bad Request: not enough rights to restrict/unrestrict chat member") + ErrNoRightsToSend = NewAPIError(400, "Bad Request: have no rights to send a message") + ErrNoRightsToSendPhoto = NewAPIError(400, "Bad Request: not enough rights to send photos to the chat") + ErrNoRightsToSendStickers = NewAPIError(400, "Bad Request: not enough rights to send stickers to the chat") + ErrNoRightsToSendGifs = NewAPIError(400, "Bad Request: CHAT_SEND_GIFS_FORBIDDEN", "sending GIFS is not allowed in this chat") + ErrNoRightsToDelete = NewAPIError(400, "Bad Request: message can't be deleted") + ErrKickingChatOwner = NewAPIError(400, "Bad Request: can't remove chat owner") + + // Super/groups errors + ErrBotKickedFromGroup = NewAPIError(403, "Forbidden: bot was kicked from the group chat") + ErrBotKickedFromSuperGroup = NewAPIError(403, "Forbidden: bot was kicked from the supergroup chat") +) + +// ErrByDescription returns APIError instance by given description. +func ErrByDescription(s string) error { + switch s { + case ErrUnauthorized.ʔ(): + return ErrUnauthorized + case ErrNotStartedByUser.ʔ(): + return ErrNotStartedByUser + case ErrNotFound.ʔ(): + return ErrNotFound + case ErrUserIsDeactivated.ʔ(): + return ErrUserIsDeactivated + case ErrToForwardNotFound.ʔ(): + return ErrToForwardNotFound + case ErrToReplyNotFound.ʔ(): + return ErrToReplyNotFound + case ErrMessageTooLong.ʔ(): + return ErrMessageTooLong + case ErrBlockedByUser.ʔ(): + return ErrBlockedByUser + case ErrToDeleteNotFound.ʔ(): + return ErrToDeleteNotFound + case ErrEmptyMessage.ʔ(): + return ErrEmptyMessage + case ErrEmptyText.ʔ(): + return ErrEmptyText + case ErrEmptyChatID.ʔ(): + return ErrEmptyChatID + case ErrChatNotFound.ʔ(): + return ErrChatNotFound + case ErrMessageNotModified.ʔ(): + return ErrMessageNotModified + case ErrSameMessageContent.ʔ(): + return ErrSameMessageContent + case ErrCantEditMessage.ʔ(): + return ErrCantEditMessage + case ErrButtonDataInvalid.ʔ(): + return ErrButtonDataInvalid + case ErrBadPollOptions.ʔ(): + return ErrBadPollOptions + case ErrNoRightsToRestrict.ʔ(): + return ErrNoRightsToRestrict + case ErrNoRightsToSend.ʔ(): + return ErrNoRightsToSend + case ErrNoRightsToSendPhoto.ʔ(): + return ErrNoRightsToSendPhoto + case ErrNoRightsToSendStickers.ʔ(): + return ErrNoRightsToSendStickers + case ErrNoRightsToSendGifs.ʔ(): + return ErrNoRightsToSendGifs + case ErrNoRightsToDelete.ʔ(): + return ErrNoRightsToDelete + case ErrKickingChatOwner.ʔ(): + return ErrKickingChatOwner + case ErrBotKickedFromGroup.ʔ(): + return ErrKickingChatOwner + case ErrBotKickedFromSuperGroup.ʔ(): + return ErrBotKickedFromSuperGroup + case ErrWrongTypeOfContent.ʔ(): + return ErrWrongTypeOfContent + case ErrBadURLContent.ʔ(): + return ErrBadURLContent + case ErrWrongFileIDSymbol.ʔ(): + return ErrWrongFileIDSymbol + case ErrWrongFileIDLength.ʔ(): + return ErrWrongFileIDLength + case ErrWrongFileIDCharacter.ʔ(): + return ErrWrongFileIDCharacter + case ErrWrongFileID.ʔ(): + return ErrWrongFileID + case ErrTooLarge.ʔ(): + return ErrTooLarge + case ErrWrongFileIDPadding.ʔ(): + return ErrWrongFileIDPadding + case ErrFailedImageProcess.ʔ(): + return ErrFailedImageProcess + case ErrInvalidStickerSet.ʔ(): + return ErrInvalidStickerSet + case ErrGroupMigrated.ʔ(): + return ErrGroupMigrated + default: + return nil + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/file.go b/vendor/gopkg.in/tucnak/telebot.v2/file.go new file mode 100644 index 0000000..6868e94 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/file.go @@ -0,0 +1,87 @@ +package telebot + +import ( + "io" + "os" +) + +// File object represents any sort of file. +type File struct { + FileID string `json:"file_id"` + UniqueID string `json:"file_unique_id"` + FileSize int `json:"file_size"` + + // file on telegram server https://core.telegram.org/bots/api#file + FilePath string `json:"file_path"` + + // file on local file system. + FileLocal string `json:"file_local"` + + // file on the internet + FileURL string `json:"file_url"` + + // file backed with io.Reader + FileReader io.Reader `json:"-"` + + fileName string +} + +// FromDisk constructs a new local (on-disk) file object. +// +// Note, it returns File, not *File for a very good reason: +// in telebot, File is pretty much an embeddable struct, +// so upon uploading media you'll need to set embedded File +// with something. NewFile() returning File makes it a one-liner. +// +// photo := &tb.Photo{File: tb.FromDisk("chicken.jpg")} +// +func FromDisk(filename string) File { + return File{FileLocal: filename} +} + +// FromURL constructs a new file on provided HTTP URL. +// +// Note, it returns File, not *File for a very good reason: +// in telebot, File is pretty much an embeddable struct, +// so upon uploading media you'll need to set embedded File +// with something. NewFile() returning File makes it a one-liner. +// +// photo := &tb.Photo{File: tb.FromURL("https://site.com/picture.jpg")} +// +func FromURL(url string) File { + return File{FileURL: url} +} + +// FromReader constructs a new file from io.Reader. +// +// Note, it returns File, not *File for a very good reason: +// in telebot, File is pretty much an embeddable struct, +// so upon uploading media you'll need to set embedded File +// with something. NewFile() returning File makes it a one-liner. +// +// photo := &tb.Photo{File: tb.FromReader(bytes.NewReader(...))} +// +func FromReader(reader io.Reader) File { + return File{FileReader: reader} +} + +func (f *File) stealRef(g *File) { + if g.OnDisk() { + f.FileLocal = g.FileLocal + } + + if g.FileURL != "" { + f.FileURL = g.FileURL + } +} + +// InCloud tells whether the file is present on Telegram servers. +func (f *File) InCloud() bool { + return f.FileID != "" +} + +// OnDisk will return true if file is present on disk. +func (f *File) OnDisk() bool { + _, err := os.Stat(f.FileLocal) + return err == nil +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/games.go b/vendor/gopkg.in/tucnak/telebot.v2/games.go new file mode 100644 index 0000000..cceba14 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/games.go @@ -0,0 +1,98 @@ +package telebot + +import ( + "encoding/json" + "strconv" +) + +// Game object represents a game. +// Their short names acts as unique identifiers. +type Game struct { + Name string `json:"game_short_name"` + + Title string `json:"title"` + Description string `json:"description"` + Photo *Photo `json:"photo"` + + // (Optional) + Text string `json:"text"` + Entities []MessageEntity `json:"text_entities"` + Animation *Animation `json:"animation"` +} + +// GameHighScore object represents one row +// of the high scores table for a game. +type GameHighScore struct { + User *User `json:"user"` + Position int `json:"position"` + + Score int `json:"score"` + Force bool `json:"force"` + NoEdit bool `json:"disable_edit_message"` +} + +// GetGameScores returns the score of the specified user +// and several of their neighbors in a game. +// +// This method will currently return scores for the target user, +// plus two of their closest neighbors on each side. +// Will also return the top three users +// if the user and his neighbors are not among them. +// +// This function will panic upon nil Editable. +func (b *Bot) GetGameScores(user Recipient, msg Editable) ([]GameHighScore, error) { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "user_id": user.Recipient(), + } + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + data, err := b.Raw("getGameHighScores", params) + if err != nil { + return nil, err + } + + var resp struct { + Result []GameHighScore + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, err + } + return resp.Result, nil +} + +// SetGameScore sets the score of the specified user in a game. +// +// If the message was sent by the bot, returns the edited Message, +// otherwise returns nil and ErrTrueResult. +// +func (b *Bot) SetGameScore(user Recipient, msg Editable, score GameHighScore) (*Message, error) { + msgID, chatID := msg.MessageSig() + + params := map[string]string{ + "user_id": user.Recipient(), + "score": strconv.Itoa(score.Score), + "force": strconv.FormatBool(score.Force), + "disable_edit_message": strconv.FormatBool(score.NoEdit), + } + + if chatID == 0 { // if inline message + params["inline_message_id"] = msgID + } else { + params["chat_id"] = strconv.FormatInt(chatID, 10) + params["message_id"] = msgID + } + + data, err := b.Raw("setGameScore", params) + if err != nil { + return nil, err + } + return extractMessage(data) +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/inline.go b/vendor/gopkg.in/tucnak/telebot.v2/inline.go new file mode 100644 index 0000000..fee643a --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/inline.go @@ -0,0 +1,134 @@ +package telebot + +import ( + "encoding/json" + "fmt" +) + +// ChosenInlineResult represents a result of an inline query that was chosen +// by the user and sent to their chat partner. +type ChosenInlineResult struct { + From User `json:"from"` + Location *Location `json:"location,omitempty"` + ResultID string `json:"result_id"` + Query string `json:"query"` + MessageID string `json:"inline_message_id"` // inline messages only! +} + +// Query is an incoming inline query. When the user sends +// an empty query, your bot could return some default or +// trending results. +type Query struct { + // Unique identifier for this query. 1-64 bytes. + ID string `json:"id"` + + // Sender. + From User `json:"from"` + + // Sender location, only for bots that request user location. + Location *Location `json:"location"` + + // Text of the query (up to 512 characters). + Text string `json:"query"` + + // Offset of the results to be returned, can be controlled by the bot. + Offset string `json:"offset"` + + // ChatType of the type of the chat, from which the inline query was sent. + ChatType string `json:"chat_type"` +} + +// QueryResponse builds a response to an inline Query. +// See also: https://core.telegram.org/bots/api#answerinlinequery +type QueryResponse struct { + // The ID of the query to which this is a response. + // + // Note: Telebot sets this field automatically! + QueryID string `json:"inline_query_id"` + + // The results for the inline query. + Results Results `json:"results"` + + // (Optional) The maximum amount of time in seconds that the result + // of the inline query may be cached on the server. + CacheTime int `json:"cache_time,omitempty"` + + // (Optional) Pass True, if results may be cached on the server side + // only for the user that sent the query. By default, results may + // be returned to any user who sends the same query. + IsPersonal bool `json:"is_personal"` + + // (Optional) Pass the offset that a client should send in the next + // query with the same text to receive more results. Pass an empty + // string if there are no more results or if you don‘t support + // pagination. Offset length can’t exceed 64 bytes. + NextOffset string `json:"next_offset"` + + // (Optional) If passed, clients will display a button with specified + // text that switches the user to a private chat with the bot and sends + // the bot a start message with the parameter switch_pm_parameter. + SwitchPMText string `json:"switch_pm_text,omitempty"` + + // (Optional) Parameter for the start message sent to the bot when user + // presses the switch button. + SwitchPMParameter string `json:"switch_pm_parameter,omitempty"` +} + +// Result represents one result of an inline query. +type Result interface { + ResultID() string + SetResultID(string) + SetContent(InputMessageContent) + SetReplyMarkup([][]InlineButton) + Process() +} + +// Results is a slice wrapper for convenient marshalling. +type Results []Result + +// MarshalJSON makes sure IQRs have proper IDs and Type variables set. +func (results Results) MarshalJSON() ([]byte, error) { + for _, result := range results { + if result.ResultID() == "" { + result.SetResultID(fmt.Sprintf("%d", &result)) + } + if err := inferIQR(result); err != nil { + return nil, err + } + } + + return json.Marshal([]Result(results)) +} + +func inferIQR(result Result) error { + switch r := result.(type) { + case *ArticleResult: + r.Type = "article" + case *AudioResult: + r.Type = "audio" + case *ContactResult: + r.Type = "contact" + case *DocumentResult: + r.Type = "document" + case *GifResult: + r.Type = "gif" + case *LocationResult: + r.Type = "location" + case *Mpeg4GifResult: + r.Type = "mpeg4_gif" + case *PhotoResult: + r.Type = "photo" + case *VenueResult: + r.Type = "venue" + case *VideoResult: + r.Type = "video" + case *VoiceResult: + r.Type = "voice" + case *StickerResult: + r.Type = "sticker" + default: + return fmt.Errorf("result %v is not supported", result) + } + + return nil +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/inline_types.go b/vendor/gopkg.in/tucnak/telebot.v2/inline_types.go new file mode 100644 index 0000000..cd745eb --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/inline_types.go @@ -0,0 +1,394 @@ +package telebot + +// ResultBase must be embedded into all IQRs. +type ResultBase struct { + // Unique identifier for this result, 1-64 Bytes. + // If left unspecified, a 64-bit FNV-1 hash will be calculated + ID string `json:"id"` + + // Ignore. This field gets set automatically. + Type string `json:"type"` + + // Optional. Content of the message to be sent. + Content *InputMessageContent `json:"input_message_content,omitempty"` + + // Optional. Inline keyboard attached to the message. + ReplyMarkup *InlineKeyboardMarkup `json:"reply_markup,omitempty"` +} + +// ResultID returns ResultBase.ID. +func (r *ResultBase) ResultID() string { + return r.ID +} + +// SetResultID sets ResultBase.ID. +func (r *ResultBase) SetResultID(id string) { + r.ID = id +} + +// SetContent sets ResultBase.Content. +func (r *ResultBase) SetContent(content InputMessageContent) { + r.Content = &content +} + +// SetReplyMarkup sets ResultBase.ReplyMarkup. +func (r *ResultBase) SetReplyMarkup(keyboard [][]InlineButton) { + r.ReplyMarkup = &InlineKeyboardMarkup{InlineKeyboard: keyboard} +} + +func (r *ResultBase) Process() { + if r.ReplyMarkup != nil { + processButtons(r.ReplyMarkup.InlineKeyboard) + } +} + +// ArticleResult represents a link to an article or web page. +// See also: https://core.telegram.org/bots/api#inlinequeryresultarticle +type ArticleResult struct { + ResultBase + + // Title of the result. + Title string `json:"title"` + + // Message text. Shortcut (and mutually exclusive to) specifying + // InputMessageContent. + Text string `json:"message_text,omitempty"` + + // Optional. URL of the result. + URL string `json:"url,omitempty"` + + // Optional. Pass True, if you don't want the URL to be shown in the message. + HideURL bool `json:"hide_url,omitempty"` + + // Optional. Short description of the result. + Description string `json:"description,omitempty"` + + // Optional. URL of the thumbnail for the result. + ThumbURL string `json:"thumb_url,omitempty"` + + // Optional. Width of the thumbnail for the result. + ThumbWidth int `json:"thumb_width,omitempty"` + + // Optional. Height of the thumbnail for the result. + ThumbHeight int `json:"thumb_height,omitempty"` +} + +// AudioResult represents a link to an mp3 audio file. +type AudioResult struct { + ResultBase + + // Title. + Title string `json:"title"` + + // A valid URL for the audio file. + URL string `json:"audio_url"` + + // Optional. Performer. + Performer string `json:"performer,omitempty"` + + // Optional. Audio duration in seconds. + Duration int `json:"audio_duration,omitempty"` + + // Optional. Caption, 0-1024 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"audio_file_id,omitempty"` +} + +// ContentResult represents a contact with a phone number. +// See also: https://core.telegram.org/bots/api#inlinequeryresultcontact +type ContactResult struct { + ResultBase + + // Contact's phone number. + PhoneNumber string `json:"phone_number"` + + // Optional. Additional data about the contact in the form of a vCard, 0-2048 bytes. + VCard string `json:"vcard,omitempty"` + + // Contact's first name. + FirstName string `json:"first_name"` + + // Optional. Contact's last name. + LastName string `json:"last_name,omitempty"` + + // Optional. URL of the thumbnail for the result. + ThumbURL string `json:"thumb_url,omitempty"` + + // Optional. Width of the thumbnail for the result. + ThumbWidth int `json:"thumb_width,omitempty"` + + // Optional. Height of the thumbnail for the result. + ThumbHeight int `json:"thumb_height,omitempty"` +} + +// DocumentResult represents a link to a file. +// See also: https://core.telegram.org/bots/api#inlinequeryresultdocument +type DocumentResult struct { + ResultBase + + // Title for the result. + Title string `json:"title"` + + // A valid URL for the file + URL string `json:"document_url"` + + // Mime type of the content of the file, either “application/pdf” or + // “application/zip”. + MIME string `json:"mime_type"` + + // Optional. Caption of the document to be sent, 0-200 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // Optional. Short description of the result. + Description string `json:"description,omitempty"` + + // Optional. URL of the thumbnail (jpeg only) for the file. + ThumbURL string `json:"thumb_url,omitempty"` + + // Optional. Width of the thumbnail for the result. + ThumbWidth int `json:"thumb_width,omitempty"` + + // Optional. Height of the thumbnail for the result. + ThumbHeight int `json:"thumb_height,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"document_file_id,omitempty"` +} + +// GifResult represents a link to an animated GIF file. +// See also: https://core.telegram.org/bots/api#inlinequeryresultgif +type GifResult struct { + ResultBase + + // A valid URL for the GIF file. File size must not exceed 1MB. + URL string `json:"gif_url"` + + // Optional. Width of the GIF. + Width int `json:"gif_width,omitempty"` + + // Optional. Height of the GIF. + Height int `json:"gif_height,omitempty"` + + // Optional. Duration of the GIF. + Duration int `json:"gif_duration,omitempty"` + + // URL of the static thumbnail for the result (jpeg or gif). + ThumbURL string `json:"thumb_url"` + + // Optional. MIME type of the thumbnail, must be one of + // “image/jpeg”, “image/gif”, or “video/mp4”. + ThumbMIME string `json:"thumb_mime_type,omitempty"` + + // Optional. Title for the result. + Title string `json:"title,omitempty"` + + // Optional. Caption of the GIF file to be sent, 0-200 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"gif_file_id,omitempty"` +} + +// LocationResult represents a location on a map. +// See also: https://core.telegram.org/bots/api#inlinequeryresultlocation +type LocationResult struct { + ResultBase + + Location + + // Location title. + Title string `json:"title"` + + // Optional. Url of the thumbnail for the result. + ThumbURL string `json:"thumb_url,omitempty"` +} + +// ResultMpeg4Gif represents a link to a video animation +// (H.264/MPEG-4 AVC video without sound). +// See also: https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif +type Mpeg4GifResult struct { + ResultBase + + // A valid URL for the MP4 file. + URL string `json:"mpeg4_url"` + + // Optional. Video width. + Width int `json:"mpeg4_width,omitempty"` + + // Optional. Video height. + Height int `json:"mpeg4_height,omitempty"` + + // Optional. Video duration. + Duration int `json:"mpeg4_duration,omitempty"` + + // URL of the static thumbnail (jpeg or gif) for the result. + ThumbURL string `json:"thumb_url,omitempty"` + + // Optional. MIME type of the thumbnail, must be one of + // “image/jpeg”, “image/gif”, or “video/mp4”. + ThumbMIME string `json:"thumb_mime_type,omitempty"` + + // Optional. Title for the result. + Title string `json:"title,omitempty"` + + // Optional. Caption of the MPEG-4 file to be sent, 0-200 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"mpeg4_file_id,omitempty"` +} + +// ResultResult represents a link to a photo. +// See also: https://core.telegram.org/bots/api#inlinequeryresultphoto +type PhotoResult struct { + ResultBase + + // A valid URL of the photo. Photo must be in jpeg format. + // Photo size must not exceed 5MB. + URL string `json:"photo_url"` + + // Optional. Width of the photo. + Width int `json:"photo_width,omitempty"` + + // Optional. Height of the photo. + Height int `json:"photo_height,omitempty"` + + // Optional. Title for the result. + Title string `json:"title,omitempty"` + + // Optional. Short description of the result. + Description string `json:"description,omitempty"` + + // Optional. Caption of the photo to be sent, 0-200 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // URL of the thumbnail for the photo. + ThumbURL string `json:"thumb_url"` + + // If Cache != "", it'll be used instead + Cache string `json:"photo_file_id,omitempty"` +} + +// VenueResult represents a venue. +// See also: https://core.telegram.org/bots/api#inlinequeryresultvenue +type VenueResult struct { + ResultBase + + Location + + // Title of the venue. + Title string `json:"title"` + + // Address of the venue. + Address string `json:"address"` + + // Optional. Foursquare identifier of the venue if known. + FoursquareID string `json:"foursquare_id,omitempty"` + + // Optional. URL of the thumbnail for the result. + ThumbURL string `json:"thumb_url,omitempty"` + + // Optional. Width of the thumbnail for the result. + ThumbWidth int `json:"thumb_width,omitempty"` + + // Optional. Height of the thumbnail for the result. + ThumbHeight int `json:"thumb_height,omitempty"` +} + +// VideoResult represents a link to a page containing an embedded +// video player or a video file. +// See also: https://core.telegram.org/bots/api#inlinequeryresultvideo +type VideoResult struct { + ResultBase + + // A valid URL for the embedded video player or video file. + URL string `json:"video_url"` + + // Mime type of the content of video url, “text/html” or “video/mp4”. + MIME string `json:"mime_type"` + + // URL of the thumbnail (jpeg only) for the video. + ThumbURL string `json:"thumb_url"` + + // Title for the result. + Title string `json:"title"` + + // Optional. Caption of the video to be sent, 0-200 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // Optional. Video width. + Width int `json:"video_width,omitempty"` + + // Optional. Video height. + Height int `json:"video_height,omitempty"` + + // Optional. Video duration in seconds. + Duration int `json:"video_duration,omitempty"` + + // Optional. Short description of the result. + Description string `json:"description,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"video_file_id,omitempty"` +} + +// VoiceResult represents a link to a voice recording in an .ogg +// container encoded with OPUS. +// +// See also: https://core.telegram.org/bots/api#inlinequeryresultvoice +type VoiceResult struct { + ResultBase + + // A valid URL for the voice recording. + URL string `json:"voice_url"` + + // Recording title. + Title string `json:"title"` + + // Optional. Recording duration in seconds. + Duration int `json:"voice_duration"` + + // Optional. Caption, 0-1024 characters. + Caption string `json:"caption,omitempty"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in the media caption. + ParseMode ParseMode `json:"parse_mode,omitempty"` + + // If Cache != "", it'll be used instead + Cache string `json:"voice_file_id,omitempty"` +} + +// StickerResult represents an inline cached sticker response. +type StickerResult struct { + ResultBase + + // If Cache != "", it'll be used instead + Cache string `json:"sticker_file_id,omitempty"` +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/input_types.go b/vendor/gopkg.in/tucnak/telebot.v2/input_types.go new file mode 100644 index 0000000..3bf85bf --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/input_types.go @@ -0,0 +1,78 @@ +package telebot + +// InputMessageContent objects represent the content of a message to be sent +// as a result of an inline query. +// See also: https://core.telegram.org/bots/api#inputmessagecontent +type InputMessageContent interface { + IsInputMessageContent() bool +} + +// InputTextMessageContent represents the content of a text message to be +// sent as the result of an inline query. +// See also: https://core.telegram.org/bots/api#inputtextmessagecontent +type InputTextMessageContent struct { + // Text of the message to be sent, 1-4096 characters. + Text string `json:"message_text"` + + // Optional. Send Markdown or HTML, if you want Telegram apps to show + // bold, italic, fixed-width text or inline URLs in your bot's message. + ParseMode string `json:"parse_mode,omitempty"` + + // Optional. Disables link previews for links in the sent message. + DisablePreview bool `json:"disable_web_page_preview"` +} + +func (input *InputTextMessageContent) IsInputMessageContent() bool { + return true +} + +// InputLocationMessageContent represents the content of a location message +// to be sent as the result of an inline query. +// See also: https://core.telegram.org/bots/api#inputlocationmessagecontent +type InputLocationMessageContent struct { + Lat float32 `json:"latitude"` + Lng float32 `json:"longitude"` +} + +func (input *InputLocationMessageContent) IsInputMessageContent() bool { + return true +} + +// InputVenueMessageContent represents the content of a venue message to +// be sent as the result of an inline query. +// See also: https://core.telegram.org/bots/api#inputvenuemessagecontent +type InputVenueMessageContent struct { + Lat float32 `json:"latitude"` + Lng float32 `json:"longitude"` + + // Name of the venue. + Title string `json:"title"` + + // Address of the venue. + Address string `json:"address"` + + // Optional. Foursquare identifier of the venue, if known. + FoursquareID string `json:"foursquare_id,omitempty"` +} + +func (input *InputVenueMessageContent) IsInputMessageContent() bool { + return true +} + +// InputContactMessageContent represents the content of a contact +// message to be sent as the result of an inline query. +// See also: https://core.telegram.org/bots/api#inputcontactmessagecontent +type InputContactMessageContent struct { + // Contact's phone number. + PhoneNumber string `json:"phone_number"` + + // Contact's first name. + FirstName string `json:"first_name"` + + // Optional. Contact's last name. + LastName string `json:"last_name,omitempty"` +} + +func (input *InputContactMessageContent) IsInputMessageContent() bool { + return true +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/media.go b/vendor/gopkg.in/tucnak/telebot.v2/media.go new file mode 100644 index 0000000..4d67c70 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/media.go @@ -0,0 +1,236 @@ +package telebot + +import ( + "encoding/json" +) + +// Album lets you group multiple media (so-called InputMedia) +// into a single message. +// +// On older clients albums look like N regular messages. +type Album []InputMedia + +// InputMedia is a generic type for all kinds of media you +// can put into an album. +type InputMedia interface { + // As some files must be uploaded (instead of referencing) + // outer layers of Telebot require it. + MediaFile() *File +} + +// Photo object represents a single photo file. +type Photo struct { + File + + Width int `json:"width"` + Height int `json:"height"` + Caption string `json:"caption,omitempty"` +} + +type photoSize struct { + File + Width int `json:"width"` + Height int `json:"height"` + Caption string `json:"caption,omitempty"` +} + +// MediaFile returns &Photo.File +func (p *Photo) MediaFile() *File { + return &p.File +} + +// UnmarshalJSON is custom unmarshaller required to abstract +// away the hassle of treating different thumbnail sizes. +// Instead, Telebot chooses the hi-res one and just sticks to +// it. +// +// I really do find it a beautiful solution. +func (p *Photo) UnmarshalJSON(jsonStr []byte) error { + var hq photoSize + + if jsonStr[0] == '{' { + if err := json.Unmarshal(jsonStr, &hq); err != nil { + return err + } + } else { + var sizes []photoSize + if err := json.Unmarshal(jsonStr, &sizes); err != nil { + return err + } + + hq = sizes[len(sizes)-1] + } + + p.File = hq.File + p.Width = hq.Width + p.Height = hq.Height + + return nil +} + +// Audio object represents an audio file. +type Audio struct { + File + + // Duration of the recording in seconds as defined by sender. + Duration int `json:"duration,omitempty"` + + // (Optional) + Caption string `json:"caption,omitempty"` + Thumbnail *Photo `json:"thumb,omitempty"` + Title string `json:"title,omitempty"` + Performer string `json:"performer,omitempty"` + MIME string `json:"mime_type,omitempty"` + FileName string `json:"file_name,omitempty"` +} + +// MediaFile returns &Audio.File +func (a *Audio) MediaFile() *File { + a.fileName = a.FileName + return &a.File +} + +// Document object represents a general file (as opposed to Photo or Audio). +// Telegram users can send files of any type of up to 1.5 GB in size. +type Document struct { + File + + // (Optional) + Thumbnail *Photo `json:"thumb,omitempty"` + Caption string `json:"caption,omitempty"` + MIME string `json:"mime_type"` + FileName string `json:"file_name,omitempty"` +} + +// MediaFile returns &Document.File +func (d *Document) MediaFile() *File { + d.fileName = d.FileName + return &d.File +} + +// Video object represents a video file. +type Video struct { + File + + Width int `json:"width"` + Height int `json:"height"` + + Duration int `json:"duration,omitempty"` + + // (Optional) + Caption string `json:"caption,omitempty"` + Thumbnail *Photo `json:"thumb,omitempty"` + SupportsStreaming bool `json:"supports_streaming,omitempty"` + MIME string `json:"mime_type,omitempty"` + FileName string `json:"file_name,omitempty"` +} + +// MediaFile returns &Video.File +func (v *Video) MediaFile() *File { + v.fileName = v.FileName + return &v.File +} + +// Animation object represents a animation file. +type Animation struct { + File + + Width int `json:"width"` + Height int `json:"height"` + Duration int `json:"duration,omitempty"` + + // (Optional) + Caption string `json:"caption,omitempty"` + Thumbnail *Photo `json:"thumb,omitempty"` + MIME string `json:"mime_type,omitempty"` + FileName string `json:"file_name,omitempty"` +} + +// MediaFile returns &Animation.File +func (a *Animation) MediaFile() *File { + a.fileName = a.FileName + return &a.File +} + +// Voice object represents a voice note. +type Voice struct { + File + + // Duration of the recording in seconds as defined by sender. + Duration int `json:"duration"` + + // (Optional) + Caption string `json:"caption,omitempty"` + MIME string `json:"mime_type,omitempty"` +} + +// VideoNote represents a video message (available in Telegram apps +// as of v.4.0). +type VideoNote struct { + File + + // Duration of the recording in seconds as defined by sender. + Duration int `json:"duration"` + + // (Optional) + Thumbnail *Photo `json:"thumb,omitempty"` + Length int `json:"length,omitempty"` +} + +// Contact object represents a contact to Telegram user +type Contact struct { + PhoneNumber string `json:"phone_number"` + FirstName string `json:"first_name"` + + // (Optional) + LastName string `json:"last_name"` + UserID int64 `json:"user_id,omitempty"` +} + +// Location object represents geographic position. +type Location struct { + // Latitude + Lat float32 `json:"latitude"` + // Longitude + Lng float32 `json:"longitude"` + + // Horizontal Accuracy + HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"` + + // Period in seconds for which the location will be updated + // (see Live Locations, should be between 60 and 86400.) + LivePeriod int `json:"live_period,omitempty"` + + Heading int `json:"heading,omitempty"` + + ProximityAlertRadius int `json:"proximity_alert_radius,omitempty"` +} + +// ProximityAlertTriggered sent whenever +// a user in the chat triggers a proximity alert set by another user. +type ProximityAlertTriggered struct { + Traveler *User `json:"traveler,omitempty"` + Watcher *User `json:"watcher,omitempty"` + Distance int `json:"distance"` +} + +// Venue object represents a venue location with name, address and +// optional foursquare ID. +type Venue struct { + Location Location `json:"location"` + Title string `json:"title"` + Address string `json:"address"` + + // (Optional) + FoursquareID string `json:"foursquare_id,omitempty"` + FoursquareType string `json:"foursquare_type,omitempty"` + GooglePlaceID string `json:"google_place_id,omitempty"` + GooglePlaceType string `json:"google_place_type,omitempty"` +} + +// Dice object represents a dice with a random value +// from 1 to 6 for currently supported base emoji. +type Dice struct { + Type DiceType `json:"emoji"` + Value int `json:"value"` +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/message.go b/vendor/gopkg.in/tucnak/telebot.v2/message.go new file mode 100644 index 0000000..4b5b24c --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/message.go @@ -0,0 +1,338 @@ +package telebot + +import ( + "strconv" + "time" +) + +// Message object represents a message. +type Message struct { + ID int `json:"message_id"` + + InlineID string `json:"-"` + + // For message sent to channels, Sender will be nil + Sender *User `json:"from"` + + // Unixtime, use Message.Time() to get time.Time + Unixtime int64 `json:"date"` + + // Conversation the message belongs to. + Chat *Chat `json:"chat"` + + // Sender of the message, sent on behalf of a chat. + SenderChat *Chat `json:"sender_chat"` + + // For forwarded messages, sender of the original message. + OriginalSender *User `json:"forward_from"` + + // For forwarded messages, chat of the original message when + // forwarded from a channel. + OriginalChat *Chat `json:"forward_from_chat"` + + // For forwarded messages, identifier of the original message + // when forwarded from a channel. + OriginalMessageID int `json:"forward_from_message_id"` + + // For forwarded messages, signature of the post author. + OriginalSignature string `json:"forward_signature"` + + // For forwarded messages, sender's name from users who + // disallow adding a link to their account. + OriginalSenderName string `json:"forward_sender_name"` + + // For forwarded messages, unixtime of the original message. + OriginalUnixtime int `json:"forward_date"` + + // For replies, ReplyTo represents the original message. + // + // Note that the Message object in this field will not + // contain further ReplyTo fields even if it + // itself is a reply. + ReplyTo *Message `json:"reply_to_message"` + + // Shows through which bot the message was sent. + Via *User `json:"via_bot"` + + // (Optional) Time of last edit in Unix + LastEdit int64 `json:"edit_date"` + + // AlbumID is the unique identifier of a media message group + // this message belongs to. + AlbumID string `json:"media_group_id"` + + // Author signature (in channels). + Signature string `json:"author_signature"` + + // For a text message, the actual UTF-8 text of the message. + Text string `json:"text"` + + // For registered commands, will contain the string payload: + // + // Ex: `/command ` or `/command@botname ` + Payload string `json:"-"` + + // For text messages, special entities like usernames, URLs, bot commands, + // etc. that appear in the text. + Entities []MessageEntity `json:"entities,omitempty"` + + // Some messages containing media, may as well have a caption. + Caption string `json:"caption,omitempty"` + + // For messages with a caption, special entities like usernames, URLs, + // bot commands, etc. that appear in the caption. + CaptionEntities []MessageEntity `json:"caption_entities,omitempty"` + + // For an audio recording, information about it. + Audio *Audio `json:"audio"` + + // For a general file, information about it. + Document *Document `json:"document"` + + // For a photo, all available sizes (thumbnails). + Photo *Photo `json:"photo"` + + // For a game, information about it. + Game *Game `json:"game"` + + // For a sticker, information about it. + Sticker *Sticker `json:"sticker"` + + // For a voice message, information about it. + Voice *Voice `json:"voice"` + + // For a video note, information about it. + VideoNote *VideoNote `json:"video_note"` + + // For a video, information about it. + Video *Video `json:"video"` + + // For a animation, information about it. + Animation *Animation `json:"animation"` + + // For a contact, contact information itself. + Contact *Contact `json:"contact"` + + // For a location, its longitude and latitude. + Location *Location `json:"location"` + + // For a venue, information about it. + Venue *Venue `json:"venue"` + + // For a poll, information the native poll. + Poll *Poll `json:"poll"` + + // For a dice, information about it. + Dice *Dice `json:"dice"` + + // For a service message, represents a user, + // that just got added to chat, this message came from. + // + // Sender leads to User, capable of invite. + // + // UserJoined might be the Bot itself. + UserJoined *User `json:"new_chat_member"` + + // For a service message, represents a user, + // that just left chat, this message came from. + // + // If user was kicked, Sender leads to a User, + // capable of this kick. + // + // UserLeft might be the Bot itself. + UserLeft *User `json:"left_chat_member"` + + // For a service message, represents a new title + // for chat this message came from. + // + // Sender would lead to a User, capable of change. + NewGroupTitle string `json:"new_chat_title"` + + // For a service message, represents all available + // thumbnails of the new chat photo. + // + // Sender would lead to a User, capable of change. + NewGroupPhoto *Photo `json:"new_chat_photo"` + + // For a service message, new members that were added to + // the group or supergroup and information about them + // (the bot itself may be one of these members). + UsersJoined []User `json:"new_chat_members"` + + // For a service message, true if chat photo just + // got removed. + // + // Sender would lead to a User, capable of change. + GroupPhotoDeleted bool `json:"delete_chat_photo"` + + // For a service message, true if group has been created. + // + // You would receive such a message if you are one of + // initial group chat members. + // + // Sender would lead to creator of the chat. + GroupCreated bool `json:"group_chat_created"` + + // For a service message, true if supergroup has been created. + // + // You would receive such a message if you are one of + // initial group chat members. + // + // Sender would lead to creator of the chat. + SuperGroupCreated bool `json:"supergroup_chat_created"` + + // For a service message, true if channel has been created. + // + // You would receive such a message if you are one of + // initial channel administrators. + // + // Sender would lead to creator of the chat. + ChannelCreated bool `json:"channel_chat_created"` + + // For a service message, the destination (supergroup) you + // migrated to. + // + // You would receive such a message when your chat has migrated + // to a supergroup. + // + // Sender would lead to creator of the migration. + MigrateTo int64 `json:"migrate_to_chat_id"` + + // For a service message, the Origin (normal group) you migrated + // from. + // + // You would receive such a message when your chat has migrated + // to a supergroup. + // + // Sender would lead to creator of the migration. + MigrateFrom int64 `json:"migrate_from_chat_id"` + + // Specified message was pinned. Note that the Message object + // in this field will not contain further ReplyTo fields even + // if it is itself a reply. + PinnedMessage *Message `json:"pinned_message"` + + // Message is an invoice for a payment. + Invoice *Invoice `json:"invoice"` + + // Message is a service message about a successful payment. + Payment *Payment `json:"successful_payment"` + + // The domain name of the website on which the user has logged in. + ConnectedWebsite string `json:"connected_website,omitempty"` + + // Inline keyboard attached to the message. + ReplyMarkup InlineKeyboardMarkup `json:"reply_markup"` + + VoiceChatSchedule *VoiceChatScheduled `json:"voice_chat_scheduled,omitempty"` + + // For a service message, a voice chat started in the chat. + VoiceChatStarted *VoiceChatStarted `json:"voice_chat_started,omitempty"` + + // For a service message, a voice chat ended in the chat. + VoiceChatEnded *VoiceChatEnded `json:"voice_chat_ended,omitempty"` + + // For a service message, some users were invited in the voice chat. + VoiceChatParticipantsInvited *VoiceChatParticipantsInvited `json:"voice_chat_participants_invited,omitempty"` + + // For a service message, represents the content of a service message, + // sent whenever a user in the chat triggers a proximity alert set by another user. + ProximityAlert *ProximityAlertTriggered `json:"proximity_alert_triggered,omitempty"` + + // For a service message, represents about a change in auto-delete timer settings. + AutoDeleteTimer *MessageAutoDeleteTimerChanged `json:"message_auto_delete_timer_changed,omitempty"` +} + +// MessageAutoDeleteTimerChanged represents a service message about a change in auto-delete timer settings. +type MessageAutoDeleteTimerChanged struct { + DeleteTime int `json:"message_auto_delete_time"` +} + +// MessageEntity object represents "special" parts of text messages, +// including hashtags, usernames, URLs, etc. +type MessageEntity struct { + // Specifies entity type. + Type EntityType `json:"type"` + + // Offset in UTF-16 code units to the start of the entity. + Offset int `json:"offset"` + + // Length of the entity in UTF-16 code units. + Length int `json:"length"` + + // (Optional) For EntityTextLink entity type only. + // + // URL will be opened after user taps on the text. + URL string `json:"url,omitempty"` + + // (Optional) For EntityTMention entity type only. + User *User `json:"user,omitempty"` + + // (Optional) For EntityCodeBlock entity type only. + Language string `json:"language,omitempty"` +} + +// MessageSig satisfies Editable interface (see Editable.) +func (m *Message) MessageSig() (string, int64) { + if m.InlineID != "" { + return m.InlineID, 0 + } + return strconv.Itoa(m.ID), m.Chat.ID +} + +// Time returns the moment of message creation in local time. +func (m *Message) Time() time.Time { + return time.Unix(m.Unixtime, 0) +} + +// LastEdited returns time.Time of last edit. +func (m *Message) LastEdited() time.Time { + return time.Unix(m.LastEdit, 0) +} + +// IsForwarded says whether message is forwarded copy of another +// message or not. +func (m *Message) IsForwarded() bool { + return m.OriginalSender != nil || m.OriginalChat != nil +} + +// IsReply says whether message is a reply to another message. +func (m *Message) IsReply() bool { + return m.ReplyTo != nil +} + +// Private returns true, if it's a personal message. +func (m *Message) Private() bool { + return m.Chat.Type == ChatPrivate +} + +// FromGroup returns true, if message came from a group OR a supergroup. +func (m *Message) FromGroup() bool { + return m.Chat.Type == ChatGroup || m.Chat.Type == ChatSuperGroup +} + +// FromChannel returns true, if message came from a channel. +func (m *Message) FromChannel() bool { + return m.Chat.Type == ChatChannel +} + +// IsService returns true, if message is a service message, +// returns false otherwise. +// +// Service messages are automatically sent messages, which +// typically occur on some global action. For instance, when +// anyone leaves the chat or chat title changes. +func (m *Message) IsService() bool { + fact := false + + fact = fact || m.UserJoined != nil + fact = fact || len(m.UsersJoined) > 0 + fact = fact || m.UserLeft != nil + fact = fact || m.NewGroupTitle != "" + fact = fact || m.NewGroupPhoto != nil + fact = fact || m.GroupPhotoDeleted + fact = fact || m.GroupCreated || m.SuperGroupCreated + fact = fact || (m.MigrateTo != m.MigrateFrom) + + return fact +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/options.go b/vendor/gopkg.in/tucnak/telebot.v2/options.go new file mode 100644 index 0000000..8ad30d7 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/options.go @@ -0,0 +1,292 @@ +package telebot + +import ( + "encoding/json" + "fmt" + "strings" +) + +// Option is a shortcut flag type for certain message features +// (so-called options). It means that instead of passing +// fully-fledged SendOptions* to Send(), you can use these +// flags instead. +// +// Supported options are defined as iota-constants. +type Option int + +const ( + // NoPreview = SendOptions.DisableWebPagePreview + NoPreview Option = iota + + // Silent = SendOptions.DisableNotification + Silent + + // ForceReply = ReplyMarkup.ForceReply + ForceReply + + // OneTimeKeyboard = ReplyMarkup.OneTimeKeyboard + OneTimeKeyboard +) + +// SendOptions has most complete control over in what way the message +// must be sent, providing an API-complete set of custom properties +// and options. +// +// Despite its power, SendOptions is rather inconvenient to use all +// the way through bot logic, so you might want to consider storing +// and re-using it somewhere or be using Option flags instead. +type SendOptions struct { + // If the message is a reply, original message. + ReplyTo *Message + + // See ReplyMarkup struct definition. + ReplyMarkup *ReplyMarkup + + // For text messages, disables previews for links in this message. + DisableWebPagePreview bool + + // Sends the message silently. iOS users will not receive a notification, Android users will receive a notification with no sound. + DisableNotification bool + + // ParseMode controls how client apps render your message. + ParseMode ParseMode + + // DisableContentDetection abilities to disable server-side file content type detection. + DisableContentDetection bool + + // AllowWithoutReply allows sending messages not a as reply if the replied-to message has already been deleted. + AllowWithoutReply bool +} + +func (og *SendOptions) copy() *SendOptions { + cp := *og + if cp.ReplyMarkup != nil { + cp.ReplyMarkup = cp.ReplyMarkup.copy() + } + return &cp +} + +// ReplyMarkup controls two convenient options for bot-user communications +// such as reply keyboard and inline "keyboard" (a grid of buttons as a part +// of the message). +type ReplyMarkup struct { + // InlineKeyboard is a grid of InlineButtons displayed in the message. + // + // Note: DO NOT confuse with ReplyKeyboard and other keyboard properties! + InlineKeyboard [][]InlineButton `json:"inline_keyboard,omitempty"` + + // ReplyKeyboard is a grid, consisting of keyboard buttons. + // + // Note: you don't need to set HideCustomKeyboard field to show custom keyboard. + ReplyKeyboard [][]ReplyButton `json:"keyboard,omitempty"` + + // ForceReply forces Telegram clients to display + // a reply interface to the user (act as if the user + // has selected the bot‘s message and tapped "Reply"). + ForceReply bool `json:"force_reply,omitempty"` + + // Requests clients to resize the keyboard vertically for optimal fit + // (e.g. make the keyboard smaller if there are just two rows of buttons). + // + // Defaults to false, in which case the custom keyboard is always of the + // same height as the app's standard keyboard. + ResizeReplyKeyboard bool `json:"resize_keyboard,omitempty"` + + // Requests clients to hide the reply keyboard as soon as it's been used. + // + // Defaults to false. + OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"` + + // Requests clients to remove the reply keyboard. + // + // Defaults to false. + ReplyKeyboardRemove bool `json:"remove_keyboard,omitempty"` + + // Use this param if you want to force reply from + // specific users only. + // + // Targets: + // 1) Users that are @mentioned in the text of the Message object; + // 2) If the bot's message is a reply (has SendOptions.ReplyTo), + // sender of the original message. + Selective bool `json:"selective,omitempty"` +} + +func (r *ReplyMarkup) copy() *ReplyMarkup { + cp := *r + + if len(r.ReplyKeyboard) > 0 { + cp.ReplyKeyboard = make([][]ReplyButton, len(r.ReplyKeyboard)) + for i, row := range r.ReplyKeyboard { + cp.ReplyKeyboard[i] = make([]ReplyButton, len(row)) + copy(cp.ReplyKeyboard[i], row) + } + } + + if len(r.InlineKeyboard) > 0 { + cp.InlineKeyboard = make([][]InlineButton, len(r.InlineKeyboard)) + for i, row := range r.InlineKeyboard { + cp.InlineKeyboard[i] = make([]InlineButton, len(row)) + copy(cp.InlineKeyboard[i], row) + } + } + + return &cp +} + +// ReplyButton represents a button displayed in reply-keyboard. +// +// Set either Contact or Location to true in order to request +// sensitive info, such as user's phone number or current location. +// (Available in private chats only.) +type ReplyButton struct { + Text string `json:"text"` + + Contact bool `json:"request_contact,omitempty"` + Location bool `json:"request_location,omitempty"` + Poll PollType `json:"request_poll,omitempty"` +} + +// InlineKeyboardMarkup represents an inline keyboard that appears +// right next to the message it belongs to. +type InlineKeyboardMarkup struct { + // Array of button rows, each represented by + // an Array of KeyboardButton objects. + InlineKeyboard [][]InlineButton `json:"inline_keyboard,omitempty"` +} + +// MarshalJSON implements json.Marshaler. It allows to pass +// PollType as keyboard's poll type instead of KeyboardButtonPollType object. +func (pt PollType) MarshalJSON() ([]byte, error) { + var aux = struct { + Type string `json:"type"` + }{ + Type: string(pt), + } + return json.Marshal(&aux) +} + +// Row represents an array of buttons, a row +type Row []Btn + +// Row create a row of buttons +func (r *ReplyMarkup) Row(many ...Btn) Row { + return many +} + +func (r *ReplyMarkup) Inline(rows ...Row) { + inlineKeys := make([][]InlineButton, 0, len(rows)) + for i, row := range rows { + keys := make([]InlineButton, 0, len(row)) + for j, btn := range row { + btn := btn.Inline() + if btn == nil { + panic(fmt.Sprintf( + "telebot: button row %d column %d is not an inline button", + i, j)) + } + keys = append(keys, *btn) + } + inlineKeys = append(inlineKeys, keys) + } + + r.InlineKeyboard = inlineKeys +} + +func (r *ReplyMarkup) Reply(rows ...Row) { + replyKeys := make([][]ReplyButton, 0, len(rows)) + for i, row := range rows { + keys := make([]ReplyButton, 0, len(row)) + for j, btn := range row { + btn := btn.Reply() + if btn == nil { + panic(fmt.Sprintf( + "telebot: button row %d column %d is not a reply button", + i, j)) + } + keys = append(keys, *btn) + } + replyKeys = append(replyKeys, keys) + } + + r.ReplyKeyboard = replyKeys +} + +func (r *ReplyMarkup) Text(text string) Btn { + return Btn{Text: text} +} + +func (r *ReplyMarkup) Contact(text string) Btn { + return Btn{Contact: true, Text: text} +} + +func (r *ReplyMarkup) Location(text string) Btn { + return Btn{Location: true, Text: text} +} + +func (r *ReplyMarkup) Poll(text string, poll PollType) Btn { + return Btn{Poll: poll, Text: text} +} + +func (r *ReplyMarkup) Data(text, unique string, data ...string) Btn { + return Btn{ + Unique: unique, + Text: text, + Data: strings.Join(data, "|"), + } +} + +func (r *ReplyMarkup) URL(text, url string) Btn { + return Btn{Text: text, URL: url} +} + +func (r *ReplyMarkup) Query(text, query string) Btn { + return Btn{Text: text, InlineQuery: query} +} + +func (r *ReplyMarkup) QueryChat(text, query string) Btn { + return Btn{Text: text, InlineQueryChat: query} +} + +func (r *ReplyMarkup) Login(text string, login *Login) Btn { + return Btn{Login: login, Text: text} +} + +// Btn is a constructor button, which will later become either a reply, or an inline button. +type Btn struct { + Unique string + Text string + URL string + Data string + InlineQuery string + InlineQueryChat string + Contact bool + Location bool + Poll PollType + Login *Login +} + +func (b Btn) Inline() *InlineButton { + return &InlineButton{ + Unique: b.Unique, + Text: b.Text, + URL: b.URL, + Data: b.Data, + InlineQuery: b.InlineQuery, + InlineQueryChat: b.InlineQueryChat, + Login: b.Login, + } +} + +func (b Btn) Reply() *ReplyButton { + if b.Unique != "" { + return nil + } + + return &ReplyButton{ + Text: b.Text, + Contact: b.Contact, + Location: b.Location, + Poll: b.Poll, + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/payments.go b/vendor/gopkg.in/tucnak/telebot.v2/payments.go new file mode 100644 index 0000000..0fa7e7f --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/payments.go @@ -0,0 +1,132 @@ +package telebot + +import ( + "encoding/json" + "math" +) + +// ShippingQuery contains information about an incoming shipping query. +type ShippingQuery struct { + Sender *User `json:"from"` + ID string `json:"id"` + Payload string `json:"invoice_payload"` + Address ShippingAddress `json:"shipping_address"` +} + +// ShippingAddress represents a shipping address. +type ShippingAddress struct { + CountryCode string `json:"country_code"` + State string `json:"state"` + City string `json:"city"` + StreetLine1 string `json:"street_line1"` + StreetLine2 string `json:"street_line2"` + PostCode string `json:"post_code"` +} + +// ShippingOption represents one shipping option. +type ShippingOption struct { + ID string `json:"id"` + Title string `json:"title"` + Prices []Price `json:"prices"` +} + +// Payment contains basic information about a successful payment. +type Payment struct { + Currency string `json:"currency"` + Total int `json:"total_amount"` + Payload string `json:"invoice_payload"` + OptionID string `json:"shipping_option_id"` + Order Order `json:"order_info"` + TelegramChargeID string `json:"telegram_payment_charge_id"` + ProviderChargeID string `json:"provider_payment_charge_id"` +} + +// PreCheckoutQuery contains information about an incoming pre-checkout query. +type PreCheckoutQuery struct { + Sender *User `json:"from"` + ID string `json:"id"` + Currency string `json:"currency"` + Payload string `json:"invoice_payload"` + Total int `json:"total_amount"` + OptionID string `json:"shipping_option_id"` + Order Order `json:"order_info"` +} + +// Order represents information about an order. +type Order struct { + Name string `json:"name"` + PhoneNumber string `json:"phone_number"` + Email string `json:"email"` + Address ShippingAddress `json:"shipping_address"` +} + +// Invoice contains basic information about an invoice. +type Invoice struct { + Title string `json:"title"` + Description string `json:"description"` + Payload string `json:"payload"` + Currency string `json:"currency"` + Prices []Price `json:"prices"` + Token string `json:"provider_token"` + Data string `json:"provider_data"` + + Photo *Photo `json:"photo"` + PhotoSize int `json:"photo_size"` + + // Unique deep-linking parameter that can be used to + // generate this invoice when used as a start parameter (0). + Start string `json:"start_parameter"` + + // Shows the total price in the smallest units of the currency. + // For example, for a price of US$ 1.45 pass amount = 145. + Total int `json:"total_amount"` + + MaxTipAmount int `json:"max_tip_amount"` + SuggestedTipAmounts []int `json:"suggested_tip_amounts"` + + NeedName bool `json:"need_name"` + NeedPhoneNumber bool `json:"need_phone_number"` + NeedEmail bool `json:"need_email"` + NeedShippingAddress bool `json:"need_shipping_address"` + SendPhoneNumber bool `json:"send_phone_number_to_provider"` + SendEmail bool `json:"send_email_to_provider"` + Flexible bool `json:"is_flexible"` +} + +// Price represents a portion of the price for goods or services. +type Price struct { + Label string `json:"label"` + Amount int `json:"amount"` +} + +// Currency contains information about supported currency for payments. +type Currency struct { + Code string `json:"code"` + Title string `json:"title"` + Symbol string `json:"symbol"` + Native string `json:"native"` + ThousandsSep string `json:"thousands_sep"` + DecimalSep string `json:"decimal_sep"` + SymbolLeft bool `json:"symbol_left"` + SpaceBetween bool `json:"space_between"` + Exp int `json:"exp"` + MinAmount interface{} `json:"min_amount"` + MaxAmount interface{} `json:"max_amount"` +} + +func (c Currency) FromTotal(total int) float64 { + return float64(total) / math.Pow(10, float64(c.Exp)) +} + +func (c Currency) ToTotal(total float64) int { + return int(total) * int(math.Pow(10, float64(c.Exp))) +} + +var SupportedCurrencies = map[string]Currency{} + +func init() { + err := json.Unmarshal([]byte(dataCurrencies), &SupportedCurrencies) + if err != nil { + panic(err) + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/poller.go b/vendor/gopkg.in/tucnak/telebot.v2/poller.go new file mode 100644 index 0000000..0f9b74d --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/poller.go @@ -0,0 +1,111 @@ +package telebot + +import ( + "time" +) + +// Poller is a provider of Updates. +// +// All pollers must implement Poll(), which accepts bot +// pointer and subscription channel and start polling +// synchronously straight away. +type Poller interface { + // Poll is supposed to take the bot object + // subscription channel and start polling + // for Updates immediately. + // + // Poller must listen for stop constantly and close + // it as soon as it's done polling. + Poll(b *Bot, updates chan Update, stop chan struct{}) +} + +// MiddlewarePoller is a special kind of poller that acts +// like a filter for updates. It could be used for spam +// handling, banning or whatever. +// +// For heavy middleware, use increased capacity. +// +type MiddlewarePoller struct { + Capacity int // Default: 1 + Poller Poller + Filter func(*Update) bool +} + +// NewMiddlewarePoller wait for it... constructs a new middleware poller. +func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *MiddlewarePoller { + return &MiddlewarePoller{ + Poller: original, + Filter: filter, + } +} + +// Poll sieves updates through middleware filter. +func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) { + if p.Capacity < 1 { + p.Capacity = 1 + } + + middle := make(chan Update, p.Capacity) + stopPoller := make(chan struct{}) + + go p.Poller.Poll(b, middle, stopPoller) + + for { + select { + case <-stop: + close(stopPoller) + return + case upd := <-middle: + if p.Filter(&upd) { + dest <- upd + } + } + } +} + +// LongPoller is a classic LongPoller with timeout. +type LongPoller struct { + Limit int + Timeout time.Duration + LastUpdateID int + + // AllowedUpdates contains the update types + // you want your bot to receive. + // + // Possible values: + // message + // edited_message + // channel_post + // edited_channel_post + // inline_query + // chosen_inline_result + // callback_query + // shipping_query + // pre_checkout_query + // poll + // poll_answer + // + AllowedUpdates []string +} + +// Poll does long polling. +func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) { + for { + select { + case <-stop: + return + default: + } + + updates, err := b.getUpdates(p.LastUpdateID+1, p.Limit, p.Timeout, p.AllowedUpdates) + if err != nil { + b.debug(err) + continue + } + + for _, update := range updates { + p.LastUpdateID = update.ID + dest <- update + } + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/polls.go b/vendor/gopkg.in/tucnak/telebot.v2/polls.go new file mode 100644 index 0000000..bd44c65 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/polls.go @@ -0,0 +1,62 @@ +package telebot + +import "time" + +// Poll contains information about a poll. +type Poll struct { + ID string `json:"id"` + Type PollType `json:"type"` + Question string `json:"question"` + Options []PollOption `json:"options"` + VoterCount int `json:"total_voter_count"` + + // (Optional) + Closed bool `json:"is_closed,omitempty"` + CorrectOption int `json:"correct_option_id,omitempty"` + MultipleAnswers bool `json:"allows_multiple_answers,omitempty"` + Explanation string `json:"explanation,omitempty"` + ParseMode ParseMode `json:"explanation_parse_mode,omitempty"` + Entities []MessageEntity `json:"explanation_entities"` + + // True by default, shouldn't be omitted. + Anonymous bool `json:"is_anonymous"` + + // (Mutually exclusive) + OpenPeriod int `json:"open_period,omitempty"` + CloseUnixdate int64 `json:"close_date,omitempty"` +} + +// PollOption contains information about one answer option in a poll. +type PollOption struct { + Text string `json:"text"` + VoterCount int `json:"voter_count"` +} + +// PollAnswer represents an answer of a user in a non-anonymous poll. +type PollAnswer struct { + PollID string `json:"poll_id"` + User User `json:"user"` + Options []int `json:"option_ids"` +} + +// IsRegular says whether poll is a regular. +func (p *Poll) IsRegular() bool { + return p.Type == PollRegular +} + +// IsQuiz says whether poll is a quiz. +func (p *Poll) IsQuiz() bool { + return p.Type == PollQuiz +} + +// CloseDate returns the close date of poll in local time. +func (p *Poll) CloseDate() time.Time { + return time.Unix(p.CloseUnixdate, 0) +} + +// AddOptions adds text options to the poll. +func (p *Poll) AddOptions(opts ...string) { + for _, t := range opts { + p.Options = append(p.Options, PollOption{Text: t}) + } +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/sendable.go b/vendor/gopkg.in/tucnak/telebot.v2/sendable.go new file mode 100644 index 0000000..3261e48 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/sendable.go @@ -0,0 +1,424 @@ +package telebot + +import ( + "encoding/json" + "fmt" + "path/filepath" + "strconv" + "strings" +) + +// Recipient is any possible endpoint you can send +// messages to: either user, group or a channel. +type Recipient interface { + // Must return legit Telegram chat_id or username + Recipient() string +} + +// Sendable is any object that can send itself. +// +// This is pretty cool, since it lets bots implement +// custom Sendables for complex kind of media or +// chat objects spanning across multiple messages. +type Sendable interface { + Send(*Bot, Recipient, *SendOptions) (*Message, error) +} + +// Send delivers media through bot b to recipient. +func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": p.Caption, + } + b.embedSendOptions(params, opt) + + msg, err := b.sendObject(&p.File, "photo", params, nil) + if err != nil { + return nil, err + } + + msg.Photo.File.stealRef(&p.File) + *p = *msg.Photo + p.Caption = msg.Caption + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": a.Caption, + "performer": a.Performer, + "title": a.Title, + } + b.embedSendOptions(params, opt) + + if a.Duration != 0 { + params["duration"] = strconv.Itoa(a.Duration) + } + + msg, err := b.sendObject(a.MediaFile(), "audio", params, thumbnailToFilemap(a.Thumbnail)) + if err != nil { + return nil, err + } + + if msg.Audio != nil { + msg.Audio.File.stealRef(&a.File) + *a = *msg.Audio + a.Caption = msg.Caption + } + + if msg.Document != nil { + msg.Document.File.stealRef(&a.File) + a.File = msg.Document.File + } + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": d.Caption, + } + b.embedSendOptions(params, opt) + + if d.FileSize != 0 { + params["file_size"] = strconv.Itoa(d.FileSize) + } + + msg, err := b.sendObject(d.MediaFile(), "document", params, thumbnailToFilemap(d.Thumbnail)) + if err != nil { + return nil, err + } + + msg.Document.File.stealRef(&d.File) + *d = *msg.Document + d.Caption = msg.Caption + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + } + b.embedSendOptions(params, opt) + + msg, err := b.sendObject(&s.File, "sticker", params, nil) + if err != nil { + return nil, err + } + + msg.Sticker.File.stealRef(&s.File) + *s = *msg.Sticker + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": v.Caption, + } + b.embedSendOptions(params, opt) + + if v.Duration != 0 { + params["duration"] = strconv.Itoa(v.Duration) + } + if v.Width != 0 { + params["width"] = strconv.Itoa(v.Width) + } + if v.Height != 0 { + params["height"] = strconv.Itoa(v.Height) + } + if v.SupportsStreaming { + params["supports_streaming"] = "true" + } + + msg, err := b.sendObject(v.MediaFile(), "video", params, thumbnailToFilemap(v.Thumbnail)) + if err != nil { + return nil, err + } + + if vid := msg.Video; vid != nil { + vid.File.stealRef(&v.File) + *v = *vid + v.Caption = msg.Caption + } else if doc := msg.Document; doc != nil { + // If video has no sound, Telegram can turn it into Document (GIF) + doc.File.stealRef(&v.File) + + v.Caption = doc.Caption + v.MIME = doc.MIME + v.Thumbnail = doc.Thumbnail + } + + return msg, nil +} + +// Send delivers animation through bot b to recipient. +func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": a.Caption, + } + b.embedSendOptions(params, opt) + + if a.Duration != 0 { + params["duration"] = strconv.Itoa(a.Duration) + } + if a.Width != 0 { + params["width"] = strconv.Itoa(a.Width) + } + if a.Height != 0 { + params["height"] = strconv.Itoa(a.Height) + } + + // Without the FileName GIF sends as a document. + if a.FileName == "" && a.File.OnDisk() { + a.FileName = filepath.Base(a.File.FileLocal) + } + + msg, err := b.sendObject(a.MediaFile(), "animation", params, nil) + if err != nil { + return nil, err + } + + if msg.Animation != nil { + msg.Animation.File.stealRef(&a.File) + *a = *msg.Animation + } else { + *a = Animation{ + File: msg.Document.File, + Thumbnail: msg.Document.Thumbnail, + MIME: msg.Document.MIME, + FileName: msg.Document.FileName, + } + } + a.Caption = msg.Caption + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (v *Voice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "caption": v.Caption, + } + b.embedSendOptions(params, opt) + + if v.Duration != 0 { + params["duration"] = strconv.Itoa(v.Duration) + } + + msg, err := b.sendObject(&v.File, "voice", params, nil) + if err != nil { + return nil, err + } + + msg.Voice.File.stealRef(&v.File) + *v = *msg.Voice + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + } + b.embedSendOptions(params, opt) + + if v.Duration != 0 { + params["duration"] = strconv.Itoa(v.Duration) + } + if v.Length != 0 { + params["length"] = strconv.Itoa(v.Length) + } + + msg, err := b.sendObject(&v.File, "videoNote", params, thumbnailToFilemap(v.Thumbnail)) + if err != nil { + return nil, err + } + + msg.VideoNote.File.stealRef(&v.File) + *v = *msg.VideoNote + + return msg, nil +} + +// Send delivers media through bot b to recipient. +func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "latitude": fmt.Sprintf("%f", x.Lat), + "longitude": fmt.Sprintf("%f", x.Lng), + "live_period": strconv.Itoa(x.LivePeriod), + } + if x.HorizontalAccuracy != nil { + params["horizontal_accuracy"] = fmt.Sprintf("%f", *x.HorizontalAccuracy) + } + if x.Heading != 0 { + params["heading"] = strconv.Itoa(x.Heading) + } + if x.ProximityAlertRadius != 0 { + params["proximity_alert_radius"] = strconv.Itoa(x.Heading) + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendLocation", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Send delivers media through bot b to recipient. +func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "latitude": fmt.Sprintf("%f", v.Location.Lat), + "longitude": fmt.Sprintf("%f", v.Location.Lng), + "title": v.Title, + "address": v.Address, + "foursquare_id": v.FoursquareID, + "foursquare_type": v.FoursquareType, + "google_place_id": v.GooglePlaceID, + "google_place_type": v.GooglePlaceType, + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendVenue", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Send delivers invoice through bot b to recipient. +func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "title": i.Title, + "description": i.Description, + "start_parameter": i.Start, + "payload": i.Payload, + "provider_token": i.Token, + "currency": i.Currency, + "max_tip_amount": strconv.Itoa(i.MaxTipAmount), + "need_name": strconv.FormatBool(i.NeedName), + "need_phone_number": strconv.FormatBool(i.NeedPhoneNumber), + "need_email": strconv.FormatBool(i.NeedEmail), + "need_shipping_address": strconv.FormatBool(i.NeedShippingAddress), + "send_phone_number_to_provider": strconv.FormatBool(i.SendPhoneNumber), + "send_email_to_provider": strconv.FormatBool(i.SendEmail), + "is_flexible": strconv.FormatBool(i.Flexible), + } + if i.Photo != nil { + if i.Photo.FileURL != "" { + params["photo_url"] = i.Photo.FileURL + } + if i.PhotoSize > 0 { + params["photo_size"] = strconv.Itoa(i.PhotoSize) + } + if i.Photo.Width > 0 { + params["photo_width"] = strconv.Itoa(i.Photo.Width) + } + if i.Photo.Height > 0 { + params["photo_height"] = strconv.Itoa(i.Photo.Height) + } + } + if len(i.Prices) > 0 { + data, _ := json.Marshal(i.Prices) + params["prices"] = string(data) + } + if len(i.SuggestedTipAmounts) > 0 { + params["suggested_tip_amounts"] = "[" + strings.Join(intsToStrs(i.SuggestedTipAmounts), ",") + "]" + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendInvoice", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Send delivers poll through bot b to recipient. +func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "question": p.Question, + "type": string(p.Type), + "is_closed": strconv.FormatBool(p.Closed), + "is_anonymous": strconv.FormatBool(p.Anonymous), + "allows_multiple_answers": strconv.FormatBool(p.MultipleAnswers), + "correct_option_id": strconv.Itoa(p.CorrectOption), + } + if p.Explanation != "" { + params["explanation"] = p.Explanation + params["explanation_parse_mode"] = p.ParseMode + } + if p.OpenPeriod != 0 { + params["open_period"] = strconv.Itoa(p.OpenPeriod) + } else if p.CloseUnixdate != 0 { + params["close_date"] = strconv.FormatInt(p.CloseUnixdate, 10) + } + b.embedSendOptions(params, opt) + + var options []string + for _, o := range p.Options { + options = append(options, o.Text) + } + + opts, _ := json.Marshal(options) + params["options"] = string(opts) + + data, err := b.Raw("sendPoll", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Send delivers dice through bot b to recipient. +func (d *Dice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "emoji": string(d.Type), + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendDice", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} + +// Send delivers game through bot b to recipient. +func (g *Game) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "game_short_name": g.Name, + } + b.embedSendOptions(params, opt) + + data, err := b.Raw("sendGame", params) + if err != nil { + return nil, err + } + + return extractMessage(data) +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/stickers.go b/vendor/gopkg.in/tucnak/telebot.v2/stickers.go new file mode 100644 index 0000000..2d878ba --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/stickers.go @@ -0,0 +1,181 @@ +package telebot + +import ( + "encoding/json" + "strconv" +) + +// Sticker object represents a WebP image, so-called sticker. +type Sticker struct { + File + Width int `json:"width"` + Height int `json:"height"` + Animated bool `json:"is_animated"` + Thumbnail *Photo `json:"thumb"` + Emoji string `json:"emoji"` + SetName string `json:"set_name"` + MaskPosition *MaskPosition `json:"mask_position"` +} + +// StickerSet represents a sticker set. +type StickerSet struct { + Name string `json:"name"` + Title string `json:"title"` + Animated bool `json:"is_animated"` + Stickers []Sticker `json:"stickers"` + Thumbnail *Photo `json:"thumb"` + PNG *File `json:"png_sticker"` + TGS *File `json:"tgs_sticker"` + Emojis string `json:"emojis"` + ContainsMasks bool `json:"contains_masks"` + MaskPosition *MaskPosition `json:"mask_position"` +} + +// MaskPosition describes the position on faces where +// a mask should be placed by default. +type MaskPosition struct { + Feature MaskFeature `json:"point"` + XShift float32 `json:"x_shift"` + YShift float32 `json:"y_shift"` + Scale float32 `json:"scale"` +} + +// UploadStickerFile uploads a .PNG file with a sticker for later use. +func (b *Bot) UploadStickerFile(to Recipient, png *File) (*File, error) { + files := map[string]File{ + "png_sticker": *png, + } + params := map[string]string{ + "user_id": to.Recipient(), + } + + data, err := b.sendFiles("uploadStickerFile", files, params) + if err != nil { + return nil, err + } + + var resp struct { + Result File + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return &resp.Result, nil +} + +// GetStickerSet returns a StickerSet on success. +func (b *Bot) GetStickerSet(name string) (*StickerSet, error) { + data, err := b.Raw("getStickerSet", map[string]string{"name": name}) + if err != nil { + return nil, err + } + + var resp struct { + Result *StickerSet + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + return resp.Result, nil +} + +// CreateNewStickerSet creates a new sticker set. +func (b *Bot) CreateNewStickerSet(to Recipient, s StickerSet) error { + files := make(map[string]File) + if s.PNG != nil { + files["png_sticker"] = *s.PNG + } + if s.TGS != nil { + files["tgs_sticker"] = *s.TGS + } + + params := map[string]string{ + "user_id": to.Recipient(), + "name": s.Name, + "title": s.Title, + "emojis": s.Emojis, + "contains_masks": strconv.FormatBool(s.ContainsMasks), + } + + if s.MaskPosition != nil { + data, err := json.Marshal(&s.MaskPosition) + if err != nil { + return err + } + params["mask_position"] = string(data) + } + + _, err := b.sendFiles("createNewStickerSet", files, params) + return err +} + +// AddStickerToSet adds new sticker to existing sticker set. +func (b *Bot) AddStickerToSet(to Recipient, s StickerSet) error { + files := make(map[string]File) + if s.PNG != nil { + files["png_sticker"] = *s.PNG + } else if s.TGS != nil { + files["tgs_sticker"] = *s.TGS + } + + params := map[string]string{ + "user_id": to.Recipient(), + "name": s.Name, + "emojis": s.Emojis, + } + + if s.MaskPosition != nil { + data, err := json.Marshal(&s.MaskPosition) + if err != nil { + return err + } + params["mask_position"] = string(data) + } + + _, err := b.sendFiles("addStickerToSet", files, params) + return err +} + +// SetStickerPositionInSet moves a sticker in set to a specific position. +func (b *Bot) SetStickerPositionInSet(sticker string, position int) error { + params := map[string]string{ + "sticker": sticker, + "position": strconv.Itoa(position), + } + + _, err := b.Raw("setStickerPositionInSet", params) + return err +} + +// DeleteStickerFromSet deletes sticker from set created by the bot. +func (b *Bot) DeleteStickerFromSet(sticker string) error { + _, err := b.Raw("deleteStickerFromSet", map[string]string{"sticker": sticker}) + return err + +} + +// SetStickerSetThumb sets the thumbnail of a sticker set. +// Animated thumbnails can be set for animated sticker sets only. +// +// Thumbnail must be a PNG image, up to 128 kilobytes in size +// and have width and height exactly 100px, or a TGS animation +// up to 32 kilobytes in size. +// +// Animated sticker set thumbnail can't be uploaded via HTTP URL. +// +func (b *Bot) SetStickerSetThumb(to Recipient, s StickerSet) error { + files := map[string]File{} + if s.PNG != nil { + files["thumb"] = *s.PNG + } else if s.TGS != nil { + files["thumb"] = *s.TGS + } + + params := map[string]string{ + "name": s.Name, + "user_id": to.Recipient(), + } + + _, err := b.sendFiles("setStickerSetThumb", files, params) + return err +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/telebot.go b/vendor/gopkg.in/tucnak/telebot.v2/telebot.go new file mode 100644 index 0000000..a8a3e3a --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/telebot.go @@ -0,0 +1,269 @@ +// Package telebot is a framework for Telegram bots. +// +// Example: +// +// package main +// +// import ( +// "time" +// tb "gopkg.in/tucnak/telebot.v2" +// ) +// +// func main() { +// b, err := tb.NewBot(tb.Settings{ +// Token: "TOKEN_HERE", +// Poller: &tb.LongPoller{Timeout: 10 * time.Second}, +// }) +// +// if err != nil { +// return +// } +// +// b.Handle(tb.OnText, func(m *tb.Message) { +// b.Send(m.Sender, "hello world") +// }) +// +// b.Start() +// } +// +package telebot + +import "github.com/pkg/errors" + +var ( + ErrBadRecipient = errors.New("telebot: recipient is nil") + ErrUnsupportedWhat = errors.New("telebot: unsupported what argument") + ErrCouldNotUpdate = errors.New("telebot: could not fetch new updates") + ErrTrueResult = errors.New("telebot: result is True") +) + +const DefaultApiURL = "https://api.telegram.org" + +// These are one of the possible events Handle() can deal with. +// +// For convenience, all Telebot-provided endpoints start with +// an "alert" character \a. +const ( + // Basic message handlers. + // + // Handler: func(*Message) + OnText = "\atext" + OnPhoto = "\aphoto" + OnAudio = "\aaudio" + OnAnimation = "\aanimation" + OnDocument = "\adocument" + OnSticker = "\asticker" + OnVideo = "\avideo" + OnVoice = "\avoice" + OnVideoNote = "\avideo_note" + OnContact = "\acontact" + OnLocation = "\alocation" + OnVenue = "\avenue" + OnEdited = "\aedited" + OnPinned = "\apinned" + OnChannelPost = "\achan_post" + OnEditedChannelPost = "\achan_edited_post" + OnDice = "\adice" + OnInvoice = "\ainvoice" + OnPayment = "\apayment" + OnGame = "\agame" + + // Will fire when bot is added to a group. + OnAddedToGroup = "\aadded_to_group" + + // Group events: + OnUserJoined = "\auser_joined" + OnUserLeft = "\auser_left" + OnNewGroupTitle = "\anew_chat_title" + OnNewGroupPhoto = "\anew_chat_photo" + OnGroupPhotoDeleted = "\achat_photo_del" + OnGroupCreated = "\agroup_created" + OnSuperGroupCreated = "\asupergroup_created" + OnChannelCreated = "\achannel_created" + + // Migration happens when group switches to + // a supergroup. You might want to update + // your internal references to this chat + // upon switching as its ID will change. + // + // Handler: func(from, to int64) + OnMigration = "\amigration" + + // Will fire on callback requests. + // + // Handler: func(*Callback) + OnCallback = "\acallback" + + // Will fire on incoming inline queries. + // + // Handler: func(*Query) + OnQuery = "\aquery" + + // Will fire on chosen inline results. + // + // Handler: func(*ChosenInlineResult) + OnChosenInlineResult = "\achosen_inline_result" + + // Will fire on ShippingQuery. + // + // Handler: func(*ShippingQuery) + OnShipping = "\ashipping_query" + + // Will fire on PreCheckoutQuery. + // + // Handler: func(*PreCheckoutQuery) + OnCheckout = "\apre_checkout_query" + + // Will fire on Poll. + // + // Handler: func(*Poll) + OnPoll = "\apoll" + + // Will fire on PollAnswer. + // + // Handler: func(*PollAnswer) + OnPollAnswer = "\apoll_answer" + + // Will fire on MyChatMember + // + // Handler: func(*ChatMemberUpdated) + OnMyChatMember = "\amy_chat_member" + + // Will fire on ChatMember + // + // Handler: func(*ChatMemberUpdated) + OnChatMember = "\achat_member" + + // Will fire on VoiceChatStarted + // + // Handler: func(*Message) + OnVoiceChatStarted = "\avoice_chat_started" + + // Will fire on VoiceChatEnded + // + // Handler: func(*Message) + OnVoiceChatEnded = "\avoice_chat_ended" + + // Will fire on VoiceChatParticipantsInvited + // + // Handler: func(*Message) + OnVoiceChatParticipantsInvited = "\avoice_chat_participants_invited" + + // Will fire on ProximityAlert + // + // Handler: func(*Message) + OnProximityAlert = "\aproximity_alert_triggered" + + // Will fire on AudoDeleteTimer + // + // Handler: func(*Message) + OnAutoDeleteTimer = "\amessage_auto_delete_timer_changed" + + // Will fire on OnVoiceChatScheduled + // + // Handler: func(*Message) + OnVoiceChatScheduled = "\avoice_chat_scheduled" +) + +// ChatAction is a client-side status indicating bot activity. +type ChatAction string + +const ( + Typing ChatAction = "typing" + UploadingPhoto ChatAction = "upload_photo" + UploadingVideo ChatAction = "upload_video" + UploadingAudio ChatAction = "upload_audio" + UploadingDocument ChatAction = "upload_document" + UploadingVNote ChatAction = "upload_video_note" + RecordingVideo ChatAction = "record_video" + RecordingAudio ChatAction = "record_audio" + RecordingVNote ChatAction = "record_video_note" + FindingLocation ChatAction = "find_location" +) + +// ParseMode determines the way client applications treat the text of the message +type ParseMode = string + +const ( + ModeDefault ParseMode = "" + ModeMarkdown ParseMode = "Markdown" + ModeMarkdownV2 ParseMode = "MarkdownV2" + ModeHTML ParseMode = "HTML" +) + +// EntityType is a MessageEntity type. +type EntityType string + +const ( + EntityMention EntityType = "mention" + EntityTMention EntityType = "text_mention" + EntityHashtag EntityType = "hashtag" + EntityCashtag EntityType = "cashtag" + EntityCommand EntityType = "bot_command" + EntityURL EntityType = "url" + EntityEmail EntityType = "email" + EntityPhone EntityType = "phone_number" + EntityBold EntityType = "bold" + EntityItalic EntityType = "italic" + EntityUnderline EntityType = "underline" + EntityStrikethrough EntityType = "strikethrough" + EntityCode EntityType = "code" + EntityCodeBlock EntityType = "pre" + EntityTextLink EntityType = "text_link" +) + +// ChatType represents one of the possible chat types. +type ChatType string + +const ( + ChatPrivate ChatType = "private" + ChatGroup ChatType = "group" + ChatSuperGroup ChatType = "supergroup" + ChatChannel ChatType = "channel" + ChatChannelPrivate ChatType = "privatechannel" +) + +// MemberStatus is one's chat status. +type MemberStatus string + +const ( + Creator MemberStatus = "creator" + Administrator MemberStatus = "administrator" + Member MemberStatus = "member" + Restricted MemberStatus = "restricted" + Left MemberStatus = "left" + Kicked MemberStatus = "kicked" +) + +// MaskFeature defines sticker mask position. +type MaskFeature string + +const ( + FeatureForehead MaskFeature = "forehead" + FeatureEyes MaskFeature = "eyes" + FeatureMouth MaskFeature = "mouth" + FeatureChin MaskFeature = "chin" +) + +// PollType defines poll types. +type PollType string + +const ( + // Despite "any" type isn't described in documentation, + // it needed for proper KeyboardButtonPollType marshaling. + PollAny PollType = "any" + + PollQuiz PollType = "quiz" + PollRegular PollType = "regular" +) + +type DiceType string + +var ( + Cube = &Dice{Type: "🎲"} + Dart = &Dice{Type: "🎯"} + Ball = &Dice{Type: "🏀"} + Goal = &Dice{Type: "⚽"} + Slot = &Dice{Type: "🎰"} + Bowl = &Dice{Type: "🎳"} +) diff --git a/vendor/gopkg.in/tucnak/telebot.v2/util.go b/vendor/gopkg.in/tucnak/telebot.v2/util.go new file mode 100644 index 0000000..844dbd3 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/util.go @@ -0,0 +1,249 @@ +package telebot + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/pkg/errors" + "log" + "net/http" + "strconv" +) + +func (b *Bot) debug(err error) { + if b.reporter != nil { + b.reporter(err) + } else { + log.Println(err) + } +} + +func (b *Bot) deferDebug() { + if r := recover(); r != nil { + if err, ok := r.(error); ok { + b.debug(err) + } else if str, ok := r.(string); ok { + b.debug(errors.Errorf("%s", str)) + } + } +} + +func (b *Bot) runHandler(handler func()) { + f := func() { + defer b.deferDebug() + handler() + } + if b.synchronous { + f() + } else { + go f() + } +} + +// wrapError returns new wrapped telebot-related error. +func wrapError(err error) error { + return errors.Wrap(err, "telebot") +} + +// extractOk checks given result for error. If result is ok returns nil. +// In other cases it extracts API error. If error is not presented +// in errors.go, it will be prefixed with `unknown` keyword. +func extractOk(data []byte) error { + // Parse the error message as JSON + var tgramApiError struct { + Ok bool `json:"ok"` + ErrorCode int `json:"error_code"` + Description string `json:"description"` + Parameters map[string]interface{} `json:"parameters"` + } + jdecoder := json.NewDecoder(bytes.NewReader(data)) + jdecoder.UseNumber() + + err := jdecoder.Decode(&tgramApiError) + if err != nil { + //return errors.Wrap(err, "can't parse JSON reply, the Telegram server is mibehaving") + // FIXME / TODO: in this case the error might be at HTTP level, or the content is not JSON (eg. image?) + return nil + } + + if tgramApiError.Ok { + // No error + return nil + } + + err = ErrByDescription(tgramApiError.Description) + if err != nil { + apierr, _ := err.(*APIError) + // Formally this is wrong, as the error is not created on the fly + // However, given the current way of handling errors, this a working + // workaround which doesn't break the API + apierr.Parameters = tgramApiError.Parameters + return apierr + } + + switch tgramApiError.ErrorCode { + case http.StatusTooManyRequests: + retryAfter, ok := tgramApiError.Parameters["retry_after"] + if !ok { + return NewAPIError(429, tgramApiError.Description) + } + retryAfterInt, _ := strconv.Atoi(fmt.Sprint(retryAfter)) + + err = FloodError{ + APIError: NewAPIError(429, tgramApiError.Description), + RetryAfter: retryAfterInt, + } + default: + err = fmt.Errorf("telegram unknown: %s (%d)", tgramApiError.Description, tgramApiError.ErrorCode) + } + + return err +} + +// extractMessage extracts common Message result from given data. +// Should be called after extractOk or b.Raw() to handle possible errors. +func extractMessage(data []byte) (*Message, error) { + var resp struct { + Result *Message + } + if err := json.Unmarshal(data, &resp); err != nil { + var resp struct { + Result bool + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, wrapError(err) + } + if resp.Result { + return nil, ErrTrueResult + } + return nil, wrapError(err) + } + return resp.Result, nil +} + +func extractOptions(how []interface{}) *SendOptions { + opts := &SendOptions{} + + for _, prop := range how { + switch opt := prop.(type) { + case *SendOptions: + opts = opt.copy() + case *ReplyMarkup: + opts.ReplyMarkup = opt.copy() + case Option: + switch opt { + case NoPreview: + opts.DisableWebPagePreview = true + case Silent: + opts.DisableNotification = true + case ForceReply: + if opts.ReplyMarkup == nil { + opts.ReplyMarkup = &ReplyMarkup{} + } + opts.ReplyMarkup.ForceReply = true + case OneTimeKeyboard: + if opts.ReplyMarkup == nil { + opts.ReplyMarkup = &ReplyMarkup{} + } + opts.ReplyMarkup.OneTimeKeyboard = true + default: + panic("telebot: unsupported flag-option") + } + case ParseMode: + opts.ParseMode = opt + default: + panic("telebot: unsupported send-option") + } + } + + return opts +} + +func (b *Bot) embedSendOptions(params map[string]string, opt *SendOptions) { + if b.parseMode != ModeDefault { + params["parse_mode"] = b.parseMode + } + + if opt == nil { + return + } + + if opt.ReplyTo != nil && opt.ReplyTo.ID != 0 { + params["reply_to_message_id"] = strconv.Itoa(opt.ReplyTo.ID) + } + + if opt.DisableWebPagePreview { + params["disable_web_page_preview"] = "true" + } + + if opt.DisableNotification { + params["disable_notification"] = "true" + } + + if opt.ParseMode != ModeDefault { + params["parse_mode"] = opt.ParseMode + } + + if opt.DisableContentDetection { + params["disable_content_type_detection"] = "true" + } + + if opt.AllowWithoutReply { + params["allow_sending_without_reply"] = "true" + } + + if opt.ReplyMarkup != nil { + processButtons(opt.ReplyMarkup.InlineKeyboard) + replyMarkup, _ := json.Marshal(opt.ReplyMarkup) + params["reply_markup"] = string(replyMarkup) + } +} + +func processButtons(keys [][]InlineButton) { + if keys == nil || len(keys) < 1 || len(keys[0]) < 1 { + return + } + + for i := range keys { + for j := range keys[i] { + key := &keys[i][j] + if key.Unique != "" { + // Format: "\f|" + data := key.Data + if data == "" { + key.Data = "\f" + key.Unique + } else { + key.Data = "\f" + key.Unique + "|" + data + } + } + } + } +} + +func embedRights(p map[string]interface{}, rights Rights) { + data, _ := json.Marshal(rights) + _ = json.Unmarshal(data, &p) +} + +func thumbnailToFilemap(thumb *Photo) map[string]File { + if thumb != nil { + return map[string]File{"thumb": thumb.File} + } + return nil +} + +func isUserInList(user *User, list []User) bool { + for _, user2 := range list { + if user.ID == user2.ID { + return true + } + } + return false +} + +func intsToStrs(ns []int) (s []string) { + for _, n := range ns { + s = append(s, strconv.Itoa(n)) + } + return +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/voice.go b/vendor/gopkg.in/tucnak/telebot.v2/voice.go new file mode 100644 index 0000000..7e8f967 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/voice.go @@ -0,0 +1,29 @@ +package telebot + +import "time" + +// VoiceChatStarted represents a service message about a voice chat +// started in the chat. +type VoiceChatStarted struct{} + +// VoiceChatEnded represents a service message about a voice chat +// ended in the chat. +type VoiceChatEnded struct { + Duration int `json:"duration"` +} + +// VoiceChatParticipantsInvited represents a service message about new +// members invited to a voice chat +type VoiceChatParticipantsInvited struct { + Users []User `json:"users"` +} + +// VoiceChatScheduled represents a service message about a voice chat scheduled in the chat. +type VoiceChatScheduled struct { + Unixtime int64 `json:"start_date"` +} + +// ExpireDate returns the point when the voice chat is supposed to be started by a chat administrator. +func (v *VoiceChatScheduled) ExpireDate() time.Time { + return time.Unix(v.Unixtime, 0) +} diff --git a/vendor/gopkg.in/tucnak/telebot.v2/webhook.go b/vendor/gopkg.in/tucnak/telebot.v2/webhook.go new file mode 100644 index 0000000..690ba68 --- /dev/null +++ b/vendor/gopkg.in/tucnak/telebot.v2/webhook.go @@ -0,0 +1,201 @@ +package telebot + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strconv" +) + +// A WebhookTLS specifies the path to a key and a cert so the poller can open +// a TLS listener +type WebhookTLS struct { + Key string + Cert string +} + +// A WebhookEndpoint describes the endpoint to which telegram will send its requests. +// This must be a public URL and can be a loadbalancer or something similar. If the +// endpoint uses TLS and the certificate is self-signed you have to add the certificate +// path of this certificate so telegram will trust it. This field can be ignored if you +// have a trusted certificate (letsencrypt, ...). +type WebhookEndpoint struct { + PublicURL string + Cert string +} + +// A Webhook configures the poller for webhooks. It opens a port on the given +// listen address. If TLS is filled, the listener will use the key and cert to open +// a secure port. Otherwise it will use plain HTTP. +// +// If you have a loadbalancer ore other infrastructure in front of your service, you +// must fill the Endpoint structure so this poller will send this data to telegram. If +// you leave these values empty, your local address will be sent to telegram which is mostly +// not what you want (at least while developing). If you have a single instance of your +// bot you should consider to use the LongPoller instead of a WebHook. +// +// You can also leave the Listen field empty. In this case it is up to the caller to +// add the Webhook to a http-mux. +// +type Webhook struct { + Listen string `json:"url"` + MaxConnections int `json:"max_connections"` + AllowedUpdates []string `json:"allowed_updates"` + + // (WebhookInfo) + HasCustomCert bool `json:"has_custom_certificate"` + PendingUpdates int `json:"pending_update_count"` + ErrorUnixtime int64 `json:"last_error_date"` + ErrorMessage string `json:"last_error_message"` + + IP string `json:"ip_address"` + DropUpdates bool `json:"drop_pending_updates"` + + TLS *WebhookTLS + Endpoint *WebhookEndpoint + + dest chan<- Update + bot *Bot +} + +func (h *Webhook) getFiles() map[string]File { + m := make(map[string]File) + + if h.TLS != nil { + m["certificate"] = FromDisk(h.TLS.Cert) + } + // check if it is overwritten by an endpoint + if h.Endpoint != nil { + if h.Endpoint.Cert == "" { + // this can be the case if there is a loadbalancer or reverseproxy in + // front with a public cert. in this case we do not need to upload it + // to telegram. we delete the certificate from the map, because someone + // can have an internal TLS listener with a private cert + delete(m, "certificate") + } else { + // someone configured a certificate + m["certificate"] = FromDisk(h.Endpoint.Cert) + } + } + return m +} + +func (h *Webhook) getParams() map[string]string { + params := make(map[string]string) + + if h.MaxConnections != 0 { + params["max_connections"] = strconv.Itoa(h.MaxConnections) + } + if len(h.AllowedUpdates) > 0 { + data, _ := json.Marshal(h.AllowedUpdates) + params["allowed_updates"] = string(data) + } + if h.IP != "" { + params["ip_address"] = h.IP + } + if h.DropUpdates { + params["drop_pending_updates"] = strconv.FormatBool(h.DropUpdates) + } + + if h.TLS != nil { + params["url"] = "https://" + h.Listen + } else { + // this will not work with telegram, they want TLS + // but i allow this because telegram will send an error + // when you register this hook. in their docs they write + // that port 80/http is allowed ... + params["url"] = "http://" + h.Listen + } + if h.Endpoint != nil { + params["url"] = h.Endpoint.PublicURL + } + return params +} + +func (h *Webhook) Poll(b *Bot, dest chan Update, stop chan struct{}) { + if err := b.SetWebhook(h); err != nil { + b.debug(err) + close(stop) + return + } + + // store the variables so the HTTP-handler can use 'em + h.dest = dest + h.bot = b + + if h.Listen == "" { + h.waitForStop(stop) + return + } + + s := &http.Server{ + Addr: h.Listen, + Handler: h, + } + + go func(stop chan struct{}) { + h.waitForStop(stop) + s.Shutdown(context.Background()) + }(stop) + + if h.TLS != nil { + s.ListenAndServeTLS(h.TLS.Cert, h.TLS.Key) + } else { + s.ListenAndServe() + } +} + +func (h *Webhook) waitForStop(stop chan struct{}) { + _, ok := <-stop + if ok { + close(stop) + } +} + +// The handler simply reads the update from the body of the requests +// and writes them to the update channel. +func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var update Update + err := json.NewDecoder(r.Body).Decode(&update) + if err != nil { + h.bot.debug(fmt.Errorf("cannot decode update: %v", err)) + return + } + h.dest <- update +} + +// GetWebhook returns current webhook status. +func (b *Bot) GetWebhook() (*Webhook, error) { + data, err := b.Raw("getWebhookInfo", nil) + if err != nil { + return nil, err + } + + var resp struct { + Result Webhook + } + if err := json.Unmarshal(data, &resp); err != nil { + return nil, err + } + return &resp.Result, nil +} + +// SetWebhook configures a bot to receive incoming +// updates via an outgoing webhook. +func (b *Bot) SetWebhook(w *Webhook) error { + _, err := b.sendFiles("setWebhook", w.getFiles(), w.getParams()) + return err +} + +// RemoveWebhook removes webhook integration. +func (b *Bot) RemoveWebhook(dropPending ...bool) error { + drop := false + if len(dropPending) > 0 { + drop = dropPending[0] + } + _, err := b.Raw("deleteWebhook", map[string]bool{ + "drop_pending_updates": drop, + }) + return err +} diff --git a/vendor/modules.txt b/vendor/modules.txt new file mode 100644 index 0000000..93aa6aa --- /dev/null +++ b/vendor/modules.txt @@ -0,0 +1,22 @@ +# github.com/confluentinc/confluent-kafka-go v1.8.2 +## explicit +github.com/confluentinc/confluent-kafka-go/kafka +github.com/confluentinc/confluent-kafka-go/kafka/librdkafka_vendor +# github.com/davecgh/go-spew v1.1.1 +## explicit +# github.com/pkg/errors v0.9.1 +## explicit +github.com/pkg/errors +# github.com/rs/zerolog v1.26.1 +## explicit; go 1.15 +github.com/rs/zerolog +github.com/rs/zerolog/internal/cbor +github.com/rs/zerolog/internal/json +github.com/rs/zerolog/log +# github.com/stretchr/testify v1.7.0 +## explicit; go 1.13 +# gopkg.in/tucnak/telebot.v2 v2.5.0 +## explicit; go 1.13 +gopkg.in/tucnak/telebot.v2 +# gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b +## explicit