|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 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 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | + |
| 14 | +package pubsublite |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + "fmt" |
| 19 | + "time" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // MaxPublishRequestCount is the maximum number of messages that can be |
| 24 | + // batched in a single publish request. |
| 25 | + MaxPublishRequestCount = 1000 |
| 26 | + |
| 27 | + // MaxPublishMessageBytes is the maximum allowed serialized size of a single |
| 28 | + // Pub/Sub message in bytes. |
| 29 | + MaxPublishMessageBytes = 1000000 |
| 30 | + |
| 31 | + // MaxPublishRequestBytes is the maximum allowed serialized size of a single |
| 32 | + // publish request (containing a batch of messages) in bytes. |
| 33 | + MaxPublishRequestBytes = 3500000 |
| 34 | +) |
| 35 | + |
| 36 | +// PublishSettings control the batching of published messages. |
| 37 | +type PublishSettings struct { |
| 38 | + // Publish a non-empty batch after this delay has passed. Must be > 0. |
| 39 | + DelayThreshold time.Duration |
| 40 | + |
| 41 | + // Publish a batch when it has this many messages. Must be > 0. The maximum is |
| 42 | + // MaxPublishRequestCount. |
| 43 | + CountThreshold int |
| 44 | + |
| 45 | + // Publish a batch when its size in bytes reaches this value. Must be > 0. The |
| 46 | + // maximum is MaxPublishRequestBytes. |
| 47 | + ByteThreshold int |
| 48 | + |
| 49 | + // The maximum time that the client will attempt to establish a publish stream |
| 50 | + // connection to the server. Must be > 0. |
| 51 | + // |
| 52 | + // The timeout is exceeded, the publisher will terminate with the last error |
| 53 | + // that occurred while trying to reconnect. Note that if the timeout duration |
| 54 | + // is long, ErrOverflow may occur first. |
| 55 | + Timeout time.Duration |
| 56 | + |
| 57 | + // The maximum number of bytes that the publisher will keep in memory before |
| 58 | + // returning ErrOverflow. Must be > 0. |
| 59 | + // |
| 60 | + // Note that Pub/Sub Lite topics are provisioned a publishing throughput |
| 61 | + // capacity, per partition, shared by all publisher clients. Setting a large |
| 62 | + // buffer size can mitigate transient publish spikes. However, consistently |
| 63 | + // attempting to publish messages at a much higher rate than the publishing |
| 64 | + // throughput capacity can cause the buffers to overflow. For more |
| 65 | + // information, see https://cloud.google.com/pubsub/lite/docs/topics. |
| 66 | + BufferedByteLimit int |
| 67 | +} |
| 68 | + |
| 69 | +// DefaultPublishSettings holds the default values for PublishSettings. |
| 70 | +var DefaultPublishSettings = PublishSettings{ |
| 71 | + DelayThreshold: 10 * time.Millisecond, |
| 72 | + CountThreshold: 100, |
| 73 | + ByteThreshold: 1e6, |
| 74 | + Timeout: 60 * time.Second, |
| 75 | + // By default set to a high limit that is not likely to occur, but prevents |
| 76 | + // OOM errors in clients. |
| 77 | + BufferedByteLimit: 1 << 30, // 1 GiB |
| 78 | +} |
| 79 | + |
| 80 | +func validatePublishSettings(settings PublishSettings) error { |
| 81 | + if settings.DelayThreshold <= 0 { |
| 82 | + return errors.New("pubsublite: invalid publish settings. DelayThreshold duration must be > 0") |
| 83 | + } |
| 84 | + if settings.Timeout <= 0 { |
| 85 | + return errors.New("pubsublite: invalid publish settings. Timeout duration must be > 0") |
| 86 | + } |
| 87 | + if settings.CountThreshold <= 0 { |
| 88 | + return errors.New("pubsublite: invalid publish settings. CountThreshold must be > 0") |
| 89 | + } |
| 90 | + if settings.CountThreshold > MaxPublishRequestCount { |
| 91 | + return fmt.Errorf("pubsublite: invalid publish settings. Maximum CountThreshold is MaxPublishRequestCount (%d)", MaxPublishRequestCount) |
| 92 | + } |
| 93 | + if settings.ByteThreshold <= 0 { |
| 94 | + return errors.New("pubsublite: invalid publish settings. ByteThreshold must be > 0") |
| 95 | + } |
| 96 | + if settings.ByteThreshold > MaxPublishRequestBytes { |
| 97 | + return fmt.Errorf("pubsublite: invalid publish settings. Maximum ByteThreshold is MaxPublishRequestBytes (%d)", MaxPublishRequestBytes) |
| 98 | + } |
| 99 | + if settings.BufferedByteLimit <= 0 { |
| 100 | + return errors.New("pubsublite: invalid publish settings. BufferedByteLimit must be > 0") |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
0 commit comments