本文整理匯總了Golang中github.com/fcavani/log.ProtoLevel函數的典型用法代碼示例。如果您正苦於以下問題:Golang ProtoLevel函數的具體用法?Golang ProtoLevel怎麽用?Golang ProtoLevel使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ProtoLevel函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: recv
func (s *protoSession) recv(val interface{}) error {
if s.status != connected {
return e.New(ErrNoConn)
}
if s.connTimeout > 0 {
err := s.conn.SetReadDeadline(time.Now().Add(s.connTimeout))
if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Send: deadline error: %v", err)
s.status = reconnect
return e.Forward(err)
}
}
dec := msgpack.NewDecoder(s.conn)
err := dec.Decode(val)
if err != nil {
s.status = reconnect
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Recv: connect error: %v", err)
s.conn.SetReadDeadline(time.Time{})
return e.Forward(err)
}
err = s.conn.SetReadDeadline(time.Time{})
if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Send: deadline error: %v", err)
s.status = reconnect
return e.Forward(err)
}
return nil
}
示例2: Init
func (c *clone) Init(inst string) error {
if c.Num <= 0 {
return e.New("number of clones invalid")
}
if c.Session == nil {
return e.New("nil Session")
}
if c.Num > c.MaxNum {
return e.New("number of initial streams is greater than the maximun")
}
c.inst = inst
c.once.Do(func() {
c.conns = make([]connInst, 0, c.Num)
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Init clones.")
for i := 0; i < c.Num; i++ {
conn, err := c.Session.NamedInstance(c.inst)
if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "clone").Printf("New instance failed: %v.", err)
continue
}
c.conns = append(c.conns, conn)
}
})
return nil
}
示例3: sortIncoming
func (p *protoSession) sortIncoming() {
go func() {
defer func() {
log.ProtoLevel().Tag("gormethods", "client", "proto").Println("Stopping sorting income data.")
p.closed <- struct{}{}
}()
F:
for {
const SleepReconect = 10 * time.Second
err := p.connect()
if err != nil {
log.Tag("gormethods", "client", "proto").Errorf("Connect error: %v", err)
p.bufs.ErrorAll(e.Forward(err))
time.Sleep(SleepReconect)
continue F
}
err = sortIncoming(p.reader, p.bufs, "client")
if err != nil {
p.lckConn.Lock()
status := p.status
p.lckConn.Unlock()
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Stopping income data. Status: %v", status)
if status == dontreconnect || status == closed {
break F
}
log.Tag("gormethods", "client", "proto").Errorf("Parsing incoming data from %v to %v failed: %v", p.conn.LocalAddr(), p.conn.RemoteAddr(), e.Trace(e.Forward(err)))
p.invConn()
p.bufs.ErrorAll(e.Forward(err))
time.Sleep(SleepReconect)
continue F
}
time.Sleep(SleepReconect)
}
}()
}
示例4: connect
func (s *protoSession) connect() error {
s.lckConn.Lock()
defer s.lckConn.Unlock()
var err error
if s.status == connected {
return nil
}
if s.status == dontreconnect {
return e.New("can't reconect any more")
}
s.status = reconnect
op := func() (bool, error) {
var err error
if s.tlsConfig == nil {
s.conn, err = net.DialTimeout(s.proto, s.addr, s.dialTimeout)
} else {
s.conn, err = tls.DialWithDialer((&net.Dialer{Timeout: s.dialTimeout}), s.proto, s.addr, s.tlsConfig)
}
if e.Contains(err, "too many open files") {
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Dial error: %v", err)
s.status = dontreconnect
return false, e.Forward(err)
} else if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "proto").Printf("Dial error: %v", err)
return true, e.Forward(err)
}
return false, nil
}
exp := backoff.NewExponentialBackOff()
err = backoff.Retry(op, exp)
if err != nil {
return e.Forward(err)
}
s.status = connected
err = s.send(&ProtoAuth{
Auth: s.auth,
Sess: s.sess,
ProtoVersion: ProtoVersion,
})
var resp ErrResp
err = s.recv(&resp)
if err != nil {
return e.Forward(err)
}
if resp.Err != nil {
return e.Forward(resp.Err)
}
//log.Tag("gormethods", "client", "proto").Printf("Connected to %v.", s.conn.RemoteAddr())
s.reader = bufio.NewReader(s.conn)
return nil
}
示例5: Close
//Close the associated gorotine and all queued wait channels.
func (b *Barrier) Close() {
if b.isclosed {
return
}
if <-b.chIsShutdown {
log.ProtoLevel().Tag("gormethods", "barrier").Printf("barrier shutdown requested, ignore close.")
return
}
ch := make(chan struct{})
b.chclose <- ch
//close(b.chclose)
<-ch
log.ProtoLevel().Tag("gormethods", "barrier").Printf("barrier close ok, chan returned")
}
示例6: Return
func (c *clone) Return(conn connInst) error {
c.lck.Lock()
defer c.lck.Unlock()
if len(c.conns) >= c.MaxNum {
err := conn.Close()
if err != nil {
return e.Forward(err)
}
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Pool is full, discart.")
return nil
}
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Put clone back in the pool.")
c.conns = append(c.conns, conn)
return nil
}
示例7: response
func (c *Client) response() (*Response, error) {
log.ProtoLevel().Tag("client", "discover").Printf("Waiting response...")
buf := make([]byte, c.BufSize)
err := c.conn.SetDeadline(time.Now().Add(c.Deadline))
if err != nil {
return nil, e.New(err)
}
n, addr, err := c.conn.ReadFromUDP(buf)
if err != nil {
return nil, e.New(err)
}
log.ProtoLevel().Tag("client", "discover").Printf("Response from %v with size %v.", addr, n)
err = c.conn.SetDeadline(time.Time{})
if err != nil {
return nil, e.New(err)
}
dec := gob.NewDecoder(bytes.NewReader(buf[:n]))
var msg Msg
err = dec.Decode(&msg)
if err != nil {
return nil, e.Push(err, e.New("error decoding response"))
}
if msg.Err != nil {
return nil, e.Forward(msg.Err)
}
if msg.From != c.ServerName {
return nil, e.New("wrong server name")
}
if msg.To != c.Name {
return nil, e.New("message isn't for me")
}
buf, err = msg.Message(c.ServerKey, c.PrivateKey)
if err != nil {
return nil, e.Push(err, e.New("error decrypting response"))
}
dec = gob.NewDecoder(bytes.NewReader(buf))
var resp Response
err = dec.Decode(&resp)
if err != nil {
return nil, e.Push(err, e.New("error decoding response"))
}
return &resp, nil
}
示例8: Init
func (c *client) Init() error {
var err error
c.session, err = openSession(c.Proto, c.Addr, c.TlsConfig, c.SessionName, c.Auth, c.DialTimeout, c.ConnTimeout)
if err != nil {
return e.Forward(err)
}
c.clones = &clones{
Num: c.NumClones,
MaxNum: c.MaxNumClones,
}
err = c.clones.Init()
if err != nil {
return e.Forward(err)
}
if c.KeepAlive == 0 {
return nil
}
go func() {
for {
time.Sleep(c.KeepAlive)
err := c.session.Keepalive()
if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "keepalive").Printf("Keepalive failed: %v", err)
}
}
}()
return nil
}
示例9: writeError
func writeError(s *servStreams, num uint32, err error) {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Can't write new stream reponse for %v: %v", s.conn.RemoteAddr(), err)
if num != 0 && num != keepAliveStream {
err = s.streams.Deactivate(num)
if err != nil {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Can't deactivate stream %v: %v", num, err)
}
err = s.bufs.Delete(num)
if err != nil {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Can't delete stream %v: %v", num, err)
}
}
s.acceptFifo <- &newAccept{
err: e.New(ErrStreamClosed),
}
}
示例10: Request
func (c *clone) Request() (connInst, error) {
c.lck.Lock()
defer c.lck.Unlock()
if len(c.conns) <= 0 {
conn, err := c.Session.NamedInstance(c.inst)
if err != nil {
return nil, e.Forward(err)
}
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Pool is empty, creating another clone.")
return conn, nil
}
clone := c.conns[len(c.conns)-1]
c.conns = c.conns[:len(c.conns)-1]
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Get object from pool.", len(c.conns))
return clone, nil
}
示例11: PingHttp
// PingHttp connect a http or https server and try to
// receive something. If the server return a code different
// of 2xx, it will fail. Ignores insecure certificates.
func PingHttp(url *url.URL) error {
resp, err := httpClient.Get(url.String())
if e.Contains(err, "connection refused") {
return e.Push(e.New(err), "get failed: connection refused")
} else if err != nil {
return e.Push(e.New(err), "get failed")
}
defer resp.Body.Close()
buf := make([]byte, 4096)
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
return e.Forward(err)
}
buf = buf[:n]
//status.Log(status.Protocol, "PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf))
log.ProtoLevel().Printf("PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf))
return e.New("returned status code %v, expected 2xx", resp.StatusCode)
}
_, err = resp.Body.Read(buf)
if err != nil && err != io.EOF {
return e.Forward(err)
}
return nil
}
示例12: newServStreams
func newServStreams(conn net.Conn, session string, auth auth.Auth, p *protoServer) (*servStreams, error) {
s := &servStreams{
p: p,
conn: conn,
bufs: newBuffers(numBuffers),
streams: newActiveStreams(),
acceptFifo: make(chan *newAccept, p.FifoLength),
session: session,
auth: auth,
streamCount: 1,
}
reader := bufio.NewReader(s.conn)
go func() {
defer func() {
s.acceptFifo <- &newAccept{
err: e.New(ErrStreamClosed),
}
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Stop sort incoming.")
}()
for {
err := sortIncoming(reader, s.bufs, "server")
if e.Equal(err, ErrNetwork) || err == nil {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("sortIncoming stoped with isclose = %v and error: %v", s.isclose, err)
s.lck.Lock()
if s.isclose {
s.lck.Unlock()
return
}
s.isclose = true
s.bufs.ErrorAll(e.New("conn close"))
s.bufs.ReqShutdownAll()
s.lck.Unlock()
return
} else if err != nil {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("sortIncoming failed: %v", err)
continue
}
}
}()
s.zero()
s.keepalive()
return s, nil
}
示例13: protocol
func (s *Server) protocol(conn InstConn) error {
dec := conn.Decoder()
defer func() {
num := conn.StreamNum()
s.lck.Lock()
if _, found := s.connections[num]; found {
delete(s.connections, num)
}
s.lck.Unlock()
conn.Close()
log.DebugLevel().Tag("server", "gormethods", "protocol").Println("protocol close")
}()
for {
s.lck.Lock()
connection, found := s.connections[conn.StreamNum()]
if !found {
connection = &Connection{}
s.connections[conn.StreamNum()] = connection
}
connection.ttl = time.Now().Add(s.Ttl)
connection.conn = conn
s.lck.Unlock()
var req msgtypes.MessageType
// se fechar a conn do outro lado deve acontecer um err aqui
err := dec.Decode(&req)
if e.Find(err, io.EOF) >= 0 {
return e.Forward(err)
} else if err != nil {
log.DebugLevel().Tag("server", "gormethods").Printf("Decode %v -> %v (%v) error: %v", conn.RemoteAddr(), conn.LocalAddr(), conn.StreamNum(), e.Trace(e.Forward(err)))
return e.Forward(err)
}
switch req {
case msgtypes.Call:
err = s.protoCall(conn)
if e.Find(err, ErrWire) >= 0 {
return e.Forward(err)
}
case msgtypes.Destroy:
err = s.protoDestroy(conn)
if e.Find(err, ErrWire) >= 0 {
return e.Forward(err)
}
return nil
case msgtypes.Channel:
err = s.protoChannel(conn)
if e.Find(err, ErrWire) >= 0 {
return e.Forward(err)
}
return nil
default:
log.ProtoLevel().Tag("server", "gormethods").Printf("Protocol fail from %v -> %v (%v) sent: %v", conn.RemoteAddr(), conn.LocalAddr(), conn.StreamNum(), req)
}
}
}
示例14: finalizer
func finalizer(client ClientInt) {
log.ProtoLevel().Tag("gormethods", "client").Printf("Finalize %v, %v", client.SessionName(), client.InstanceName())
err := client.Destroy()
if err != nil {
log.Error("Client finalizer failed:", err)
}
err = client.CloseClient()
if err != nil {
log.Error("Client finalizer failed:", err)
}
}
示例15: keepalive
func (s *servStreams) keepalive() {
go func() {
defer func() {
log.ProtoLevel().Tag("gormethods", "server", "proto").Println("Stop keepalive.")
}()
buf := make([]byte, payloadSize)
for {
n, err := s.bufs.Read(keepAliveStream, buf)
if err != nil {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Can't read from stream %v: %v", keepAliveStream, err)
break
}
if n != payloadSize {
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Not enougth data from stream %v", keepAliveStream)
continue
}
cmd, _, err := parseCmd(buf)
if err != nil {
continue
}
switch cmd {
case keepAlive:
buf := newStreamResponse(ok, keepAliveStream)
for count := 0; count < len(buf); {
n, err := s.write(keepAliveStream, buf[count:])
if e.Equal(err, ErrNetwork) {
writeError(s, keepAliveStream, e.Forward(err))
return
} else if err != nil {
writeError(s, keepAliveStream, e.Forward(err))
break
}
count += n
}
default:
log.ProtoLevel().Tag("gormethods", "server", "proto").Printf("Protocol error on stream %v: %v", keepAliveStream, cmd)
continue
}
}
}()
}