├── .gitignore ├── LICENSE ├── README.md ├── distchan.go ├── distchan_test.go └── example ├── adder ├── client.go └── server │ └── server.go └── capitalizer ├── client.go └── server └── server.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | .*.swp 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # distchan 2 | 3 | Package distchan enables Go channels to be used for distributed computation. 4 | Improved stable version by covrom. 5 | 6 | ## Why? 7 | 8 | While Go's concurrency story around single-process data pipelines is 9 | [great](https://blog.golang.org/pipelines), its ability to distribute workloads 10 | across multiple machines is relatively lacking. There are several options here 11 | (notably [glow](https://github.com/chrislusf/glow) and 12 | [gleam](https://github.com/chrislusf/gleam)), but they lack the type-safety and 13 | ease-of-use that Go's built-in channels provide. 14 | 15 | ## How? 16 | 17 | In a nutshell: standard `net` primitives, Gob encoding, and reflection. 18 | 19 | Architecturally, the package assumes a client-server model of workload 20 | distribution. One server can have as many clients connected to it as you want, 21 | and work will be distributed across them using a simple round-robin algorithm. 22 | 23 | ## Example 24 | 25 | As a simple example, let's say that capitalizing letters in a string is very 26 | computationally expensive, and you want to distribute that work across a number 27 | of nodes. First, you'll need to create a server in charge of defining the work 28 | to be done: 29 | 30 | ```go 31 | package main 32 | 33 | import ( 34 | "log" 35 | "net" 36 | 37 | "github.com/covrom/distchan" 38 | ) 39 | 40 | func main() { 41 | ln, err := net.Listen("tcp", "localhost:5678") 42 | if err != nil { 43 | log.Fatal(err) 44 | } 45 | 46 | var ( 47 | out = make(chan string) 48 | in = make(chan string) 49 | server = distchan.NewServer(ln, out, in) 50 | ) 51 | 52 | server.Start() 53 | server.WaitUntilReady() // wait until we have at least one worker available 54 | 55 | go producer(out) 56 | 57 | for s := range in { 58 | println(s) 59 | } 60 | } 61 | 62 | func producer(out chan<- string) { 63 | // send strings to be capitalized to out 64 | out <- "hello world" 65 | // don't forget to close the channel! this is how all connected 66 | // clients know that there's no more work coming. 67 | close(out) 68 | } 69 | ``` 70 | 71 | Then you'll need to create a client, or worker. It's similarly easy to get wired 72 | up, so you can focus on the hard part: capitalizing strings: 73 | 74 | ```go 75 | package main 76 | 77 | import ( 78 | "log" 79 | "net" 80 | "strings" 81 | 82 | "github.com/covrom/distchan" 83 | ) 84 | 85 | func main() { 86 | conn, err := net.Dial("tcp", "localhost:5678") // must be able to connect to the server 87 | if err != nil { 88 | log.Fatal(err) 89 | } 90 | 91 | var ( 92 | out = make(chan string) 93 | in = make(chan string) 94 | ) 95 | 96 | client := distchan.NewClient(conn, out, in).Start() 97 | 98 | // Loop over all input from the server... 99 | for input := range in { 100 | capitalized := strings.ToUpper(input) 101 | // ...and send the results back. 102 | out <- capitalized 103 | } 104 | 105 | close(out) 106 | <-client.Done() 107 | } 108 | ``` 109 | 110 | Check out the `example` folder for more examples. 111 | -------------------------------------------------------------------------------- /distchan.go: -------------------------------------------------------------------------------- 1 | package distchan 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "encoding/gob" 7 | "errors" 8 | "io" 9 | "log" 10 | "net" 11 | "os" 12 | "reflect" 13 | "runtime" 14 | "sync" 15 | "sync/atomic" 16 | ) 17 | 18 | // ErrorInIsNotChannel raises when 'in' is not nil and not a channel 19 | // ErrorOutIsNotChannel raises when 'out' is not nil and not a channel 20 | var ( 21 | ErrorInIsNotChannel = errors.New("Parameter 'in' is not a channel") 22 | ErrorOutIsNotChannel = errors.New("Parameter 'out' is not a channel") 23 | ErrorBadRequest = errors.New("Bad request format") 24 | ) 25 | 26 | // sync.Pool for connection buffers 27 | var bufPool = sync.Pool{} 28 | 29 | func getBuffer() bytes.Buffer { 30 | b := bufPool.Get() 31 | if b != nil { 32 | return b.(bytes.Buffer) 33 | } 34 | return bytes.Buffer{} 35 | } 36 | 37 | func putBuffer(b bytes.Buffer) { 38 | b.Reset() 39 | bufPool.Put(b) 40 | } 41 | 42 | // NewServer registers a pair of channels with an active listener. Gob-encoded 43 | // messages received on the listener will be passed to in; any values passed to 44 | // out will be gob-encoded and written to one open connection. The server uses 45 | // a simple round-robin strategy when deciding which connection to send the message 46 | // to; no client is favored over any others. 47 | // 48 | // Note that the returned value's Start() method must be called before any 49 | // messages will be passed. This gives the user an opportunity to register 50 | // encoders and decoders before any data passes over the network. 51 | func NewServer(ln net.Listener, out, in interface{}) (*Server, error) { 52 | if in != nil && reflect.ValueOf(in).Kind() != reflect.Chan { 53 | return nil, ErrorInIsNotChannel 54 | } 55 | if out != nil && reflect.ValueOf(out).Kind() != reflect.Chan { 56 | return nil, ErrorOutIsNotChannel 57 | } 58 | return &Server{ 59 | ln: ln, 60 | outv: out, 61 | inv: in, 62 | closed: make(chan struct{}), 63 | done: make(chan struct{}), 64 | logger: log.New(os.Stdout, "[distchan] ", log.Lshortfile), 65 | chconn: make(chan clientConn), 66 | chbroad: make(chan net.Conn), //non-buffered 67 | }, nil 68 | } 69 | 70 | // Server represents a registration between a network listener and a pair 71 | // of channels, one for input and one for output. 72 | type Server struct { 73 | ln net.Listener 74 | inv, outv interface{} 75 | mu sync.RWMutex 76 | chconn chan clientConn 77 | conncnt int32 78 | chbroad chan net.Conn 79 | encoders, decoders []Transformer 80 | closed, done chan struct{} 81 | logger *log.Logger 82 | } 83 | 84 | type clientConn struct { 85 | c net.Conn 86 | } 87 | 88 | // Start instructs the server to begin serving messages. 89 | func (s *Server) Start() *Server { 90 | go s.handleIncomingConnections() 91 | if s.outv != nil { 92 | go s.handleOutgoingMessages() 93 | } 94 | return s 95 | } 96 | 97 | // Stop instructs the server to stop serving messages. 98 | func (s *Server) Stop() { 99 | if err := s.ln.Close(); err != nil { 100 | s.logger.Printf("error closing listener: %s\n", err) 101 | } 102 | } 103 | 104 | // AddEncoder adds a new encoder to the server. Any outbound messages 105 | // will be passed through all registered encoders before being sent 106 | // over the wire. See the tests for an example of encoding the data 107 | // using AES encryption. 108 | func (s *Server) AddEncoder(f Transformer) *Server { 109 | s.encoders = append(s.encoders, f) 110 | return s 111 | } 112 | 113 | // AddDecoder adds a new decoder to the server. Any inbound messages 114 | // will be passed through all registered decoders before being sent 115 | // to the channel. See the tests for an example of decoding the data 116 | // using AES encryption. 117 | func (s *Server) AddDecoder(f Transformer) *Server { 118 | s.decoders = append(s.decoders, f) 119 | return s 120 | } 121 | 122 | // Ready returns true if there are any clients currently connected. 123 | func (s *Server) Ready() bool { 124 | return atomic.LoadInt32(&s.conncnt) > 0 125 | } 126 | 127 | // WaitUntilReady blocks until the server has at least one client available. 128 | func (s *Server) WaitUntilReady() { 129 | for { 130 | runtime.Gosched() 131 | if s.Ready() { 132 | return 133 | } 134 | } 135 | } 136 | 137 | // Logger exposes the server's internal logger so that it can be configured. 138 | // For example, if you want the logs to go somewhere besides standard output 139 | // (the default), you can use s.Logger().SetOutput(...). 140 | func (s *Server) Logger() *log.Logger { 141 | return s.logger 142 | } 143 | 144 | func (s *Server) handleIncomingConnections() { 145 | go func() { 146 | for { 147 | conn, err := s.ln.Accept() 148 | if err != nil { 149 | // for now, assume it's a "use of closed network connection" error 150 | close(s.closed) 151 | close(s.chconn) 152 | return 153 | } 154 | cc := clientConn{c: conn} 155 | s.chconn <- cc 156 | } 157 | }() 158 | 159 | for cc := range s.chconn { 160 | go s.handleIncomingMessages(cc) 161 | } 162 | } 163 | 164 | func (s *Server) handleIncomingMessages(conn clientConn) { 165 | var ( 166 | buf = getBuffer() 167 | dec = gob.NewDecoder(&buf) 168 | et reflect.Type 169 | done = make(chan struct{}) 170 | ) 171 | 172 | if s.inv != nil { 173 | et = reflect.TypeOf(s.inv).Elem() 174 | } 175 | 176 | defer close(done) 177 | 178 | atomic.AddInt32(&s.conncnt, 1) 179 | 180 | go func(c net.Conn, d chan struct{}) { 181 | // wait for closing and send close to client 182 | select { 183 | case <-s.closed: 184 | if err := binary.Write(c, binary.LittleEndian, signature); err != nil { 185 | s.logger.Println(err) 186 | } 187 | if err := binary.Write(c, binary.LittleEndian, int32(-1)); err != nil { 188 | s.logger.Println(err) 189 | } 190 | // c.Close() 191 | <-d 192 | case <-d: 193 | } 194 | 195 | atomic.AddInt32(&s.conncnt, -1) 196 | 197 | }(conn.c, done) 198 | 199 | go func(c net.Conn, d chan struct{}) { 200 | // wait for broadcasting and send it to client 201 | for { 202 | select { 203 | case <-d: 204 | // close(s.chbroad) 205 | s.chbroad <- c 206 | return 207 | case s.chbroad <- c: 208 | // push client connection in broadcast queue 209 | } 210 | } 211 | }(conn.c, done) 212 | 213 | for { 214 | buf.Reset() 215 | 216 | err := readChunk(&buf, conn.c) 217 | if err != nil { 218 | if err != io.EOF { 219 | s.logger.Println(err) 220 | } 221 | break 222 | } 223 | 224 | if s.inv != nil { 225 | 226 | for _, decoder := range s.decoders { 227 | dc := decoder(&buf) 228 | buf.Reset() 229 | if _, err = io.Copy(&buf, dc); err != nil { 230 | s.logger.Panicln(err) 231 | } 232 | } 233 | 234 | x := reflect.New(et) 235 | if err := dec.DecodeValue(x); err != nil { 236 | if err == io.EOF { 237 | break 238 | } 239 | s.logger.Panicln(err) 240 | } 241 | 242 | reflect.ValueOf(s.inv).Send(x.Elem()) 243 | } 244 | } 245 | 246 | if buf.Cap() <= 1<<22 { 247 | putBuffer(buf) 248 | } 249 | } 250 | 251 | func (s *Server) handleOutgoingMessages() { 252 | var ( 253 | buf = getBuffer() 254 | enc = gob.NewEncoder(&buf) 255 | ) 256 | 257 | for { 258 | buf.Reset() 259 | 260 | x, ok := reflect.ValueOf(s.outv).Recv() 261 | if !ok { 262 | break 263 | } 264 | 265 | if err := enc.EncodeValue(x); err != nil { 266 | s.logger.Println(err) 267 | continue 268 | } 269 | 270 | for _, encoder := range s.encoders { 271 | ec := encoder(&buf) 272 | buf.Reset() 273 | if _, err := io.Copy(&buf, ec); err != nil { 274 | s.logger.Panicln(err) 275 | } 276 | } 277 | 278 | seen := make(map[net.Conn]bool) 279 | 280 | for i := int32(0); i < atomic.LoadInt32(&s.conncnt); { 281 | select { 282 | // case <-s.closed: 283 | // break 284 | case c, ok := <-s.chbroad: 285 | if ok { 286 | // we need only different connections 287 | if _, ok := seen[c]; !ok { 288 | seen[c] = true 289 | bts := buf 290 | if err := writeChunk(c, &bts, bts.Len()); err != nil { 291 | s.logger.Println(err) 292 | } 293 | i++ 294 | } 295 | } 296 | } 297 | } 298 | } 299 | 300 | if err := s.ln.Close(); err != nil { 301 | s.logger.Printf("error closing listener: %s\n", err) 302 | } 303 | 304 | if buf.Cap() <= 1<<22 { 305 | putBuffer(buf) 306 | } 307 | } 308 | 309 | func NewClient(conn net.Conn, out, in interface{}) (*Client, error) { 310 | if in != nil && reflect.ValueOf(in).Kind() != reflect.Chan { 311 | return nil, ErrorInIsNotChannel 312 | } 313 | if out != nil && reflect.ValueOf(out).Kind() != reflect.Chan { 314 | return nil, ErrorOutIsNotChannel 315 | } 316 | return &Client{ 317 | conn: conn, 318 | outv: out, 319 | inv: in, 320 | logger: log.New(os.Stdout, "[distchan] ", log.Lshortfile), 321 | done: make(chan struct{}), 322 | }, nil 323 | } 324 | 325 | // Transformer represents a function that does an arbitrary transformation 326 | // on a piece of data. It's used for defining custom encoders and decoders 327 | // for modifying how data is sent across the wire. 328 | type Transformer func(src io.Reader) io.Reader 329 | 330 | // Client represents a registration between a network connection and a pair 331 | // of channels. See the documentation for Server for more details. 332 | type Client struct { 333 | conn net.Conn 334 | inv, outv interface{} 335 | encoders, decoders []Transformer 336 | started bool 337 | logger *log.Logger 338 | done chan struct{} 339 | } 340 | 341 | // AddEncoder adds a new encoder to the client. Any outbound messages 342 | // will be passed through all registered encoders before being sent 343 | // over the wire. See the tests for an example of encoding the data 344 | // using AES encryption. 345 | func (c *Client) AddEncoder(f Transformer) *Client { 346 | c.encoders = append(c.encoders, f) 347 | return c 348 | } 349 | 350 | // AddDecoder adds a new decoder to the client. Any inbound messages 351 | // will be passed through all registered decoders before being sent 352 | // to the channel. See the tests for an example of decoding the data 353 | // using AES encryption. 354 | func (c *Client) AddDecoder(f Transformer) *Client { 355 | c.decoders = append(c.decoders, f) 356 | return c 357 | } 358 | 359 | // Start instructs the client to begin serving messages. 360 | func (c *Client) Start() *Client { 361 | if c.inv != nil { 362 | go c.handleIncomingMessages() 363 | } 364 | if c.outv != nil { 365 | go c.handleOutgoingMessages() 366 | } 367 | c.started = true 368 | return c 369 | } 370 | 371 | // Done returns a channel that will be closed once all in-flight data has been 372 | // handled. 373 | func (c *Client) Done() <-chan struct{} { 374 | return c.done 375 | } 376 | 377 | // Logger exposes the client's internal logger so that it can be configured. 378 | // For example, if you want the logs to go somewhere besides standard output 379 | // (the default), you can use c.Logger().SetOutput(...). 380 | func (c *Client) Logger() *log.Logger { 381 | return c.logger 382 | } 383 | 384 | func (c *Client) handleIncomingMessages() { 385 | var ( 386 | buf = getBuffer() 387 | // The gob decoder uses a buffer because its underlying reader 388 | // can't change without running into an "unknown type id" error. 389 | dec = gob.NewDecoder(&buf) 390 | et = reflect.TypeOf(c.inv).Elem() 391 | ) 392 | 393 | defer func() { 394 | reflect.ValueOf(c.inv).Close() 395 | 396 | // A panic can happen if the underlying channel was closed 397 | // and we tried to send on it, or if there was a decryption 398 | // failure. We don't want the panic to go all the way to the 399 | // top, but we do want to stop processing and log the error. 400 | if r := recover(); r != nil { 401 | c.logger.Println(r) 402 | } 403 | }() 404 | 405 | for { 406 | buf.Reset() 407 | 408 | err := readChunk(&buf, c.conn) 409 | if err != nil { 410 | if err == io.EOF { 411 | break 412 | } 413 | panic(err) 414 | } 415 | 416 | for _, decoder := range c.decoders { 417 | dc := decoder(&buf) 418 | buf.Reset() 419 | if _, err = io.Copy(&buf, dc); err != nil { 420 | c.logger.Panicln(err) 421 | } 422 | } 423 | 424 | x := reflect.New(et) 425 | if err := dec.DecodeValue(x); err != nil { 426 | if err == io.EOF { 427 | break 428 | } 429 | panic(err) 430 | } 431 | 432 | reflect.ValueOf(c.inv).Send(x.Elem()) 433 | } 434 | 435 | if buf.Cap() <= 1<<22 { 436 | putBuffer(buf) 437 | } 438 | } 439 | 440 | func (c *Client) handleOutgoingMessages() { 441 | var ( 442 | buf = getBuffer() 443 | enc = gob.NewEncoder(&buf) 444 | ) 445 | 446 | for { 447 | buf.Reset() 448 | 449 | x, ok := reflect.ValueOf(c.outv).Recv() 450 | if !ok { 451 | break 452 | } 453 | if err := enc.EncodeValue(x); err != nil { 454 | c.logger.Panicln(err) 455 | } 456 | 457 | for _, encoder := range c.encoders { 458 | ec := encoder(&buf) 459 | buf.Reset() 460 | if _, err := io.Copy(&buf, ec); err != nil { 461 | c.logger.Panicln(err) 462 | } 463 | } 464 | 465 | if err := writeChunk(c.conn, &buf, buf.Len()); err != nil { 466 | c.logger.Printf("error writing value to connection: %s\n", err) 467 | } 468 | } 469 | 470 | if buf.Cap() <= 1<<22 { 471 | putBuffer(buf) 472 | } 473 | 474 | close(c.done) 475 | } 476 | 477 | var signature int32 = 0x7f38b034 // protecting from bad incoming data 478 | 479 | func readChunk(buf io.Writer, r io.Reader) error { 480 | var n int32 481 | if err := binary.Read(r, binary.LittleEndian, &n); err != nil { 482 | return err 483 | } 484 | 485 | if n != signature { 486 | return ErrorBadRequest 487 | } 488 | 489 | if err := binary.Read(r, binary.LittleEndian, &n); err != nil { 490 | return err 491 | } 492 | 493 | if n == -1 { 494 | return io.EOF 495 | } 496 | 497 | if _, err := io.CopyN(buf, r, int64(n)); err != nil { 498 | return err 499 | } 500 | 501 | return nil 502 | } 503 | 504 | func writeChunk(w io.Writer, buf io.Reader, lenbuf int) error { 505 | if err := binary.Write(w, binary.LittleEndian, signature); err != nil { 506 | return err 507 | } 508 | if err := binary.Write(w, binary.LittleEndian, int32(lenbuf)); err != nil { 509 | return err 510 | } 511 | if _, err := io.Copy(w, buf); err != nil { 512 | return err 513 | } 514 | return nil 515 | } 516 | -------------------------------------------------------------------------------- /distchan_test.go: -------------------------------------------------------------------------------- 1 | package distchan_test 2 | 3 | import ( 4 | "bytes" 5 | "crypto/aes" 6 | "crypto/cipher" 7 | "crypto/rand" 8 | "io" 9 | "io/ioutil" 10 | "net" 11 | "reflect" 12 | "testing" 13 | 14 | "github.com/covrom/distchan" 15 | ) 16 | 17 | func TestDistchan(t *testing.T) { 18 | ln, err := net.Listen("tcp", "localhost:9450") 19 | if err != nil { 20 | t.Fatal(err) 21 | } 22 | 23 | sch := make(chan string) 24 | server, _ := distchan.NewServer(ln, sch, nil) 25 | server.Start() 26 | 27 | conn, err := net.Dial(ln.Addr().Network(), ln.Addr().String()) 28 | if err != nil { 29 | t.Fatal(err) 30 | } 31 | 32 | inCh := make(chan string) 33 | cli, _ := distchan.NewClient(conn, nil, inCh) 34 | cli.Start() 35 | 36 | go func() { 37 | sch <- "why" 38 | sch <- "hello" 39 | sch <- "there" 40 | sch <- "world" 41 | close(sch) 42 | }() 43 | 44 | var received []string 45 | for msg := range inCh { 46 | received = append(received, msg) 47 | } 48 | 49 | if !reflect.DeepEqual(received, []string{"why", "hello", "there", "world"}) { 50 | t.Errorf("received unexpected values: %v", received) 51 | } 52 | } 53 | 54 | func Encrypter(key []byte) distchan.Transformer { 55 | return func(buf io.Reader) io.Reader { 56 | block, err := aes.NewCipher(key) 57 | if err != nil { 58 | panic(err) 59 | } 60 | plaintext, _ := ioutil.ReadAll(buf) 61 | ciphertext := make([]byte, aes.BlockSize+len(plaintext)) 62 | iv := ciphertext[:aes.BlockSize] 63 | if _, err := io.ReadFull(rand.Reader, iv); err != nil { 64 | panic(err) 65 | } 66 | 67 | stream := cipher.NewCFBEncrypter(block, iv) 68 | stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext) 69 | 70 | return bytes.NewReader(ciphertext) 71 | } 72 | } 73 | 74 | func Decrypter(key []byte) distchan.Transformer { 75 | return func(buf io.Reader) io.Reader { 76 | block, err := aes.NewCipher(key) 77 | if err != nil { 78 | panic(err) 79 | } 80 | ciphertext,_ := ioutil.ReadAll(buf) 81 | if len(ciphertext) < aes.BlockSize { 82 | panic("ciphertext too short") 83 | } 84 | iv := ciphertext[:aes.BlockSize] 85 | ciphertext = ciphertext[aes.BlockSize:] 86 | 87 | stream := cipher.NewCFBDecrypter(block, iv) 88 | stream.XORKeyStream(ciphertext, ciphertext) 89 | 90 | return bytes.NewReader(ciphertext) 91 | } 92 | } 93 | 94 | func TestEncryptionSuccess(t *testing.T) { 95 | ln, err := net.Listen("tcp", "localhost:9450") 96 | if err != nil { 97 | t.Fatal(err) 98 | } 99 | 100 | sch := make(chan string) 101 | server, _ := distchan.NewServer(ln, sch, nil) 102 | server.AddEncoder(Encrypter([]byte("the-key-has-to-be-32-bytes-long!"))) 103 | server.Start() 104 | 105 | go func() { 106 | sch <- "why" 107 | sch <- "hello" 108 | sch <- "there" 109 | sch <- "world" 110 | close(sch) 111 | }() 112 | 113 | conn, err := net.Dial(ln.Addr().Network(), ln.Addr().String()) 114 | if err != nil { 115 | t.Fatal(err) 116 | } 117 | 118 | inCh := make(chan string) 119 | client, _ := distchan.NewClient(conn, nil, inCh) 120 | client.AddDecoder(Decrypter([]byte("the-key-has-to-be-32-bytes-long!"))) 121 | client.Start() 122 | 123 | var received []string 124 | for msg := range inCh { 125 | received = append(received, msg) 126 | } 127 | 128 | if !reflect.DeepEqual(received, []string{"why", "hello", "there", "world"}) { 129 | t.Errorf("received unexpected values: %v", received) 130 | } 131 | } 132 | 133 | func TestEncryptionFailure(t *testing.T) { 134 | ln, err := net.Listen("tcp", "localhost:9450") 135 | if err != nil { 136 | t.Fatal(err) 137 | } 138 | 139 | sch := make(chan string) 140 | server, _ := distchan.NewServer(ln, sch, nil) 141 | server.AddEncoder(Encrypter([]byte("the-key-has-to-be-32-bytes-long!"))) 142 | server.Start() 143 | 144 | go func() { 145 | sch <- "why" 146 | sch <- "hello" 147 | sch <- "there" 148 | sch <- "world" 149 | close(sch) 150 | }() 151 | 152 | conn, err := net.Dial(ln.Addr().Network(), ln.Addr().String()) 153 | if err != nil { 154 | t.Fatal(err) 155 | } 156 | 157 | inCh := make(chan string) 158 | client, _ := distchan.NewClient(conn, nil, inCh) 159 | client.Logger().SetOutput(ioutil.Discard) 160 | // Note that the key here is different. 161 | client.AddDecoder(Decrypter([]byte("the-key-has-to-be-32-bytes-long?"))) 162 | client.Start() 163 | 164 | var received []string 165 | for msg := range inCh { 166 | received = append(received, msg) 167 | } 168 | 169 | if len(received) > 0 { 170 | t.Error("received values when none were expected") 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /example/adder/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/gob" 5 | "flag" 6 | "fmt" 7 | "log" 8 | "net" 9 | 10 | "github.com/covrom/distchan" 11 | ) 12 | 13 | type AdderInput struct { 14 | ID string 15 | A, B int 16 | } 17 | 18 | type AdderOutput struct { 19 | ID string 20 | Answer int 21 | } 22 | 23 | func main() { 24 | addr := flag.String("addr", "", "address to connect to") 25 | flag.Parse() 26 | 27 | if *addr == "" { 28 | log.Fatal("no server address specified") 29 | } 30 | 31 | gob.Register(AdderInput{}) 32 | gob.Register(AdderOutput{}) 33 | var ( 34 | out = make(chan AdderOutput) 35 | in = make(chan AdderInput) 36 | ) 37 | 38 | conn, err := net.Dial("tcp", *addr) 39 | if err != nil { 40 | panic(err) 41 | } 42 | 43 | cli, _ := distchan.NewClient(conn, out, in) 44 | cli.Start() 45 | 46 | fmt.Println("waiting for input...") 47 | for input := range in { 48 | fmt.Printf("[%s] processing %d + %d\n", input.ID, input.A, input.B) 49 | answer := input.A + input.B 50 | out <- AdderOutput{ 51 | ID: input.ID, 52 | Answer: answer, 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /example/adder/server/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | crypto_rand "crypto/rand" 5 | "encoding/gob" 6 | "flag" 7 | "fmt" 8 | "io" 9 | "log" 10 | "math/rand" 11 | "net" 12 | "time" 13 | 14 | "github.com/covrom/distchan" 15 | ) 16 | 17 | type AdderInput struct { 18 | ID string 19 | A, B int 20 | } 21 | 22 | type AdderOutput struct { 23 | ID string 24 | Answer int 25 | } 26 | 27 | func producer(out chan<- AdderInput) { 28 | for { 29 | input := AdderInput{ 30 | ID: newUUID(), 31 | A: rand.Intn(100), 32 | B: rand.Intn(100), 33 | } 34 | fmt.Printf("[%s] requesting answer to %d + %d\n", input.ID, input.A, input.B) 35 | out <- input 36 | } 37 | } 38 | 39 | // ID generation method graciously borrowed from https://play.golang.org/p/4FkNSiUDMg 40 | func newUUID() string { 41 | uuid := make([]byte, 16) 42 | n, err := io.ReadFull(crypto_rand.Reader, uuid) 43 | if n != len(uuid) || err != nil { 44 | panic(err) 45 | } 46 | uuid[8] = uuid[8]&^0xc0 | 0x80 47 | uuid[6] = uuid[6]&^0xf0 | 0x40 48 | return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) 49 | } 50 | 51 | func main() { 52 | bindAddr := flag.String("bind", "", "address to bind to") 53 | flag.Parse() 54 | 55 | if *bindAddr == "" { 56 | log.Fatal("no bind address specified") 57 | } 58 | 59 | rand.Seed(time.Now().Unix()) 60 | gob.Register(AdderInput{}) 61 | gob.Register(AdderOutput{}) 62 | var ( 63 | out = make(chan AdderInput) 64 | in = make(chan AdderOutput) 65 | ) 66 | 67 | ln, err := net.Listen("tcp", *bindAddr) 68 | if err != nil { 69 | panic(err) 70 | } 71 | 72 | server,_ := distchan.NewServer(ln, out, in) 73 | server.Start() 74 | 75 | fmt.Println("waiting for clients to connect...") 76 | server.WaitUntilReady() 77 | fmt.Println("...and go!") 78 | 79 | go producer(out) 80 | 81 | for result := range in { 82 | fmt.Printf("[%s] received answer: %d\n", result.ID, result.Answer) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /example/capitalizer/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net" 6 | "strings" 7 | 8 | "github.com/covrom/distchan" 9 | ) 10 | 11 | func main() { 12 | conn, err := net.Dial("tcp", "localhost:5678") // must be able to connect to the server 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | 17 | var ( 18 | out = make(chan string) 19 | in = make(chan string) 20 | ) 21 | 22 | client,_ := distchan.NewClient(conn, out, in) 23 | client.Start() 24 | 25 | // Loop over all input from the server... 26 | for input := range in { 27 | capitalized := strings.ToUpper(input) 28 | // ...and send the results back. 29 | out <- capitalized 30 | } 31 | 32 | close(out) 33 | <-client.Done() 34 | } 35 | -------------------------------------------------------------------------------- /example/capitalizer/server/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net" 6 | 7 | "github.com/covrom/distchan" 8 | ) 9 | 10 | func main() { 11 | ln, err := net.Listen("tcp", "localhost:5678") 12 | if err != nil { 13 | log.Fatal(err) 14 | } 15 | 16 | var ( 17 | out = make(chan string) 18 | in = make(chan string) 19 | server,_ = distchan.NewServer(ln, out, in) 20 | ) 21 | 22 | server.Start() 23 | server.WaitUntilReady() // wait until we have at least one worker available 24 | 25 | go producer(out) 26 | 27 | for s := range in { 28 | println(s) 29 | } 30 | } 31 | 32 | func producer(out chan<- string) { 33 | // send strings to be capitalized to out 34 | out <- "hello world" 35 | // don't forget to close the channel! this is how all connected 36 | // clients know that there's no more work coming. 37 | close(out) 38 | } 39 | --------------------------------------------------------------------------------