本文整理匯總了Golang中github.com/fcavani/e.Contains函數的典型用法代碼示例。如果您正苦於以下問題:Golang Contains函數的具體用法?Golang Contains怎麽用?Golang Contains使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Contains函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: write
func (s *servStreams) write(stream uint32, b []byte) (int, error) {
s.lckSend.Lock()
defer s.lckSend.Unlock()
buf := make([]byte, sepsize+binary.MaxVarintLen32*2)
header(buf, stream, len(b))
n, err := s.conn.Write(append(buf, b...))
if e.Contains(err, "use of closed network connection") || e.Contains(err, "EOF") || e.Contains(err, "i/o timeout") {
return n, e.Push(err, ErrNetwork)
}
return n, nil
}
示例2: forceRestart
func (vs *VirtualServer) forceRestart() error {
vs.status.Log(status.Verbose, "Forcing kill server %v.", vs.VirtualServerName)
pid, err := util.ReadPidInt(vs.PidFile)
if e.Contains(err, ErrNoSuchFile) {
vs.status.Log(status.Verbose, "Server %v is about to restart. Pid file not found.", vs.VirtualServerName)
err := vs.forceKill()
if err != nil {
msg := "Forcekill script failed in " + vs.VirtualServerName + " with error: " + err.Error() + ". Giving up restart server."
msg += " Trace: " + e.Trace(e.Forward(err))
vs.status.Log(status.Verbose, msg)
return nil
}
goto restart
} else if err != nil {
return e.Forward(err)
}
err = syscall.Kill(pid, 9)
if err != nil {
return e.Forward(err)
}
restart:
err = vs.beforeStart()
if err != nil {
return e.Forward(err)
}
err = vs.restart()
if err != nil {
return e.Forward(err)
}
return nil
}
示例3: exec
func (d *Daemon) exec(cmd string, args ...string) error {
done := make(chan error, 1)
c := exec.Command(cmd, args...)
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := c.Start()
if err != nil {
return e.Forward(err)
}
go func() {
done <- c.Wait()
}()
select {
case <-time.After(d.ExecTimeout):
<-done // allow goroutine to exit
err := c.Process.Kill()
if err != nil {
d.status.Log(status.Debug, "Failed to kill command %v for %v.", cmd, d.DaemonName)
return e.Forward(err)
}
d.status.Log(status.Debug, "Command %v exec for %v was killed, timeout.", cmd, d.DaemonName)
case err := <-done:
if e.Contains(err, "exit status") {
msg := "Command %v exec for %v exited with this error: %v"
d.status.Log(status.Debug, msg, cmd, d.DaemonName, e.Trace(e.Forward(err)))
return e.Forward(err)
} else if err != nil {
return e.Forward(err)
}
}
return nil
}
示例4: exec
func (vs *VirtualServer) exec(cmd string, args ...string) error {
done := make(chan error, 1)
c := exec.Command(cmd, args...)
err := c.Start()
if err != nil {
return e.Forward(err)
}
go func() {
done <- c.Wait()
}()
select {
case <-time.After(vs.ExecTimeout):
<-done // allow goroutine to exit
err := c.Process.Kill()
if err != nil {
vs.status.Log(status.Verbose, "Failed to kill command %v for %v.", cmd, vs.VirtualServerName)
return e.Forward(err)
}
vs.status.Log(status.Verbose, "Command %v exec for %v was killed, timeout.", cmd, vs.VirtualServerName)
return e.New("execution timeout")
case err := <-done:
if e.Contains(err, "exit status") {
msg := "Command %v exec for %v existed with this error: %v"
vs.status.Log(status.Verbose, msg, cmd, vs.VirtualServerName, e.Trace(e.Forward(err)))
return e.Forward(err)
} else if err != nil {
return e.Forward(err)
}
}
return nil
}
示例5: 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
}
示例6: Start
func (p *protoServer) Start() error {
var err error
if p.FnInst == nil {
log.Tag("server", "gormethods").Errorf("FnInst function is nil!")
return e.New("failed to init server")
}
if p.TlsConfig != nil {
p.listener, err = tls.Listen(p.Proto, p.Addr, p.TlsConfig)
} else {
p.listener, err = reuse.Listen(p.Proto, p.Addr)
}
if err != nil {
log.Tag("server", "gormethods").Errorf("Can't listen on interface %v proto %v: %v", p.Addr, p.Proto, e.Forward(err))
return e.Forward(err)
}
err = p.chmodSocket(0660)
if err != nil {
log.Tag("server", "gormethods", "proto").Errorf("Fail to chmod socket %v: %v", p.listener.Addr(), e.Forward(err))
return e.Forward(err)
}
p.closedListener = make(chan struct{})
go func() {
defer func() {
log.Tag("server", "gormethods", "proto").Println("Listener accept close.")
p.closedListener <- struct{}{}
}()
for {
// Ok1
conn, err := p.listener.Accept()
if e.Contains(err, "use of closed network connection") {
p.listener.Close()
return
} else if e.Contains(err, "too many open files") {
p.listener.Close()
log.Tag("server", "gormethods").Error(err)
return
} else if err != nil {
p.listener.Close()
log.Tag("server", "gormethods", "proto").Errorf("Accept for %v failed: %v", p.listener.Addr(), e.Forward(err))
return
}
go p.accept(conn)
}
}()
return nil
}
示例7: Write
func (s *stream) Write(b []byte) (n int, err error) {
s.lckSend.Lock()
defer s.lckSend.Unlock()
buf := make([]byte, offset3+len(b))
err = header(buf, s.num, len(b))
if err != nil {
return 0, e.Forward(err)
}
copy(buf[offset3:], b)
err = s.connect()
if err != nil {
return 0, e.Forward(err)
}
if s.connTimeout > 0 {
err = s.conn.SetWriteDeadline(time.Now().Add(s.connTimeout))
if err != nil {
return 0, e.Forward(err)
}
}
n, err = s.conn.Write(buf)
if err == io.EOF || e.Contains(err, "i/o timeout") || e.Contains(err, "closed") {
s.lckSend.Unlock()
s.invConn()
s.lckSend.Lock()
return 0, e.Forward(err)
} else if err != nil {
s.lckSend.Unlock()
s.invConn()
s.lckSend.Lock()
return 0, e.Forward(err)
}
err = s.conn.SetWriteDeadline(time.Time{})
if err != nil {
return 0, e.Forward(err)
}
n = n - offset3
return
}
示例8: PingUDP
// PingUDP try to send something to UDP port
// and receive some thing. If connections is refused
// the host is considered down. If some other error,
// besides timeout, the host is down. If timeoutr
// I can't determine if the host is there or not.
func PingUDP(url *url.URL) error {
conn, err := net.DialTimeout("udp", url.Host, DialTimeout)
if err != nil {
return e.New(err)
}
defer conn.Close()
err = conn.SetDeadline(time.Now().Add(Deadline))
_, err = conn.Write([]byte("Hi!"))
if err != nil {
return e.New(err)
}
buf := make([]byte, 100)
_, err = conn.Read(buf)
if e.Contains(err, "connection refused") {
return e.Push(err, ErrNotAnwsered)
} else if err != nil && !e.Contains(err, "i/o timeout") {
return e.New(err)
}
return nil
}
示例9: 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
}
示例10: Get
func (t *txMongoDb) Get(key string) (interface{}, error) {
val := types.Make(t.tentry)
inter := val.Interface()
err := t.c.Find(bson.M{"key": key}).One(inter)
if _, ok := err.(*mgo.QueryError); ok {
return nil, e.New(ErrKeyNotFound)
} else if e.Contains(err, "not found") {
return nil, e.New(ErrKeyNotFound)
} else if err != nil {
return nil, e.New(err)
}
return inter, nil
}
示例11: sendResp
func (a *Server) sendResp(resp *Response, to string, tokey *rsa.PublicKey, addr *net.UDPAddr) {
log.ProtoLevel().Tag("server", "discover").Printf("Send response from %v to %v", a.conn.LocalAddr(), addr)
respBuf := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(respBuf)
err := enc.Encode(resp)
if err != nil {
log.Tag("discover", "server").Printf("Server - Protocol fail for %v with error: %v", addr, e.Trace(e.New(err)))
a.sendErr(addr, e.Push(err, e.New("error enconding response")))
return
}
msg, err := NewMsg(a.Name, to, a.PrivateKey, tokey, respBuf.Bytes())
if err != nil {
log.Tag("discover", "server").Printf("Server - Protocol fail for %v with error: %v", addr, e.Trace(e.New(err)))
a.sendErr(addr, e.Push(err, e.New("error creating new response message")))
return
}
respBuf = bytes.NewBuffer([]byte{})
enc = gob.NewEncoder(respBuf)
err = enc.Encode(msg)
if err != nil {
log.Tag("discover", "server").Printf("Server - Protocol fail for %v with error: %v", addr, e.Trace(e.New(err)))
a.sendErr(addr, e.Push(err, e.New("error enconding response")))
return
}
if respBuf.Len() > a.BufSize {
log.Tag("discover", "server").Printf("Server - Protocol fail for %v message is too big (%v).", addr, respBuf.Len())
a.sendErr(addr, e.Push(err, e.New("response is too long %v", respBuf.Len())))
return
}
n, oob, err := a.conn.WriteMsgUDP(respBuf.Bytes(), nil, addr)
if e.Contains(err, "use of closed network connection") {
return
} else if err != nil {
log.Tag("discover", "server").Printf("Server - WriteMsgUDP (%v) failed: %v", addr, e.Trace(e.New(err)))
return
}
if oob != 0 {
log.Tag("discover", "server").Printf("Server - WriteMsgUDP to %v failed: %v, %v", addr, n, oob)
return
}
if n != respBuf.Len() {
log.Tag("discover", "server").Printf("Server - WriteMsgUDP to %v failed: %v, %v", addr, n, oob)
return
}
}
示例12: beforeStart
func (vs *VirtualServer) beforeStart() error {
vs.status.Log(status.Verbose, "Starting command %v.", vs.BeforeStartCmd)
c := exec.Command(vs.BeforeStartCmd, vs.BeforeStartArgs...)
buf, err := c.CombinedOutput()
if e.Contains(err, "exit status") {
msg := "Command " + vs.BeforeStartCmd + " exec for " + vs.VirtualServerName + " existed with this error: " + err.Error()
msg += "\nBefore start output is:\n\n"
msg += string(buf)
vs.status.Log(status.Verbose, "Command "+vs.BeforeStartCmd+" exec failed.", msg)
return e.Forward(err)
} else if err != nil {
return e.Forward(err)
}
vs.status.Log(status.Verbose, "Command %v done.", vs.BeforeStartCmd)
return nil
}
示例13: Put
func (t *txBoltDb) Put(key string, data interface{}) error {
buf, err := t.enc.Encode(data)
if err != nil {
return e.Forward(err)
}
defer func() {
t.bufs = append(t.bufs, buf)
}()
err = t.b.Put([]byte(key), buf)
if e.Contains(err, "tx not writable") {
return e.New(ErrReadOnly)
} else if err != nil {
return e.New(err)
}
return nil
}
示例14: restartProcess
func (d *Daemon) restartProcess() error {
d.status.Log(status.Verbose, "Process %v is about to restart.", d.DaemonName)
err := d.exec(d.RestartCmd, d.RestartArgs...)
if e.Contains(err, "exit status") {
d.status.Log(status.Verbose, "Restart command %v failed for %v with error: %v.", d.RestartCmd, d.DaemonName, e.Trace(e.Forward(err)))
if d.ForceKillCmd != "" {
err := d.forceKill()
if err != nil {
return e.Push(err, ErrGiveUpRestart)
}
} else {
err := d.kill(syscall.SIGTERM)
if e.Equal(err, ErrNoPid) {
d.status.Log(status.Verbose, "Starting force kill for %v.", d.DaemonName)
err := d.forceKill()
if err != nil {
return e.Push(err, ErrGiveUpRestart)
}
} else if err != nil {
err := d.kill(syscall.SIGKILL)
if err != nil {
d.status.Log(status.Verbose, "Starting force kill for %v.", d.DaemonName)
err := d.forceKill()
if err != nil {
return e.Push(err, ErrGiveUpRestart)
}
}
}
}
d.status.Log(status.Verbose, "Process %v is about to restart.", d.DaemonName)
err = d.exec(d.RestartCmd, d.RestartArgs...)
if err != nil {
err := d.forceKill()
if err != nil {
return e.Push(err, ErrGiveUpRestart)
}
d.status.Log(status.Verbose, "Process %v is about to restart, for the last time.", d.DaemonName)
err = d.exec(d.RestartCmd, d.RestartArgs...)
if err != nil {
return e.Push(err, ErrGiveUpRestart)
}
}
} else if err != nil {
return e.Forward(err)
}
return nil
}
示例15: InfoDB
// InfoDB reports the status of the database.
// url is the url for the database and dbname is the name of the new database.
func InfoDB(url *url.URL, dbname string) (*DatabaseInfo, error) {
c := &client{url}
code, data, err := c.get(dbname, "", nil)
if e.Contains(err, Err404) {
return nil, e.Push(err, ErrDbNotFound)
} else if err != nil {
return nil, e.Push(err, "can't get database information")
}
if code != http.StatusOK {
return nil, e.New("can't get database information, wrong response code: %v", code)
}
di := new(DatabaseInfo)
err = json.Unmarshal(data, di)
if err != nil {
return nil, e.Push(err, "can't get database information")
}
return di, nil
}