|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "log" |
| 20 | + "net" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + "strconv" |
| 24 | + "sync/atomic" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/gorilla/mux" |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/credentials/insecure" |
| 30 | + |
| 31 | + "github.com/dapr/dapr/tests/apps/utils" |
| 32 | + "github.com/dapr/go-sdk/client" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + pubsubName = "messagebus" |
| 37 | + topicName = "pubsub-topic-streaming" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + appPort = 3000 |
| 42 | + daprGRPCPort int |
| 43 | + sdkClient client.Client |
| 44 | +) |
| 45 | + |
| 46 | +func init() { |
| 47 | + p := os.Getenv("PORT") |
| 48 | + if p != "" && p != "0" { |
| 49 | + appPort, _ = strconv.Atoi(p) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// indexHandler is the handler for root path |
| 54 | +func indexHandler(w http.ResponseWriter, r *http.Request) { |
| 55 | + w.WriteHeader(http.StatusOK) |
| 56 | +} |
| 57 | + |
| 58 | +// appRouter initializes restful api router |
| 59 | +func appRouter() http.Handler { |
| 60 | + router := mux.NewRouter().StrictSlash(true) |
| 61 | + |
| 62 | + // Log requests and their processing time |
| 63 | + router.Use(utils.LoggerMiddleware) |
| 64 | + |
| 65 | + router.HandleFunc("/", indexHandler).Methods("GET") |
| 66 | + router.HandleFunc("/tests/streaming-order-publish", streamingOrderPublishHandler).Methods("POST") |
| 67 | + |
| 68 | + router.Use(mux.CORSMethodMiddleware(router)) |
| 69 | + |
| 70 | + return router |
| 71 | +} |
| 72 | + |
| 73 | +func streamingOrderPublishHandler(w http.ResponseWriter, r *http.Request) { |
| 74 | + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 75 | + defer cancel() |
| 76 | + |
| 77 | + count := r.URL.Query().Get("count") |
| 78 | + numberOfMessages, err := strconv.Atoi(count) |
| 79 | + if err != nil { |
| 80 | + log.Printf("error parsing count: %v", err) |
| 81 | + w.WriteHeader(http.StatusBadRequest) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + sentMessagesChan := make(chan int, numberOfMessages) |
| 86 | + messageCount := atomic.Int32{} |
| 87 | + messageCount.Store(0) |
| 88 | + |
| 89 | + for { |
| 90 | + messageID := int(messageCount.Load()) |
| 91 | + err := sdkClient.PublishEvent(ctx, pubsubName, topicName, []byte(strconv.Itoa(messageID))) |
| 92 | + if err != nil { |
| 93 | + log.Printf("error publishing message: %v", err) |
| 94 | + } |
| 95 | + sentMessagesChan <- messageID |
| 96 | + messageCount.Add(1) |
| 97 | + |
| 98 | + if int(messageCount.Load()) == numberOfMessages { |
| 99 | + log.Printf("all messages sent successfully") |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + messages := make([]int, 0) |
| 105 | + for msg := range sentMessagesChan { |
| 106 | + messages = append(messages, msg) |
| 107 | + if len(messages) == numberOfMessages { |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + close(sentMessagesChan) |
| 113 | + |
| 114 | + w.WriteHeader(http.StatusOK) |
| 115 | + err = json.NewEncoder(w).Encode(messages) |
| 116 | + if err != nil { |
| 117 | + log.Printf("error writing response: %v", err) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func main() { |
| 122 | + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 123 | + defer cancel() |
| 124 | + |
| 125 | + daprGRPCPort = utils.PortFromEnv("DAPR_GRPC_PORT", 50001) |
| 126 | + conn, err := grpc.DialContext( |
| 127 | + ctx, |
| 128 | + net.JoinHostPort("127.0.0.1", strconv.Itoa(daprGRPCPort)), |
| 129 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 130 | + grpc.WithBlock(), |
| 131 | + ) |
| 132 | + if err != nil { |
| 133 | + log.Fatalf("Error connecting to Dapr's gRPC endpoint on port %d: %v", daprGRPCPort, err) |
| 134 | + } |
| 135 | + |
| 136 | + sdkClient = client.NewClientWithConnection(conn) |
| 137 | + |
| 138 | + log.Printf("pubsub-publisher-streaming - listening on http://localhost:%d", appPort) |
| 139 | + utils.StartServer(appPort, appRouter, true, false) |
| 140 | +} |
0 commit comments