github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/doc.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 /* 13 Package rpc provides access to the exported methods of an object across a network 14 or other I/O connection. After creating a server instance objects can be registered, 15 making it visible from the outside. Exported methods that follow specific 16 conventions can be called remotely. It also has support for the publish/subscribe 17 pattern. 18 19 Methods that satisfy the following criteria are made available for remote access: 20 - object must be exported 21 - method must be exported 22 - method returns 0, 1 (response or error) or 2 (response and error) values 23 - method argument(s) must be exported or builtin types 24 - method returned value(s) must be exported or builtin types 25 26 An example method: 27 func (s *CalcService) Add(a, b int) (int, error) 28 29 When the returned error isn't nil the returned integer is ignored and the error is 30 send back to the client. Otherwise the returned integer is send back to the client. 31 32 Optional arguments are supported by accepting pointer values as arguments. E.g. 33 if we want to do the addition in an optional finite field we can accept a mod 34 argument as pointer value. 35 36 func (s *CalService) Add(a, b int, mod *int) (int, error) 37 38 This RPC method can be called with 2 integers and a null value as third argument. 39 In that case the mod argument will be nil. Or it can be called with 3 integers, 40 in that case mod will be pointing to the given third argument. Since the optional 41 argument is the last argument the RPC package will also accept 2 integers as 42 arguments. It will pass the mod argument as nil to the RPC method. 43 44 The server offers the ServeCodec method which accepts a ServerCodec instance. It will 45 read requests from the codec, process the request and sends the response back to the 46 client using the codec. The server can execute requests concurrently. Responses 47 can be sent back to the client out of order. 48 49 An example server which uses the JSON codec: 50 type CalculatorService struct {} 51 52 func (s *CalculatorService) Add(a, b int) int { 53 return a + b 54 } 55 56 func (s *CalculatorService Div(a, b int) (int, error) { 57 if b == 0 { 58 return 0, errors.New("divide by zero") 59 } 60 return a/b, nil 61 } 62 63 calculator := new(CalculatorService) 64 server := NewServer() 65 server.RegisterName("calculator", calculator") 66 67 l, _ := net.ListenUnix("unix", &net.UnixAddr{Net: "unix", Name: "/tmp/calculator.sock"}) 68 for { 69 c, _ := l.AcceptUnix() 70 codec := v2.NewJSONCodec(c) 71 go server.ServeCodec(codec) 72 } 73 74 The package also supports the publish subscribe pattern through the use of subscriptions. 75 A method that is considered eligible for notifications must satisfy the following criteria: 76 - object must be exported 77 - method must be exported 78 - first method argument type must be context.Context 79 - method argument(s) must be exported or builtin types 80 - method must return the tuple Subscription, error 81 82 An example method: 83 func (s *BlockChainService) NewBlocks(ctx context.Context) (Subscription, error) { 84 ... 85 } 86 87 Subscriptions are deleted when: 88 - the user sends an unsubscribe request 89 - the connection which was used to create the subscription is closed. This can be initiated 90 by the client and server. The server will close the connection on an write error or when 91 the queue of buffered notifications gets too big. 92 */ 93 package rpc