github.com/philippseith/signalr@v0.6.3/options.go (about)

     1  package signalr
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/go-kit/log"
     9  	"github.com/go-kit/log/level"
    10  )
    11  
    12  // TimeoutInterval is the interval one Party will consider the other Party disconnected
    13  // if it hasn't received a message (including keep-alive) in it.
    14  // The recommended value is double the KeepAliveInterval value.
    15  // Default is 30 seconds.
    16  func TimeoutInterval(timeout time.Duration) func(Party) error {
    17  	return func(p Party) error {
    18  		p.setTimeout(timeout)
    19  		return nil
    20  	}
    21  }
    22  
    23  // HandshakeTimeout is the interval if the other Party doesn't send an initial handshake message within,
    24  // the connection is closed. This is an advanced setting that should only be modified
    25  // if handshake timeout errors are occurring due to severe network latency.
    26  // For more detail on the handshake process,
    27  // see https://github.com/dotnet/aspnetcore/blob/master/src/SignalR/docs/specs/HubProtocol.md
    28  func HandshakeTimeout(timeout time.Duration) func(Party) error {
    29  	return func(p Party) error {
    30  		p.setHandshakeTimeout(timeout)
    31  		return nil
    32  	}
    33  }
    34  
    35  // KeepAliveInterval is the interval if the Party hasn't sent a message within,
    36  // a ping message is sent automatically to keep the connection open.
    37  // When changing KeepAliveInterval, change the Timeout setting on the other Party.
    38  // The recommended Timeout value is double the KeepAliveInterval value.
    39  // Default is 15 seconds.
    40  func KeepAliveInterval(interval time.Duration) func(Party) error {
    41  	return func(p Party) error {
    42  		p.setKeepAliveInterval(interval)
    43  		return nil
    44  	}
    45  }
    46  
    47  // StreamBufferCapacity is the maximum number of items that can be buffered for client upload streams.
    48  // If this limit is reached, the processing of invocations is blocked until the server processes stream items.
    49  // Default is 10.
    50  func StreamBufferCapacity(capacity uint) func(Party) error {
    51  	return func(p Party) error {
    52  		if capacity == 0 {
    53  			return errors.New("unsupported StreamBufferCapacity 0")
    54  		}
    55  		p.setStreamBufferCapacity(capacity)
    56  		return nil
    57  	}
    58  }
    59  
    60  // MaximumReceiveMessageSize is the maximum size in bytes of a single incoming hub message.
    61  // Default is 32768 bytes (32KB)
    62  func MaximumReceiveMessageSize(sizeInBytes uint) func(Party) error {
    63  	return func(p Party) error {
    64  		if sizeInBytes == 0 {
    65  			return errors.New("unsupported maximumReceiveMessageSize 0")
    66  		}
    67  		p.setMaximumReceiveMessageSize(sizeInBytes)
    68  		return nil
    69  	}
    70  }
    71  
    72  // ChanReceiveTimeout is the timeout for processing stream items from the client, after StreamBufferCapacity was reached
    73  // If the hub method is not able to process a stream item during the timeout duration,
    74  // the server will send a completion with error.
    75  // Default is 5 seconds.
    76  func ChanReceiveTimeout(timeout time.Duration) func(Party) error {
    77  	return func(p Party) error {
    78  		p.setChanReceiveTimeout(timeout)
    79  		return nil
    80  	}
    81  }
    82  
    83  // EnableDetailedErrors If true, detailed exception messages are returned to the other
    84  // Party when an exception is thrown in a Hub method.
    85  // The default is false, as these exception messages can contain sensitive information.
    86  func EnableDetailedErrors(enable bool) func(Party) error {
    87  	return func(p Party) error {
    88  		p.setEnableDetailedErrors(enable)
    89  		return nil
    90  	}
    91  }
    92  
    93  // StructuredLogger is the simplest logging interface for structured logging.
    94  // See github.com/go-kit/log
    95  type StructuredLogger interface {
    96  	Log(keyVals ...interface{}) error
    97  }
    98  
    99  // Logger sets the logger used by the Party to log info events.
   100  // If debug is true, debug log event are generated, too
   101  func Logger(logger StructuredLogger, debug bool) func(Party) error {
   102  	return func(p Party) error {
   103  		i, d := buildInfoDebugLogger(logger, debug)
   104  		p.setLoggers(i, d)
   105  		return nil
   106  	}
   107  }
   108  
   109  type recoverLogger struct {
   110  	logger log.Logger
   111  }
   112  
   113  func (r *recoverLogger) Log(keyVals ...interface{}) error {
   114  	defer func() {
   115  		if err := recover(); err != nil {
   116  			fmt.Printf("recovering from panic in logger: %v\n", err)
   117  		}
   118  	}()
   119  	return r.logger.Log(keyVals...)
   120  }
   121  
   122  func buildInfoDebugLogger(logger log.Logger, debug bool) (log.Logger, log.Logger) {
   123  	if debug {
   124  		logger = level.NewFilter(logger, level.AllowDebug())
   125  	} else {
   126  		logger = level.NewFilter(logger, level.AllowInfo())
   127  	}
   128  	infoLogger := &recoverLogger{level.Info(logger)}
   129  	debugLogger := log.With(&recoverLogger{level.Debug(logger)}, "caller", log.DefaultCaller)
   130  	return infoLogger, debugLogger
   131  }