Exemple #1
0
func enqueueTask(id uint64, task []byte, output io.Writer) {
	encoder := codec.NewEncoder(output, hAsocket)
	// make http tunable
	encoder.Encode([]interface{}{id, invoke, []interface{}{"http"}})
	encoder.Encode([]interface{}{id, chunk, []interface{}{task}})
	encoder.Encode([]interface{}{id, close, []interface{}{}})
}
Exemple #2
0
func (o *Overlord) handleConnection(conn io.ReadWriteCloser) {
	defer conn.Close()
	decoder := codec.NewDecoder(conn, hAsocket)
	encoder := codec.NewEncoder(conn, hAsocket)
	for {
		var msg cocaine.Message
		if err := decoder.Decode(&msg); err != nil {
			log.Printf("protocol decoder error %v", err)
			return
		}
		switch msg.Session {
		case utilitySession:
			if msg.MsgType == handshake {
				log.Println("replying to heartbeat")
				encoder.Encode(msg)
			}
		default:
			o.mu.Lock()
			if ch, ok := o.sessions[msg.Session]; ok {
				select {
				case ch <- &msg:
				case <-time.After(time.Second * 2):
					log.Println("didn't send reply during 2 seconds")
				}
			} else {
				log.Printf("Invalid session %v", msg)
			}
			o.mu.Unlock()
		}
	}
}
func (sock *asyncRWSocket) writeloop() {
	go func() {
		var buf = bufio.NewWriter(sock.conn)
		encoder := codec.NewEncoder(buf, hAsocket)
		for incoming := range sock.upstreamBuf.out {
			err := encoder.Encode(incoming)
			if err != nil {
				sock.close()
				// blackhole all pending writes. See #31
				go func() {
					for _ = range sock.upstreamBuf.out {
						// pass
					}
				}()
				return
			}
			buf.Flush()
		}
	}()
}