socket_vm.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package socket
  6. import (
  7. "net"
  8. "time"
  9. "golang.org/x/net/context"
  10. )
  11. // Dial connects to the address addr on the network protocol.
  12. // The address format is host:port, where host may be a hostname or an IP address.
  13. // Known protocols are "tcp" and "udp".
  14. // The returned connection satisfies net.Conn, and is valid while ctx is valid;
  15. // if the connection is to be used after ctx becomes invalid, invoke SetContext
  16. // with the new context.
  17. func Dial(ctx context.Context, protocol, addr string) (*Conn, error) {
  18. conn, err := net.Dial(protocol, addr)
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &Conn{conn}, nil
  23. }
  24. // DialTimeout is like Dial but takes a timeout.
  25. // The timeout includes name resolution, if required.
  26. func DialTimeout(ctx context.Context, protocol, addr string, timeout time.Duration) (*Conn, error) {
  27. conn, err := net.DialTimeout(protocol, addr, timeout)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &Conn{conn}, nil
  32. }
  33. // LookupIP returns the given host's IP addresses.
  34. func LookupIP(ctx context.Context, host string) (addrs []net.IP, err error) {
  35. return net.LookupIP(host)
  36. }
  37. // Conn represents a socket connection.
  38. // It implements net.Conn.
  39. type Conn struct {
  40. net.Conn
  41. }
  42. // SetContext sets the context that is used by this Conn.
  43. // It is usually used only when using a Conn that was created in a different context,
  44. // such as when a connection is created during a warmup request but used while
  45. // servicing a user request.
  46. func (cn *Conn) SetContext(ctx context.Context) {
  47. // This function is not required on managed VMs.
  48. }
  49. // KeepAlive signals that the connection is still in use.
  50. // It may be called to prevent the socket being closed due to inactivity.
  51. func (cn *Conn) KeepAlive() error {
  52. // This function is not required on managed VMs.
  53. return nil
  54. }