| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you 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 main |
| |
| import ( |
| "context" |
| "flag" |
| "log" |
| "net" |
| "time" |
| |
| "github.com/apache/iggy/examples/go/common" |
| "github.com/apache/iggy/foreign/go/client" |
| "github.com/apache/iggy/foreign/go/client/tcp" |
| iggcon "github.com/apache/iggy/foreign/go/contracts" |
| ) |
| |
| var ( |
| StreamID = uint32(0) |
| TopicID = uint32(0) |
| PartitionID = uint32(0) |
| BatchesLimit = uint32(5) |
| ) |
| |
| func main() { |
| serverAddr, tcpOptions := getTcpOptions() |
| |
| cli, err := client.NewIggyClient( |
| client.WithTcp(tcpOptions...), |
| ) |
| if err != nil { |
| log.Fatal(err) |
| } |
| defer func() { |
| if err := cli.Close(); err != nil { |
| log.Printf("Error closing client: %v", err) |
| } |
| }() |
| |
| ctx := context.Background() |
| log.Printf("Connecting to server at %s", serverAddr) |
| if err := cli.Connect(ctx); err != nil { |
| log.Printf("Failed to connect to server at %s: %v", serverAddr, err) |
| } |
| |
| _, err = cli.LoginUser(ctx, common.DefaultRootUsername, common.DefaultRootPassword) |
| if err != nil { |
| log.Fatal(err) |
| } |
| |
| err = consumeMessages(ctx, cli) |
| if err != nil { |
| log.Fatal(err) |
| } |
| } |
| |
| func consumeMessages(ctx context.Context, client iggcon.Client) error { |
| interval := 500 * time.Millisecond |
| log.Printf( |
| "Messages will be consumed from stream: %d, topic: %d, partition: %d with interval %s.", |
| StreamID, TopicID, PartitionID, interval, |
| ) |
| |
| offset := uint64(0) |
| const messagePerBatch = 10 |
| consumedBatches := uint32(0) |
| consumer := iggcon.DefaultConsumer() |
| for { |
| if consumedBatches == BatchesLimit { |
| log.Printf("Consumed %d batches of messages, exiting.", consumedBatches) |
| return nil |
| } |
| |
| streamIdentifier, _ := iggcon.NewIdentifier(StreamID) |
| topicIdentifier, _ := iggcon.NewIdentifier(TopicID) |
| pollMessages, err := client. |
| PollMessages( |
| ctx, |
| streamIdentifier, |
| topicIdentifier, |
| consumer, |
| iggcon.OffsetPollingStrategy(offset), |
| messagePerBatch, |
| false, |
| &PartitionID, |
| ) |
| if err != nil { |
| return err |
| } |
| |
| if len(pollMessages.Messages) == 0 { |
| log.Println("No messages found.") |
| select { |
| case <-ctx.Done(): |
| return nil |
| case <-time.After(interval): |
| } |
| continue |
| } |
| |
| offset += uint64(len(pollMessages.Messages)) |
| for _, message := range pollMessages.Messages { |
| err = handleMessage(message) |
| if err != nil { |
| return err |
| } |
| } |
| consumedBatches++ |
| select { |
| case <-ctx.Done(): |
| return nil |
| case <-time.After(interval): |
| } |
| } |
| } |
| |
| func handleMessage(message iggcon.IggyMessage) error { |
| payload := string(message.Payload) |
| log.Printf( |
| "Handling message at offset: %d, payload: %s...", |
| message.Header.Offset, payload) |
| return nil |
| } |
| |
| func getTcpOptions() (string, []tcp.Option) { |
| tcpServerAddr := flag.String("tcp-server-address", "127.0.0.1:8090", "TCP server address") |
| tlsEnabled := flag.Bool("tls", false, "Enable TLS") |
| tlsCAFile := flag.String("tls-ca-file", "", "Path to CA certificate file for TLS") |
| tlsDomain := flag.String("tls-domain", "", "TLS server domain name for SNI") |
| flag.Parse() |
| |
| if _, err := net.ResolveTCPAddr("tcp", *tcpServerAddr); err != nil { |
| log.Fatalf("Invalid server address %s! Usage: --tcp-server-address <server-address>", *tcpServerAddr) |
| } |
| |
| tcpOptions := []tcp.Option{ |
| tcp.WithServerAddress(*tcpServerAddr), |
| } |
| |
| if *tlsEnabled { |
| var tlsOpts []tcp.TLSOption |
| if *tlsCAFile != "" { |
| tlsOpts = append(tlsOpts, tcp.WithTLSCAFile(*tlsCAFile)) |
| } |
| if *tlsDomain != "" { |
| tlsOpts = append(tlsOpts, tcp.WithTLSDomain(*tlsDomain)) |
| } |
| tcpOptions = append(tcpOptions, tcp.WithTLS(tlsOpts...)) |
| log.Printf("TLS enabled with CA file: %s, domain: %s", *tlsCAFile, *tlsDomain) |
| } |
| |
| return *tcpServerAddr, tcpOptions |
| } |