github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/rony.go (about) 1 package rony 2 3 import ( 4 "mime/multipart" 5 "net" 6 7 "github.com/dgraph-io/badger/v3" 8 "github.com/goccy/go-json" 9 "github.com/prometheus/client_golang/prometheus" 10 "github.com/ronaksoft/rony/internal/metrics" 11 "github.com/ronaksoft/rony/log" 12 "google.golang.org/protobuf/proto" 13 ) 14 15 /* 16 Creation Time: 2021 - Jan - 07 17 Created by: (ehsan) 18 Maintainers: 19 1. Ehsan N. Moosa (E2) 20 Auditor: Ehsan N. Moosa (E2) 21 Copyright Ronak Software Group 2020 22 */ 23 24 // Cluster is the component which create and present the whole cluster of Edge nodes. 25 type Cluster interface { 26 Start() error 27 Shutdown() 28 Join(addr ...string) (int, error) 29 Leave() error 30 Members() []ClusterMember 31 MembersByReplicaSet(replicaSets ...uint64) []ClusterMember 32 MemberByID(string) ClusterMember 33 MemberByHash(uint64) ClusterMember 34 ReplicaSet() uint64 35 ServerID() string 36 TotalReplicas() int 37 Addr() string 38 SetGatewayAddrs(hostPorts []string) error 39 SetTunnelAddrs(hostPorts []string) error 40 Subscribe(d ClusterDelegate) 41 } 42 43 type ClusterMember interface { 44 Proto(info *Edge) *Edge 45 ServerID() string 46 ReplicaSet() uint64 47 GatewayAddr() []string 48 TunnelAddr() []string 49 Dial() (net.Conn, error) 50 } 51 52 type ClusterDelegate interface { 53 OnJoin(hash uint64) 54 OnLeave(hash uint64) 55 } 56 57 type GatewayDelegate interface { 58 OnConnect(c Conn, kvs ...*KeyValue) 59 OnMessage(c Conn, streamID int64, data []byte) 60 OnClose(c Conn) 61 } 62 63 // Gateway defines the gateway interface where clients could connect 64 // and communicate with the Edge servers 65 type Gateway interface { 66 Start() 67 Run() 68 Shutdown() 69 GetConn(connID uint64) Conn 70 Addr() []string 71 Protocol() GatewayProtocol 72 Subscribe(d GatewayDelegate) 73 } 74 75 type GatewayProtocol int32 76 77 const ( 78 Undefined GatewayProtocol = 0 79 Dummy GatewayProtocol = 1 << iota 80 Http 81 Websocket 82 Quic 83 Grpc 84 TCP = Http | Websocket // Http & Websocket 85 ) 86 87 var protocolNames = map[GatewayProtocol]string{ 88 Undefined: "Undefined", 89 Dummy: "Dummy", 90 Http: "Http", 91 Websocket: "Websocket", 92 Quic: "Quic", 93 Grpc: "Grpc", 94 TCP: "TCP", 95 } 96 97 func (p GatewayProtocol) String() string { 98 return protocolNames[p] 99 } 100 101 // HTTP methods were copied from net/http. 102 const ( 103 MethodWild = "*" 104 MethodGet = "GET" // RFC 7231, 4.3.1 105 MethodHead = "HEAD" // RFC 7231, 4.3.2 106 MethodPost = "POST" // RFC 7231, 4.3.3 107 MethodPut = "PUT" // RFC 7231, 4.3.4 108 MethodPatch = "PATCH" // RFC 5789 109 MethodDelete = "DELETE" // RFC 7231, 4.3.5 110 MethodConnect = "CONNECT" // RFC 7231, 4.3.6 111 MethodOptions = "OPTIONS" // RFC 7231, 4.3.7 112 MethodTrace = "TRACE" // RFC 7231, 4.3.8 113 ) 114 115 // Tunnel provides the communication channel between Edge servers. 116 // Tunnel is similar to Gateway in functionalities. 117 // However, Tunnel should be optimized for inter-communication between Edge servers, 118 // and Gateway is optimized for client-server communications. 119 type Tunnel interface { 120 Start() 121 Run() 122 Shutdown() 123 Addr() []string 124 } 125 126 type ( 127 LocalDB = badger.DB 128 StoreTxn = badger.Txn 129 ) 130 131 // Conn defines the Connection interface 132 type Conn interface { 133 ConnID() uint64 134 ClientIP() string 135 WriteBinary(streamID int64, data []byte) error 136 // Persistent returns FALSE if this connection will be closed when edge.DispatchCtx has 137 // been done. i.e. HTTP connections. It returns TRUE if this connection still alive when 138 // edge.DispatchCtx has been done. i.e. WebSocket connections 139 Persistent() bool 140 Get(key string) interface{} 141 Set(key string, val interface{}) 142 Walk(func(k string, v interface{}) bool) 143 } 144 145 // RestConn is same as Conn, but it supports REST format apis. 146 type RestConn interface { 147 Conn 148 ReadHeader(key string) string 149 WriteStatus(status int) 150 WriteHeader(key, value string) 151 MultiPart() (*multipart.Form, error) 152 RequestURI() string 153 Schema() string 154 Method() string 155 Host() string 156 Path() string 157 Body() []byte 158 Redirect(statusCode int, newHostPort string) 159 RedirectURL(statusCode int, newURL string) 160 } 161 162 type LogLevel = log.Level 163 164 // SetLogLevel is used for debugging purpose 165 func SetLogLevel(l LogLevel) { 166 log.SetLevel(l) 167 } 168 169 func RegisterPrometheus(registerer prometheus.Registerer) { 170 metrics.Register(registerer) 171 } 172 173 // Router could be used by Edge servers to find entities and redirect clients to the right Edge server. 174 type Router interface { 175 Set(entityID string, replicaSet uint64, replace bool) error 176 Get(entityID string) (replicaSet uint64, err error) 177 } 178 179 type IMessage interface { 180 proto.Message 181 json.Marshaler 182 json.Unmarshaler 183 }