| 1 | // Copyright (c) 2021 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | package thriftudpscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/ |
| 22 | |
| 23 | import ( |
| 24 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 25 | |
| 26 | "github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thriftscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/" |
| 27 | ) |
| 28 | |
| 29 | // TMultiUDPTransport does multiUDP as a thrift.TTransport |
| 30 | type TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport# struct { |
| 31 | transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. []thriftscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/.TTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/TTransport# |
| 32 | } |
| 33 | |
| 34 | // NewTMultiUDPClientTransport creates a set of net.UDPConn-backed TTransports for Thrift clients |
| 35 | // All writes are buffered and flushed in one UDP packet. If locHostPort is not "", it |
| 36 | // will be used as the local address for the connection |
| 37 | // Example: |
| 38 | // trans, err := thriftudp.NewTMultiUDPClientTransport([]string{"192.168.1.1:9090","192.168.1.2:9090"}, "") |
| 39 | func NewTMultiUDPClientTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/NewTMultiUDPClientTransport().( |
| 40 | destHostPorts []string, |
| 41 | locHostPort string, |
| 42 | ) (*TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#, error) { |
| 43 | var transports []thriftscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/.TTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/TTransport# |
| 44 | for i := range destHostPorts { |
| 45 | trans, err := NewTUDPClientTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/NewTUDPClientTransport().(destHostPorts[i], locHostPort) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | transports = append(transports, trans) |
| 50 | } |
| 51 | |
| 52 | return &TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#{transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports.: transports}, nil |
| 53 | } |
| 54 | |
| 55 | // Open the connections of the underlying transports |
| 56 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) Openscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#Open().() error { |
| 57 | for _, trans := range p.transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. { |
| 58 | if err := trans.Openscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/TTransport#Open.(); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // IsOpen returns true if the connections of the underlying transports are open |
| 66 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) IsOpenscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#IsOpen().() bool { |
| 67 | for _, trans := range p.transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. { |
| 68 | if open := trans.IsOpenscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/TTransport#IsOpen.(); !open { |
| 69 | return false |
| 70 | } |
| 71 | } |
| 72 | return true |
| 73 | } |
| 74 | |
| 75 | // Close closes the connections of the underlying transports |
| 76 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) Closescip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#Close().() error { |
| 77 | for _, trans := range p.transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. { |
| 78 | if err := trans.Closescip-go gomod github.com/golang/go/src go1.20 io/Closer#Close().(); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | } |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // Read is not supported for multiple underlying transports |
| 86 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) Readscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#Read().(buf []byte) (int, error) { |
| 87 | // Not applicable, required by TTransport however |
| 88 | return 0, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("not supported") |
| 89 | } |
| 90 | |
| 91 | // RemainingBytes is not supported for multiple underlying transports |
| 92 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) RemainingBytesscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#RemainingBytes().() uint64 { |
| 93 | // Not applicable, required by TTransport however |
| 94 | return 0 |
| 95 | } |
| 96 | |
| 97 | // Write writes specified buf to the write buffer of underlying transports |
| 98 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) Writescip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#Write().(buff []byte) (int, error) { |
| 99 | n := 0 |
| 100 | for _, trans := range p.transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. { |
| 101 | written, err := trans.Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(buff) |
| 102 | if err != nil { |
| 103 | return n, err |
| 104 | } |
| 105 | if written > n { |
| 106 | n = written |
| 107 | } |
| 108 | } |
| 109 | return n, nil |
| 110 | } |
| 111 | |
| 112 | // Flush flushes the write buffer of the underlying transports |
| 113 | func (p *TMultiUDPTransportscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#) Flushscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#Flush().() error { |
| 114 | for _, trans := range p.transportsscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/m3/thriftudp`/TMultiUDPTransport#transports. { |
| 115 | if err := trans.Flushscip-go gomod github.com/uber-go/tally/v4 . `github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift`/Flusher#Flush.(); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | } |
| 119 | return nil |
| 120 | } |