-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_transport.go
39 lines (33 loc) · 933 Bytes
/
server_transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package transport
import (
"reflect"
"sync"
)
type serverTransportFunc func(address, network, protocol string, opts ...ServerTransportOption) ServerTransport
var (
serverTransportFuncs = map[string]serverTransportFunc{}
sMutex = sync.RWMutex{}
)
type ServerTransport interface {
ListenAndServe() error
Close()
}
func RegisterServerTransportFunc(network string, fn serverTransportFunc) {
value := reflect.ValueOf(fn)
if fn == nil || value.Kind() == reflect.Pointer && value.IsNil() {
panic("register nil server transport")
}
if network == "" {
panic("register empty network of server transport")
}
sMutex.Lock()
defer sMutex.Unlock()
serverTransportFuncs[network] = fn
}
func NewServerTransport(address, network, protocol string, opts ...ServerTransportOption) ServerTransport {
fn, ok := serverTransportFuncs[network]
if !ok {
return nil
}
return fn(address, network, protocol, opts...)
}